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.VirtualControlLoopEvent;
42 import org.onap.policy.controlloop.VirtualControlLoopNotification;
43 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
45 public class VfwControlLoopTest extends ControlLoopBase implements TopicListener {
48 * Setup the simulator.
51 public static void setUpBeforeClass() {
52 ControlLoopBase.setUpBeforeClass(
53 "../archetype-cl-amsterdam/src/main/resources/archetype-resources/src/"
54 + "main/resources/__closedLoopControlName__.drl",
55 "src/test/resources/yaml/policy_ControlLoop_vFW.yaml",
56 "service=ServiceDemo;resource=Res1Demo;type=operational",
58 "org.onap.closed_loop.ServiceDemo:VNFS:1.0.0");
62 public void successTest() {
65 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
66 * to be pulled from the queue
68 for (TopicSink sink : noopTopics) {
69 assertTrue(sink.start());
74 * Create a unique requestId
76 requestId = UUID.randomUUID();
79 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
82 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET);
85 kieSession.fireUntilHalt();
87 // allow object clean-up
88 kieSession.fireAllRules();
90 } catch (Exception e) {
92 logger.warn(e.toString());
98 * The only fact in memory should be Params
100 assertEquals(1, kieSession.getFactCount());
103 * Print what's left in memory
105 dumpFacts(kieSession);
109 public void aaiFailTests() {
112 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
113 * to be pulled from the queue
115 for (TopicSink sink : noopTopics) {
116 assertTrue(sink.start());
121 * Create a unique requestId
123 requestId = UUID.randomUUID();
126 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
129 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "error");
131 kieSession.fireUntilHalt();
133 // allow object clean-up
134 kieSession.fireAllRules();
136 } catch (Exception e) {
138 logger.warn(e.toString());
139 fail(e.getMessage());
143 * The only fact in memory should be Params
145 assertEquals(1, kieSession.getFactCount());
148 * Print what's left in memory
150 dumpFacts(kieSession);
153 * Create a unique requestId
155 requestId = UUID.randomUUID();
158 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
162 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "getFail");
165 kieSession.fireUntilHalt();
167 // allow object clean-up
168 kieSession.fireAllRules();
170 } catch (Exception e) {
172 logger.warn(e.toString());
173 fail(e.getMessage());
177 * The only fact in memory should be Params
179 assertEquals(1, kieSession.getFactCount());
182 * Print what's left in memory
184 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);
200 } else if ("APPC-CL".equals(topic)) {
201 obj = org.onap.policy.appc.util.Serialization.gsonPretty.fromJson(event,
202 org.onap.policy.appc.Request.class);
205 if (obj instanceof VirtualControlLoopNotification) {
206 VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
207 String policyName = notification.getPolicyName();
208 if (policyName.endsWith("EVENT")) {
209 logger.debug("Rule Fired: " + notification.getPolicyName());
210 assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
211 } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
212 logger.debug("Rule Fired: " + notification.getPolicyName());
213 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
214 assertNotNull(notification.getMessage());
215 assertTrue(notification.getMessage().startsWith("Sending guard query"));
216 } else if (policyName.endsWith("GUARD.RESPONSE")) {
217 logger.debug("Rule Fired: " + notification.getPolicyName());
218 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
219 assertNotNull(notification.getMessage());
220 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
221 } else if (policyName.endsWith("GUARD_PERMITTED")) {
222 logger.debug("Rule Fired: " + notification.getPolicyName());
223 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
224 assertNotNull(notification.getMessage());
225 assertTrue(notification.getMessage().startsWith("actor=APPC"));
226 } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
227 logger.debug("Rule Fired: " + notification.getPolicyName());
229 logger.debug("The operation timed out");
230 fail("Operation Timed Out");
231 } else if (policyName.endsWith("APPC.RESPONSE")) {
232 logger.debug("Rule Fired: " + notification.getPolicyName());
233 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS.equals(notification.getNotification()));
234 assertNotNull(notification.getMessage());
235 assertTrue(notification.getMessage().startsWith("actor=APPC"));
236 sendEvent(pair.first, requestId, ControlLoopEventStatus.ABATED);
237 } else if (policyName.endsWith("EVENT.MANAGER")) {
238 logger.debug("Rule Fired: " + notification.getPolicyName());
239 if ("error".equals(notification.getAai().get("generic-vnf.vnf-name"))) {
240 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
241 assertEquals("Target vnf-id could not be found", notification.getMessage());
242 } else if ("getFail".equals(notification.getAai().get("generic-vnf.vnf-name"))) {
243 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
245 assertTrue(ControlLoopNotificationType.FINAL_SUCCESS.equals(notification.getNotification()));
248 } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
249 logger.debug("Rule Fired: " + notification.getPolicyName());
251 logger.debug("The control loop timed out");
252 fail("Control Loop Timed Out");
254 } else if (obj instanceof Request) {
255 assertTrue(((Request) obj).getCommonHeader().getSubRequestId().equals("1"));
256 assertNotNull(((Request) obj).getPayload().get("generic-vnf.vnf-id"));
258 logger.debug("\n============ APPC received the request!!! ===========\n");
261 * Simulate a success response from APPC and insert the response into the working memory
263 Response appcResponse = new Response((Request) obj);
264 appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
265 appcResponse.getStatus().setValue("SUCCESS");
266 kieSession.insert(appcResponse);
271 * This method is used to simulate event messages from DCAE that start the control loop (onset
272 * message) or end the control loop (abatement message).
274 * @param policy the controlLoopName comes from the policy
275 * @param requestId the requestId for this event
276 * @param status could be onset or abated
278 protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status) {
279 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
280 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
281 event.setRequestId(requestId);
282 event.setTarget("generic-vnf.vnf-name");
283 event.setClosedLoopAlarmStart(Instant.now());
284 event.setAai(new HashMap<>());
285 event.getAai().put("generic-vnf.vnf-name", "testGenericVnfID");
286 event.setClosedLoopEventStatus(status);
287 kieSession.insert(event);
291 * This method is used to simulate event messages from DCAE that start the control loop (onset
292 * message) or end the control loop (abatement message).
294 * @param policy the controlLoopName comes from the policy
295 * @param requestId the requestId for this event
296 * @param status could be onset or abated
298 protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status, String vnfId) {
299 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
300 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
301 event.setRequestId(requestId);
302 event.setTarget("generic-vnf.vnf-name");
303 event.setClosedLoopAlarmStart(Instant.now());
304 event.setAai(new HashMap<>());
305 event.getAai().put("generic-vnf.vnf-name", vnfId);
306 event.setClosedLoopEventStatus(status);
307 kieSession.insert(event);