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.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.VirtualControlLoopEvent;
44 import org.onap.policy.controlloop.VirtualControlLoopNotification;
45 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
46 import org.onap.policy.drools.event.comm.TopicEndpoint;
47 import org.onap.policy.drools.event.comm.TopicListener;
48 import org.onap.policy.drools.event.comm.TopicSink;
49 import org.onap.policy.drools.event.comm.Topic.CommInfrastructure;
50 import org.onap.policy.drools.http.server.HttpServletServer;
51 import org.onap.policy.drools.properties.PolicyProperties;
52 import org.onap.policy.drools.protocol.coders.EventProtocolCoder;
53 import org.onap.policy.drools.protocol.coders.JsonProtocolFilter;
54 import org.onap.policy.drools.system.PolicyEngine;
55 import org.onap.policy.so.SORequest;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
59 public class VDNSControlLoopTest implements TopicListener {
61 private static final Logger logger = LoggerFactory.getLogger(VDNSControlLoopTest.class);
63 private static List<? extends TopicSink> noopTopics;
65 private KieSession kieSession;
66 private Util.Pair<ControlLoopPolicy, String> pair;
67 private UUID requestID;
70 /* Set environment properties */
78 public static void setUpSimulator() {
79 PolicyEngine.manager.configure(new Properties());
80 assertTrue(PolicyEngine.manager.start());
81 Properties noopSinkProperties = new Properties();
82 noopSinkProperties.put(PolicyProperties.PROPERTY_NOOP_SINK_TOPICS, "POLICY-CL-MGT");
83 noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events", "org.onap.policy.controlloop.VirtualControlLoopNotification");
84 noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events.custom.gson", "org.onap.policy.controlloop.util.Serialization,gsonPretty");
85 noopTopics = TopicEndpoint.manager.addTopicSinks(noopSinkProperties);
87 EventProtocolCoder.manager.addEncoder("junit.groupId", "junit.artifactId", "POLICY-CL-MGT", "org.onap.policy.controlloop.VirtualControlLoopNotification", new JsonProtocolFilter(), null, null, 1111);
93 } catch (Exception e) {
99 public static void tearDownSimulator() {
100 HttpServletServer.factory.destroy();
101 PolicyEngine.manager.shutdown();
105 public void successTest() {
108 * Start the kie session
111 kieSession = startSession("../archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl",
112 "src/test/resources/yaml/policy_ControlLoop_SO-test.yaml",
116 } catch (IOException e) {
118 logger.debug("Could not create kieSession");
119 fail("Could not create kieSession");
123 * Allows the PolicyEngine to callback to this object to
124 * notify that there is an event ready to be pulled
127 for (TopicSink sink : noopTopics) {
128 assertTrue(sink.start());
133 * Create a unique requestId
135 requestID = UUID.randomUUID();
138 * Simulate an onset event the policy engine will
139 * receive from DCAE to kick off processing through
142 sendEvent(pair.a, requestID, ControlLoopEventStatus.ONSET);
144 kieSession.fireUntilHalt();
147 * The only fact in memory should be Params
149 assertEquals(1, kieSession.getFactCount());
152 * Print what's left in memory
154 dumpFacts(kieSession);
157 * Gracefully shut down the kie session
159 kieSession.dispose();
163 * This method will start a kie session and instantiate
166 * @param droolsTemplate
169 * the yaml file containing the policies
174 * @param policyVersion
175 * version of the policy
176 * @return the kieSession to be used to insert facts
177 * @throws IOException
179 private KieSession startSession(String droolsTemplate,
183 String policyVersion) throws IOException {
186 * Load policies from yaml
188 pair = Util.loadYaml(yamlFile);
190 assertNotNull(pair.a);
191 assertNotNull(pair.a.getControlLoop());
192 assertNotNull(pair.a.getControlLoop().getControlLoopName());
193 assertTrue(pair.a.getControlLoop().getControlLoopName().length() > 0);
196 * Construct a kie session
198 final KieSession kieSession = Util.buildContainer(droolsTemplate,
199 pair.a.getControlLoop().getControlLoopName(),
203 URLEncoder.encode(pair.b, "UTF-8"));
206 * Retrieve the Policy Engine
209 logger.debug("============");
210 logger.debug(URLEncoder.encode(pair.b, "UTF-8"));
211 logger.debug("============");
218 * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
220 public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
222 * Pull the object that was sent out to DMAAP and make
223 * sure it is a ControlLoopNoticiation of type active
226 if ("POLICY-CL-MGT".equals(topic)) {
227 obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event, org.onap.policy.controlloop.VirtualControlLoopNotification.class);
230 if (obj instanceof VirtualControlLoopNotification) {
231 VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
232 String policyName = notification.policyName;
233 if (policyName.endsWith("EVENT")) {
234 logger.debug("Rule Fired: " + notification.policyName);
235 assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.notification));
237 else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
238 logger.debug("Rule Fired: " + notification.policyName);
239 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.notification));
240 assertNotNull(notification.message);
241 assertTrue(notification.message.startsWith("Sending guard query"));
243 else if (policyName.endsWith("GUARD.RESPONSE")) {
244 logger.debug("Rule Fired: " + notification.policyName);
245 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.notification));
246 assertNotNull(notification.message);
247 assertTrue(notification.message.toLowerCase().endsWith("permit"));
249 else if (policyName.endsWith("GUARD_PERMITTED")) {
250 logger.debug("Rule Fired: " + notification.policyName);
251 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.notification));
252 assertNotNull(notification.message);
253 assertTrue(notification.message.startsWith("actor=SO"));
255 else if (policyName.endsWith("OPERATION.TIMEOUT")) {
256 logger.debug("Rule Fired: " + notification.policyName);
258 logger.debug("The operation timed out");
259 fail("Operation Timed Out");
261 else if (policyName.endsWith("SO.RESPONSE")) {
262 logger.debug("Rule Fired: " + notification.policyName);
263 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS.equals(notification.notification));
264 assertNotNull(notification.message);
265 assertTrue(notification.message.startsWith("actor=SO"));
267 else if (policyName.endsWith("EVENT.MANAGER")) {
268 logger.debug("Rule Fired: " + notification.policyName);
269 assertTrue(ControlLoopNotificationType.FINAL_SUCCESS.equals(notification.notification));
272 else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
273 logger.debug("Rule Fired: " + notification.policyName);
275 logger.debug("The control loop timed out");
276 fail("Control Loop Timed Out");
279 else if (obj instanceof SORequest) {
280 logger.debug("\n============ SO received the request!!! ===========\n");
285 * This method is used to simulate event messages from DCAE
286 * that start the control loop (onset message) or end the
287 * control loop (abatement message).
289 * @param policy the controlLoopName comes from the policy
290 * @param requestID the requestId for this event
291 * @param status could be onset or abated
293 protected void sendEvent(ControlLoopPolicy policy, UUID requestID, ControlLoopEventStatus status) {
294 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
295 event.closedLoopControlName = policy.getControlLoop().getControlLoopName();
296 event.requestID = requestID;
297 event.target = "vserver.vserver-name";
298 event.closedLoopAlarmStart = Instant.now();
299 event.AAI = new HashMap<>();
300 event.AAI.put("vserver.vserver-name", "dfw1lb01lb01");
301 event.AAI.put("vserver.is-closed-loop-disabled", "false");
302 event.closedLoopEventStatus = status;
303 kieSession.insert(event);
307 * This method will dump all the facts in the working memory.
309 * @param kieSession the session containing the facts
311 public void dumpFacts(KieSession kieSession) {
312 logger.debug("Fact Count: {}", kieSession.getFactCount());
313 for (FactHandle handle : kieSession.getFactHandles()) {
314 logger.debug("FACT: {}", handle);