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