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.appclcm.AppcLcmBody;
34 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
35 import org.onap.policy.appclcm.AppcLcmInput;
36 import org.onap.policy.appclcm.AppcLcmOutput;
37 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
38 import org.onap.policy.common.endpoints.event.comm.TopicListener;
39 import org.onap.policy.common.endpoints.event.comm.TopicSink;
40 import org.onap.policy.controlloop.ControlLoopEventStatus;
41 import org.onap.policy.controlloop.ControlLoopNotificationType;
42 import org.onap.policy.controlloop.ControlLoopTargetType;
43 import org.onap.policy.controlloop.VirtualControlLoopEvent;
44 import org.onap.policy.controlloop.VirtualControlLoopNotification;
45 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
47 public class VcpeControlLoopTest 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_vCPE.yaml",
58 "service=ServiceDemo;resource=Res1Demo;type=operational", "CL_vCPE",
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, "OzVServer", true);
85 kieSession.fireUntilHalt();
87 // allow object clean-up
88 kieSession.fireAllRules();
91 * The only fact in memory should be Params
93 assertEquals(1, kieSession.getFactCount());
96 * Print what's left in memory
98 dumpFacts(kieSession);
102 public void aaiGetFailTest() {
105 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
106 * to be pulled from the queue
108 for (TopicSink sink : noopTopics) {
109 assertTrue(sink.start());
114 * Create a unique requestId
116 requestId = UUID.randomUUID();
119 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
122 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "getFail", false);
124 kieSession.fireUntilHalt();
126 // allow object clean-up
127 kieSession.fireAllRules();
130 * The only fact in memory should be Params
132 assertEquals(1, kieSession.getFactCount());
135 * Print what's left in memory
137 dumpFacts(kieSession);
143 * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
146 public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
148 * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
152 if ("POLICY-CL-MGT".equals(topic)) {
153 obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
154 org.onap.policy.controlloop.VirtualControlLoopNotification.class);
155 } else if ("APPC-LCM-READ".equals(topic)) {
156 obj = org.onap.policy.appclcm.util.Serialization.gsonJunit.fromJson(event,
157 org.onap.policy.appclcm.AppcLcmDmaapWrapper.class);
160 if (obj instanceof VirtualControlLoopNotification) {
161 VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
162 String policyName = notification.getPolicyName();
163 if (policyName.endsWith("EVENT")) {
164 logger.debug("Rule Fired: " + notification.getPolicyName());
166 ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
167 } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
168 logger.debug("Rule Fired: " + notification.getPolicyName());
170 ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
171 assertNotNull(notification.getMessage());
172 assertTrue(notification.getMessage().startsWith("Sending guard query"));
173 } else if (policyName.endsWith("GUARD.RESPONSE")) {
174 logger.debug("Rule Fired: " + notification.getPolicyName());
176 ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
177 assertNotNull(notification.getMessage());
178 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
179 } else if (policyName.endsWith("GUARD_PERMITTED")) {
180 logger.debug("Rule Fired: " + notification.getPolicyName());
182 ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
183 assertNotNull(notification.getMessage());
184 assertTrue(notification.getMessage().startsWith("actor=APPC"));
185 } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
186 logger.debug("Rule Fired: " + notification.getPolicyName());
188 logger.debug("The operation timed out");
189 fail("Operation Timed Out");
190 } else if (policyName.endsWith("APPC.LCM.RESPONSE")) {
191 logger.debug("Rule Fired: " + notification.getPolicyName());
192 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS
193 .equals(notification.getNotification()));
194 assertNotNull(notification.getMessage());
195 assertTrue(notification.getMessage().startsWith("actor=APPC"));
196 sendEvent(pair.first, requestId, ControlLoopEventStatus.ABATED);
197 } else if (policyName.endsWith("EVENT.MANAGER")) {
198 logger.debug("Rule Fired: " + notification.getPolicyName());
199 if ("getFail".equals(notification.getAai().get("generic-vnf.vnf-name"))) {
200 assertEquals(ControlLoopNotificationType.FINAL_FAILURE,
201 notification.getNotification());
204 assertEquals(ControlLoopNotificationType.FINAL_SUCCESS,
205 notification.getNotification());
208 } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
209 logger.debug("Rule Fired: " + notification.getPolicyName());
211 logger.debug("The control loop timed out");
212 fail("Control Loop Timed Out");
214 } else if (obj instanceof AppcLcmDmaapWrapper) {
216 * The request should be of type LcmRequestWrapper and the subrequestid should be 1
218 AppcLcmDmaapWrapper dmaapRequest = (AppcLcmDmaapWrapper) obj;
219 AppcLcmInput appcRequest = dmaapRequest.getBody().getInput();
220 assertTrue(appcRequest.getCommonHeader().getSubRequestId().equals("1"));
221 assertNotNull(appcRequest.getActionIdentifiers().get("vnf-id"));
223 logger.debug("\n============ APPC received the request!!! ===========\n");
226 * Simulate a success response from APPC and insert the response into the working memory
228 AppcLcmDmaapWrapper dmaapResponse = new AppcLcmDmaapWrapper();
229 AppcLcmOutput appcResponse = new AppcLcmOutput(appcRequest);
230 appcResponse.getStatus().setCode(400);
231 appcResponse.getStatus().setMessage("AppC success");
232 dmaapResponse.setBody(new AppcLcmBody());
233 dmaapResponse.getBody().setOutput(appcResponse);
235 kieSession.insert(dmaapResponse);
240 * This method is used to simulate event messages from DCAE that start the control loop (onset
241 * message) or end the control loop (abatement message).
243 * @param policy the controlLoopName comes from the policy
244 * @param requestId the requestId for this event
245 * @param status could be onset or abated
247 protected void sendEvent(ControlLoopPolicy policy, UUID requestId,
248 ControlLoopEventStatus status) {
249 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
250 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
251 event.setRequestId(requestId);
252 event.setTarget("generic-vnf.vnf-name");
253 event.setClosedLoopAlarmStart(Instant.now());
254 event.setAai(new HashMap<>());
255 event.getAai().put("generic-vnf.vnf-name", "testGenericVnfName");
256 event.setClosedLoopEventStatus(status);
257 kieSession.insert(event);
260 protected void sendEvent(ControlLoopPolicy policy, UUID requestId,
261 ControlLoopEventStatus status, String vnfName, boolean isEnriched) {
262 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
263 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
264 event.setRequestId(requestId);
265 event.setTarget("generic-vnf.vnf-name");
266 event.setTargetType(ControlLoopTargetType.VNF);
267 event.setClosedLoopAlarmStart(Instant.now());
268 event.setAai(new HashMap<>());
269 event.getAai().put("generic-vnf.vnf-name", vnfName);
270 event.getAai().put("vserver.vserver-name", vnfName);
272 event.getAai().put("generic-vnf.in-maint", "false");
273 event.getAai().put("generic-vnf.is-closed-loop-disabled", "false");
274 event.getAai().put("generic-vnf.orchestration-status", "Created");
275 event.getAai().put("generic-vnf.prov-status", "ACTIVE");
276 event.getAai().put("generic-vnf.resource-version", "1");
277 event.getAai().put("generic-vnf.service-id", "e8cb8968-5411-478b-906a-f28747de72cd");
278 event.getAai().put("generic-vnf.vnf-id", "63b31229-9a3a-444f-9159-04ce2dca3be9");
279 event.getAai().put("generic-vnf.vnf-type", "vCPEInfraService10/vCPEInfraService10 0");
281 event.setClosedLoopEventStatus(status);
282 kieSession.insert(event);