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.yaml.snakeyaml.Yaml;
44 import org.yaml.snakeyaml.constructor.Constructor;
46 public class ControlLoopGuardBuilderTest {
49 public void testControlLoopGuard() {
54 ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard());
56 // Assert there is no guard policies yet
58 Results results = builder.buildSpecification();
59 boolean no_guard_policies = false;
60 for (Message m : results.getMessages()) {
61 if (m.getMessage().equals("ControlLoop Guard should have at least one guard policies") && m.getLevel() == MessageLevel.ERROR) {
62 no_guard_policies = true;
66 assertTrue(no_guard_policies);
68 // Add a guard policy without limit constraint
70 String clname = "CL_vUSP123";
71 LinkedList<String> targets = new LinkedList<String>();
75 MatchParameters matchParameters = new MatchParameters(clname, "APPC", "Restart", targets);
76 GuardPolicy policy1 = new GuardPolicy("id123", "guardpolicy1", "description aaa", matchParameters);
77 builder = builder.addGuardPolicy(policy1);
79 // Assert there is no limit constraint associated with the only guard policy
81 results = builder.buildSpecification();
82 boolean no_constraint = false;
83 for (Message m : results.getMessages()) {
84 if (m.getMessage().equals("Guard policy guardpolicy1 does not have any limit constraint") && m.getLevel() == MessageLevel.ERROR) {
89 assertTrue(no_constraint);
91 // Add a constraint to policy1
93 Map<String, String> active_time_range = new HashMap<String, String>();
94 active_time_range.put("start", "00:00:00-05:00");
95 active_time_range.put("end", "23:59:59-05:00");
96 List<String> blacklist = new LinkedList<String>();
97 blacklist.add("eNodeB_common_id1");
98 blacklist.add("eNodeB_common_id2");
99 Map<String, String> time_window = new HashMap<String, String>();
100 time_window.put("value", "10");
101 time_window.put("units", "minute");
102 Constraint cons = new Constraint(5, time_window, active_time_range, blacklist);
103 builder = builder.addLimitConstraint(policy1.getId(), cons);
105 // Add a duplicate constraint to policy1
107 builder = builder.addLimitConstraint(policy1.getId(), cons);
109 // Assert there are duplicate constraints associated with the only guard policy
111 results = builder.buildSpecification();
112 boolean duplicate_constraint = false;
113 for (Message m : results.getMessages()) {
114 if (m.getMessage().equals("Guard policy guardpolicy1 has duplicate limit constraints") && m.getLevel() == MessageLevel.WARNING) {
115 duplicate_constraint = true;
119 assertTrue(duplicate_constraint);
121 // Remove the duplicate constraint
123 builder = builder.removeLimitConstraint(policy1.getId(), cons);
125 // Add a duplicate guard policy
127 builder = builder.addGuardPolicy(policy1);
128 builder = builder.addLimitConstraint(policy1.getId(), cons);
130 // Assert there are duplicate guard policies
132 results = builder.buildSpecification();
133 boolean duplicate_guard_policy = false;
134 for (Message m : results.getMessages()) {
135 if (m.getMessage().equals("There are duplicate guard policies") && m.getLevel() == MessageLevel.WARNING) {
136 duplicate_guard_policy = true;
140 assertTrue(duplicate_guard_policy);
142 // Remove the duplicate guard policy
144 builder = builder.removeGuardPolicy(policy1);
146 // Assert there are no Error/Warning message
148 results = builder.buildSpecification();
149 assertTrue(results.getMessages().size() == 1);
151 } catch (BuilderException e) {
152 fail(e.getMessage());
157 public void test1() {
158 this.test("src/test/resources/v2.0.0-guard/policy_guard_ONAP_demo_vDNS.yaml");
162 public void test2() {
163 this.test("src/test/resources/v2.0.0-guard/policy_guard_appc_restart.yaml");
166 public void test(String testFile) {
167 try (InputStream is = new FileInputStream(new File(testFile))) {
169 // Read the yaml into our Java Object
171 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
172 Object obj = yaml.load(is);
174 assertTrue(obj instanceof ControlLoopGuard);
175 ControlLoopGuard guardTobuild = (ControlLoopGuard) obj;
177 // Now we're going to try to use the builder to build this.
179 ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(guardTobuild.getGuard());
183 if (guardTobuild.getGuards() != null) {
184 builder = builder.addGuardPolicy(guardTobuild.getGuards().toArray(new GuardPolicy[guardTobuild.getGuards().size()]));
187 // Build the specification
189 Results results = builder.buildSpecification();
191 // Print out the specification
193 System.out.println(results.getSpecification());
195 } catch (FileNotFoundException e) {
196 fail(e.getLocalizedMessage());
197 } catch (IOException e) {
198 fail(e.getLocalizedMessage());
199 } catch (BuilderException e) {
200 fail(e.getLocalizedMessage());