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