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