2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.policy.controlloop.policy.guard;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
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;
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;
45 public class ControlLoopGuardBuilderTest {
48 public void testControlLoopGuard() {
53 ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard());
55 // Assert there is no guard policies yet
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;
65 assertTrue(no_guard_policies);
67 // Add a guard policy without limit constraint
69 String clname = "CL_vUSP123";
70 LinkedList<String> targets = new LinkedList<String>();
74 MatchParameters matchParameters = new MatchParameters(clname, "APPC", "Restart", targets);
75 GuardPolicy policy1 = new GuardPolicy("id123", "guardpolicy1", "description aaa", matchParameters);
76 builder = builder.addGuardPolicy(policy1);
78 // Assert there is no limit constraint associated with the only guard policy
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) {
88 assertTrue(no_constraint);
90 // Add a constraint to policy1
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);
104 // Add a duplicate constraint to policy1
106 builder = builder.addLimitConstraint(policy1.getId(), cons);
108 // Assert there are duplicate constraints associated with the only guard policy
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;
118 assertTrue(duplicate_constraint);
120 // Remove the duplicate constraint
122 builder = builder.removeLimitConstraint(policy1.getId(), cons);
124 // Add a duplicate guard policy
126 builder = builder.addGuardPolicy(policy1);
127 builder = builder.addLimitConstraint(policy1.getId(), cons);
129 // Assert there are duplicate guard policies
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;
139 assertTrue(duplicate_guard_policy);
141 // Remove the duplicate guard policy
143 builder = builder.removeGuardPolicy(policy1);
145 // Assert there are no Error/Warning message
147 results = builder.buildSpecification();
148 assertTrue(results.getMessages().size() == 1);
150 } catch (BuilderException e) {
151 fail(e.getMessage());
156 public void test1() {
157 this.test("src/test/resources/v2.0.0-guard/policy_guard_ONAP_demo_vDNS.yaml");
161 public void test2() {
162 this.test("src/test/resources/v2.0.0-guard/policy_guard_1707_appc.yaml");
165 public void test(String testFile) {
166 try (InputStream is = new FileInputStream(new File(testFile))) {
168 // Read the yaml into our Java Object
170 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
171 Object obj = yaml.load(is);
173 assertTrue(obj instanceof ControlLoopGuard);
174 ControlLoopGuard guardTobuild = (ControlLoopGuard) obj;
176 // Now we're going to try to use the builder to build this.
178 ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(guardTobuild.getGuard());
182 if (guardTobuild.getGuards() != null) {
183 builder = builder.addGuardPolicy(guardTobuild.getGuards().toArray(new GuardPolicy[guardTobuild.getGuards().size()]));
186 // Build the specification
188 Results results = builder.buildSpecification();
190 // Print out the specification
192 System.out.println(results.getSpecification());
194 } catch (FileNotFoundException e) {
195 fail(e.getLocalizedMessage());
196 } catch (IOException e) {
197 fail(e.getLocalizedMessage());
198 } catch (BuilderException e) {
199 fail(e.getLocalizedMessage());