2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Huawei. 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 CcvpnControlLoopTest 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.yaml",
56 "Connectivity Reroute",
61 public void testSuccess() throws IOException {
64 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
65 * to be pulled from the queue
67 for (TopicSink sink : noopTopics) {
68 assertTrue(sink.start());
73 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
76 sendEvent(pair.first);
78 kieSession.fireUntilHalt();
80 // allow object clean-up
81 kieSession.fireAllRules();
84 * The only fact in memory should be Params
86 assertEquals(1, kieSession.getFactCount());
89 * Print what's left in memory
91 dumpFacts(kieSession);
95 public void testNullRequest() throws IOException {
98 * Allows the PolicyEngine to callback to this object to notify that there is an event ready
99 * to be pulled from the queue
101 for (TopicSink sink : noopTopics) {
102 assertTrue(sink.start());
107 * Simulate an onset event the policy engine will receive from DCAE to kick off processing
111 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
112 event.setClosedLoopControlName(pair.first.getControlLoop().getControlLoopName());
113 event.setRequestId(UUID.randomUUID());
114 event.setClosedLoopEventClient("DCAE.HolmesInstance");
115 event.setTargetType(ControlLoopTargetType.VM);
116 event.setTarget("vserver.vserver-name");
117 event.setFrom("DCAE");
118 event.setClosedLoopAlarmStart(Instant.now());
119 event.setAai(new HashMap<String, String>());
120 event.getAai().put("vserver.vserver-name", "nullRequest");
121 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
122 kieSession.insert(event);
124 kieSession.fireUntilHalt();
126 // allow object clean-up
127 kieSession.fireAllRules();
130 * The only fact in memory should be Params
132 assertEquals(1, kieSession.getFactCount());
135 * Print what's left in memory
137 dumpFacts(kieSession);
143 * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
146 public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
148 * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
152 if ("POLICY-CL-MGT".equals(topic)) {
153 obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
154 VirtualControlLoopNotification.class);
157 if (obj instanceof VirtualControlLoopNotification) {
158 VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
159 String policyName = notification.getPolicyName();
160 if (policyName.endsWith("EVENT")) {
161 logger.debug("Rule Fired: " + notification.getPolicyName());
162 assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
163 } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
164 logger.debug("Rule Fired: " + notification.getPolicyName());
165 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
166 assertNotNull(notification.getMessage());
167 assertTrue(notification.getMessage().startsWith("Sending guard query"));
168 } else if (policyName.endsWith("GUARD.RESPONSE")) {
169 logger.debug("Rule Fired: " + notification.getPolicyName());
170 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
171 assertNotNull(notification.getMessage());
172 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
173 } else if (policyName.endsWith("GUARD_PERMITTED")) {
174 logger.debug("Rule Fired: " + notification.getPolicyName());
175 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
176 assertNotNull(notification.getMessage());
177 assertTrue(notification.getMessage().startsWith("actor=SDNC"));
178 } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
179 logger.debug("Rule Fired: " + notification.getPolicyName());
181 logger.debug("The operation timed out");
182 fail("Operation Timed Out");
183 } else if (policyName.endsWith("SDNC.RESPONSE")) {
184 logger.debug("Rule Fired: " + notification.getPolicyName());
185 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS.equals(notification.getNotification()));
186 assertNotNull(notification.getMessage());
187 assertTrue(notification.getMessage().startsWith("actor=SDNC"));
188 } else if (policyName.endsWith("EVENT.MANAGER")) {
189 logger.debug("Rule Fired: " + notification.getPolicyName());
190 if ("nullRequest".equals(notification.getAai().get("vserver.vserver-name"))) {
191 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
193 assertEquals(ControlLoopNotificationType.FINAL_SUCCESS, notification.getNotification());
196 } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
197 logger.debug("Rule Fired: " + notification.getPolicyName());
199 logger.debug("The control loop timed out");
200 fail("Control Loop Timed Out");
202 } else if (obj instanceof SdncRequest) {
203 logger.debug("\n============ SDNC received the request!!! ===========\n");
208 * This method is used to simulate event messages from DCAE that start the control loop (onset
209 * message) or end the control loop (abatement message).
211 * @param policy the controlLoopName comes from the policy
213 protected void sendEvent(ControlLoopPolicy policy) {
214 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
215 event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
216 event.setRequestId(UUID.randomUUID());
217 event.setClosedLoopEventClient("DCAE.HolmesInstance");
218 event.setTargetType(ControlLoopTargetType.VM);
219 event.setTarget("vserver.vserver-name");
220 event.setFrom("DCAE");
221 event.setClosedLoopAlarmStart(Instant.now());
222 event.setAai(new HashMap<String, String>());
223 event.getAai().put("vserver.vserver-name", "TBD");
224 event.getAai().put("globalSubscriberId", "e151059a-d924-4629-845f-264db19e50b4");
225 event.getAai().put("serviceType", "SOTN");
226 event.getAai().put("service-instance.service-instance-id", "service-instance-id-example-1");
227 event.getAai().put("network-information.network-id", "id");
228 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
229 kieSession.insert(event);