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",
60 "org.onap.closed_loop.ServiceDemo:VNFS:1.0.0");
64 public void successTest() {
67 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
68 * to be pulled from the queue
70 for (TopicSink sink : noopTopics) {
71 assertTrue(sink.start());
76 * Create a unique requestId
78 requestId = UUID.randomUUID();
81 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
84 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "vCPEInfraVNF13", true);
86 kieSession.fireUntilHalt();
88 // allow object clean-up
89 kieSession.fireAllRules();
92 * The only fact in memory should be Params
94 assertEquals(1, kieSession.getFactCount());
97 * Print what's left in memory
99 dumpFacts(kieSession);
103 public void aaiGetFailTest() {
106 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
107 * to be pulled from the queue
109 for (TopicSink sink : noopTopics) {
110 assertTrue(sink.start());
115 * Create a unique requestId
117 requestId = UUID.randomUUID();
120 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
123 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "getFail", false);
125 kieSession.fireUntilHalt();
127 // allow object clean-up
128 kieSession.fireAllRules();
131 * The only fact in memory should be Params
133 assertEquals(1, kieSession.getFactCount());
136 * Print what's left in memory
138 dumpFacts(kieSession);
144 * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
147 public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
149 * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
153 if ("POLICY-CL-MGT".equals(topic)) {
154 obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
155 org.onap.policy.controlloop.VirtualControlLoopNotification.class);
156 } else if ("APPC-LCM-READ".equals(topic)) {
157 obj = org.onap.policy.appclcm.util.Serialization.gsonJunit.fromJson(event,
158 org.onap.policy.appclcm.AppcLcmDmaapWrapper.class);
161 if (obj instanceof VirtualControlLoopNotification) {
162 VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
163 String policyName = notification.getPolicyName();
164 if (policyName.endsWith("EVENT")) {
165 logger.debug("Rule Fired: " + notification.getPolicyName());
166 assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
167 } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
168 logger.debug("Rule Fired: " + notification.getPolicyName());
169 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
170 assertNotNull(notification.getMessage());
171 assertTrue(notification.getMessage().startsWith("Sending guard query"));
172 } else if (policyName.endsWith("GUARD.RESPONSE")) {
173 logger.debug("Rule Fired: " + notification.getPolicyName());
174 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
175 assertNotNull(notification.getMessage());
176 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
177 } else if (policyName.endsWith("GUARD_PERMITTED")) {
178 logger.debug("Rule Fired: " + notification.getPolicyName());
179 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
180 assertNotNull(notification.getMessage());
181 assertTrue(notification.getMessage().startsWith("actor=APPC"));
182 } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
183 logger.debug("Rule Fired: " + notification.getPolicyName());
185 logger.debug("The operation timed out");
186 fail("Operation Timed Out");
187 } else if (policyName.endsWith("APPC.LCM.RESPONSE")) {
188 logger.debug("Rule Fired: " + notification.getPolicyName());
189 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS.equals(notification.getNotification()));
190 assertNotNull(notification.getMessage());
191 assertTrue(notification.getMessage().startsWith("actor=APPC"));
192 sendEvent(pair.first, requestId, ControlLoopEventStatus.ABATED);
193 } else if (policyName.endsWith("EVENT.MANAGER")) {
194 logger.debug("Rule Fired: " + notification.getPolicyName());
195 if ("getFail".equals(notification.getAai().get("generic-vnf.vnf-name"))) {
196 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
199 assertEquals(ControlLoopNotificationType.FINAL_SUCCESS, notification.getNotification());
202 } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
203 logger.debug("Rule Fired: " + notification.getPolicyName());
205 logger.debug("The control loop timed out");
206 fail("Control Loop Timed Out");
208 } else if (obj instanceof AppcLcmDmaapWrapper) {
210 * The request should be of type LcmRequestWrapper and the subrequestid should be 1
212 AppcLcmDmaapWrapper dmaapRequest = (AppcLcmDmaapWrapper) obj;
213 AppcLcmInput appcRequest = dmaapRequest.getBody().getInput();
214 assertTrue(appcRequest.getCommonHeader().getSubRequestId().equals("1"));
215 assertNotNull(appcRequest.getActionIdentifiers().get("vnf-id"));
217 logger.debug("\n============ APPC received the request!!! ===========\n");
220 * Simulate a success response from APPC and insert the response into the working memory
222 AppcLcmDmaapWrapper dmaapResponse = new AppcLcmDmaapWrapper();
223 AppcLcmOutput appcResponse = new AppcLcmOutput(appcRequest);
224 appcResponse.getStatus().setCode(400);
225 appcResponse.getStatus().setMessage("AppC success");
226 dmaapResponse.setBody(new AppcLcmBody());
227 dmaapResponse.getBody().setOutput(appcResponse);
229 kieSession.insert(dmaapResponse);
234 * This method is used to simulate event messages from DCAE that start the control loop (onset
235 * message) or end the control loop (abatement message).
237 * @param policy the controlLoopName comes from the policy
238 * @param requestId the requestId for this event
239 * @param status could be onset or abated
241 protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status) {
242 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
243 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
244 event.setRequestId(requestId);
245 event.setTarget("generic-vnf.vnf-name");
246 event.setClosedLoopAlarmStart(Instant.now());
247 event.setAai(new HashMap<>());
248 event.getAai().put("generic-vnf.vnf-name", "testGenericVnfName");
249 event.setClosedLoopEventStatus(status);
250 kieSession.insert(event);
253 protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status, String vnfName,
254 boolean isEnriched) {
255 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
256 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
257 event.setRequestId(requestId);
258 event.setTarget("generic-vnf.vnf-name");
259 event.setTargetType(ControlLoopTargetType.VNF);
260 event.setClosedLoopAlarmStart(Instant.now());
261 event.setAai(new HashMap<>());
262 event.getAai().put("generic-vnf.vnf-name", vnfName);
264 event.getAai().put("generic-vnf.in-maint", "false");
265 event.getAai().put("generic-vnf.is-closed-loop-disabled", "false");
266 event.getAai().put("generic-vnf.orchestration-status", "Created");
267 event.getAai().put("generic-vnf.prov-status", "ACTIVE");
268 event.getAai().put("generic-vnf.resource-version", "1");
269 event.getAai().put("generic-vnf.service-id", "e8cb8968-5411-478b-906a-f28747de72cd");
270 event.getAai().put("generic-vnf.vnf-id", "63b31229-9a3a-444f-9159-04ce2dca3be9");
271 event.getAai().put("generic-vnf.vnf-type", "vCPEInfraService10/vCPEInfraService10 0");
273 event.setClosedLoopEventStatus(status);
274 kieSession.insert(event);