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