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.openecomp.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.openecomp.policy.controlloop.policy.builder.BuilderException;
38 import org.openecomp.policy.controlloop.policy.builder.Message;
39 import org.openecomp.policy.controlloop.policy.builder.MessageLevel;
40 import org.openecomp.policy.controlloop.policy.builder.Results;
41 import org.openecomp.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 GuardPolicy policy1 = new GuardPolicy("1111", "guardpolicy1", "guardpolicy1", "APPC", "restart");
70 builder = builder.addGuardPolicy(policy1);
72 // Assert there is no limit constraint associated with the only guard policy
74 results = builder.buildSpecification();
75 boolean no_constraint = false;
76 for (Message m : results.getMessages()) {
77 if (m.getMessage().equals("Guard policy guardpolicy1 does not have any limit constraint") && m.getLevel() == MessageLevel.ERROR) {
82 assertTrue(no_constraint);
84 // Add a constraint to policy1
86 Map<String, String> time_in_range = new HashMap<String, String>();
87 time_in_range.put("arg2", "PT5H");
88 time_in_range.put("arg3", "PT24H");
89 List<String> blacklist = new LinkedList<String>();
90 blacklist.add("eNodeB_common_id1");
91 blacklist.add("eNodeB_common_id2");
92 Constraint cons = new Constraint(5, "PT15M", time_in_range, blacklist);
93 builder = builder.addLimitConstraint(policy1.getId(), cons);
95 // Add a duplicate constraint to policy1
97 builder = builder.addLimitConstraint(policy1.getId(), cons);
99 // Assert there are duplicate constraints associated with the only guard policy
101 results = builder.buildSpecification();
102 boolean duplicate_constraint = false;
103 for (Message m : results.getMessages()) {
104 if (m.getMessage().equals("Guard policy guardpolicy1 has duplicate limit constraints") && m.getLevel() == MessageLevel.WARNING) {
105 duplicate_constraint = true;
109 assertTrue(duplicate_constraint);
111 // Remove the duplicate constraint
113 builder = builder.removeLimitConstraint(policy1.getId(), cons);
115 // Add a duplicate guard policy
117 builder = builder.addGuardPolicy(policy1);
118 builder = builder.addLimitConstraint(policy1.getId(), cons);
120 // Assert there are duplicate guard policies
122 results = builder.buildSpecification();
123 boolean duplicate_guard_policy = false;
124 for (Message m : results.getMessages()) {
125 if (m.getMessage().equals("There are duplicate guard policies") && m.getLevel() == MessageLevel.WARNING) {
126 duplicate_guard_policy = true;
130 assertTrue(duplicate_guard_policy);
132 // Remove the duplicate guard policy
134 builder = builder.removeGuardPolicy(policy1);
136 // Assert there are no Error/Warning message
138 results = builder.buildSpecification();
139 assertTrue(results.getMessages().size() == 1);
141 } catch (BuilderException e) {
142 fail(e.getMessage());
147 public void test1() {
148 this.test("src/test/resources/v2.0.0-guard/policy_guard_OpenECOMP_demo_vDNS.yaml");
152 public void test2() {
153 this.test("src/test/resources/v2.0.0-guard/policy_guard_1707_appc.yaml");
156 public void test(String testFile) {
157 try (InputStream is = new FileInputStream(new File(testFile))) {
159 // Read the yaml into our Java Object
161 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
162 Object obj = yaml.load(is);
164 assertTrue(obj instanceof ControlLoopGuard);
165 ControlLoopGuard guardTobuild = (ControlLoopGuard) obj;
167 // Now we're going to try to use the builder to build this.
169 ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(guardTobuild.getGuard());
173 if (guardTobuild.getGuards() != null) {
174 builder = builder.addGuardPolicy(guardTobuild.getGuards().toArray(new GuardPolicy[guardTobuild.getGuards().size()]));
177 // Build the specification
179 Results results = builder.buildSpecification();
181 // Print out the specification
183 System.out.println(results.getSpecification());
185 } catch (FileNotFoundException e) {
186 fail(e.getLocalizedMessage());
187 } catch (IOException e) {
188 fail(e.getLocalizedMessage());
189 } catch (BuilderException e) {
190 fail(e.getLocalizedMessage());