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 static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
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;
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;
48 public class ControlLoopGuardBuilderTest {
49 private static final Logger logger = LoggerFactory.getLogger(ControlLoopGuardBuilderTest.class);
51 public void testControlLoopGuard() {
56 ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard());
58 // Assert there is no guard policies yet
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;
68 assertTrue(no_guard_policies);
70 // Add a guard policy without limit constraint
72 String clname = "CL_vUSP123";
73 LinkedList<String> targets = new LinkedList<String>();
77 MatchParameters matchParameters = new MatchParameters(clname, "APPC", "Restart", targets);
78 GuardPolicy policy1 = new GuardPolicy("id123", "guardpolicy1", "description aaa", matchParameters);
79 builder = builder.addGuardPolicy(policy1);
81 // Assert there is no limit constraint associated with the only guard policy
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) {
91 assertTrue(no_constraint);
93 // Add a constraint to policy1
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);
107 // Add a duplicate constraint to policy1
109 builder = builder.addLimitConstraint(policy1.getId(), cons);
111 // Assert there are duplicate constraints associated with the only guard policy
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;
121 assertTrue(duplicate_constraint);
123 // Remove the duplicate constraint
125 builder = builder.removeLimitConstraint(policy1.getId(), cons);
127 // Add a duplicate guard policy
129 builder = builder.addGuardPolicy(policy1);
130 builder = builder.addLimitConstraint(policy1.getId(), cons);
132 // Assert there are duplicate guard policies
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;
142 assertTrue(duplicate_guard_policy);
144 // Remove the duplicate guard policy
146 builder = builder.removeGuardPolicy(policy1);
148 // Assert there are no Error/Warning message
150 results = builder.buildSpecification();
151 assertTrue(results.getMessages().size() == 1);
153 } catch (BuilderException e) {
154 fail(e.getMessage());
159 public void test1() {
160 this.test("src/test/resources/v2.0.0-guard/policy_guard_ONAP_demo_vDNS.yaml");
164 public void test2() {
165 this.test("src/test/resources/v2.0.0-guard/policy_guard_appc_restart.yaml");
168 public void test(String testFile) {
169 try (InputStream is = new FileInputStream(new File(testFile))) {
171 // Read the yaml into our Java Object
173 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
174 Object obj = yaml.load(is);
176 assertTrue(obj instanceof ControlLoopGuard);
177 ControlLoopGuard guardTobuild = (ControlLoopGuard) obj;
179 // Now we're going to try to use the builder to build this.
181 ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(guardTobuild.getGuard());
185 if (guardTobuild.getGuards() != null) {
186 builder = builder.addGuardPolicy(guardTobuild.getGuards().toArray(new GuardPolicy[guardTobuild.getGuards().size()]));
189 // Build the specification
191 Results results = builder.buildSpecification();
193 // Print out the specification
195 logger.debug(results.getSpecification());
197 } catch (FileNotFoundException e) {
198 fail(e.getLocalizedMessage());
199 } catch (IOException e) {
200 fail(e.getLocalizedMessage());
201 } catch (BuilderException e) {
202 fail(e.getLocalizedMessage());