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.appc.Request;
34 import org.onap.policy.appc.Response;
35 import org.onap.policy.appc.ResponseCode;
36 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
37 import org.onap.policy.common.endpoints.event.comm.TopicListener;
38 import org.onap.policy.common.endpoints.event.comm.TopicSink;
39 import org.onap.policy.controlloop.ControlLoopEventStatus;
40 import org.onap.policy.controlloop.ControlLoopNotificationType;
41 import org.onap.policy.controlloop.ControlLoopTargetType;
42 import org.onap.policy.controlloop.VirtualControlLoopEvent;
43 import org.onap.policy.controlloop.VirtualControlLoopNotification;
44 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
46 public class VfwControlLoopTest extends ControlLoopBase implements TopicListener {
49 * Setup the simulator.
52 public static void setUpBeforeClass() {
53 ControlLoopBase.setUpBeforeClass(
54 "../archetype-cl-amsterdam/src/main/resources/archetype-resources/src/"
55 + "main/resources/__closedLoopControlName__.drl",
56 "src/test/resources/yaml/policy_ControlLoop_vFW.yaml",
57 "service=ServiceDemo;resource=Res1Demo;type=operational",
59 "org.onap.closed_loop.ServiceDemo:VNFS:1.0.0");
63 public void successTest() {
66 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
67 * to be pulled from the queue
69 for (TopicSink sink : noopTopics) {
70 assertTrue(sink.start());
75 * Create a unique requestId
77 requestId = UUID.randomUUID();
80 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
83 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET);
86 kieSession.fireUntilHalt();
88 // allow object clean-up
89 kieSession.fireAllRules();
91 } catch (Exception e) {
93 logger.warn(e.toString());
99 * The only fact in memory should be Params
101 assertEquals(1, kieSession.getFactCount());
104 * Print what's left in memory
106 dumpFacts(kieSession);
110 public void aaiFailTests() {
113 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
114 * to be pulled from the queue
116 for (TopicSink sink : noopTopics) {
117 assertTrue(sink.start());
122 * Create a unique requestId
124 requestId = UUID.randomUUID();
127 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
130 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "error");
132 kieSession.fireUntilHalt();
134 // allow object clean-up
135 kieSession.fireAllRules();
137 } catch (Exception e) {
139 logger.warn(e.toString());
140 fail(e.getMessage());
144 * The only fact in memory should be Params
146 assertEquals(1, kieSession.getFactCount());
149 * Print what's left in memory
151 dumpFacts(kieSession);
154 * Create a unique requestId
156 requestId = UUID.randomUUID();
159 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
163 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "getFail");
166 kieSession.fireUntilHalt();
168 // allow object clean-up
169 kieSession.fireAllRules();
171 } catch (Exception e) {
173 logger.warn(e.toString());
174 fail(e.getMessage());
178 * The only fact in memory should be Params
180 assertEquals(1, kieSession.getFactCount());
183 * Print what's left in memory
185 dumpFacts(kieSession);
189 * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
192 public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
194 * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
198 if ("POLICY-CL-MGT".equals(topic)) {
199 obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
200 org.onap.policy.controlloop.VirtualControlLoopNotification.class);
201 } else if ("APPC-CL".equals(topic)) {
202 obj = org.onap.policy.appc.util.Serialization.gsonPretty.fromJson(event,
203 org.onap.policy.appc.Request.class);
206 if (obj instanceof VirtualControlLoopNotification) {
207 VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
208 String policyName = notification.getPolicyName();
209 if (policyName.endsWith("EVENT")) {
210 logger.debug("Rule Fired: " + notification.getPolicyName());
211 assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
212 } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
213 logger.debug("Rule Fired: " + notification.getPolicyName());
214 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
215 assertNotNull(notification.getMessage());
216 assertTrue(notification.getMessage().startsWith("Sending guard query"));
217 } else if (policyName.endsWith("GUARD.RESPONSE")) {
218 logger.debug("Rule Fired: " + notification.getPolicyName());
219 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
220 assertNotNull(notification.getMessage());
221 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
222 } else if (policyName.endsWith("GUARD_PERMITTED")) {
223 logger.debug("Rule Fired: " + notification.getPolicyName());
224 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
225 assertNotNull(notification.getMessage());
226 assertTrue(notification.getMessage().startsWith("actor=APPC"));
227 } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
228 logger.debug("Rule Fired: " + notification.getPolicyName());
230 logger.debug("The operation timed out");
231 fail("Operation Timed Out");
232 } else if (policyName.endsWith("APPC.RESPONSE")) {
233 logger.debug("Rule Fired: " + notification.getPolicyName());
234 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS.equals(notification.getNotification()));
235 assertNotNull(notification.getMessage());
236 assertTrue(notification.getMessage().startsWith("actor=APPC"));
237 sendEvent(pair.first, requestId, ControlLoopEventStatus.ABATED);
238 } else if (policyName.endsWith("EVENT.MANAGER")) {
239 logger.debug("Rule Fired: " + notification.getPolicyName());
240 if ("error".equals(notification.getAai().get("generic-vnf.vnf-name"))) {
241 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
242 assertEquals("Target vnf-id could not be found", notification.getMessage());
243 } else if ("getFail".equals(notification.getAai().get("generic-vnf.vnf-name"))) {
244 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
246 assertTrue(ControlLoopNotificationType.FINAL_SUCCESS.equals(notification.getNotification()));
249 } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
250 logger.debug("Rule Fired: " + notification.getPolicyName());
252 logger.debug("The control loop timed out");
253 fail("Control Loop Timed Out");
255 } else if (obj instanceof Request) {
256 assertTrue(((Request) obj).getCommonHeader().getSubRequestId().equals("1"));
257 assertNotNull(((Request) obj).getPayload().get("generic-vnf.vnf-id"));
259 logger.debug("\n============ APPC received the request!!! ===========\n");
262 * Simulate a success response from APPC and insert the response into the working memory
264 Response appcResponse = new Response((Request) obj);
265 appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
266 appcResponse.getStatus().setValue("SUCCESS");
267 kieSession.insert(appcResponse);
272 * This method is used to simulate event messages from DCAE that start the control loop (onset
273 * message) or end the control loop (abatement message).
275 * @param policy the controlLoopName comes from the policy
276 * @param requestId the requestId for this event
277 * @param status could be onset or abated
279 protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status) {
280 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
281 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
282 event.setRequestId(requestId);
283 event.setTarget("generic-vnf.vnf-name");
284 event.setTargetType(ControlLoopTargetType.VNF);
285 event.setClosedLoopAlarmStart(Instant.now());
286 event.setAai(new HashMap<>());
287 event.getAai().put("generic-vnf.vnf-name", "testGenericVnfID");
288 event.setClosedLoopEventStatus(status);
289 kieSession.insert(event);
293 * This method is used to simulate event messages from DCAE that start the control loop (onset
294 * message) or end the control loop (abatement message).
296 * @param policy the controlLoopName comes from the policy
297 * @param requestId the requestId for this event
298 * @param status could be onset or abated
300 protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status, String vnfId) {
301 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
302 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
303 event.setRequestId(requestId);
304 event.setTarget("generic-vnf.vnf-name");
305 event.setTargetType(ControlLoopTargetType.VNF);
306 event.setClosedLoopAlarmStart(Instant.now());
307 event.setAai(new HashMap<>());
308 event.getAai().put("generic-vnf.vnf-name", vnfId);
309 event.setClosedLoopEventStatus(status);
310 kieSession.insert(event);