2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
28 import java.io.IOException;
29 import java.net.URLEncoder;
30 import java.time.Instant;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Properties;
34 import java.util.UUID;
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.kie.api.runtime.KieSession;
40 import org.kie.api.runtime.rule.FactHandle;
41 import org.onap.policy.appc.Request;
42 import org.onap.policy.appc.Response;
43 import org.onap.policy.appc.ResponseCode;
44 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
45 import org.onap.policy.common.endpoints.event.comm.TopicEndpoint;
46 import org.onap.policy.common.endpoints.event.comm.TopicListener;
47 import org.onap.policy.common.endpoints.event.comm.TopicSink;
48 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
49 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
50 import org.onap.policy.controlloop.ControlLoopEventStatus;
51 import org.onap.policy.controlloop.ControlLoopNotificationType;
52 import org.onap.policy.controlloop.VirtualControlLoopEvent;
53 import org.onap.policy.controlloop.VirtualControlLoopNotification;
54 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
55 import org.onap.policy.drools.protocol.coders.EventProtocolCoder;
56 import org.onap.policy.drools.protocol.coders.JsonProtocolFilter;
57 import org.onap.policy.drools.system.PolicyController;
58 import org.onap.policy.drools.system.PolicyEngine;
59 import org.onap.policy.drools.utils.logging.LoggerUtil;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
63 public class VFWControlLoopTest implements TopicListener {
65 private static final Logger logger = LoggerFactory.getLogger(VFWControlLoopTest.class);
67 private static List<? extends TopicSink> noopTopics;
69 private static KieSession kieSession;
70 private static Util.Pair<ControlLoopPolicy, String> pair;
71 private UUID requestID;
74 /* Set environment properties */
78 LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO");
82 public static void setUpSimulator() {
83 PolicyEngine.manager.configure(new Properties());
84 assertTrue(PolicyEngine.manager.start());
85 Properties noopSinkProperties = new Properties();
86 noopSinkProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS, "APPC-CL,POLICY-CL-MGT");
87 noopSinkProperties.put("noop.sink.topics.APPC-CL.events", "org.onap.policy.appc.Response");
88 noopSinkProperties.put("noop.sink.topics.APPC-CL.events.custom.gson",
89 "org.onap.policy.appc.util.Serialization,gsonPretty");
90 noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events",
91 "org.onap.policy.controlloop.VirtualControlLoopNotification");
92 noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events.custom.gson",
93 "org.onap.policy.controlloop.util.Serialization,gsonPretty");
94 noopTopics = TopicEndpoint.manager.addTopicSinks(noopSinkProperties);
96 EventProtocolCoder.manager.addEncoder("junit.groupId", "junit.artifactId", "POLICY-CL-MGT",
97 "org.onap.policy.controlloop.VirtualControlLoopNotification", new JsonProtocolFilter(), null, null,
99 EventProtocolCoder.manager.addEncoder("junit.groupId", "junit.artifactId", "APPC-CL",
100 "org.onap.policy.appc.Request", new JsonProtocolFilter(), null, null, 1111);
104 Util.buildGuardSim();
105 } catch (Exception e) {
106 fail(e.getMessage());
110 * Start the kie session
113 kieSession = startSession(
114 "../archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl",
115 "src/test/resources/yaml/policy_ControlLoop_vFW.yaml",
116 "service=ServiceDemo;resource=Res1Demo;type=operational", "CL_vFW",
117 "org.onap.closed_loop.ServiceDemo:VNFS:1.0.0");
118 } catch (IOException e) {
120 logger.debug("Could not create kieSession");
121 fail("Could not create kieSession");
126 public static void tearDownSimulator() {
128 * Gracefully shut down the kie session
130 kieSession.dispose();
132 PolicyEngine.manager.stop();
133 HttpServletServer.factory.destroy();
134 PolicyController.factory.shutdown();
135 TopicEndpoint.manager.shutdown();
139 public void successTest() {
142 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
143 * to be pulled from the queue
145 for (TopicSink sink : noopTopics) {
146 assertTrue(sink.start());
151 * Create a unique requestId
153 requestID = UUID.randomUUID();
156 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
159 sendEvent(pair.a, requestID, ControlLoopEventStatus.ONSET);
162 kieSession.fireUntilHalt();
163 } catch (Exception e) {
165 logger.warn(e.toString());
171 * The only fact in memory should be Params
173 assertEquals(1, kieSession.getFactCount());
176 * Print what's left in memory
178 dumpFacts(kieSession);
182 public void aaiFailTests() {
185 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
186 * to be pulled from the queue
188 for (TopicSink sink : noopTopics) {
189 assertTrue(sink.start());
194 * Create a unique requestId
196 requestID = UUID.randomUUID();
199 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
202 sendEvent(pair.a, requestID, ControlLoopEventStatus.ONSET, "error");
204 kieSession.fireUntilHalt();
205 } catch (Exception e) {
207 logger.warn(e.toString());
208 fail(e.getMessage());
212 * The only fact in memory should be Params
214 assertEquals(1, kieSession.getFactCount());
217 * Print what's left in memory
219 dumpFacts(kieSession);
222 * Create a unique requestId
224 requestID = UUID.randomUUID();
227 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
231 sendEvent(pair.a, requestID, ControlLoopEventStatus.ONSET, "getFail");
234 kieSession.fireUntilHalt();
235 } catch (Exception e) {
237 logger.warn(e.toString());
238 fail(e.getMessage());
242 * The only fact in memory should be Params
244 assertEquals(1, kieSession.getFactCount());
247 * Print what's left in memory
249 dumpFacts(kieSession);
253 * This method will start a kie session and instantiate the Policy Engine.
255 * @param droolsTemplate the DRL rules file
256 * @param yamlFile the yaml file containing the policies
257 * @param policyScope scope for policy
258 * @param policyName name of the policy
259 * @param policyVersion version of the policy
260 * @return the kieSession to be used to insert facts
261 * @throws IOException
263 private static KieSession startSession(String droolsTemplate, String yamlFile, String policyScope,
264 String policyName, String policyVersion) throws IOException {
267 * Load policies from yaml
269 pair = Util.loadYaml(yamlFile);
271 assertNotNull(pair.a);
272 assertNotNull(pair.a.getControlLoop());
273 assertNotNull(pair.a.getControlLoop().getControlLoopName());
274 assertTrue(pair.a.getControlLoop().getControlLoopName().length() > 0);
277 * Construct a kie session
279 final KieSession kieSession = Util.buildContainer(droolsTemplate, pair.a.getControlLoop().getControlLoopName(),
280 policyScope, policyName, policyVersion, URLEncoder.encode(pair.b, "UTF-8"));
283 * Retrieve the Policy Engine
286 logger.debug("============");
287 logger.debug(URLEncoder.encode(pair.b, "UTF-8"));
288 logger.debug("============");
294 * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
297 public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
299 * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
303 if ("POLICY-CL-MGT".equals(topic)) {
304 obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
305 org.onap.policy.controlloop.VirtualControlLoopNotification.class);
306 } else if ("APPC-CL".equals(topic)) {
307 obj = org.onap.policy.appc.util.Serialization.gsonPretty.fromJson(event,
308 org.onap.policy.appc.Request.class);
311 if (obj instanceof VirtualControlLoopNotification) {
312 VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
313 String policyName = notification.getPolicyName();
314 if (policyName.endsWith("EVENT")) {
315 logger.debug("Rule Fired: " + notification.getPolicyName());
316 assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
317 } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
318 logger.debug("Rule Fired: " + notification.getPolicyName());
319 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
320 assertNotNull(notification.getMessage());
321 assertTrue(notification.getMessage().startsWith("Sending guard query"));
322 } else if (policyName.endsWith("GUARD.RESPONSE")) {
323 logger.debug("Rule Fired: " + notification.getPolicyName());
324 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
325 assertNotNull(notification.getMessage());
326 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
327 } else if (policyName.endsWith("GUARD_PERMITTED")) {
328 logger.debug("Rule Fired: " + notification.getPolicyName());
329 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
330 assertNotNull(notification.getMessage());
331 assertTrue(notification.getMessage().startsWith("actor=APPC"));
332 } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
333 logger.debug("Rule Fired: " + notification.getPolicyName());
335 logger.debug("The operation timed out");
336 fail("Operation Timed Out");
337 } else if (policyName.endsWith("APPC.RESPONSE")) {
338 logger.debug("Rule Fired: " + notification.getPolicyName());
339 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS.equals(notification.getNotification()));
340 assertNotNull(notification.getMessage());
341 assertTrue(notification.getMessage().startsWith("actor=APPC"));
342 sendEvent(pair.a, requestID, ControlLoopEventStatus.ABATED);
343 } else if (policyName.endsWith("EVENT.MANAGER")) {
344 logger.debug("Rule Fired: " + notification.getPolicyName());
345 if ("error".equals(notification.getAai().get("generic-vnf.vnf-name"))) {
346 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
347 assertEquals("Target vnf-id could not be found", notification.getMessage());
348 } else if ("getFail".equals(notification.getAai().get("generic-vnf.vnf-name"))) {
349 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
351 assertTrue(ControlLoopNotificationType.FINAL_SUCCESS.equals(notification.getNotification()));
354 } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
355 logger.debug("Rule Fired: " + notification.getPolicyName());
357 logger.debug("The control loop timed out");
358 fail("Control Loop Timed Out");
360 } else if (obj instanceof Request) {
361 assertTrue(((Request) obj).getCommonHeader().getSubRequestId().equals("1"));
362 assertNotNull(((Request) obj).getPayload().get("generic-vnf.vnf-id"));
364 logger.debug("\n============ APPC received the request!!! ===========\n");
367 * Simulate a success response from APPC and insert the response into the working memory
369 Response appcResponse = new Response((Request) obj);
370 appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
371 appcResponse.getStatus().setValue("SUCCESS");
372 kieSession.insert(appcResponse);
377 * This method is used to simulate event messages from DCAE that start the control loop (onset
378 * message) or end the control loop (abatement message).
380 * @param policy the controlLoopName comes from the policy
381 * @param requestID the requestId for this event
382 * @param status could be onset or abated
384 protected void sendEvent(ControlLoopPolicy policy, UUID requestID, ControlLoopEventStatus status) {
385 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
386 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
387 event.setRequestId(requestID);
388 event.setTarget("generic-vnf.vnf-name");
389 event.setClosedLoopAlarmStart(Instant.now());
390 event.setAai(new HashMap<>());
391 event.getAai().put("generic-vnf.vnf-name", "testGenericVnfID");
392 event.setClosedLoopEventStatus(status);
393 kieSession.insert(event);
397 * This method is used to simulate event messages from DCAE that start the control loop (onset
398 * message) or end the control loop (abatement message).
400 * @param policy the controlLoopName comes from the policy
401 * @param requestID the requestId for this event
402 * @param status could be onset or abated
404 protected void sendEvent(ControlLoopPolicy policy, UUID requestID, ControlLoopEventStatus status, String vnfId) {
405 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
406 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
407 event.setRequestId(requestID);
408 event.setTarget("generic-vnf.vnf-name");
409 event.setClosedLoopAlarmStart(Instant.now());
410 event.setAai(new HashMap<>());
411 event.getAai().put("generic-vnf.vnf-name", vnfId);
412 event.setClosedLoopEventStatus(status);
413 kieSession.insert(event);
417 * This method will dump all the facts in the working memory.
419 * @param kieSession the session containing the facts
421 public void dumpFacts(KieSession kieSession) {
422 logger.debug("Fact Count: {}", kieSession.getFactCount());
423 for (FactHandle handle : kieSession.getFactHandles()) {
424 logger.debug("FACT: {}", handle);