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.appclcm.AppcLcmBody;
36 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
37 import org.onap.policy.appclcm.AppcLcmInput;
38 import org.onap.policy.appclcm.AppcLcmOutput;
39 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
40 import org.onap.policy.common.endpoints.event.comm.TopicListener;
41 import org.onap.policy.common.endpoints.event.comm.TopicSink;
42 import org.onap.policy.controlloop.ControlLoopEventStatus;
43 import org.onap.policy.controlloop.ControlLoopNotificationType;
44 import org.onap.policy.controlloop.ControlLoopTargetType;
45 import org.onap.policy.controlloop.VirtualControlLoopEvent;
46 import org.onap.policy.controlloop.VirtualControlLoopNotification;
47 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
49 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
50 public class VcpeControlLoopTest extends ControlLoopBase implements TopicListener {
53 * Setup the simulator.
56 public static void setUpBeforeClass() {
57 ControlLoopBase.setUpBeforeClass(
58 "../archetype-cl-amsterdam/src/main/resources/archetype-resources"
59 + "/src/main/resources/__closedLoopControlName__.drl",
60 "src/test/resources/yaml/policy_ControlLoop_vCPE.yaml",
61 "service=ServiceDemo;resource=Res1Demo;type=operational", "CL_vCPE",
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, "OzVServer", true);
88 kieSession.fireUntilHalt();
90 // allow object clean-up
91 kieSession.fireAllRules();
94 * The only fact in memory should be Params
96 assertEquals(1, kieSession.getFactCount());
99 * Print what's left in memory
101 dumpFacts(kieSession);
105 public void testBAaiGetFail() {
108 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
109 * to be pulled from the queue
111 for (TopicSink sink : noopTopics) {
112 assertTrue(sink.start());
117 * Create a unique requestId
119 requestId = UUID.randomUUID();
122 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
125 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "getFail", false);
127 kieSession.fireUntilHalt();
129 // allow object clean-up
130 kieSession.fireAllRules();
133 * The only fact in memory should be Params
135 assertEquals(1, kieSession.getFactCount());
138 * Print what's left in memory
140 dumpFacts(kieSession);
146 * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
149 public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
151 * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
155 if ("POLICY-CL-MGT".equals(topic)) {
156 obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
157 org.onap.policy.controlloop.VirtualControlLoopNotification.class);
158 } else if ("APPC-LCM-READ".equals(topic)) {
159 obj = org.onap.policy.appclcm.util.Serialization.gsonJunit.fromJson(event,
160 org.onap.policy.appclcm.AppcLcmDmaapWrapper.class);
163 if (obj instanceof VirtualControlLoopNotification) {
164 VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
165 String policyName = notification.getPolicyName();
166 if (policyName.endsWith("EVENT")) {
167 logger.debug("Rule Fired: " + notification.getPolicyName());
169 ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
170 } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
171 logger.debug("Rule Fired: " + notification.getPolicyName());
173 ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
174 assertNotNull(notification.getMessage());
175 assertTrue(notification.getMessage().startsWith("Sending guard query"));
176 } else if (policyName.endsWith("GUARD.RESPONSE")) {
177 logger.debug("Rule Fired: " + notification.getPolicyName());
179 ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
180 assertNotNull(notification.getMessage());
181 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
182 } else if (policyName.endsWith("GUARD_PERMITTED")) {
183 logger.debug("Rule Fired: " + notification.getPolicyName());
185 ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
186 assertNotNull(notification.getMessage());
187 assertTrue(notification.getMessage().startsWith("actor=APPC"));
188 } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
189 logger.debug("Rule Fired: " + notification.getPolicyName());
191 logger.debug("The operation timed out");
192 fail("Operation Timed Out");
193 } else if (policyName.endsWith("APPC.LCM.RESPONSE")) {
194 logger.debug("Rule Fired: " + notification.getPolicyName());
195 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS
196 .equals(notification.getNotification()));
197 assertNotNull(notification.getMessage());
198 assertTrue(notification.getMessage().startsWith("actor=APPC"));
199 sendEvent(pair.first, requestId, ControlLoopEventStatus.ABATED);
200 } else if (policyName.endsWith("EVENT.MANAGER")) {
201 logger.debug("Rule Fired: " + notification.getPolicyName());
202 if ("getFail".equals(notification.getAai().get("generic-vnf.vnf-name"))) {
203 assertEquals(ControlLoopNotificationType.FINAL_FAILURE,
204 notification.getNotification());
207 assertEquals(ControlLoopNotificationType.FINAL_SUCCESS,
208 notification.getNotification());
211 } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
212 logger.debug("Rule Fired: " + notification.getPolicyName());
214 logger.debug("The control loop timed out");
215 fail("Control Loop Timed Out");
217 } else if (obj instanceof AppcLcmDmaapWrapper) {
219 * The request should be of type LcmRequestWrapper and the subrequestid should be 1
221 AppcLcmDmaapWrapper dmaapRequest = (AppcLcmDmaapWrapper) obj;
222 AppcLcmInput appcRequest = dmaapRequest.getBody().getInput();
223 assertTrue(appcRequest.getCommonHeader().getSubRequestId().equals("1"));
224 assertNotNull(appcRequest.getActionIdentifiers().get("vnf-id"));
226 logger.debug("\n============ APPC received the request!!! ===========\n");
229 * Simulate a success response from APPC and insert the response into the working memory
231 AppcLcmDmaapWrapper dmaapResponse = new AppcLcmDmaapWrapper();
232 AppcLcmOutput appcResponse = new AppcLcmOutput(appcRequest);
233 appcResponse.getStatus().setCode(400);
234 appcResponse.getStatus().setMessage("AppC success");
235 dmaapResponse.setBody(new AppcLcmBody());
236 dmaapResponse.getBody().setOutput(appcResponse);
238 kieSession.insert(dmaapResponse);
243 * This method is used to simulate event messages from DCAE that start the control loop (onset
244 * message) or end the control loop (abatement message).
246 * @param policy the controlLoopName comes from the policy
247 * @param requestId the requestId for this event
248 * @param status could be onset or abated
250 protected void sendEvent(ControlLoopPolicy policy, UUID requestId,
251 ControlLoopEventStatus status) {
252 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
253 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
254 event.setRequestId(requestId);
255 event.setTarget("generic-vnf.vnf-name");
256 event.setClosedLoopAlarmStart(Instant.now());
257 event.setAai(new HashMap<>());
258 event.getAai().put("generic-vnf.vnf-name", "testGenericVnfName");
259 event.setClosedLoopEventStatus(status);
260 kieSession.insert(event);
263 protected void sendEvent(ControlLoopPolicy policy, UUID requestId,
264 ControlLoopEventStatus status, String vnfName, boolean isEnriched) {
265 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
266 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
267 event.setRequestId(requestId);
268 event.setTarget("generic-vnf.vnf-name");
269 event.setTargetType(ControlLoopTargetType.VNF);
270 event.setClosedLoopAlarmStart(Instant.now());
271 event.setAai(new HashMap<>());
272 event.getAai().put("generic-vnf.vnf-name", vnfName);
273 event.getAai().put("vserver.vserver-name", vnfName);
275 event.getAai().put("generic-vnf.in-maint", "false");
276 event.getAai().put("generic-vnf.is-closed-loop-disabled", "false");
277 event.getAai().put("generic-vnf.orchestration-status", "Created");
278 event.getAai().put("generic-vnf.prov-status", "ACTIVE");
279 event.getAai().put("generic-vnf.resource-version", "1");
280 event.getAai().put("generic-vnf.service-id", "e8cb8968-5411-478b-906a-f28747de72cd");
281 event.getAai().put("generic-vnf.vnf-id", "63b31229-9a3a-444f-9159-04ce2dca3be9");
282 event.getAai().put("generic-vnf.vnf-type", "vCPEInfraService10/vCPEInfraService10 0");
284 event.setClosedLoopEventStatus(status);
285 kieSession.insert(event);