2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-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 VdnsControlLoopTest 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-test.yaml",
61 public void testSuccessTest() {
64 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
65 * to be pulled from the queue
67 for (TopicSink sink : noopTopics) {
68 assertTrue(sink.start());
73 * Create a unique requestId
75 requestId = UUID.randomUUID();
78 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
81 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET);
83 kieSession.fireUntilHalt();
85 // allow object clean-up
86 kieSession.fireAllRules();
89 * The only fact in memory should be Params
91 assertEquals(1, kieSession.getFactCount());
94 * Print what's left in memory
96 dumpFacts(kieSession);
100 public void testNamedQueryFail() {
103 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
104 * to be pulled from the queue
106 for (TopicSink sink : noopTopics) {
107 assertTrue(sink.start());
112 * Create a unique requestId
114 requestId = UUID.randomUUID();
117 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
120 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "error");
122 kieSession.fireUntilHalt();
124 // allow object clean-up
125 kieSession.fireAllRules();
128 * The only fact in memory should be Params
130 assertEquals(1, kieSession.getFactCount());
133 * Print what's left in memory
135 dumpFacts(kieSession);
139 public void testAaiGetFail() {
142 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
143 * to be pulled from the queue
145 for (TopicSink sink : noopTopics) {
146 assertTrue(sink.start());
151 * Create a unique requestId
153 requestId = UUID.randomUUID();
156 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
159 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "getFail");
162 kieSession.fireUntilHalt();
164 // allow object clean-up
165 kieSession.fireAllRules();
167 } catch (Exception e) {
169 logger.warn(e.toString());
170 fail(e.getMessage());
175 * The only fact in memory should be Params
177 assertEquals(1, kieSession.getFactCount());
180 * Print what's left in memory
182 dumpFacts(kieSession);
188 * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
191 public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
193 * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
197 if ("POLICY-CL-MGT".equals(topic)) {
198 obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
199 org.onap.policy.controlloop.VirtualControlLoopNotification.class);
202 if (obj instanceof VirtualControlLoopNotification) {
203 VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
204 String policyName = notification.getPolicyName();
205 if (policyName.endsWith("EVENT")) {
206 logger.debug("Rule Fired: " + notification.getPolicyName());
207 assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
208 } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
209 logger.debug("Rule Fired: " + notification.getPolicyName());
210 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
211 assertNotNull(notification.getMessage());
212 assertTrue(notification.getMessage().startsWith("Sending guard query"));
213 } else if (policyName.endsWith("GUARD.RESPONSE")) {
214 logger.debug("Rule Fired: " + notification.getPolicyName());
215 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
216 assertNotNull(notification.getMessage());
217 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
218 } else if (policyName.endsWith("GUARD_PERMITTED")) {
219 logger.debug("Rule Fired: " + notification.getPolicyName());
220 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
221 assertNotNull(notification.getMessage());
222 assertTrue(notification.getMessage().startsWith("actor=SO"));
223 } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
224 logger.debug("Rule Fired: " + notification.getPolicyName());
226 logger.debug("The operation timed out");
227 fail("Operation Timed Out");
228 } else if (policyName.endsWith("SO.RESPONSE")) {
229 logger.debug("Rule Fired: " + notification.getPolicyName());
230 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS.equals(notification.getNotification()));
231 assertNotNull(notification.getMessage());
232 assertTrue(notification.getMessage().startsWith("actor=SO"));
233 } else if (policyName.endsWith("EVENT.MANAGER")) {
234 logger.debug("Rule Fired: " + notification.getPolicyName());
235 if ("error".equals(notification.getAai().get("vserver.vserver-name"))) {
236 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
237 } else if ("getFail".equals(notification.getAai().get("vserver.vserver-name"))) {
238 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
240 assertTrue(ControlLoopNotificationType.FINAL_SUCCESS.equals(notification.getNotification()));
243 } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
244 logger.debug("Rule Fired: " + notification.getPolicyName());
246 logger.debug("The control loop timed out");
247 fail("Control Loop Timed Out");
249 } else if (obj instanceof SoRequest) {
250 logger.debug("\n============ SO received the request!!! ===========\n");
255 * This method is used to simulate event messages from DCAE that start the control loop (onset
256 * message) or end the control loop (abatement message).
258 * @param policy the controlLoopName comes from the policy
259 * @param requestId the requestId for this event
260 * @param status could be onset or abated
262 protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status) {
263 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
264 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
265 event.setRequestId(requestId);
266 event.setTarget("vserver.vserver-name");
267 event.setTargetType(ControlLoopTargetType.VNF);
268 event.setClosedLoopAlarmStart(Instant.now());
269 event.setAai(new HashMap<>());
270 event.getAai().put("vserver.vserver-name", "OzVServer");
271 event.getAai().put("vserver.is-closed-loop-disabled", "false");
272 event.getAai().put("vserver.prov-status", "ACTIVE");
273 event.setClosedLoopEventStatus(status);
274 kieSession.insert(event);
277 protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status,
278 String vserverName) {
279 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
280 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
281 event.setRequestId(requestId);
282 event.setTarget("vserver.vserver-name");
283 event.setTargetType(ControlLoopTargetType.VNF);
284 event.setClosedLoopAlarmStart(Instant.now());
285 event.setAai(new HashMap<>());
286 event.getAai().put("vserver.vserver-name", vserverName);
287 event.setClosedLoopEventStatus(status);
288 kieSession.insert(event);