2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 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.ControlLoopTargetType;
39 import org.onap.policy.controlloop.VirtualControlLoopEvent;
40 import org.onap.policy.controlloop.VirtualControlLoopNotification;
41 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
42 import org.onap.policy.so.SoRequest;
44 public class VdnsControlLoopCqTest extends ControlLoopBase implements TopicListener {
47 * Setup the simulator.
50 public static void setUpBeforeClass() {
51 ControlLoopBase.setUpBeforeClass(
52 "../archetype-cl-amsterdam/src/main/resources/archetype-resources/"
53 + "src/main/resources/__closedLoopControlName__.drl",
54 "src/test/resources/yaml/policy_ControlLoop_SO_Cq-test.yaml", "type=operational",
56 SupportUtil.setCustomQuery("true");
60 public void testSuccess() {
63 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
64 * to be pulled from the queue
66 for (TopicSink sink : noopTopics) {
67 assertTrue(sink.start());
72 * Create a unique requestId
74 requestId = UUID.randomUUID();
77 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
80 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET);
82 kieSession.fireUntilHalt();
84 // allow object clean-up
85 kieSession.fireAllRules();
88 * The only fact in memory should be Params
90 assertEquals(1, kieSession.getFactCount());
93 * Print what's left in memory
95 dumpFacts(kieSession);
99 public void testAaiGetFail() {
102 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
103 * to be pulled from the queue
105 for (TopicSink sink : noopTopics) {
106 assertTrue(sink.start());
111 * Create a unique requestId
113 requestId = UUID.randomUUID();
116 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
119 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "getFail");
122 kieSession.fireUntilHalt();
124 // allow object clean-up
125 kieSession.fireAllRules();
127 } catch (Exception e) {
129 logger.warn(e.toString());
130 fail(e.getMessage());
134 * The only fact in memory should be Params
136 assertEquals(1, kieSession.getFactCount());
139 * Print what's left in memory
141 dumpFacts(kieSession);
147 * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
150 public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
152 * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
156 if ("POLICY-CL-MGT".equals(topic)) {
157 obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
158 org.onap.policy.controlloop.VirtualControlLoopNotification.class);
161 if (obj instanceof VirtualControlLoopNotification) {
162 VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
163 String policyName = notification.getPolicyName();
164 if (policyName.endsWith("EVENT")) {
165 logger.debug("Rule Fired: " + notification.getPolicyName());
167 ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
168 } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
169 logger.debug("Rule Fired: " + notification.getPolicyName());
171 ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
172 assertNotNull(notification.getMessage());
173 assertTrue(notification.getMessage().startsWith("Sending guard query"));
174 } else if (policyName.endsWith("GUARD.RESPONSE")) {
175 logger.debug("Rule Fired: " + notification.getPolicyName());
177 ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
178 assertNotNull(notification.getMessage());
179 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
180 } else if (policyName.endsWith("GUARD_PERMITTED")) {
181 logger.debug("Rule Fired: " + notification.getPolicyName());
183 ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
184 assertNotNull(notification.getMessage());
185 assertTrue(notification.getMessage().startsWith("actor=SO"));
186 } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
187 logger.debug("Rule Fired: " + notification.getPolicyName());
189 logger.debug("The operation timed out");
190 fail("Operation Timed Out");
191 } else if (policyName.endsWith("SO.RESPONSE")) {
192 logger.debug("Rule Fired: " + notification.getPolicyName());
193 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS
194 .equals(notification.getNotification()));
195 assertNotNull(notification.getMessage());
196 assertTrue(notification.getMessage().startsWith("actor=SO"));
197 } else if (policyName.endsWith("EVENT.MANAGER")) {
198 logger.debug("Rule Fired: " + notification.getPolicyName());
199 if ("error".equals(notification.getAai().get("vserver.vserver-name"))) {
200 assertEquals(ControlLoopNotificationType.FINAL_FAILURE,
201 notification.getNotification());
202 } else if ("getFail".equals(notification.getAai().get("vserver.vserver-name"))) {
203 assertEquals(ControlLoopNotificationType.FINAL_FAILURE,
204 notification.getNotification());
206 assertTrue(ControlLoopNotificationType.FINAL_SUCCESS
207 .equals(notification.getNotification()));
210 } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
211 logger.debug("Rule Fired: " + notification.getPolicyName());
213 logger.debug("The control loop timed out");
214 fail("Control Loop Timed Out");
216 } else if (obj instanceof SoRequest) {
217 logger.debug("\n============ SO received the request!!! ===========\n");
222 * This method is used to simulate event messages from DCAE that start the control loop (onset
223 * message) or end the control loop (abatement message).
225 * @param policy the controlLoopName comes from the policy
226 * @param requestId the requestId for this event
227 * @param status could be onset or abated
229 protected void sendEvent(ControlLoopPolicy policy, UUID requestId,
230 ControlLoopEventStatus status) {
231 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
232 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
233 event.setRequestId(requestId);
234 event.setTarget("vserver.vserver-name");
235 event.setTargetType(ControlLoopTargetType.VNF);
236 event.setClosedLoopAlarmStart(Instant.now());
237 event.setAai(new HashMap<>());
238 event.getAai().put("vserver.vserver-name", "OzVServer");
239 event.getAai().put("vserver.is-closed-loop-disabled", "false");
240 event.getAai().put("vserver.prov-status", "ACTIVE");
241 event.setClosedLoopEventStatus(status);
242 kieSession.insert(event);
245 protected void sendEvent(ControlLoopPolicy policy, UUID requestId,
246 ControlLoopEventStatus status, String vserverName) {
247 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
248 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
249 event.setRequestId(requestId);
250 event.setTarget("vserver.vserver-name");
251 event.setTargetType(ControlLoopTargetType.VNF);
252 event.setClosedLoopAlarmStart(Instant.now());
253 event.setAai(new HashMap<>());
254 event.getAai().put("vserver.vserver-name", vserverName);
255 event.setClosedLoopEventStatus(status);
256 kieSession.insert(event);