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