2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Huawei Technologies Co., Ltd. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
20 package org.onap.policy.template.demo;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
27 import java.io.IOException;
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.common.endpoints.event.comm.Topic.CommInfrastructure;
34 import org.onap.policy.common.endpoints.event.comm.TopicListener;
35 import org.onap.policy.common.endpoints.event.comm.TopicSink;
36 import org.onap.policy.controlloop.ControlLoopEventStatus;
37 import org.onap.policy.controlloop.ControlLoopNotificationType;
38 import org.onap.policy.controlloop.ControlLoopTargetType;
39 import org.onap.policy.controlloop.VirtualControlLoopEvent;
40 import org.onap.policy.controlloop.VirtualControlLoopNotification;
41 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
42 import org.onap.policy.sdnc.SdncRequest;
44 public class CcvpnBwControlLoopTest extends ControlLoopBase implements TopicListener {
47 * Setup the simulator.
50 public static void setUpBeforeClass() {
51 ControlLoopBase.setUpBeforeClass(
52 "../archetype-cl-amsterdam/src/main/resources/archetype-resources/"
53 + "src/main/resources/__closedLoopControlName__.drl",
54 "src/test/resources/yaml/policy_ControlLoop_CCVPN_BW.yaml",
56 "BandwidthOnDemand", "2.0.0");
60 public void testSuccess() throws IOException {
63 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
64 * to be pulled from the queue
66 for (TopicSink sink : noopTopics) {
67 assertTrue(sink.start());
72 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
75 sendEvent(pair.first);
77 kieSession.fireUntilHalt();
79 // allow object clean-up
80 kieSession.fireAllRules();
83 * The only fact in memory should be Params
85 assertEquals(1, kieSession.getFactCount());
88 * Print what's left in memory
90 dumpFacts(kieSession);
94 public void testNullRequest() throws IOException {
97 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
98 * to be pulled from the queue
100 for (TopicSink sink : noopTopics) {
101 assertTrue(sink.start());
106 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
110 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
111 event.setClosedLoopControlName(pair.first.getControlLoop().getControlLoopName());
112 event.setRequestId(UUID.randomUUID());
113 event.setClosedLoopEventClient("DCAE.HolmesInstance");
114 event.setTargetType(ControlLoopTargetType.VM);
115 event.setTarget("vserver.vserver-name");
116 event.setFrom("DCAE");
117 event.setClosedLoopAlarmStart(Instant.now());
118 event.setAai(new HashMap<String, String>());
119 event.getAai().put("vserver.vserver-name", "nullRequest");
120 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
121 kieSession.insert(event);
123 kieSession.fireUntilHalt();
125 // allow object clean-up
126 kieSession.fireAllRules();
129 * The only fact in memory should be Params
131 assertEquals(1, kieSession.getFactCount());
134 * Print what's left in memory
136 dumpFacts(kieSession);
142 * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
145 public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
147 * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
151 if ("POLICY-CL-MGT".equals(topic)) {
152 obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
153 VirtualControlLoopNotification.class);
156 if (obj instanceof VirtualControlLoopNotification) {
157 VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
158 String policyName = notification.getPolicyName();
159 if (policyName.endsWith("EVENT")) {
160 logger.debug("Rule Fired: " + notification.getPolicyName());
161 assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
162 } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
163 logger.debug("Rule Fired: " + notification.getPolicyName());
164 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
165 assertNotNull(notification.getMessage());
166 assertTrue(notification.getMessage().startsWith("Sending guard query"));
167 } else if (policyName.endsWith("GUARD.RESPONSE")) {
168 logger.debug("Rule Fired: " + notification.getPolicyName());
169 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
170 assertNotNull(notification.getMessage());
171 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
172 } else if (policyName.endsWith("GUARD_PERMITTED")) {
173 logger.debug("Rule Fired: " + notification.getPolicyName());
174 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
175 assertNotNull(notification.getMessage());
176 assertTrue(notification.getMessage().startsWith("actor=SDNC"));
177 } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
178 logger.debug("Rule Fired: " + notification.getPolicyName());
180 logger.debug("The operation timed out");
181 fail("Operation Timed Out");
182 } else if (policyName.endsWith("SDNC.RESPONSE")) {
183 logger.debug("Rule Fired: " + notification.getPolicyName());
184 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS.equals(notification.getNotification()));
185 assertNotNull(notification.getMessage());
186 assertTrue(notification.getMessage().startsWith("actor=SDNC"));
187 } else if (policyName.endsWith("EVENT.MANAGER")) {
188 logger.debug("Rule Fired: " + notification.getPolicyName());
189 if ("nullRequest".equals(notification.getAai().get("vserver.vserver-name"))) {
190 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
192 assertEquals(ControlLoopNotificationType.FINAL_SUCCESS, notification.getNotification());
195 } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
196 logger.debug("Rule Fired: " + notification.getPolicyName());
198 logger.debug("The control loop timed out");
199 fail("Control Loop Timed Out");
201 } else if (obj instanceof SdncRequest) {
202 logger.debug("\n============ SDNC received the request!!! ===========\n");
207 * This method is used to simulate event messages from DCAE that start the control loop (onset
208 * message) or end the control loop (abatement message).
210 * @param policy the controlLoopName comes from the policy
212 protected void sendEvent(ControlLoopPolicy policy) {
213 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
214 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
215 event.setRequestId(UUID.randomUUID());
216 event.setClosedLoopEventClient("DCAE.HolmesInstance");
217 event.setTargetType(ControlLoopTargetType.VM);
218 event.setTarget("vserver.vserver-name");
219 event.setFrom("DCAE");
220 event.setClosedLoopAlarmStart(Instant.now());
221 event.setAai(new HashMap<String, String>());
222 event.getAai().put("vserver.vserver-name", "TBD");
223 event.getAai().put("globalSubscriberId", "e151059a-d924-4629-845f-264db19e50b4");
224 event.getAai().put("serviceType", "siteService");
225 event.getAai().put("service-instance.service-instance-id", "service-instance-id-example-1");
226 event.getAai().put("vserver.is-closed-loop-disabled", "false");
227 event.getAai().put("vserver.prov-status", "ACTIVE");
228 event.getAai().put("bandwidth", "500");
229 event.getAai().put("bandwidth-change-time", "20190326-05:20:38:668");
230 event.getAai().put("vnfId", "vnf-id");
231 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
232 kieSession.insert(event);