2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 Intel Corp. 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.controlloop.ControlLoopEventStatus;
42 import org.onap.policy.controlloop.ControlLoopNotificationType;
43 import org.onap.policy.controlloop.ControlLoopTargetType;
44 import org.onap.policy.controlloop.VirtualControlLoopEvent;
45 import org.onap.policy.controlloop.VirtualControlLoopNotification;
46 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
47 import org.onap.policy.drools.event.comm.TopicEndpoint;
48 import org.onap.policy.drools.event.comm.TopicListener;
49 import org.onap.policy.drools.event.comm.TopicSink;
50 import org.onap.policy.drools.event.comm.Topic.CommInfrastructure;
51 import org.onap.policy.drools.http.server.HttpServletServer;
52 import org.onap.policy.drools.properties.PolicyProperties;
53 import org.onap.policy.drools.protocol.coders.EventProtocolCoder;
54 import org.onap.policy.drools.protocol.coders.JsonProtocolFilter;
55 import org.onap.policy.drools.system.PolicyController;
56 import org.onap.policy.drools.system.PolicyEngine;
57 import org.onap.policy.drools.utils.LoggerUtil;
58 import org.onap.policy.vfc.VFCRequest;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
63 public class VFCControlLoopTest implements TopicListener {
65 private static final Logger logger = LoggerFactory.getLogger(VFCControlLoopTest.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 */
79 LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO");
83 public static void setUpSimulator() {
84 PolicyEngine.manager.configure(new Properties());
85 assertTrue(PolicyEngine.manager.start());
86 Properties noopSinkProperties = new Properties();
87 noopSinkProperties.put(PolicyProperties.PROPERTY_NOOP_SINK_TOPICS, "POLICY-CL-MGT");
88 noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events", "org.onap.policy.controlloop.VirtualControlLoopNotification");
89 noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events.custom.gson", "org.onap.policy.controlloop.util.Serialization,gsonPretty");
90 noopTopics = TopicEndpoint.manager.addTopicSinks(noopSinkProperties);
92 EventProtocolCoder.manager.addEncoder("junit.groupId", "junit.artifactId", "POLICY-CL-MGT", "org.onap.policy.controlloop.VirtualControlLoopNotification", new JsonProtocolFilter(), null, null, 1111);
98 } catch (Exception e) {
103 * Start the kie session
106 kieSession = startSession("../archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl",
107 "src/test/resources/yaml/policy_ControlLoop_VFC.yaml",
111 } catch (IOException e) {
113 logger.debug("Could not create kieSession");
114 fail("Could not create kieSession");
119 public static void tearDownSimulator() {
122 * Gracefully shut down the kie session
124 kieSession.dispose();
126 PolicyEngine.manager.stop();
127 HttpServletServer.factory.destroy();
128 PolicyController.factory.shutdown();
129 TopicEndpoint.manager.shutdown();
133 public void successTest() throws IOException {
136 * Allows the PolicyEngine to callback to this object to
137 * notify that there is an event ready to be pulled
140 for (TopicSink sink : noopTopics) {
141 assertTrue(sink.start());
146 * Create a unique requestId
148 requestID = UUID.randomUUID();
151 * Simulate an onset event the policy engine will
152 * receive from DCAE to kick off processing through
155 sendEvent(pair.a, requestID, ControlLoopEventStatus.ONSET);
157 kieSession.fireUntilHalt();
160 * The only fact in memory should be Params
162 assertEquals(1, kieSession.getFactCount());
165 * Print what's left in memory
167 dumpFacts(kieSession);
171 * This method will start a kie session and instantiate
174 * @param droolsTemplate
177 * the yaml file containing the policies
182 * @param policyVersion
183 * version of the policy
184 * @return the kieSession to be used to insert facts
185 * @throws IOException
187 private static KieSession startSession(String droolsTemplate,
191 String policyVersion) throws IOException {
194 * Load policies from yaml
196 pair = Util.loadYaml(yamlFile);
198 assertNotNull(pair.a);
199 assertNotNull(pair.a.getControlLoop());
200 assertNotNull(pair.a.getControlLoop().getControlLoopName());
201 assertTrue(pair.a.getControlLoop().getControlLoopName().length() > 0);
204 * Construct a kie session
206 final KieSession kieSession = Util.buildContainer(droolsTemplate,
207 pair.a.getControlLoop().getControlLoopName(),
211 URLEncoder.encode(pair.b, "UTF-8"));
214 * Retrieve the Policy Engine
217 logger.debug("============");
218 logger.debug(URLEncoder.encode(pair.b, "UTF-8"));
219 logger.debug("============");
226 * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
228 public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
230 * Pull the object that was sent out to DMAAP and make
231 * sure it is a ControlLoopNoticiation of type active
234 if ("POLICY-CL-MGT".equals(topic)) {
235 obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event, org.onap.policy.controlloop.VirtualControlLoopNotification.class);
238 if (obj instanceof VirtualControlLoopNotification) {
239 VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
240 String policyName = notification.policyName;
241 if (policyName.endsWith("EVENT")) {
242 logger.debug("Rule Fired: " + notification.policyName);
243 assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.notification));
245 else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
246 logger.debug("Rule Fired: " + notification.policyName);
247 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.notification));
248 assertNotNull(notification.message);
249 assertTrue(notification.message.startsWith("Sending guard query"));
251 else if (policyName.endsWith("GUARD.RESPONSE")) {
252 logger.debug("Rule Fired: " + notification.policyName);
253 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.notification));
254 assertNotNull(notification.message);
255 assertTrue(notification.message.toLowerCase().endsWith("permit"));
257 else if (policyName.endsWith("GUARD_PERMITTED")) {
258 logger.debug("Rule Fired: " + notification.policyName);
259 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.notification));
260 assertNotNull(notification.message);
261 assertTrue(notification.message.startsWith("actor=VFC"));
263 else if (policyName.endsWith("OPERATION.TIMEOUT")) {
264 logger.debug("Rule Fired: " + notification.policyName);
266 logger.debug("The operation timed out");
267 fail("Operation Timed Out");
269 else if (policyName.endsWith("VFC.RESPONSE")) {
270 logger.debug("Rule Fired: " + notification.policyName);
271 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS.equals(notification.notification));
272 assertNotNull(notification.message);
273 assertTrue(notification.message.startsWith("actor=VFC"));
275 else if (policyName.endsWith("EVENT.MANAGER")) {
276 logger.debug("Rule Fired: " + notification.policyName);
277 assertTrue(ControlLoopNotificationType.FINAL_SUCCESS.equals(notification.notification));
280 else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
281 logger.debug("Rule Fired: " + notification.policyName);
283 logger.debug("The control loop timed out");
284 fail("Control Loop Timed Out");
287 else if (obj instanceof VFCRequest) {
288 logger.debug("\n============ VFC received the request!!! ===========\n");
293 * This method is used to simulate event messages from DCAE
294 * that start the control loop (onset message) or end the
295 * control loop (abatement message).
297 * @param policy the controlLoopName comes from the policy
298 * @param requestID the requestId for this event
299 * @param status could be onset or abated
301 protected void sendEvent(ControlLoopPolicy policy, UUID requestID, ControlLoopEventStatus status) {
302 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
303 event.closedLoopControlName = policy.getControlLoop().getControlLoopName();
304 event.requestID = UUID.randomUUID();
305 event.closedLoopEventClient = "tca.instance00009";
306 event.target_type = ControlLoopTargetType.VM;
307 event.target = "VM_NAME";
309 event.closedLoopAlarmStart = Instant.now();
310 event.AAI = new HashMap<String, String>();
311 event.AAI.put("vserver.vserver-name", "vserver-name-16102016-aai3255-data-11-1");
312 event.AAI.put("vserver.vserver-id", "vserver-id-16102016-aai3255-data-11-1");
313 event.AAI.put("generic-vnf.vnf-id", "vnf-id-16102016-aai3255-data-11-1");
314 event.AAI.put("service-instance.service-instance-id", "service-instance-id-16102016-aai3255-data-11-1");
315 event.AAI.put("vserver.is-closed-loop-disabled", "false");
316 event.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
317 kieSession.insert(event);
320 public static void dumpFacts(KieSession kieSession) {
321 logger.debug("Fact Count: " + kieSession.getFactCount());
322 for (FactHandle handle : kieSession.getFactHandles()) {
323 logger.debug("FACT: " + handle);