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