25bef2ebb2ea17f965ccde5acd659fd250996bfc
[policy/drools-applications.git] /
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.appclcm.LcmRequest;
34 import org.onap.policy.appclcm.LcmRequestWrapper;
35 import org.onap.policy.appclcm.LcmResponse;
36 import org.onap.policy.appclcm.LcmResponseWrapper;
37 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
38 import org.onap.policy.common.endpoints.event.comm.TopicListener;
39 import org.onap.policy.common.endpoints.event.comm.TopicSink;
40 import org.onap.policy.controlloop.ControlLoopEventStatus;
41 import org.onap.policy.controlloop.ControlLoopNotificationType;
42 import org.onap.policy.controlloop.ControlLoopTargetType;
43 import org.onap.policy.controlloop.VirtualControlLoopEvent;
44 import org.onap.policy.controlloop.VirtualControlLoopNotification;
45 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
46
47 public class VcpeControlLoopTest extends ControlLoopBase implements TopicListener {
48
49     /**
50      * Setup the simulator.
51      */
52     @BeforeClass
53     public static void setUpBeforeClass() {
54         ControlLoopBase.setUpBeforeClass(
55             "../archetype-cl-amsterdam/src/main/resources/archetype-resources"
56                             + "/src/main/resources/__closedLoopControlName__.drl",
57             "src/test/resources/yaml/policy_ControlLoop_vCPE.yaml",
58             "service=ServiceDemo;resource=Res1Demo;type=operational",
59             "CL_vCPE",
60             "org.onap.closed_loop.ServiceDemo:VNFS:1.0.0");
61         SupportUtil.setCustomQuery("false");
62     }
63
64     @Test
65     public void successTest() {
66
67         /*
68          * Allows the PolicyEngine to callback to this object to notify that there is an event ready
69          * to be pulled from the queue
70          */
71         for (TopicSink sink : noopTopics) {
72             assertTrue(sink.start());
73             sink.register(this);
74         }
75
76         /*
77          * Create a unique requestId
78          */
79         requestId = UUID.randomUUID();
80
81         /*
82          * Simulate an onset event the policy engine will receive from DCAE to kick off processing
83          * through the rules
84          */
85         sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "vCPEInfraVNF13", true);
86
87         kieSession.fireUntilHalt();
88
89         // allow object clean-up
90         kieSession.fireAllRules();
91
92         /*
93          * The only fact in memory should be Params
94          */
95         assertEquals(1, kieSession.getFactCount());
96
97         /*
98          * Print what's left in memory
99          */
100         dumpFacts(kieSession);
101     }
102
103     @Test
104     public void aaiGetFailTest() {
105
106         /*
107          * Allows the PolicyEngine to callback to this object to notify that there is an event ready
108          * to be pulled from the queue
109          */
110         for (TopicSink sink : noopTopics) {
111             assertTrue(sink.start());
112             sink.register(this);
113         }
114
115         /*
116          * Create a unique requestId
117          */
118         requestId = UUID.randomUUID();
119
120         /*
121          * Simulate an onset event the policy engine will receive from DCAE to kick off processing
122          * through the rules
123          */
124         sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "getFail", false);
125
126         kieSession.fireUntilHalt();
127
128         // allow object clean-up
129         kieSession.fireAllRules();
130
131         /*
132          * The only fact in memory should be Params
133          */
134         assertEquals(1, kieSession.getFactCount());
135
136         /*
137          * Print what's left in memory
138          */
139         dumpFacts(kieSession);
140     }
141
142     /*
143      * (non-Javadoc)
144      *
145      * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
146      */
147     @Override
148     public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
149         /*
150          * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
151          * of type active
152          */
153         Object obj = null;
154         if ("POLICY-CL-MGT".equals(topic)) {
155             obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
156                     org.onap.policy.controlloop.VirtualControlLoopNotification.class);
157         } else if ("APPC-LCM-READ".equals(topic)) {
158             obj = org.onap.policy.appclcm.util.Serialization.gsonJunit.fromJson(event,
159                     org.onap.policy.appclcm.LcmRequestWrapper.class);
160         }
161         assertNotNull(obj);
162         if (obj instanceof VirtualControlLoopNotification) {
163             VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
164             String policyName = notification.getPolicyName();
165             if (policyName.endsWith("EVENT")) {
166                 logger.debug("Rule Fired: " + notification.getPolicyName());
167                 assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
168             } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
169                 logger.debug("Rule Fired: " + notification.getPolicyName());
170                 assertTrue(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(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
176                 assertNotNull(notification.getMessage());
177                 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
178             } else if (policyName.endsWith("GUARD_PERMITTED")) {
179                 logger.debug("Rule Fired: " + notification.getPolicyName());
180                 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
181                 assertNotNull(notification.getMessage());
182                 assertTrue(notification.getMessage().startsWith("actor=APPC"));
183             } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
184                 logger.debug("Rule Fired: " + notification.getPolicyName());
185                 kieSession.halt();
186                 logger.debug("The operation timed out");
187                 fail("Operation Timed Out");
188             } else if (policyName.endsWith("APPC.LCM.RESPONSE")) {
189                 logger.debug("Rule Fired: " + notification.getPolicyName());
190                 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS.equals(notification.getNotification()));
191                 assertNotNull(notification.getMessage());
192                 assertTrue(notification.getMessage().startsWith("actor=APPC"));
193                 sendEvent(pair.first, requestId, ControlLoopEventStatus.ABATED);
194             } else if (policyName.endsWith("EVENT.MANAGER")) {
195                 logger.debug("Rule Fired: " + notification.getPolicyName());
196                 if ("getFail".equals(notification.getAai().get("generic-vnf.vnf-name"))) {
197                     assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
198                     kieSession.halt();
199                 } else {
200                     assertEquals(ControlLoopNotificationType.FINAL_SUCCESS, notification.getNotification());
201                     kieSession.halt();
202                 }
203             } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
204                 logger.debug("Rule Fired: " + notification.getPolicyName());
205                 kieSession.halt();
206                 logger.debug("The control loop timed out");
207                 fail("Control Loop Timed Out");
208             }
209         } else if (obj instanceof LcmRequestWrapper) {
210             /*
211              * The request should be of type LcmRequestWrapper and the subrequestid should be 1
212              */
213             LcmRequestWrapper dmaapRequest = (LcmRequestWrapper) obj;
214             LcmRequest appcRequest = dmaapRequest.getBody();
215             assertTrue(appcRequest.getCommonHeader().getSubRequestId().equals("1"));
216             assertNotNull(appcRequest.getActionIdentifiers().get("vnf-id"));
217
218             logger.debug("\n============ APPC received the request!!! ===========\n");
219
220             /*
221              * Simulate a success response from APPC and insert the response into the working memory
222              */
223             LcmResponseWrapper dmaapResponse = new LcmResponseWrapper();
224             LcmResponse appcResponse = new LcmResponse(appcRequest);
225             appcResponse.getStatus().setCode(400);
226             appcResponse.getStatus().setMessage("AppC success");
227             dmaapResponse.setBody(appcResponse);
228             kieSession.insert(dmaapResponse);
229         }
230     }
231
232     /**
233      * This method is used to simulate event messages from DCAE that start the control loop (onset
234      * message) or end the control loop (abatement message).
235      *
236      * @param policy the controlLoopName comes from the policy
237      * @param requestId the requestId for this event
238      * @param status could be onset or abated
239      */
240     protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status) {
241         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
242         event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
243         event.setRequestId(requestId);
244         event.setTarget("generic-vnf.vnf-name");
245         event.setClosedLoopAlarmStart(Instant.now());
246         event.setAai(new HashMap<>());
247         event.getAai().put("generic-vnf.vnf-name", "testGenericVnfName");
248         event.setClosedLoopEventStatus(status);
249         kieSession.insert(event);
250     }
251
252     protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status, String vnfName,
253             boolean isEnriched) {
254         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
255         event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
256         event.setRequestId(requestId);
257         event.setTarget("generic-vnf.vnf-name");
258         event.setTargetType(ControlLoopTargetType.VNF);
259         event.setClosedLoopAlarmStart(Instant.now());
260         event.setAai(new HashMap<>());
261         event.getAai().put("generic-vnf.vnf-name", vnfName);
262         if (isEnriched) {
263             event.getAai().put("generic-vnf.in-maint", "false");
264             event.getAai().put("generic-vnf.is-closed-loop-disabled", "false");
265             event.getAai().put("generic-vnf.orchestration-status", "Created");
266             event.getAai().put("generic-vnf.prov-status", "ACTIVE");
267             event.getAai().put("generic-vnf.resource-version", "1");
268             event.getAai().put("generic-vnf.service-id", "e8cb8968-5411-478b-906a-f28747de72cd");
269             event.getAai().put("generic-vnf.vnf-id", "63b31229-9a3a-444f-9159-04ce2dca3be9");
270             event.getAai().put("generic-vnf.vnf-type", "vCPEInfraService10/vCPEInfraService10 0");
271         }
272         event.setClosedLoopEventStatus(status);
273         kieSession.insert(event);
274     }
275 }