Modify to work with new drools-pdp
[policy/drools-applications.git] / controlloop / templates / template.demo / src / test / java / org / onap / policy / template / demo / VdnsControlLoopTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * demo
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.template.demo;
22
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;
27
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.VirtualControlLoopEvent;
39 import org.onap.policy.controlloop.VirtualControlLoopNotification;
40 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
41 import org.onap.policy.so.SoRequest;
42
43 public class VdnsControlLoopTest extends ControlLoopBase implements TopicListener {
44
45     /**
46      * Setup the simulator.
47      */
48     @BeforeClass
49     public static void setUpBeforeClass() {
50         ControlLoopBase.setUpBeforeClass(
51             "../archetype-cl-amsterdam/src/main/resources/archetype-resources/"
52                             + "src/main/resources/__closedLoopControlName__.drl",
53             "src/test/resources/yaml/policy_ControlLoop_SO-test.yaml",
54             "type=operational",
55             "CL_vDNS",
56             "v2.0");
57     }
58
59     @Test
60     public void successTest() {
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          * Create a unique requestId
73          */
74         requestId = UUID.randomUUID();
75
76         /*
77          * Simulate an onset event the policy engine will receive from DCAE to kick off processing
78          * through the rules
79          */
80         sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET);
81
82         kieSession.fireUntilHalt();
83
84         // allow object clean-up
85         kieSession.fireAllRules();
86
87         /*
88          * The only fact in memory should be Params
89          */
90         assertEquals(1, kieSession.getFactCount());
91
92         /*
93          * Print what's left in memory
94          */
95         dumpFacts(kieSession);
96     }
97
98     @Test
99     public void namedQueryFailTest() {
100
101         /*
102          * Allows the PolicyEngine to callback to this object to notify that there is an event ready
103          * to be pulled from the queue
104          */
105         for (TopicSink sink : noopTopics) {
106             assertTrue(sink.start());
107             sink.register(this);
108         }
109
110         /*
111          * Create a unique requestId
112          */
113         requestId = UUID.randomUUID();
114
115         /*
116          * Simulate an onset event the policy engine will receive from DCAE to kick off processing
117          * through the rules
118          */
119         sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "error");
120
121         kieSession.fireUntilHalt();
122
123         // allow object clean-up
124         kieSession.fireAllRules();
125
126         /*
127          * The only fact in memory should be Params
128          */
129         assertEquals(1, kieSession.getFactCount());
130
131         /*
132          * Print what's left in memory
133          */
134         dumpFacts(kieSession);
135     }
136
137     @Test
138     public void aaiGetFailTest() {
139
140         /*
141          * Allows the PolicyEngine to callback to this object to notify that there is an event ready
142          * to be pulled from the queue
143          */
144         for (TopicSink sink : noopTopics) {
145             assertTrue(sink.start());
146             sink.register(this);
147         }
148
149         /*
150          * Create a unique requestId
151          */
152         requestId = UUID.randomUUID();
153
154         /*
155          * Simulate an onset event the policy engine will receive from DCAE to kick off processing
156          * through the rules
157          */
158         sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "getFail");
159
160         try {
161             kieSession.fireUntilHalt();
162
163             // allow object clean-up
164             kieSession.fireAllRules();
165
166         } catch (Exception e) {
167             e.printStackTrace();
168             logger.warn(e.toString());
169             fail(e.getMessage());
170         }
171
172
173         /*
174          * The only fact in memory should be Params
175          */
176         assertEquals(1, kieSession.getFactCount());
177
178         /*
179          * Print what's left in memory
180          */
181         dumpFacts(kieSession);
182     }
183
184     /*
185      * (non-Javadoc)
186      *
187      * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
188      */
189     @Override
190     public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
191         /*
192          * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
193          * of type active
194          */
195         Object obj = null;
196         if ("POLICY-CL-MGT".equals(topic)) {
197             obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
198                     org.onap.policy.controlloop.VirtualControlLoopNotification.class);
199         }
200         assertNotNull(obj);
201         if (obj instanceof VirtualControlLoopNotification) {
202             VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
203             String policyName = notification.getPolicyName();
204             if (policyName.endsWith("EVENT")) {
205                 logger.debug("Rule Fired: " + notification.getPolicyName());
206                 assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
207             } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
208                 logger.debug("Rule Fired: " + notification.getPolicyName());
209                 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
210                 assertNotNull(notification.getMessage());
211                 assertTrue(notification.getMessage().startsWith("Sending guard query"));
212             } else if (policyName.endsWith("GUARD.RESPONSE")) {
213                 logger.debug("Rule Fired: " + notification.getPolicyName());
214                 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
215                 assertNotNull(notification.getMessage());
216                 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
217             } else if (policyName.endsWith("GUARD_PERMITTED")) {
218                 logger.debug("Rule Fired: " + notification.getPolicyName());
219                 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
220                 assertNotNull(notification.getMessage());
221                 assertTrue(notification.getMessage().startsWith("actor=SO"));
222             } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
223                 logger.debug("Rule Fired: " + notification.getPolicyName());
224                 kieSession.halt();
225                 logger.debug("The operation timed out");
226                 fail("Operation Timed Out");
227             } else if (policyName.endsWith("SO.RESPONSE")) {
228                 logger.debug("Rule Fired: " + notification.getPolicyName());
229                 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS.equals(notification.getNotification()));
230                 assertNotNull(notification.getMessage());
231                 assertTrue(notification.getMessage().startsWith("actor=SO"));
232             } else if (policyName.endsWith("EVENT.MANAGER")) {
233                 logger.debug("Rule Fired: " + notification.getPolicyName());
234                 if ("error".equals(notification.getAai().get("vserver.vserver-name"))) {
235                     assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
236                 } else if ("getFail".equals(notification.getAai().get("vserver.vserver-name"))) {
237                     assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
238                 } else {
239                     assertTrue(ControlLoopNotificationType.FINAL_SUCCESS.equals(notification.getNotification()));
240                 }
241                 kieSession.halt();
242             } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
243                 logger.debug("Rule Fired: " + notification.getPolicyName());
244                 kieSession.halt();
245                 logger.debug("The control loop timed out");
246                 fail("Control Loop Timed Out");
247             }
248         } else if (obj instanceof SoRequest) {
249             logger.debug("\n============ SO received the request!!! ===========\n");
250         }
251     }
252
253     /**
254      * This method is used to simulate event messages from DCAE that start the control loop (onset
255      * message) or end the control loop (abatement message).
256      *
257      * @param policy the controlLoopName comes from the policy
258      * @param requestId the requestId for this event
259      * @param status could be onset or abated
260      */
261     protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status) {
262         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
263         event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
264         event.setRequestId(requestId);
265         event.setTarget("vserver.vserver-name");
266         event.setClosedLoopAlarmStart(Instant.now());
267         event.setAai(new HashMap<>());
268         event.getAai().put("vserver.vserver-name", "dfw1lb01lb01");
269         event.getAai().put("vserver.is-closed-loop-disabled", "false");
270         event.getAai().put("vserver.prov-status", "ACTIVE");
271         event.setClosedLoopEventStatus(status);
272         kieSession.insert(event);
273     }
274
275     protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status,
276             String vserverName) {
277         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
278         event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
279         event.setRequestId(requestId);
280         event.setTarget("vserver.vserver-name");
281         event.setClosedLoopAlarmStart(Instant.now());
282         event.setAai(new HashMap<>());
283         event.getAai().put("vserver.vserver-name", vserverName);
284         event.setClosedLoopEventStatus(status);
285         kieSession.insert(event);
286     }
287 }