2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 Intel Corp. All rights reserved.
6 * Modifications Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.template.demo;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
29 import java.io.IOException;
30 import java.time.Instant;
31 import java.util.HashMap;
32 import java.util.UUID;
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.ControlLoopTargetType;
41 import org.onap.policy.controlloop.VirtualControlLoopEvent;
42 import org.onap.policy.controlloop.VirtualControlLoopNotification;
43 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
44 import org.onap.policy.vfc.VfcRequest;
47 public class VfcControlLoopTest extends ControlLoopBase implements TopicListener {
50 * Setup the simulator.
53 public static void setUpBeforeClass() {
54 ControlLoopBase.setUpBeforeClass(
55 "../archetype-cl-amsterdam/src/main/resources/archetype-resources/"
56 + "src/main/resources/__closedLoopControlName__.drl",
57 "src/test/resources/yaml/policy_ControlLoop_VFC.yaml",
64 public void successTest() throws IOException {
67 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
68 * to be pulled from the queue
70 for (TopicSink sink : noopTopics) {
71 assertTrue(sink.start());
76 * Create a unique requestId
78 requestId = UUID.randomUUID();
81 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
84 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET);
86 kieSession.fireUntilHalt();
88 // allow object clean-up
89 kieSession.fireAllRules();
92 * The only fact in memory should be Params
94 assertEquals(1, kieSession.getFactCount());
97 * Print what's left in memory
99 dumpFacts(kieSession);
103 public void nullRequestTest() throws IOException {
106 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
107 * to be pulled from the queue
109 for (TopicSink sink : noopTopics) {
110 assertTrue(sink.start());
115 * Create a unique requestId
117 requestId = UUID.randomUUID();
120 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
124 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
125 event.setClosedLoopControlName(pair.first.getControlLoop().getControlLoopName());
126 event.setRequestId(UUID.randomUUID());
127 event.setClosedLoopEventClient("tca.instance00009");
128 event.setTargetType(ControlLoopTargetType.VM);
129 event.setTarget("vserver.vserver-name");
130 event.setFrom("DCAE");
131 event.setClosedLoopAlarmStart(Instant.now());
132 event.setAai(new HashMap<String, String>());
133 event.getAai().put("vserver.vserver-name", "nullRequest");
134 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
135 kieSession.insert(event);
137 kieSession.fireUntilHalt();
139 // allow object clean-up
140 kieSession.fireAllRules();
143 * The only fact in memory should be Params
145 assertEquals(1, kieSession.getFactCount());
148 * Print what's left in memory
150 dumpFacts(kieSession);
156 * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
159 public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
161 * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
165 if ("POLICY-CL-MGT".equals(topic)) {
166 obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
167 org.onap.policy.controlloop.VirtualControlLoopNotification.class);
170 if (obj instanceof VirtualControlLoopNotification) {
171 VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
172 String policyName = notification.getPolicyName();
173 if (policyName.endsWith("EVENT")) {
174 logger.debug("Rule Fired: " + notification.getPolicyName());
175 assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
176 } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
177 logger.debug("Rule Fired: " + notification.getPolicyName());
178 assertTrue(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());
183 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
184 assertNotNull(notification.getMessage());
185 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
186 } else if (policyName.endsWith("GUARD_PERMITTED")) {
187 logger.debug("Rule Fired: " + notification.getPolicyName());
188 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
189 assertNotNull(notification.getMessage());
190 assertTrue(notification.getMessage().startsWith("actor=VFC"));
191 } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
192 logger.debug("Rule Fired: " + notification.getPolicyName());
194 logger.debug("The operation timed out");
195 fail("Operation Timed Out");
196 } else if (policyName.endsWith("VFC.RESPONSE")) {
197 logger.debug("Rule Fired: " + notification.getPolicyName());
198 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS.equals(notification.getNotification()));
199 assertNotNull(notification.getMessage());
200 assertTrue(notification.getMessage().startsWith("actor=VFC"));
201 } else if (policyName.endsWith("EVENT.MANAGER")) {
202 logger.debug("Rule Fired: " + notification.getPolicyName());
203 if ("nullRequest".equals(notification.getAai().get("vserver.vserver-name"))) {
204 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
206 assertEquals(ControlLoopNotificationType.FINAL_SUCCESS, 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 VfcRequest) {
216 logger.debug("\n============ VFC 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, ControlLoopEventStatus status) {
229 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
230 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
231 event.setRequestId(UUID.randomUUID());
232 event.setClosedLoopEventClient("tca.instance00009");
233 event.setTargetType(ControlLoopTargetType.VM);
234 event.setTarget("vserver.vserver-name");
235 event.setFrom("DCAE");
236 event.setClosedLoopAlarmStart(Instant.now());
237 event.setAai(new HashMap<String, String>());
238 event.getAai().put("vserver.vserver-name", "vserver-name-16102016-aai3255-data-11-1");
239 event.getAai().put("vserver.vserver-id", "vserver-id-16102016-aai3255-data-11-1");
240 event.getAai().put("generic-vnf.vnf-id", "vnf-id-16102016-aai3255-data-11-1");
241 event.getAai().put("service-instance.service-instance-id", "service-instance-id-16102016-aai3255-data-11-1");
242 event.getAai().put("vserver.is-closed-loop-disabled", "false");
243 event.getAai().put("vserver.prov-status", "ACTIVE");
244 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
245 kieSession.insert(event);