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