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=========================================================
21 package org.onap.policy.template.demo;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
29 import java.io.IOException;
30 import java.net.URLEncoder;
31 import java.time.Instant;
32 import java.util.HashMap;
33 import java.util.UUID;
35 import org.junit.Ignore;
36 import org.junit.Test;
37 import org.kie.api.runtime.KieSession;
38 import org.kie.api.runtime.rule.FactHandle;
39 import org.onap.policy.appc.Request;
40 import org.onap.policy.appc.Response;
41 import org.onap.policy.appc.ResponseCode;
42 import org.onap.policy.appc.ResponseValue;
43 import org.onap.policy.controlloop.ControlLoopEventStatus;
44 import org.onap.policy.controlloop.ControlLoopNotificationType;
46 import org.onap.policy.controlloop.VirtualControlLoopEvent;
47 import org.onap.policy.controlloop.VirtualControlLoopNotification;
48 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
49 import org.onap.policy.controlloop.policy.TargetType;
50 import org.onap.policy.drools.impl.PolicyEngineJUnitImpl;
51 import org.onap.policy.guard.PolicyGuard;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 //import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;
57 public class ControlLoopXacmlGuardTest {
58 private static final Logger logger = LoggerFactory.getLogger(ControlLoopXacmlGuardTest.class);
63 this.runTest("src/main/resources/ControlLoop_Template_xacml_guard.drl",
64 "src/test/resources/yaml/policy_ControlLoop_Service123.yaml",
65 "service=Service123;resource=Res123;type=operational",
67 "org.onap.closed_loop.Service123:VNFS:0.0.1");
68 } catch (IOException e) {
74 public void runTest(String droolsTemplate,
78 String policyVersion) throws IOException {
80 // Pull info from the yaml
82 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml(yamlFile);
84 assertNotNull(pair.a);
85 assertNotNull(pair.a.getControlLoop());
86 assertNotNull(pair.a.getControlLoop().getControlLoopName());
87 assertTrue(pair.a.getControlLoop().getControlLoopName().length() > 0);
91 final KieSession kieSession = Util.buildContainer(droolsTemplate,
92 pair.a.getControlLoop().getControlLoopName(),
96 URLEncoder.encode(pair.b, "UTF-8"));
99 logger.debug("============");
100 logger.debug(URLEncoder.encode(pair.b, "UTF-8"));
101 logger.debug("============");
103 final PolicyEngineJUnitImpl engine = (PolicyEngineJUnitImpl) kieSession.getGlobal("Engine");
106 // Initial fire of rules
108 kieSession.fireAllRules();
110 // Kick a thread that starts testing
112 new Thread(new Runnable() {
121 // Let's use a unique ID for the request and
122 // a unique trigger source.
124 UUID requestID = UUID.randomUUID();
125 String triggerSourceName = "foobartriggersource36";
129 sendGoodEvents(kieSession, pair.a, requestID, triggerSourceName);
130 obj = engine.subscribe("UEB", "POLICY-CL-MGT");
132 assertTrue(obj instanceof VirtualControlLoopNotification);
133 assertTrue(((VirtualControlLoopNotification)obj).notification.equals(ControlLoopNotificationType.ACTIVE));
135 // Give the control loop a little time to acquire the lock and publish the request
140 // "About to query Guard" notification (Querying about Restart)
141 obj = engine.subscribe("UEB", "POLICY-CL-MGT");
143 logger.debug("\n\n####################### GOING TO QUERY GUARD about Restart!!!!!!");
144 logger.debug("Rule: {} Message {}", ((VirtualControlLoopNotification)obj).policyName, ((VirtualControlLoopNotification)obj).message);
145 assertTrue(obj instanceof VirtualControlLoopNotification);
146 assertTrue(((VirtualControlLoopNotification)obj).notification.equals(ControlLoopNotificationType.OPERATION));
149 // "Response from Guard" notification
150 obj = engine.subscribe("UEB", "POLICY-CL-MGT");
152 logger.debug("Rule: {} Message {}", ((VirtualControlLoopNotification)obj).policyName, ((VirtualControlLoopNotification)obj).message);
153 assertTrue(obj instanceof VirtualControlLoopNotification);
154 assertTrue(((VirtualControlLoopNotification)obj).notification.equals(ControlLoopNotificationType.OPERATION));
157 if(true == ((VirtualControlLoopNotification)obj).message.contains("Guard result: Deny")){
159 // "About to query Guard" notification (Querying about Rebuild)
160 obj = engine.subscribe("UEB", "POLICY-CL-MGT");
162 logger.debug("\n\n####################### GOING TO QUERY GUARD about Rebuild!!!!!!");
163 logger.debug("Rule: {} Message", ((VirtualControlLoopNotification)obj).policyName, ((VirtualControlLoopNotification)obj).message);
164 assertTrue(obj instanceof VirtualControlLoopNotification);
165 assertTrue(((VirtualControlLoopNotification)obj).notification.equals(ControlLoopNotificationType.OPERATION));
169 // "Response from Guard" notification
170 obj = engine.subscribe("UEB", "POLICY-CL-MGT");
172 logger.debug("Rule: {} Message {}", ((VirtualControlLoopNotification)obj).policyName, ((VirtualControlLoopNotification)obj).message);
173 assertTrue(obj instanceof VirtualControlLoopNotification);
174 assertTrue(((VirtualControlLoopNotification)obj).notification.equals(ControlLoopNotificationType.OPERATION));
177 if(true == ((VirtualControlLoopNotification)obj).message.contains("Guard result: Deny")){
179 // "About to query Guard" notification (Querying about Migrate)
180 obj = engine.subscribe("UEB", "POLICY-CL-MGT");
182 logger.debug("\n\n####################### GOING TO QUERY GUARD!!!!!!");
183 logger.debug("Rule: {} Message {}", ((VirtualControlLoopNotification)obj).policyName, ((VirtualControlLoopNotification)obj).message);
184 assertTrue(obj instanceof VirtualControlLoopNotification);
185 assertTrue(((VirtualControlLoopNotification)obj).notification.equals(ControlLoopNotificationType.OPERATION));
189 // "Response from Guard" notification
190 obj = engine.subscribe("UEB", "POLICY-CL-MGT");
192 logger.debug("Rule: " + ((VirtualControlLoopNotification)obj).policyName +" Message: " + ((VirtualControlLoopNotification)obj).message);
193 assertTrue(obj instanceof VirtualControlLoopNotification);
194 assertTrue(((VirtualControlLoopNotification)obj).notification.equals(ControlLoopNotificationType.OPERATION));
197 if(true == ((VirtualControlLoopNotification)obj).message.contains("Guard result: Deny")){
198 //All the 3 operations were Denied by Guard
206 // In case one of the operations was permitted by Guard
208 if(true == ((VirtualControlLoopNotification)obj).message.contains("Guard result: Permit")){
209 obj = engine.subscribe("UEB", "POLICY-CL-MGT");
211 logger.debug("Rule: {} Message {}", ((VirtualControlLoopNotification)obj).policyName, ((VirtualControlLoopNotification)obj).message);
212 assertTrue(obj instanceof VirtualControlLoopNotification);
213 assertTrue(((VirtualControlLoopNotification)obj).notification.equals(ControlLoopNotificationType.OPERATION));
217 obj = engine.subscribe("UEB", "APPC-CL");
219 assertTrue(obj instanceof Request);
220 assertTrue(((Request)obj).CommonHeader.SubRequestID.equals("1"));
222 logger.debug("\n============ APP-C Got request!!! ===========\n");
224 // Ok - let's simulate ACCEPT
228 // now wait for it to finish
233 // Now we are going to success it
235 Response response = new Response((Request) obj);
236 response.Status.Code = ResponseCode.SUCCESS.getValue();
237 response.Status.Value = ResponseValue.SUCCESS.toString();
238 response.Status.Description = "AppC success";
239 kieSession.insert(response);
241 // Give it some time to process
245 // Insert the abatement event
247 sendAbatement(kieSession, pair.a, requestID, triggerSourceName);
249 // now wait for it to finish
253 // Ensure they released the lock
255 assertFalse(PolicyGuard.isLocked(TargetType.VM, triggerSourceName, requestID));
261 } catch (InterruptedException e) {
262 logger.error("Test thread got InterruptedException ", e.getLocalizedMessage());
263 } catch (AssertionError e) {
264 logger.error("Test thread got AssertionError ", e.getLocalizedMessage());
266 } catch (Exception e) {
267 logger.error("Test thread got Exception ", e.getLocalizedMessage());
275 // Start firing rules
277 kieSession.fireUntilHalt();
279 // Dump working memory
281 dumpFacts(kieSession);
283 // See if there is anything left in memory
285 assertEquals(1, kieSession.getFactCount());
287 for (FactHandle handle : kieSession.getFactHandles()) {
288 Object fact = kieSession.getObject(handle);
289 assertEquals("", "org.onap.policy.controlloop.Params", fact.getClass().getName());
296 public static void dumpFacts(KieSession kieSession) {
297 logger.debug("Fact Count: {}", kieSession.getFactCount());
298 for (FactHandle handle : kieSession.getFactHandles()) {
299 logger.debug("FACT: {}", handle);
303 protected void sendAbatement(KieSession kieSession, ControlLoopPolicy policy, UUID requestID, String triggerSourceName) throws InterruptedException {
304 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
305 event.closedLoopControlName = policy.getControlLoop().getControlLoopName();
306 event.requestID = requestID;
307 event.target = "vserver.vserver-name";
308 event.closedLoopAlarmStart = Instant.now().minusSeconds(5);
309 event.closedLoopAlarmEnd = Instant.now();
310 event.AAI = new HashMap<String, String>();
311 event.AAI.put("cloud-region.identity-url", "foo");
312 event.AAI.put("vserver.selflink", "bar");
313 event.AAI.put("vserver.is-closed-loop-disabled", "false");
314 event.AAI.put("generic-vnf.vnf-name", "testGenericVnfName");
315 event.closedLoopEventStatus = ControlLoopEventStatus.ABATED;
316 kieSession.insert(event);
319 protected void sendGoodEvents(KieSession kieSession, ControlLoopPolicy policy, UUID requestID, String triggerSourceName) throws InterruptedException {
320 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
321 event.closedLoopControlName = policy.getControlLoop().getControlLoopName();
322 event.requestID = requestID;
323 event.target = "vserver.vserver-name";
324 event.closedLoopAlarmStart = Instant.now();
325 event.AAI = new HashMap<String, String>();
326 event.AAI.put("cloud-region.identity-url", "foo");
327 event.AAI.put("vserver.selflink", "bar");
328 event.AAI.put("vserver.is-closed-loop-disabled", "false");
329 event.AAI.put("vserver.vserver-name", "testGenericVnfName");
330 event.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
331 kieSession.insert(event);
336 protected void sendBadEvents(KieSession kieSession, ControlLoopPolicy policy, UUID requestID, String triggerSourceName) throws InterruptedException {
338 // Insert a bad Event
340 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
341 event.closedLoopControlName = policy.getControlLoop().getControlLoopName();
342 kieSession.insert(event);
345 // add the request id
347 event.requestID = requestID;
348 kieSession.insert(event);
353 event.AAI = new HashMap<String, String>();
354 event.AAI.put("cloud-region.identity-url", "foo");
355 event.AAI.put("vserver.selflink", "bar");
356 event.AAI.put("vserver.vserver-name", "vmfoo");
357 kieSession.insert(event);
360 // set a valid status
362 event.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
363 kieSession.insert(event);
366 // add a trigger sourcename
368 kieSession.insert(event);
371 // add is closed-loop-disabled
373 event.AAI.put("vserver.is-closed-loop-disabled", "true");
374 kieSession.insert(event);
379 event.AAI.put("vserver.is-closed-loop-disabled", "false");
380 kieSession.insert(event);
383 // Add target, but bad.
385 event.target = "VM_BLAH";
386 kieSession.insert(event);