1a5bf398245aa17fb71186f88c016e460f5544d5
[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.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 VdnsControlLoopCqTest 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_Cq-test.yaml", "type=operational",
55             "CL_vDNS", "v2.0");
56         SupportUtil.setCustomQuery("true");
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 aaiGetFailTest() {
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, "getFail");
120
121         try {
122             kieSession.fireUntilHalt();
123
124             // allow object clean-up
125             kieSession.fireAllRules();
126
127         } catch (Exception e) {
128             e.printStackTrace();
129             logger.warn(e.toString());
130             fail(e.getMessage());
131         }
132
133         /*
134          * The only fact in memory should be Params
135          */
136         assertEquals(1, kieSession.getFactCount());
137
138         /*
139          * Print what's left in memory
140          */
141         dumpFacts(kieSession);
142     }
143
144     /*
145      * (non-Javadoc)
146      *
147      * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
148      */
149     @Override
150     public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
151         /*
152          * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
153          * of type active
154          */
155         Object obj = null;
156         if ("POLICY-CL-MGT".equals(topic)) {
157             obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
158                 org.onap.policy.controlloop.VirtualControlLoopNotification.class);
159         }
160         assertNotNull(obj);
161         if (obj instanceof VirtualControlLoopNotification) {
162             VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
163             String policyName = notification.getPolicyName();
164             if (policyName.endsWith("EVENT")) {
165                 logger.debug("Rule Fired: " + notification.getPolicyName());
166                 assertTrue(
167                     ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
168             } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
169                 logger.debug("Rule Fired: " + notification.getPolicyName());
170                 assertTrue(
171                     ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
172                 assertNotNull(notification.getMessage());
173                 assertTrue(notification.getMessage().startsWith("Sending guard query"));
174             } else if (policyName.endsWith("GUARD.RESPONSE")) {
175                 logger.debug("Rule Fired: " + notification.getPolicyName());
176                 assertTrue(
177                     ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
178                 assertNotNull(notification.getMessage());
179                 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
180             } else if (policyName.endsWith("GUARD_PERMITTED")) {
181                 logger.debug("Rule Fired: " + notification.getPolicyName());
182                 assertTrue(
183                     ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
184                 assertNotNull(notification.getMessage());
185                 assertTrue(notification.getMessage().startsWith("actor=SO"));
186             } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
187                 logger.debug("Rule Fired: " + notification.getPolicyName());
188                 kieSession.halt();
189                 logger.debug("The operation timed out");
190                 fail("Operation Timed Out");
191             } else if (policyName.endsWith("SO.RESPONSE")) {
192                 logger.debug("Rule Fired: " + notification.getPolicyName());
193                 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS
194                     .equals(notification.getNotification()));
195                 assertNotNull(notification.getMessage());
196                 assertTrue(notification.getMessage().startsWith("actor=SO"));
197             } else if (policyName.endsWith("EVENT.MANAGER")) {
198                 logger.debug("Rule Fired: " + notification.getPolicyName());
199                 if ("error".equals(notification.getAai().get("vserver.vserver-name"))) {
200                     assertEquals(ControlLoopNotificationType.FINAL_FAILURE,
201                         notification.getNotification());
202                 } else if ("getFail".equals(notification.getAai().get("vserver.vserver-name"))) {
203                     assertEquals(ControlLoopNotificationType.FINAL_FAILURE,
204                         notification.getNotification());
205                 } else {
206                     assertTrue(ControlLoopNotificationType.FINAL_SUCCESS
207                         .equals(notification.getNotification()));
208                 }
209                 kieSession.halt();
210             } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
211                 logger.debug("Rule Fired: " + notification.getPolicyName());
212                 kieSession.halt();
213                 logger.debug("The control loop timed out");
214                 fail("Control Loop Timed Out");
215             }
216         } else if (obj instanceof SoRequest) {
217             logger.debug("\n============ SO received the request!!! ===========\n");
218         }
219     }
220
221     /**
222      * This method is used to simulate event messages from DCAE that start the control loop (onset
223      * message) or end the control loop (abatement message).
224      *
225      * @param policy the controlLoopName comes from the policy
226      * @param requestId the requestId for this event
227      * @param status could be onset or abated
228      */
229     protected void sendEvent(ControlLoopPolicy policy, UUID requestId,
230         ControlLoopEventStatus status) {
231         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
232         event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
233         event.setRequestId(requestId);
234         event.setTarget("vserver.vserver-name");
235         event.setTargetType(ControlLoopTargetType.VNF);
236         event.setClosedLoopAlarmStart(Instant.now());
237         event.setAai(new HashMap<>());
238         event.getAai().put("vserver.vserver-name", "Ete_vFWCLvFWSNK_7ba1fbde_0");
239         event.getAai().put("vserver.is-closed-loop-disabled", "false");
240         event.getAai().put("vserver.prov-status", "ACTIVE");
241         event.setClosedLoopEventStatus(status);
242         kieSession.insert(event);
243     }
244
245     protected void sendEvent(ControlLoopPolicy policy, UUID requestId,
246         ControlLoopEventStatus status, String vserverName) {
247         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
248         event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
249         event.setRequestId(requestId);
250         event.setTarget("vserver.vserver-name");
251         event.setTargetType(ControlLoopTargetType.VNF);
252         event.setClosedLoopAlarmStart(Instant.now());
253         event.setAai(new HashMap<>());
254         event.getAai().put("vserver.vserver-name", vserverName);
255         event.setClosedLoopEventStatus(status);
256         kieSession.insert(event);
257     }
258 }