remove dependency of drool-application/yaml in actors
[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-2018 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 import static org.junit.Assert.fail;
27
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.HashMap;
34 import java.util.LinkedList;
35 import java.util.List;
36 import java.util.Map;
37
38 import org.junit.Test;
39 import org.onap.policy.controlloop.policy.builder.BuilderException;
40 import org.onap.policy.controlloop.policy.builder.Message;
41 import org.onap.policy.controlloop.policy.builder.MessageLevel;
42 import org.onap.policy.controlloop.policy.builder.Results;
43 import org.onap.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.yaml.snakeyaml.Yaml;
47 import org.yaml.snakeyaml.constructor.Constructor;
48
49 public class ControlLoopGuardBuilderTest {
50     private static final Logger logger = LoggerFactory.getLogger(ControlLoopGuardBuilderTest.class);
51     
52     @Test
53     public void testControlLoopGuard() {
54         try {
55             //
56             // Create a builder
57             //
58             ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard());
59             //
60             // Assert there is no guard policies yet
61             //
62             Results results = builder.buildSpecification();
63             boolean noGuardPolicies = false;
64             for (Message m : results.getMessages()) {
65                 if (m.getMessage().equals("ControlLoop Guard should have at least one guard policies") 
66                                 && m.getLevel() == MessageLevel.ERROR) {
67                     noGuardPolicies = true;
68                     break;
69                 }
70             }
71             assertTrue(noGuardPolicies);
72             //
73             // Add a guard policy without limit constraint
74             //
75             String clname = "CL_vUSP123";
76             LinkedList<String> targets = new LinkedList<String>();
77             targets.add("s1");
78             targets.add("s2");
79             targets.add("s3");
80             MatchParameters matchParameters = new MatchParameters(clname, "APPC", "Restart", targets);
81             GuardPolicy policy1 = new GuardPolicy("id123", "guardpolicy1", "description aaa", matchParameters);
82             builder = builder.addGuardPolicy(policy1);
83             //
84             // Assert there is no limit constraint associated with the only guard policy
85             //
86             results = builder.buildSpecification();
87             boolean noConstraint = false;
88             for (Message m : results.getMessages()) {
89                 if (m.getMessage().equals("Guard policy guardpolicy1 does not have any limit constraint") 
90                                 && m.getLevel() == MessageLevel.ERROR) {
91                     noConstraint = true;
92                     break;
93                 }
94             }
95             assertTrue(noConstraint);
96             //
97             // Add a constraint to policy1
98             //
99             Map<String, String> activeTimeRange = new HashMap<String, String>();
100             activeTimeRange.put("start", "00:00:00-05:00");
101             activeTimeRange.put("end", "23:59:59-05:00");
102             List<String> blacklist = new LinkedList<String>();
103             blacklist.add("eNodeB_common_id1");
104             blacklist.add("eNodeB_common_id2");
105             Map<String, String> timeWindow = new HashMap<String, String>();
106             timeWindow.put("value", "10");
107             timeWindow.put("units", "minute");
108             Constraint cons = new Constraint(5, timeWindow, activeTimeRange, blacklist);
109             builder = builder.addLimitConstraint(policy1.getId(), cons);
110             //
111             // Add a duplicate constraint to policy1
112             //
113             builder = builder.addLimitConstraint(policy1.getId(), cons);
114             //
115             // Assert there are duplicate constraints associated with the only guard policy
116             //
117             results = builder.buildSpecification();
118             boolean duplicateConstraint = false;
119             for (Message m : results.getMessages()) {
120                 if (m.getMessage().equals("Guard policy guardpolicy1 has duplicate limit constraints") 
121                                 && m.getLevel() == MessageLevel.WARNING) {
122                     duplicateConstraint = true;
123                     break;
124                 }
125             }
126             assertTrue(duplicateConstraint);
127             //
128             // Remove the duplicate constraint
129             //
130             builder = builder.removeLimitConstraint(policy1.getId(), cons);
131             //
132             // Add a duplicate guard policy 
133             //
134             builder = builder.addGuardPolicy(policy1);
135             builder = builder.addLimitConstraint(policy1.getId(), cons);
136             //
137             // Assert there are duplicate guard policies
138             //
139             results = builder.buildSpecification();
140             boolean duplicateGuardPolicy = false;
141             for (Message m : results.getMessages()) {
142                 if (m.getMessage().equals("There are duplicate guard policies") 
143                                 && m.getLevel() == MessageLevel.WARNING) {
144                     duplicateGuardPolicy = true;
145                     break;
146                 }
147             }
148             assertTrue(duplicateGuardPolicy);
149             //
150             // Remove the duplicate guard policy
151             //
152             builder = builder.removeGuardPolicy(policy1);
153             //
154             // Assert there are no Error/Warning message
155             //
156             results = builder.buildSpecification();
157             assertTrue(results.getMessages().size() == 1);
158             //
159         } catch (BuilderException e) {
160             fail(e.getMessage());
161         }
162     }
163     
164     @Test
165     public void test1() {
166         this.test("src/test/resources/v2.0.0-guard/policy_guard_ONAP_demo_vDNS.yaml");
167     }
168     
169     @Test
170     public void test2() {
171         this.test("src/test/resources/v2.0.0-guard/policy_guard_appc_restart.yaml");
172     }
173     
174     /**
175      * Do the actual test.
176      * 
177      * @param testFile input test file
178      */
179     public void test(String testFile) {
180         try (InputStream is = new FileInputStream(new File(testFile))) {
181             //
182             // Read the yaml into our Java Object
183             //
184             Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
185             Object obj = yaml.load(is);
186             assertNotNull(obj);
187             assertTrue(obj instanceof ControlLoopGuard);
188             ControlLoopGuard guardTobuild = (ControlLoopGuard) obj;
189             //
190             // Now we're going to try to use the builder to build this.
191             //
192             ControlLoopGuardBuilder builder = 
193                             ControlLoopGuardBuilder.Factory.buildControlLoopGuard(guardTobuild.getGuard());
194             //
195             // Add guard policy
196             //
197             if (guardTobuild.getGuards() != null) {
198                 builder = builder.addGuardPolicy(guardTobuild.getGuards().toArray(
199                                 new GuardPolicy[guardTobuild.getGuards().size()]));
200             }
201             //
202             // Build the specification
203             //
204             Results results = builder.buildSpecification();
205             //
206             // Print out the specification
207             //
208             logger.debug(results.getSpecification());
209             //
210         } catch (FileNotFoundException e) {
211             fail(e.getLocalizedMessage());
212         } catch (IOException e) {
213             fail(e.getLocalizedMessage());
214         } catch (BuilderException e) {
215             fail(e.getLocalizedMessage());
216         }
217     }
218 }