2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.time.Instant;
29 import java.util.HashMap;
30 import java.util.UUID;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
36 import org.onap.policy.common.endpoints.event.comm.TopicListener;
37 import org.onap.policy.common.endpoints.event.comm.TopicSink;
38 import org.onap.policy.controlloop.ControlLoopEventStatus;
39 import org.onap.policy.controlloop.ControlLoopNotificationType;
40 import org.onap.policy.controlloop.VirtualControlLoopEvent;
41 import org.onap.policy.controlloop.VirtualControlLoopNotification;
42 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
43 import org.onap.policy.so.SoRequest;
45 public class VdnsControlLoopCqTest extends ControlLoopBase implements TopicListener {
48 * Setup the simulator.
51 public static void setUpBeforeClass() {
52 ControlLoopBase.setUpBeforeClass(
53 "../archetype-cl-amsterdam/src/main/resources/archetype-resources/"
54 + "src/main/resources/__closedLoopControlName__.drl",
55 "src/test/resources/yaml/policy_ControlLoop_SO_Cq-test.yaml", "type=operational",
57 SupportUtil.setCustomQuery("true");
61 public static void tearDownAfterClass() {
62 SupportUtil.setCustomQuery("false");
63 ControlLoopBase.tearDownAfterClass();
67 public void successTest() {
70 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
71 * to be pulled from the queue
73 for (TopicSink sink : noopTopics) {
74 assertTrue(sink.start());
79 * Create a unique requestId
81 requestId = UUID.randomUUID();
84 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
87 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET);
89 kieSession.fireUntilHalt();
91 // allow object clean-up
92 kieSession.fireAllRules();
95 * The only fact in memory should be Params
97 assertEquals(1, kieSession.getFactCount());
100 * Print what's left in memory
102 dumpFacts(kieSession);
106 public void aaiGetFailTest() {
109 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
110 * to be pulled from the queue
112 for (TopicSink sink : noopTopics) {
113 assertTrue(sink.start());
118 * Create a unique requestId
120 requestId = UUID.randomUUID();
123 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
126 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "getFail");
129 kieSession.fireUntilHalt();
131 // allow object clean-up
132 kieSession.fireAllRules();
134 } catch (Exception e) {
136 logger.warn(e.toString());
137 fail(e.getMessage());
141 * The only fact in memory should be Params
143 assertEquals(1, kieSession.getFactCount());
146 * Print what's left in memory
148 dumpFacts(kieSession);
154 * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
157 public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
159 * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
163 if ("POLICY-CL-MGT".equals(topic)) {
164 obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
165 org.onap.policy.controlloop.VirtualControlLoopNotification.class);
168 if (obj instanceof VirtualControlLoopNotification) {
169 VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
170 String policyName = notification.getPolicyName();
171 if (policyName.endsWith("EVENT")) {
172 logger.debug("Rule Fired: " + notification.getPolicyName());
174 ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
175 } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
176 logger.debug("Rule Fired: " + notification.getPolicyName());
178 ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
179 assertNotNull(notification.getMessage());
180 assertTrue(notification.getMessage().startsWith("Sending guard query"));
181 } else if (policyName.endsWith("GUARD.RESPONSE")) {
182 logger.debug("Rule Fired: " + notification.getPolicyName());
184 ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
185 assertNotNull(notification.getMessage());
186 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
187 } else if (policyName.endsWith("GUARD_PERMITTED")) {
188 logger.debug("Rule Fired: " + notification.getPolicyName());
190 ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
191 assertNotNull(notification.getMessage());
192 assertTrue(notification.getMessage().startsWith("actor=SO"));
193 } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
194 logger.debug("Rule Fired: " + notification.getPolicyName());
196 logger.debug("The operation timed out");
197 fail("Operation Timed Out");
198 } else if (policyName.endsWith("SO.RESPONSE")) {
199 logger.debug("Rule Fired: " + notification.getPolicyName());
200 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS
201 .equals(notification.getNotification()));
202 assertNotNull(notification.getMessage());
203 assertTrue(notification.getMessage().startsWith("actor=SO"));
204 } else if (policyName.endsWith("EVENT.MANAGER")) {
205 logger.debug("Rule Fired: " + notification.getPolicyName());
206 if ("error".equals(notification.getAai().get("vserver.vserver-name"))) {
207 assertEquals(ControlLoopNotificationType.FINAL_FAILURE,
208 notification.getNotification());
209 } else if ("getFail".equals(notification.getAai().get("vserver.vserver-name"))) {
210 assertEquals(ControlLoopNotificationType.FINAL_FAILURE,
211 notification.getNotification());
213 assertTrue(ControlLoopNotificationType.FINAL_SUCCESS
214 .equals(notification.getNotification()));
217 } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
218 logger.debug("Rule Fired: " + notification.getPolicyName());
220 logger.debug("The control loop timed out");
221 fail("Control Loop Timed Out");
223 } else if (obj instanceof SoRequest) {
224 logger.debug("\n============ SO received the request!!! ===========\n");
229 * This method is used to simulate event messages from DCAE that start the control loop (onset
230 * message) or end the control loop (abatement message).
232 * @param policy the controlLoopName comes from the policy
233 * @param requestId the requestId for this event
234 * @param status could be onset or abated
236 protected void sendEvent(ControlLoopPolicy policy, UUID requestId,
237 ControlLoopEventStatus status) {
238 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
239 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
240 event.setRequestId(requestId);
241 event.setTarget("vserver.vserver-name");
242 event.setClosedLoopAlarmStart(Instant.now());
243 event.setAai(new HashMap<>());
244 event.getAai().put("vserver.vserver-name", "Ete_vFWCLvFWSNK_7ba1fbde_0");
245 event.getAai().put("vserver.is-closed-loop-disabled", "false");
246 event.getAai().put("vserver.prov-status", "ACTIVE");
247 event.setClosedLoopEventStatus(status);
248 kieSession.insert(event);
251 protected void sendEvent(ControlLoopPolicy policy, UUID requestId,
252 ControlLoopEventStatus status, String vserverName) {
253 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
254 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
255 event.setRequestId(requestId);
256 event.setTarget("vserver.vserver-name");
257 event.setClosedLoopAlarmStart(Instant.now());
258 event.setAai(new HashMap<>());
259 event.getAai().put("vserver.vserver-name", vserverName);
260 event.setClosedLoopEventStatus(status);
261 kieSession.insert(event);