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.LcmRequest;
34 import org.onap.policy.appclcm.LcmRequestWrapper;
35 import org.onap.policy.appclcm.LcmResponse;
36 import org.onap.policy.appclcm.LcmResponseWrapper;
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");
61 SupportUtil.setCustomQuery("false");
65 public void successTest() {
68 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
69 * to be pulled from the queue
71 for (TopicSink sink : noopTopics) {
72 assertTrue(sink.start());
77 * Create a unique requestId
79 requestId = UUID.randomUUID();
82 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
85 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "vCPEInfraVNF13", true);
87 kieSession.fireUntilHalt();
89 // allow object clean-up
90 kieSession.fireAllRules();
93 * The only fact in memory should be Params
95 assertEquals(1, kieSession.getFactCount());
98 * Print what's left in memory
100 dumpFacts(kieSession);
104 public void aaiGetFailTest() {
107 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
108 * to be pulled from the queue
110 for (TopicSink sink : noopTopics) {
111 assertTrue(sink.start());
116 * Create a unique requestId
118 requestId = UUID.randomUUID();
121 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
124 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "getFail", false);
126 kieSession.fireUntilHalt();
128 // allow object clean-up
129 kieSession.fireAllRules();
132 * The only fact in memory should be Params
134 assertEquals(1, kieSession.getFactCount());
137 * Print what's left in memory
139 dumpFacts(kieSession);
145 * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
148 public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
150 * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
154 if ("POLICY-CL-MGT".equals(topic)) {
155 obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
156 org.onap.policy.controlloop.VirtualControlLoopNotification.class);
157 } else if ("APPC-LCM-READ".equals(topic)) {
158 obj = org.onap.policy.appclcm.util.Serialization.gsonJunit.fromJson(event,
159 org.onap.policy.appclcm.LcmRequestWrapper.class);
162 if (obj instanceof VirtualControlLoopNotification) {
163 VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
164 String policyName = notification.getPolicyName();
165 if (policyName.endsWith("EVENT")) {
166 logger.debug("Rule Fired: " + notification.getPolicyName());
167 assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
168 } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
169 logger.debug("Rule Fired: " + notification.getPolicyName());
170 assertTrue(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());
175 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
176 assertNotNull(notification.getMessage());
177 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
178 } else if (policyName.endsWith("GUARD_PERMITTED")) {
179 logger.debug("Rule Fired: " + notification.getPolicyName());
180 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
181 assertNotNull(notification.getMessage());
182 assertTrue(notification.getMessage().startsWith("actor=APPC"));
183 } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
184 logger.debug("Rule Fired: " + notification.getPolicyName());
186 logger.debug("The operation timed out");
187 fail("Operation Timed Out");
188 } else if (policyName.endsWith("APPC.LCM.RESPONSE")) {
189 logger.debug("Rule Fired: " + notification.getPolicyName());
190 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS.equals(notification.getNotification()));
191 assertNotNull(notification.getMessage());
192 assertTrue(notification.getMessage().startsWith("actor=APPC"));
193 sendEvent(pair.first, requestId, ControlLoopEventStatus.ABATED);
194 } else if (policyName.endsWith("EVENT.MANAGER")) {
195 logger.debug("Rule Fired: " + notification.getPolicyName());
196 if ("getFail".equals(notification.getAai().get("generic-vnf.vnf-name"))) {
197 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
200 assertEquals(ControlLoopNotificationType.FINAL_SUCCESS, notification.getNotification());
203 } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
204 logger.debug("Rule Fired: " + notification.getPolicyName());
206 logger.debug("The control loop timed out");
207 fail("Control Loop Timed Out");
209 } else if (obj instanceof LcmRequestWrapper) {
211 * The request should be of type LcmRequestWrapper and the subrequestid should be 1
213 LcmRequestWrapper dmaapRequest = (LcmRequestWrapper) obj;
214 LcmRequest appcRequest = dmaapRequest.getBody();
215 assertTrue(appcRequest.getCommonHeader().getSubRequestId().equals("1"));
216 assertNotNull(appcRequest.getActionIdentifiers().get("vnf-id"));
218 logger.debug("\n============ APPC received the request!!! ===========\n");
221 * Simulate a success response from APPC and insert the response into the working memory
223 LcmResponseWrapper dmaapResponse = new LcmResponseWrapper();
224 LcmResponse appcResponse = new LcmResponse(appcRequest);
225 appcResponse.getStatus().setCode(400);
226 appcResponse.getStatus().setMessage("AppC success");
227 dmaapResponse.setBody(appcResponse);
228 kieSession.insert(dmaapResponse);
233 * This method is used to simulate event messages from DCAE that start the control loop (onset
234 * message) or end the control loop (abatement message).
236 * @param policy the controlLoopName comes from the policy
237 * @param requestId the requestId for this event
238 * @param status could be onset or abated
240 protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status) {
241 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
242 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
243 event.setRequestId(requestId);
244 event.setTarget("generic-vnf.vnf-name");
245 event.setClosedLoopAlarmStart(Instant.now());
246 event.setAai(new HashMap<>());
247 event.getAai().put("generic-vnf.vnf-name", "testGenericVnfName");
248 event.setClosedLoopEventStatus(status);
249 kieSession.insert(event);
252 protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status, String vnfName,
253 boolean isEnriched) {
254 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
255 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
256 event.setRequestId(requestId);
257 event.setTarget("generic-vnf.vnf-name");
258 event.setTargetType(ControlLoopTargetType.VNF);
259 event.setClosedLoopAlarmStart(Instant.now());
260 event.setAai(new HashMap<>());
261 event.getAai().put("generic-vnf.vnf-name", vnfName);
263 event.getAai().put("generic-vnf.in-maint", "false");
264 event.getAai().put("generic-vnf.is-closed-loop-disabled", "false");
265 event.getAai().put("generic-vnf.orchestration-status", "Created");
266 event.getAai().put("generic-vnf.prov-status", "ACTIVE");
267 event.getAai().put("generic-vnf.resource-version", "1");
268 event.getAai().put("generic-vnf.service-id", "e8cb8968-5411-478b-906a-f28747de72cd");
269 event.getAai().put("generic-vnf.vnf-id", "63b31229-9a3a-444f-9159-04ce2dca3be9");
270 event.getAai().put("generic-vnf.vnf-type", "vCPEInfraService10/vCPEInfraService10 0");
272 event.setClosedLoopEventStatus(status);
273 kieSession.insert(event);