2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-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 VdnsControlLoopTest 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-test.yaml",
60 public void successTest() {
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 namedQueryFailTest() {
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, "error");
121 kieSession.fireUntilHalt();
123 // allow object clean-up
124 kieSession.fireAllRules();
127 * The only fact in memory should be Params
129 assertEquals(1, kieSession.getFactCount());
132 * Print what's left in memory
134 dumpFacts(kieSession);
138 public void aaiGetFailTest() {
141 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
142 * to be pulled from the queue
144 for (TopicSink sink : noopTopics) {
145 assertTrue(sink.start());
150 * Create a unique requestId
152 requestId = UUID.randomUUID();
155 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
158 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "getFail");
161 kieSession.fireUntilHalt();
163 // allow object clean-up
164 kieSession.fireAllRules();
166 } catch (Exception e) {
168 logger.warn(e.toString());
169 fail(e.getMessage());
174 * The only fact in memory should be Params
176 assertEquals(1, kieSession.getFactCount());
179 * Print what's left in memory
181 dumpFacts(kieSession);
187 * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
190 public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
192 * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
196 if ("POLICY-CL-MGT".equals(topic)) {
197 obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
198 org.onap.policy.controlloop.VirtualControlLoopNotification.class);
201 if (obj instanceof VirtualControlLoopNotification) {
202 VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
203 String policyName = notification.getPolicyName();
204 if (policyName.endsWith("EVENT")) {
205 logger.debug("Rule Fired: " + notification.getPolicyName());
206 assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
207 } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
208 logger.debug("Rule Fired: " + notification.getPolicyName());
209 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
210 assertNotNull(notification.getMessage());
211 assertTrue(notification.getMessage().startsWith("Sending guard query"));
212 } else if (policyName.endsWith("GUARD.RESPONSE")) {
213 logger.debug("Rule Fired: " + notification.getPolicyName());
214 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
215 assertNotNull(notification.getMessage());
216 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
217 } else if (policyName.endsWith("GUARD_PERMITTED")) {
218 logger.debug("Rule Fired: " + notification.getPolicyName());
219 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
220 assertNotNull(notification.getMessage());
221 assertTrue(notification.getMessage().startsWith("actor=SO"));
222 } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
223 logger.debug("Rule Fired: " + notification.getPolicyName());
225 logger.debug("The operation timed out");
226 fail("Operation Timed Out");
227 } else if (policyName.endsWith("SO.RESPONSE")) {
228 logger.debug("Rule Fired: " + notification.getPolicyName());
229 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS.equals(notification.getNotification()));
230 assertNotNull(notification.getMessage());
231 assertTrue(notification.getMessage().startsWith("actor=SO"));
232 } else if (policyName.endsWith("EVENT.MANAGER")) {
233 logger.debug("Rule Fired: " + notification.getPolicyName());
234 if ("error".equals(notification.getAai().get("vserver.vserver-name"))) {
235 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
236 } else if ("getFail".equals(notification.getAai().get("vserver.vserver-name"))) {
237 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
239 assertTrue(ControlLoopNotificationType.FINAL_SUCCESS.equals(notification.getNotification()));
242 } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
243 logger.debug("Rule Fired: " + notification.getPolicyName());
245 logger.debug("The control loop timed out");
246 fail("Control Loop Timed Out");
248 } else if (obj instanceof SoRequest) {
249 logger.debug("\n============ SO received the request!!! ===========\n");
254 * This method is used to simulate event messages from DCAE that start the control loop (onset
255 * message) or end the control loop (abatement message).
257 * @param policy the controlLoopName comes from the policy
258 * @param requestId the requestId for this event
259 * @param status could be onset or abated
261 protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status) {
262 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
263 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
264 event.setRequestId(requestId);
265 event.setTarget("vserver.vserver-name");
266 event.setClosedLoopAlarmStart(Instant.now());
267 event.setAai(new HashMap<>());
268 event.getAai().put("vserver.vserver-name", "dfw1lb01lb01");
269 event.getAai().put("vserver.is-closed-loop-disabled", "false");
270 event.getAai().put("vserver.prov-status", "ACTIVE");
271 event.setClosedLoopEventStatus(status);
272 kieSession.insert(event);
275 protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status,
276 String vserverName) {
277 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
278 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
279 event.setRequestId(requestId);
280 event.setTarget("vserver.vserver-name");
281 event.setClosedLoopAlarmStart(Instant.now());
282 event.setAai(new HashMap<>());
283 event.getAai().put("vserver.vserver-name", vserverName);
284 event.setClosedLoopEventStatus(status);
285 kieSession.insert(event);