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
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=========================================================
21 package org.onap.policy.controlloop.policy.guard;
23 import org.junit.Test;
24 import org.yaml.snakeyaml.Yaml;
25 import org.yaml.snakeyaml.constructor.Constructor;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.junit.Assert.fail;
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;
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;
47 public class ControlLoopGuardBuilderTest {
50 public void testControlLoopGuard() {
55 ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard());
57 // Assert there is no guard policies yet
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;
67 assertTrue(no_guard_policies);
69 // Add a guard policy without limit constraint
71 String clname = "CL_vUSP123";
72 LinkedList<String> targets = new LinkedList<String>();
76 MatchParameters matchParameters = new MatchParameters(clname, "APPC", "Restart", targets);
77 GuardPolicy policy1 = new GuardPolicy("id123", "guardpolicy1", "description aaa", matchParameters);
78 builder = builder.addGuardPolicy(policy1);
80 // Assert there is no limit constraint associated with the only guard policy
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) {
90 assertTrue(no_constraint);
92 // Add a constraint to policy1
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);
106 // Add a duplicate constraint to policy1
108 builder = builder.addLimitConstraint(policy1.id, cons);
110 // Assert there are duplicate constraints associated with the only guard policy
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;
120 assertTrue(duplicate_constraint);
122 // Remove the duplicate constraint
124 builder = builder.removeLimitConstraint(policy1.id, cons);
126 // Add a duplicate guard policy
128 builder = builder.addGuardPolicy(policy1);
129 builder = builder.addLimitConstraint(policy1.id, cons);
131 // Assert there are duplicate guard policies
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;
141 assertTrue(duplicate_guard_policy);
143 // Remove the duplicate guard policy
145 builder = builder.removeGuardPolicy(policy1);
147 // Assert there are no Error/Warning message
149 results = builder.buildSpecification();
150 assertTrue(results.getMessages().size() == 1);
152 } catch (BuilderException e) {
153 fail(e.getMessage());
158 public void test1() {
159 this.test("src/test/resources/v2.0.0-guard/policy_guard_ONAP_demo_vDNS.yaml");
163 public void test2() {
164 this.test("src/test/resources/v2.0.0-guard/policy_guard_appc_restart.yaml");
167 public void test(String testFile) {
168 try (InputStream is = new FileInputStream(new File(testFile))) {
170 // Read the yaml into our Java Object
172 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
173 Object obj = yaml.load(is);
175 assertTrue(obj instanceof ControlLoopGuard);
176 ControlLoopGuard guardTobuild = (ControlLoopGuard) obj;
178 // Now we're going to try to use the builder to build this.
180 ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(guardTobuild.guard);
184 if (guardTobuild.guards != null) {
185 builder = builder.addGuardPolicy(guardTobuild.guards.toArray(new GuardPolicy[guardTobuild.guards.size()]));
188 // Build the specification
190 Results results = builder.buildSpecification();
192 // Print out the specification
194 System.out.println(results.getSpecification());
196 } catch (FileNotFoundException e) {
197 fail(e.getLocalizedMessage());
198 } catch (IOException e) {
199 fail(e.getLocalizedMessage());
200 } catch (BuilderException e) {
201 fail(e.getLocalizedMessage());