bf81c2d190b20364a3cbd6f1d677b643808dbc4f
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 Huawei Technologies Co., Ltd. All rights reserved.
4  * Modifications Copyright (C) 2019 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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=========================================================
18  */
19
20 package org.onap.policy.template.demo;
21
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;
26
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;
43
44 public class CcvpnBwControlLoopTest extends ControlLoopBase implements TopicListener {
45
46     /**
47      * Setup the simulator.
48      */
49     @BeforeClass
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",
55             "type=operational",
56             "BandwidthOnDemand", "2.0.0");
57     }
58
59     @Test
60     public void successTest() throws IOException {
61
62         /*
63          * Allows the PolicyEngine to callback to this object to notify that there is an event ready
64          * to be pulled from the queue
65          */
66         for (TopicSink sink : noopTopics) {
67             assertTrue(sink.start());
68             sink.register(this);
69         }
70
71         /*
72          * Simulate an onset event the policy engine will receive from DCAE to kick off processing
73          * through the rules
74          */
75         sendEvent(pair.first);
76
77         kieSession.fireUntilHalt();
78
79         // allow object clean-up
80         kieSession.fireAllRules();
81
82         /*
83          * The only fact in memory should be Params
84          */
85         assertEquals(1, kieSession.getFactCount());
86
87         /*
88          * Print what's left in memory
89          */
90         dumpFacts(kieSession);
91     }
92
93     @Test
94     public void nullRequestTest() throws IOException {
95
96         /*
97          * Allows the PolicyEngine to callback to this object to notify that there is an event ready
98          * to be pulled from the queue
99          */
100         for (TopicSink sink : noopTopics) {
101             assertTrue(sink.start());
102             sink.register(this);
103         }
104
105         /*
106          * Simulate an onset event the policy engine will receive from DCAE to kick off processing
107          * through the rules
108          */
109
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);
122
123         kieSession.fireUntilHalt();
124
125         // allow object clean-up
126         kieSession.fireAllRules();
127
128         /*
129          * The only fact in memory should be Params
130          */
131         assertEquals(1, kieSession.getFactCount());
132
133         /*
134          * Print what's left in memory
135          */
136         dumpFacts(kieSession);
137     }
138
139     /*
140      * (non-Javadoc)
141      *
142      * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
143      */
144     @Override
145     public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
146         /*
147          * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
148          * of type active
149          */
150         Object obj = null;
151         if ("POLICY-CL-MGT".equals(topic)) {
152             obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
153                     VirtualControlLoopNotification.class);
154         }
155         assertNotNull(obj);
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());
179                 kieSession.halt();
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());
191                 } else {
192                     assertEquals(ControlLoopNotificationType.FINAL_SUCCESS, notification.getNotification());
193                 }
194                 kieSession.halt();
195             } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
196                 logger.debug("Rule Fired: " + notification.getPolicyName());
197                 kieSession.halt();
198                 logger.debug("The control loop timed out");
199                 fail("Control Loop Timed Out");
200             }
201         } else if (obj instanceof SdncRequest) {
202             logger.debug("\n============ SDNC received the request!!! ===========\n");
203         }
204     }
205
206     /**
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).
209      *
210      * @param policy the controlLoopName comes from the policy
211      */
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);
233     }
234 }