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;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
34 import org.onap.policy.common.endpoints.event.comm.TopicListener;
35 import org.onap.policy.common.endpoints.event.comm.TopicSink;
36 import org.onap.policy.controlloop.ControlLoopEventStatus;
37 import org.onap.policy.controlloop.ControlLoopNotificationType;
38 import org.onap.policy.controlloop.VirtualControlLoopEvent;
39 import org.onap.policy.controlloop.VirtualControlLoopNotification;
40 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
41 import org.onap.policy.so.SoRequest;
43 public class VdnsControlLoopCqTest extends ControlLoopBase implements TopicListener {
46 * Setup the simulator.
49 public static void setUpBeforeClass() {
50 ControlLoopBase.setUpBeforeClass(
51 "../archetype-cl-amsterdam/src/main/resources/archetype-resources/"
52 + "src/main/resources/__closedLoopControlName__.drl",
53 "src/test/resources/yaml/policy_ControlLoop_SO_Cq-test.yaml", "type=operational",
55 SupportUtil.setCustomQuery("true");
59 public void successTest() {
62 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
63 * to be pulled from the queue
65 for (TopicSink sink : noopTopics) {
66 assertTrue(sink.start());
71 * Create a unique requestId
73 requestId = UUID.randomUUID();
76 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
79 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET);
81 kieSession.fireUntilHalt();
83 // allow object clean-up
84 kieSession.fireAllRules();
87 * The only fact in memory should be Params
89 assertEquals(1, kieSession.getFactCount());
92 * Print what's left in memory
94 dumpFacts(kieSession);
98 public void aaiGetFailTest() {
101 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
102 * to be pulled from the queue
104 for (TopicSink sink : noopTopics) {
105 assertTrue(sink.start());
110 * Create a unique requestId
112 requestId = UUID.randomUUID();
115 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
118 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "getFail");
121 kieSession.fireUntilHalt();
123 // allow object clean-up
124 kieSession.fireAllRules();
126 } catch (Exception e) {
128 logger.warn(e.toString());
129 fail(e.getMessage());
133 * The only fact in memory should be Params
135 assertEquals(1, kieSession.getFactCount());
138 * Print what's left in memory
140 dumpFacts(kieSession);
146 * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
149 public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
151 * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
155 if ("POLICY-CL-MGT".equals(topic)) {
156 obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
157 org.onap.policy.controlloop.VirtualControlLoopNotification.class);
160 if (obj instanceof VirtualControlLoopNotification) {
161 VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
162 String policyName = notification.getPolicyName();
163 if (policyName.endsWith("EVENT")) {
164 logger.debug("Rule Fired: " + notification.getPolicyName());
166 ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
167 } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
168 logger.debug("Rule Fired: " + notification.getPolicyName());
170 ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
171 assertNotNull(notification.getMessage());
172 assertTrue(notification.getMessage().startsWith("Sending guard query"));
173 } else if (policyName.endsWith("GUARD.RESPONSE")) {
174 logger.debug("Rule Fired: " + notification.getPolicyName());
176 ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
177 assertNotNull(notification.getMessage());
178 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
179 } else if (policyName.endsWith("GUARD_PERMITTED")) {
180 logger.debug("Rule Fired: " + notification.getPolicyName());
182 ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
183 assertNotNull(notification.getMessage());
184 assertTrue(notification.getMessage().startsWith("actor=SO"));
185 } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
186 logger.debug("Rule Fired: " + notification.getPolicyName());
188 logger.debug("The operation timed out");
189 fail("Operation Timed Out");
190 } else if (policyName.endsWith("SO.RESPONSE")) {
191 logger.debug("Rule Fired: " + notification.getPolicyName());
192 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS
193 .equals(notification.getNotification()));
194 assertNotNull(notification.getMessage());
195 assertTrue(notification.getMessage().startsWith("actor=SO"));
196 } else if (policyName.endsWith("EVENT.MANAGER")) {
197 logger.debug("Rule Fired: " + notification.getPolicyName());
198 if ("error".equals(notification.getAai().get("vserver.vserver-name"))) {
199 assertEquals(ControlLoopNotificationType.FINAL_FAILURE,
200 notification.getNotification());
201 } else if ("getFail".equals(notification.getAai().get("vserver.vserver-name"))) {
202 assertEquals(ControlLoopNotificationType.FINAL_FAILURE,
203 notification.getNotification());
205 assertTrue(ControlLoopNotificationType.FINAL_SUCCESS
206 .equals(notification.getNotification()));
209 } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
210 logger.debug("Rule Fired: " + notification.getPolicyName());
212 logger.debug("The control loop timed out");
213 fail("Control Loop Timed Out");
215 } else if (obj instanceof SoRequest) {
216 logger.debug("\n============ SO received the request!!! ===========\n");
221 * This method is used to simulate event messages from DCAE that start the control loop (onset
222 * message) or end the control loop (abatement message).
224 * @param policy the controlLoopName comes from the policy
225 * @param requestId the requestId for this event
226 * @param status could be onset or abated
228 protected void sendEvent(ControlLoopPolicy policy, UUID requestId,
229 ControlLoopEventStatus status) {
230 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
231 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
232 event.setRequestId(requestId);
233 event.setTarget("vserver.vserver-name");
234 event.setClosedLoopAlarmStart(Instant.now());
235 event.setAai(new HashMap<>());
236 event.getAai().put("vserver.vserver-name", "Ete_vFWCLvFWSNK_7ba1fbde_0");
237 event.getAai().put("vserver.is-closed-loop-disabled", "false");
238 event.getAai().put("vserver.prov-status", "ACTIVE");
239 event.setClosedLoopEventStatus(status);
240 kieSession.insert(event);
243 protected void sendEvent(ControlLoopPolicy policy, UUID requestId,
244 ControlLoopEventStatus status, String vserverName) {
245 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
246 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
247 event.setRequestId(requestId);
248 event.setTarget("vserver.vserver-name");
249 event.setClosedLoopAlarmStart(Instant.now());
250 event.setAai(new HashMap<>());
251 event.getAai().put("vserver.vserver-name", vserverName);
252 event.setClosedLoopEventStatus(status);
253 kieSession.insert(event);