aboutsummaryrefslogtreecommitdiffstats
path: root/ECOMP-ControlloopPolicy/src/test/java/org/openecomp/policy/controlloop/policy/guard/ControlLoopGuardBuilderTest.java
blob: 9dff9f33c0ec7121fd3c3452b50e6428967578cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*-
 * ============LICENSE_START=======================================================
 * ECOMP Policy Engine
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.openecomp.policy.controlloop.policy.guard;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.openecomp.policy.controlloop.policy.builder.BuilderException;
import org.openecomp.policy.controlloop.policy.builder.Message;
import org.openecomp.policy.controlloop.policy.builder.MessageLevel;
import org.openecomp.policy.controlloop.policy.builder.Results;
import org.openecomp.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

public class ControlLoopGuardBuilderTest {
	
	@Test
	public void testControlLoopGuard() {
		try {
			//
			// Create a builder
			//
			ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard());
			//
			// Assert there is no guard policies yet
			//
			Results results = builder.buildSpecification();
			boolean no_guard_policies = false;
			for (Message m : results.getMessages()) {
				if (m.getMessage().equals("ControlLoop Guard should have at least one guard policies") && m.getLevel() == MessageLevel.ERROR) {
					no_guard_policies = true;
					break;
				}
			}
			assertTrue(no_guard_policies);
			//
			// Add a guard policy without limit constraint
			//
            String clname = "CL_vUSP123";
            LinkedList<String> targets = new LinkedList<String>();
            targets.add("s1");
            targets.add("s2");
            targets.add("s3");
            MatchParameters matchParameters = new MatchParameters(clname, "APPC", "Restart", targets);
            GuardPolicy policy1 = new GuardPolicy("id123", "guardpolicy1", "description aaa", matchParameters);
			builder = builder.addGuardPolicy(policy1);
			//
			// Assert there is no limit constraint associated with the only guard policy
			//
			results = builder.buildSpecification();
			boolean no_constraint = false;
			for (Message m : results.getMessages()) {
				if (m.getMessage().equals("Guard policy guardpolicy1 does not have any limit constraint") && m.getLevel() == MessageLevel.ERROR) {
					no_constraint = true;
					break;
				}
			}
			assertTrue(no_constraint);
			//
			// Add a constraint to policy1
			//
            Map<String, String> active_time_range = new HashMap<String, String>();
            active_time_range.put("start", "00:00:00-05:00");
            active_time_range.put("end", "23:59:59-05:00");
			List<String> blacklist = new LinkedList<String>();
			blacklist.add("eNodeB_common_id1");
			blacklist.add("eNodeB_common_id2");
            Map<String, String> time_window = new HashMap<String, String>();
            time_window.put("value", "10");
            time_window.put("units", "minute");
            Constraint cons = new Constraint(5, time_window, active_time_range, blacklist);
			builder = builder.addLimitConstraint(policy1.getId(), cons);
			//
			// Add a duplicate constraint to policy1
			//
			builder = builder.addLimitConstraint(policy1.getId(), cons);
			//
			// Assert there are duplicate constraints associated with the only guard policy
			//
			results = builder.buildSpecification();
			boolean duplicate_constraint = false;
			for (Message m : results.getMessages()) {
				if (m.getMessage().equals("Guard policy guardpolicy1 has duplicate limit constraints") && m.getLevel() == MessageLevel.WARNING) {
					duplicate_constraint = true;
					break;
				}
			}
			assertTrue(duplicate_constraint);
			//
			// Remove the duplicate constraint
			//
			builder = builder.removeLimitConstraint(policy1.getId(), cons);
			//
			// Add a duplicate guard policy 
			//
			builder = builder.addGuardPolicy(policy1);
			builder = builder.addLimitConstraint(policy1.getId(), cons);
			//
			// Assert there are duplicate guard policies
			//
			results = builder.buildSpecification();
			boolean duplicate_guard_policy = false;
			for (Message m : results.getMessages()) {
				if (m.getMessage().equals("There are duplicate guard policies") && m.getLevel() == MessageLevel.WARNING) {
					duplicate_guard_policy = true;
					break;
				}
			}
			assertTrue(duplicate_guard_policy);
			//
			// Remove the duplicate guard policy
			//
			builder = builder.removeGuardPolicy(policy1);
			//
			// Assert there are no Error/Warning message
			//
			results = builder.buildSpecification();
			assertTrue(results.getMessages().size() == 1);
			//
		} catch (BuilderException e) {
			fail(e.getMessage());
		}
	}
	
	@Test
	public void test1() {
		this.test("src/test/resources/v2.0.0-guard/policy_guard_OpenECOMP_demo_vDNS.yaml");
	}
	
	@Test
	public void test2() {
		this.test("src/test/resources/v2.0.0-guard/policy_guard_1707_appc.yaml");
	}
	
	public void test(String testFile) {
		try (InputStream is = new FileInputStream(new File(testFile))) {
			//
			// Read the yaml into our Java Object
			//
			Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
			Object obj = yaml.load(is);
			assertNotNull(obj);
			assertTrue(obj instanceof ControlLoopGuard);
			ControlLoopGuard guardTobuild = (ControlLoopGuard) obj;
			//
			// Now we're going to try to use the builder to build this.
			//
			ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(guardTobuild.getGuard());
			//
			// Add guard policy
			//
			if (guardTobuild.getGuards() != null) {
				builder = builder.addGuardPolicy(guardTobuild.getGuards().toArray(new GuardPolicy[guardTobuild.getGuards().size()]));
			}
			//
			// Build the specification
			//
			Results results = builder.buildSpecification();
			//
			// Print out the specification
			//
			System.out.println(results.getSpecification());
			//
		} catch (FileNotFoundException e) {
			fail(e.getLocalizedMessage());
		} catch (IOException e) {
			fail(e.getLocalizedMessage());
		} catch (BuilderException e) {
			fail(e.getLocalizedMessage());
		}
	}
}