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.FixMethodOrder;
33 import org.junit.Test;
34 import org.junit.runners.MethodSorters;
35 import org.onap.policy.appc.Request;
36 import org.onap.policy.appc.Response;
37 import org.onap.policy.appc.ResponseCode;
38 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
39 import org.onap.policy.common.endpoints.event.comm.TopicListener;
40 import org.onap.policy.common.endpoints.event.comm.TopicSink;
41 import org.onap.policy.controlloop.ControlLoopEventStatus;
42 import org.onap.policy.controlloop.ControlLoopNotificationType;
43 import org.onap.policy.controlloop.ControlLoopTargetType;
44 import org.onap.policy.controlloop.VirtualControlLoopEvent;
45 import org.onap.policy.controlloop.VirtualControlLoopNotification;
46 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
48 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
49 public class VfwControlLoopTest extends ControlLoopBase implements TopicListener {
52 * Setup the simulator.
55 public static void setUpBeforeClass() {
56 ControlLoopBase.setUpBeforeClass(
57 "../archetype-cl-amsterdam/src/main/resources/archetype-resources/src/"
58 + "main/resources/__closedLoopControlName__.drl",
59 "src/test/resources/yaml/policy_ControlLoop_vFW.yaml",
60 "service=ServiceDemo;resource=Res1Demo;type=operational",
62 "org.onap.closed_loop.ServiceDemo:VNFS:1.0.0");
66 public void testASuccess() {
69 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
70 * to be pulled from the queue
72 for (TopicSink sink : noopTopics) {
73 assertTrue(sink.start());
78 * Create a unique requestId
80 requestId = UUID.randomUUID();
83 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
86 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET);
89 kieSession.fireUntilHalt();
91 // allow object clean-up
92 kieSession.fireAllRules();
94 } catch (Exception e) {
96 logger.warn(e.toString());
102 * The only fact in memory should be Params
104 assertEquals(1, kieSession.getFactCount());
107 * Print what's left in memory
109 dumpFacts(kieSession);
113 public void testBAaiFail() {
116 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
117 * to be pulled from the queue
119 for (TopicSink sink : noopTopics) {
120 assertTrue(sink.start());
125 * Create a unique requestId
127 requestId = UUID.randomUUID();
130 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
133 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "error");
135 kieSession.fireUntilHalt();
137 // allow object clean-up
138 kieSession.fireAllRules();
140 } catch (Exception e) {
142 logger.warn(e.toString());
143 fail(e.getMessage());
147 * The only fact in memory should be Params
149 assertEquals(1, kieSession.getFactCount());
152 * Print what's left in memory
154 dumpFacts(kieSession);
157 * Create a unique requestId
159 requestId = UUID.randomUUID();
162 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
166 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "getFail");
169 kieSession.fireUntilHalt();
171 // allow object clean-up
172 kieSession.fireAllRules();
174 } catch (Exception e) {
176 logger.warn(e.toString());
177 fail(e.getMessage());
181 * The only fact in memory should be Params
183 assertEquals(1, kieSession.getFactCount());
186 * Print what's left in memory
188 dumpFacts(kieSession);
192 * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
195 public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
197 * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
201 if ("POLICY-CL-MGT".equals(topic)) {
202 obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
203 org.onap.policy.controlloop.VirtualControlLoopNotification.class);
204 } else if ("APPC-CL".equals(topic)) {
205 obj = org.onap.policy.appc.util.Serialization.gsonPretty.fromJson(event,
206 org.onap.policy.appc.Request.class);
209 if (obj instanceof VirtualControlLoopNotification) {
210 VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
211 String policyName = notification.getPolicyName();
212 if (policyName.endsWith("EVENT")) {
213 logger.debug("Rule Fired: " + notification.getPolicyName());
214 assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
215 } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
216 logger.debug("Rule Fired: " + notification.getPolicyName());
217 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
218 assertNotNull(notification.getMessage());
219 assertTrue(notification.getMessage().startsWith("Sending guard query"));
220 } else if (policyName.endsWith("GUARD.RESPONSE")) {
221 logger.debug("Rule Fired: " + notification.getPolicyName());
222 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
223 assertNotNull(notification.getMessage());
224 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
225 } else if (policyName.endsWith("GUARD_PERMITTED")) {
226 logger.debug("Rule Fired: " + notification.getPolicyName());
227 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
228 assertNotNull(notification.getMessage());
229 assertTrue(notification.getMessage().startsWith("actor=APPC"));
230 } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
231 logger.debug("Rule Fired: " + notification.getPolicyName());
233 logger.debug("The operation timed out");
234 fail("Operation Timed Out");
235 } else if (policyName.endsWith("APPC.RESPONSE")) {
236 logger.debug("Rule Fired: " + notification.getPolicyName());
237 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS.equals(notification.getNotification()));
238 assertNotNull(notification.getMessage());
239 assertTrue(notification.getMessage().startsWith("actor=APPC"));
240 sendEvent(pair.first, requestId, ControlLoopEventStatus.ABATED);
241 } else if (policyName.endsWith("EVENT.MANAGER")) {
242 logger.debug("Rule Fired: " + notification.getPolicyName());
243 if ("error".equals(notification.getAai().get("generic-vnf.vnf-name"))) {
244 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
245 assertEquals("Target vnf-id could not be found", notification.getMessage());
246 } else if ("getFail".equals(notification.getAai().get("generic-vnf.vnf-name"))) {
247 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
249 assertTrue(ControlLoopNotificationType.FINAL_SUCCESS.equals(notification.getNotification()));
252 } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
253 logger.debug("Rule Fired: " + notification.getPolicyName());
255 logger.debug("The control loop timed out");
256 fail("Control Loop Timed Out");
258 } else if (obj instanceof Request) {
259 assertTrue(((Request) obj).getCommonHeader().getSubRequestId().equals("1"));
260 assertNotNull(((Request) obj).getPayload().get("generic-vnf.vnf-id"));
262 logger.debug("\n============ APPC received the request!!! ===========\n");
265 * Simulate a success response from APPC and insert the response into the working memory
267 Response appcResponse = new Response((Request) obj);
268 appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
269 appcResponse.getStatus().setValue("SUCCESS");
270 kieSession.insert(appcResponse);
275 * This method is used to simulate event messages from DCAE that start the control loop (onset
276 * message) or end the control loop (abatement message).
278 * @param policy the controlLoopName comes from the policy
279 * @param requestId the requestId for this event
280 * @param status could be onset or abated
282 protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status) {
283 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
284 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
285 event.setRequestId(requestId);
286 event.setTarget("generic-vnf.vnf-name");
287 event.setTargetType(ControlLoopTargetType.VNF);
288 event.setClosedLoopAlarmStart(Instant.now());
289 event.setAai(new HashMap<>());
290 event.getAai().put("generic-vnf.vnf-name", "testGenericVnfID");
291 event.getAai().put("vserver.vserver-name", "testVserverName");
292 event.setClosedLoopEventStatus(status);
293 kieSession.insert(event);
297 * This method is used to simulate event messages from DCAE that start the control loop (onset
298 * message) or end the control loop (abatement message).
300 * @param policy the controlLoopName comes from the policy
301 * @param requestId the requestId for this event
302 * @param status could be onset or abated
304 protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status, String vnfId) {
305 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
306 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
307 event.setRequestId(requestId);
308 event.setTarget("generic-vnf.vnf-name");
309 event.setTargetType(ControlLoopTargetType.VNF);
310 event.setClosedLoopAlarmStart(Instant.now());
311 event.setAai(new HashMap<>());
312 event.getAai().put("generic-vnf.vnf-name", vnfId);
313 event.getAai().put("vserver.vserver-name", vnfId);
314 event.setClosedLoopEventStatus(status);
315 kieSession.insert(event);