2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Wipro Limited Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.template.demo;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
29 import java.time.Instant;
30 import java.util.HashMap;
31 import java.util.UUID;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
35 import org.onap.policy.common.endpoints.event.comm.TopicListener;
36 import org.onap.policy.common.endpoints.event.comm.TopicSink;
37 import org.onap.policy.controlloop.ControlLoopEventStatus;
38 import org.onap.policy.controlloop.ControlLoopNotificationType;
39 import org.onap.policy.controlloop.ControlLoopTargetType;
40 import org.onap.policy.controlloop.VirtualControlLoopEvent;
41 import org.onap.policy.controlloop.VirtualControlLoopNotification;
42 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
43 import org.onap.policy.sdnr.PciRequest;
44 import org.onap.policy.sdnr.PciRequestWrapper;
45 import org.onap.policy.sdnr.PciResponse;
46 import org.onap.policy.sdnr.PciResponseWrapper;
48 public class VpciControlLoopTest extends ControlLoopBase implements TopicListener {
51 * Setup the simulator.
54 public static void setUpBeforeClass() {
55 ControlLoopBase.setUpBeforeClass(
56 "../archetype-cl-amsterdam/src/main/resources/archetype-resources"
57 + "/src/main/resources/__closedLoopControlName__.drl",
58 "src/test/resources/yaml/policy_ControlLoop_vPCI.yaml",
65 public void successTest() {
68 * Allows the PolicyEngine to callback to this object to notify that there is an
69 * event ready 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
83 * processing through the rules
85 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, 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);
105 public void aaiGetFailTest() {
108 * Allows the PolicyEngine to callback to this object to notify that there is an
109 * event ready 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
123 * processing through the rules
125 sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, 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);
148 * org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.
152 public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
153 logger.debug("\n============ onTopicEvent!!! ===========\n");
154 logger.debug("topic: {}, event: {}", topic, event);
156 * Pull the object that was sent out to DMAAP and make sure it is a
157 * ControlLoopNoticiation of type active
160 if ("POLICY-CL-MGT".equals(topic)) {
161 obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
162 org.onap.policy.controlloop.VirtualControlLoopNotification.class);
163 } else if ("SDNR-CL".equals(topic)) {
164 obj = org.onap.policy.sdnr.util.Serialization.gsonJunit.fromJson(event,
165 org.onap.policy.sdnr.PciRequestWrapper.class);
168 if (obj instanceof VirtualControlLoopNotification) {
169 VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
170 String policyName = notification.getPolicyName();
171 logger.debug("Rule Fired: {}", policyName);
172 if (policyName.endsWith("EVENT")) {
173 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
174 } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
175 assertEquals(ControlLoopNotificationType.OPERATION, notification.getNotification());
176 assertNotNull(notification.getMessage());
177 assertTrue(notification.getMessage().startsWith("Sending guard query"));
178 } else if (policyName.endsWith("GUARD.RESPONSE")) {
179 assertEquals(ControlLoopNotificationType.OPERATION, notification.getNotification());
180 assertNotNull(notification.getMessage());
181 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
182 } else if (policyName.endsWith("GUARD_PERMITTED")) {
183 assertEquals(ControlLoopNotificationType.OPERATION, notification.getNotification());
184 assertNotNull(notification.getMessage());
185 assertTrue(notification.getMessage().startsWith("actor=SDNR"));
186 } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
188 logger.debug("The operation timed out");
189 fail("Operation Timed Out");
190 } else if (policyName.endsWith("SDNR.RESPONSE")) {
191 assertEquals(ControlLoopNotificationType.OPERATION_SUCCESS, notification.getNotification());
192 assertNotNull(notification.getMessage());
193 assertTrue(notification.getMessage().startsWith("actor=SDNR"));
194 } else if (policyName.endsWith("EVENT.MANAGER")) {
195 if ("getFail".equals(notification.getAai().get("generic-vnf.vnf-id"))) {
196 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
199 assertEquals(ControlLoopNotificationType.FINAL_SUCCESS, notification.getNotification());
202 } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
204 logger.debug("The control loop timed out");
205 fail("Control Loop Timed Out");
207 } else if (obj instanceof PciRequestWrapper) {
209 * The request should be of type PciRequestWrapper and the subrequestid should
212 PciRequestWrapper dmaapRequest = (PciRequestWrapper) obj;
213 PciRequest pciRequest = dmaapRequest.getBody();
214 assertEquals("1", pciRequest.getCommonHeader().getSubRequestId());
216 logger.debug("\n============ SDNR received the request!!! ===========\n");
217 logger.debug("\n============ dmaapRequest ===========\n {} ", dmaapRequest);
218 logger.debug("\n============ pciRequest ===========\n {}", pciRequest);
221 * Simulate a success response from SDNR and insert the response into the
224 PciResponse pciResponse = new PciResponse(pciRequest);
225 pciResponse.getStatus().setCode(200);
226 pciResponse.getStatus().setValue("SUCCESS");
227 StringBuilder sb = new StringBuilder();
228 sb.append("{ \"Configurations\":[ { \"Status\": { \"Code\": 200, \"Value\":"
229 + " \"SUCCESS\" }, \"data\":{ \"FAPService\":{ \"alias\":"
230 + "\"Network1\", \"X0005b9Lte\" : { \"PnfName\" : \"cu1\" }, \"CellConfig\":"
231 + "{ \"LTE\":{ \"RAN\":{ \"Common\":{ \"CellIdentity\":" + "\"1\" } } } } } } } ] }");
233 pciResponse.setPayload(sb.toString());
234 PciResponseWrapper dmaapResponse = new PciResponseWrapper();
235 dmaapResponse.setBody(pciResponse);
236 dmaapResponse.setType("response");
237 logger.debug("\n============ SDNR sending response!!! ===========\n");
238 logger.debug("\n============ dmaapResponse ===========\n {}", dmaapResponse);
239 logger.debug("\n============ pciResponse ===========\n {}", pciResponse);
240 kieSession.insert(dmaapResponse);
245 * This method is used to simulate event messages from DCAE that start the
246 * control loop (onset message).
249 * the controlLoopName comes from the policy
251 * the requestId for this event
255 protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status,
256 boolean isEnriched) {
257 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
258 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
259 event.setRequestId(requestId);
260 event.setTarget("generic-vnf.vnf-id");
261 event.setTargetType(ControlLoopTargetType.VNF);
262 event.setClosedLoopAlarmStart(Instant.now());
263 event.setAai(new HashMap<>());
265 event.getAai().put("generic-vnf.is-closed-loop-disabled", "false");
266 event.getAai().put("generic-vnf.prov-status", "ACTIVE");
267 event.getAai().put("generic-vnf.vnf-id", "notused");
269 event.getAai().put("generic-vnf.vnf-id", "getFail");
271 event.setClosedLoopEventStatus(status);
272 StringBuilder sb = new StringBuilder();
273 sb.append("{ \"Configurations\":[ { \"data\":{ \"FAPService\":"
274 + " { \"alias\":\"Cell1\", \"X0005b9Lte\" : { \"PhyCellIdInUse\" :"
275 + " \"35\", \"PnfName\" : \"cu1\" }, \"CellConfig\":{ \"LTE\":{ \"RAN\":"
276 + "{ \"Common\":{ \"CellIdentity\":\"1\" } } } } } } } ] }");
278 event.setPayload(sb.toString());
279 logger.debug("\n============ Policy receiving ONSET event !!! ===========\n");
280 logger.debug("\n============ event ===========\n {}", event);
281 kieSession.insert(event);