a2148120b13ceaed32065ea94ff676261c0aeb97
[policy/models.git] / models-interactions / model-yaml / src / test / java / org / onap / policy / controlloop / policy / guard / ControlLoopGuardBuilderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-yaml unit test
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controlloop.policy.guard;
23
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.InputStream;
30 import java.util.HashMap;
31 import java.util.LinkedList;
32 import java.util.List;
33 import java.util.Map;
34 import org.junit.Test;
35 import org.onap.policy.controlloop.policy.builder.BuilderException;
36 import org.onap.policy.controlloop.policy.builder.Message;
37 import org.onap.policy.controlloop.policy.builder.MessageLevel;
38 import org.onap.policy.controlloop.policy.builder.Results;
39 import org.onap.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.yaml.snakeyaml.Yaml;
43 import org.yaml.snakeyaml.constructor.Constructor;
44
45 public class ControlLoopGuardBuilderTest {
46     private static final Logger logger = LoggerFactory.getLogger(ControlLoopGuardBuilderTest.class);
47
48     @Test
49     public void testControlLoopGuard() throws BuilderException {
50         //
51         // Create a builder
52         //
53         ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard());
54         //
55         // Assert there is no guard policies yet
56         //
57         Results results = builder.buildSpecification();
58         boolean noGuardPolicies = false;
59         for (Message m : results.getMessages()) {
60             if ("ControlLoop Guard should have at least one guard policies".equals(m.getMessage())
61                             && m.getLevel() == MessageLevel.ERROR) {
62                 noGuardPolicies = true;
63                 break;
64             }
65         }
66         assertTrue(noGuardPolicies);
67         //
68         // Add a guard policy without limit constraint
69         //
70         String clname = "CL_vUSP123";
71         List<String> targets = new LinkedList<>();
72         targets.add("s1");
73         targets.add("s2");
74         targets.add("s3");
75         MatchParameters matchParameters = new MatchParameters(clname, "APPC", "Restart", targets);
76         GuardPolicy policy1 = new GuardPolicy("id123", "guardpolicy1", "description aaa", matchParameters);
77         builder = builder.addGuardPolicy(policy1);
78         //
79         // Assert there is no limit constraint associated with the only guard policy
80         //
81         results = builder.buildSpecification();
82         boolean noConstraint = false;
83         for (Message m : results.getMessages()) {
84             if ("Guard policy guardpolicy1 does not have any limit constraint".equals(m.getMessage())
85                             && m.getLevel() == MessageLevel.ERROR) {
86                 noConstraint = true;
87                 break;
88             }
89         }
90         assertTrue(noConstraint);
91         //
92         // Add a constraint to policy1
93         //
94         Map<String, String> activeTimeRange = new HashMap<>();
95         activeTimeRange.put("start", "00:00:00-05:00");
96         activeTimeRange.put("end", "23:59:59-05:00");
97         List<String> blacklist = new LinkedList<>();
98         blacklist.add("eNodeB_common_id1");
99         blacklist.add("eNodeB_common_id2");
100         Map<String, String> timeWindow = new HashMap<>();
101         timeWindow.put("value", "10");
102         timeWindow.put("units", "minute");
103         Constraint cons = new Constraint(5, timeWindow, activeTimeRange, blacklist);
104         builder = builder.addLimitConstraint(policy1.getId(), cons);
105         //
106         // Add a duplicate constraint to policy1
107         //
108         builder = builder.addLimitConstraint(policy1.getId(), cons);
109         //
110         // Assert there are duplicate constraints associated with the only guard policy
111         //
112         results = builder.buildSpecification();
113         boolean duplicateConstraint = false;
114         for (Message m : results.getMessages()) {
115             if ("Guard policy guardpolicy1 has duplicate limit constraints".equals(m.getMessage())
116                             && m.getLevel() == MessageLevel.WARNING) {
117                 duplicateConstraint = true;
118                 break;
119             }
120         }
121         assertTrue(duplicateConstraint);
122         //
123         // Remove the duplicate constraint
124         //
125         builder = builder.removeLimitConstraint(policy1.getId(), cons);
126         //
127         // Add a duplicate guard policy
128         //
129         builder = builder.addGuardPolicy(policy1);
130         builder = builder.addLimitConstraint(policy1.getId(), cons);
131         //
132         // Assert there are duplicate guard policies
133         //
134         results = builder.buildSpecification();
135         boolean duplicateGuardPolicy = false;
136         for (Message m : results.getMessages()) {
137             if ("There are duplicate guard policies".equals(m.getMessage())
138                             && m.getLevel() == MessageLevel.WARNING) {
139                 duplicateGuardPolicy = true;
140                 break;
141             }
142         }
143         assertTrue(duplicateGuardPolicy);
144         //
145         // Remove the duplicate guard policy
146         //
147         builder = builder.removeGuardPolicy(policy1);
148         //
149         // Assert there are no Error/Warning message
150         //
151         results = builder.buildSpecification();
152         assertTrue(results.getMessages().size() == 1);
153     }
154
155     @Test
156     public void test1() throws Exception {
157         this.test("src/test/resources/v2.0.0-guard/policy_guard_ONAP_demo_vDNS.yaml");
158     }
159
160     @Test
161     public void test2() throws Exception {
162         this.test("src/test/resources/v2.0.0-guard/policy_guard_appc_restart.yaml");
163     }
164
165     /**
166      * Do the actual test.
167      *
168      * @param testFile input test file
169      * @throws Exception if an error occurs
170      */
171     public void test(String testFile) throws Exception {
172         try (InputStream is = new FileInputStream(new File(testFile))) {
173             //
174             // Read the yaml into our Java Object
175             //
176             Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
177             Object obj = yaml.load(is);
178             assertNotNull(obj);
179             assertTrue(obj instanceof ControlLoopGuard);
180             ControlLoopGuard guardTobuild = (ControlLoopGuard) obj;
181             //
182             // Now we're going to try to use the builder to build this.
183             //
184             ControlLoopGuardBuilder builder =
185                             ControlLoopGuardBuilder.Factory.buildControlLoopGuard(guardTobuild.getGuard());
186             //
187             // Add guard policy
188             //
189             if (guardTobuild.getGuards() != null) {
190                 builder = builder.addGuardPolicy(guardTobuild.getGuards().toArray(
191                                 new GuardPolicy[guardTobuild.getGuards().size()]));
192             }
193             //
194             // Build the specification
195             //
196             Results results = builder.buildSpecification();
197             //
198             // Print out the specification
199             //
200             logger.debug(results.getSpecification());
201         }
202     }
203 }