da81db394ed898fa3d288c87d10d08a5efa8bc82
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-yaml unit test
4  * ================================================================================
5  * Copyright (C) 2017 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.yaml.snakeyaml.Yaml;
44 import org.yaml.snakeyaml.constructor.Constructor;
45
46 public class ControlLoopGuardBuilderTest {
47     
48     @Test
49     public void testControlLoopGuard() {
50         try {
51             //
52             // Create a builder
53             //
54             ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard());
55             //
56             // Assert there is no guard policies yet
57             //
58             Results results = builder.buildSpecification();
59             boolean no_guard_policies = false;
60             for (Message m : results.getMessages()) {
61                 if (m.getMessage().equals("ControlLoop Guard should have at least one guard policies") && m.getLevel() == MessageLevel.ERROR) {
62                     no_guard_policies = true;
63                     break;
64                 }
65             }
66             assertTrue(no_guard_policies);
67             //
68             // Add a guard policy without limit constraint
69             //
70             String clname = "CL_vUSP123";
71             LinkedList<String> targets = new LinkedList<String>();
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 no_constraint = false;
83             for (Message m : results.getMessages()) {
84                 if (m.getMessage().equals("Guard policy guardpolicy1 does not have any limit constraint") && m.getLevel() == MessageLevel.ERROR) {
85                     no_constraint = true;
86                     break;
87                 }
88             }
89             assertTrue(no_constraint);
90             //
91             // Add a constraint to policy1
92             //
93             Map<String, String> active_time_range = new HashMap<String, String>();
94             active_time_range.put("start", "00:00:00-05:00");
95             active_time_range.put("end", "23:59:59-05:00");
96             List<String> blacklist = new LinkedList<String>();
97             blacklist.add("eNodeB_common_id1");
98             blacklist.add("eNodeB_common_id2");
99             Map<String, String> time_window = new HashMap<String, String>();
100             time_window.put("value", "10");
101             time_window.put("units", "minute");
102             Constraint cons = new Constraint(5, time_window, active_time_range, blacklist);
103             builder = builder.addLimitConstraint(policy1.getId(), cons);
104             //
105             // Add a duplicate constraint to policy1
106             //
107             builder = builder.addLimitConstraint(policy1.getId(), cons);
108             //
109             // Assert there are duplicate constraints associated with the only guard policy
110             //
111             results = builder.buildSpecification();
112             boolean duplicate_constraint = false;
113             for (Message m : results.getMessages()) {
114                 if (m.getMessage().equals("Guard policy guardpolicy1 has duplicate limit constraints") && m.getLevel() == MessageLevel.WARNING) {
115                     duplicate_constraint = true;
116                     break;
117                 }
118             }
119             assertTrue(duplicate_constraint);
120             //
121             // Remove the duplicate constraint
122             //
123             builder = builder.removeLimitConstraint(policy1.getId(), cons);
124             //
125             // Add a duplicate guard policy 
126             //
127             builder = builder.addGuardPolicy(policy1);
128             builder = builder.addLimitConstraint(policy1.getId(), cons);
129             //
130             // Assert there are duplicate guard policies
131             //
132             results = builder.buildSpecification();
133             boolean duplicate_guard_policy = false;
134             for (Message m : results.getMessages()) {
135                 if (m.getMessage().equals("There are duplicate guard policies") && m.getLevel() == MessageLevel.WARNING) {
136                     duplicate_guard_policy = true;
137                     break;
138                 }
139             }
140             assertTrue(duplicate_guard_policy);
141             //
142             // Remove the duplicate guard policy
143             //
144             builder = builder.removeGuardPolicy(policy1);
145             //
146             // Assert there are no Error/Warning message
147             //
148             results = builder.buildSpecification();
149             assertTrue(results.getMessages().size() == 1);
150             //
151         } catch (BuilderException e) {
152             fail(e.getMessage());
153         }
154     }
155     
156     @Test
157     public void test1() {
158         this.test("src/test/resources/v2.0.0-guard/policy_guard_ONAP_demo_vDNS.yaml");
159     }
160     
161     @Test
162     public void test2() {
163         this.test("src/test/resources/v2.0.0-guard/policy_guard_appc_restart.yaml");
164     }
165     
166     public void test(String testFile) {
167         try (InputStream is = new FileInputStream(new File(testFile))) {
168             //
169             // Read the yaml into our Java Object
170             //
171             Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
172             Object obj = yaml.load(is);
173             assertNotNull(obj);
174             assertTrue(obj instanceof ControlLoopGuard);
175             ControlLoopGuard guardTobuild = (ControlLoopGuard) obj;
176             //
177             // Now we're going to try to use the builder to build this.
178             //
179             ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(guardTobuild.getGuard());
180             //
181             // Add guard policy
182             //
183             if (guardTobuild.getGuards() != null) {
184                 builder = builder.addGuardPolicy(guardTobuild.getGuards().toArray(new GuardPolicy[guardTobuild.getGuards().size()]));
185             }
186             //
187             // Build the specification
188             //
189             Results results = builder.buildSpecification();
190             //
191             // Print out the specification
192             //
193             System.out.println(results.getSpecification());
194             //
195         } catch (FileNotFoundException e) {
196             fail(e.getLocalizedMessage());
197         } catch (IOException e) {
198             fail(e.getLocalizedMessage());
199         } catch (BuilderException e) {
200             fail(e.getLocalizedMessage());
201         }
202     }
203 }