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