Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Swap |
|
| 4.0;4 |
1 | 2 | /** |
2 | * Copyright (c) 2014-2015, Thindeck.com | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: 1) Redistributions of source code must retain the above | |
8 | * copyright notice, this list of conditions and the following | |
9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
10 | * copyright notice, this list of conditions and the following | |
11 | * disclaimer in the documentation and/or other materials provided | |
12 | * with the distribution. 3) Neither the name of the thindeck.com nor | |
13 | * the names of its contributors may be used to endorse or promote | |
14 | * products derived from this software without specific prior written | |
15 | * permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | */ | |
30 | package com.thindeck.agents; | |
31 | ||
32 | import com.google.common.base.Joiner; | |
33 | import com.jcabi.aspects.Immutable; | |
34 | import com.jcabi.log.Logger; | |
35 | import com.jcabi.xml.XML; | |
36 | import com.thindeck.api.Agent; | |
37 | import org.xembly.Directive; | |
38 | import org.xembly.Directives; | |
39 | ||
40 | /** | |
41 | * Swap BLUE and GREEN containers. | |
42 | * | |
43 | * @author Yegor Bugayenko (yegor@teamed.io) | |
44 | * @version $Id$ | |
45 | * @since 0.1 | |
46 | * @checkstyle MultipleStringLiteralsCheck (500 lines) | |
47 | */ | |
48 | @Immutable | |
49 | 1 | public final class Swap implements Agent { |
50 | ||
51 | @Override | |
52 | public Iterable<Directive> exec(final XML deck) { | |
53 | 0 | final boolean ready = !deck.nodes( |
54 | Joiner.on(" and ").join( | |
55 | "/deck/containers[not(container/@waste)", | |
56 | "not(container[@state='dead' and @type='blue'])", | |
57 | "not(container[@state='unknown' and @type='blue'])", | |
58 | "container/@type='blue']" | |
59 | ) | |
60 | ).isEmpty(); | |
61 | 0 | final Directives dirs = new Directives(); |
62 | 0 | if (ready) { |
63 | 0 | final String today = new Today().iso(); |
64 | 0 | for (final XML ctr : deck.nodes("/deck/containers/container")) { |
65 | 0 | final String name = ctr.xpath("name/text()").get(0); |
66 | 0 | dirs.xpath( |
67 | String.format( | |
68 | "/deck/containers/container[name='%s']", | |
69 | name | |
70 | ) | |
71 | ); | |
72 | 0 | final String img = ctr.xpath("image/text()").get(0); |
73 | 0 | if ("blue".equals(ctr.xpath("@type").get(0))) { |
74 | 0 | dirs.attr("type", "green"); |
75 | 0 | Logger.info(this, "Blue container %s set to green", name); |
76 | 0 | dirs.xpath("/deck/images/image").attr("waste", today); |
77 | 0 | dirs.xpath( |
78 | String.format( | |
79 | "/deck/images/image[name='%s']", | |
80 | img | |
81 | ) | |
82 | ).attr("type", "green").xpath("@waste").remove(); | |
83 | 0 | Logger.info( |
84 | this, "Image %s set to green, others to waste", img | |
85 | ); | |
86 | } else { | |
87 | 0 | dirs.attr("waste", today); |
88 | 0 | Logger.info(this, "Green container %s set to waste", name); |
89 | 0 | dirs.xpath( |
90 | String.format( | |
91 | "/deck/images/image[name='%s']", | |
92 | img | |
93 | ) | |
94 | ).attr("waste", today); | |
95 | 0 | Logger.info(this, "Image %s set to waste", img); |
96 | } | |
97 | 0 | } |
98 | } | |
99 | 0 | return dirs; |
100 | } | |
101 | } |