f41bd874b9bcc0442676d463b2429b5347d7d046
[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.AppcLcmBody;
34 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
35 import org.onap.policy.appclcm.AppcLcmInput;
36 import org.onap.policy.appclcm.AppcLcmOutput;
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", "CL_vCPE",
59             "org.onap.closed_loop.ServiceDemo:VNFS:1.0.0");
60     }
61
62     @Test
63     public void successTest() {
64
65         /*
66          * Allows the PolicyEngine to callback to this object to notify that there is an event ready
67          * to be pulled from the queue
68          */
69         for (TopicSink sink : noopTopics) {
70             assertTrue(sink.start());
71             sink.register(this);
72         }
73
74         /*
75          * Create a unique requestId
76          */
77         requestId = UUID.randomUUID();
78
79         /*
80          * Simulate an onset event the policy engine will receive from DCAE to kick off processing
81          * through the rules
82          */
83         sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "OzVServer", true);
84
85         kieSession.fireUntilHalt();
86
87         // allow object clean-up
88         kieSession.fireAllRules();
89
90         /*
91          * The only fact in memory should be Params
92          */
93         assertEquals(1, kieSession.getFactCount());
94
95         /*
96          * Print what's left in memory
97          */
98         dumpFacts(kieSession);
99     }
100
101     @Test
102     public void aaiGetFailTest() {
103
104         /*
105          * Allows the PolicyEngine to callback to this object to notify that there is an event ready
106          * to be pulled from the queue
107          */
108         for (TopicSink sink : noopTopics) {
109             assertTrue(sink.start());
110             sink.register(this);
111         }
112
113         /*
114          * Create a unique requestId
115          */
116         requestId = UUID.randomUUID();
117
118         /*
119          * Simulate an onset event the policy engine will receive from DCAE to kick off processing
120          * through the rules
121          */
122         sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "getFail", false);
123
124         kieSession.fireUntilHalt();
125
126         // allow object clean-up
127         kieSession.fireAllRules();
128
129         /*
130          * The only fact in memory should be Params
131          */
132         assertEquals(1, kieSession.getFactCount());
133
134         /*
135          * Print what's left in memory
136          */
137         dumpFacts(kieSession);
138     }
139
140     /*
141      * (non-Javadoc)
142      *
143      * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
144      */
145     @Override
146     public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
147         /*
148          * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
149          * of type active
150          */
151         Object obj = null;
152         if ("POLICY-CL-MGT".equals(topic)) {
153             obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
154                 org.onap.policy.controlloop.VirtualControlLoopNotification.class);
155         } else if ("APPC-LCM-READ".equals(topic)) {
156             obj = org.onap.policy.appclcm.util.Serialization.gsonJunit.fromJson(event,
157                 org.onap.policy.appclcm.AppcLcmDmaapWrapper.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=APPC"));
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("APPC.LCM.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=APPC"));
196                 sendEvent(pair.first, requestId, ControlLoopEventStatus.ABATED);
197             } else if (policyName.endsWith("EVENT.MANAGER")) {
198                 logger.debug("Rule Fired: " + notification.getPolicyName());
199                 if ("getFail".equals(notification.getAai().get("generic-vnf.vnf-name"))) {
200                     assertEquals(ControlLoopNotificationType.FINAL_FAILURE,
201                         notification.getNotification());
202                     kieSession.halt();
203                 } else {
204                     assertEquals(ControlLoopNotificationType.FINAL_SUCCESS,
205                         notification.getNotification());
206                     kieSession.halt();
207                 }
208             } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
209                 logger.debug("Rule Fired: " + notification.getPolicyName());
210                 kieSession.halt();
211                 logger.debug("The control loop timed out");
212                 fail("Control Loop Timed Out");
213             }
214         } else if (obj instanceof AppcLcmDmaapWrapper) {
215             /*
216              * The request should be of type LcmRequestWrapper and the subrequestid should be 1
217              */
218             AppcLcmDmaapWrapper dmaapRequest = (AppcLcmDmaapWrapper) obj;
219             AppcLcmInput appcRequest = dmaapRequest.getBody().getInput();
220             assertTrue(appcRequest.getCommonHeader().getSubRequestId().equals("1"));
221             assertNotNull(appcRequest.getActionIdentifiers().get("vnf-id"));
222
223             logger.debug("\n============ APPC received the request!!! ===========\n");
224
225             /*
226              * Simulate a success response from APPC and insert the response into the working memory
227              */
228             AppcLcmDmaapWrapper dmaapResponse = new AppcLcmDmaapWrapper();
229             AppcLcmOutput appcResponse = new AppcLcmOutput(appcRequest);
230             appcResponse.getStatus().setCode(400);
231             appcResponse.getStatus().setMessage("AppC success");
232             dmaapResponse.setBody(new AppcLcmBody());
233             dmaapResponse.getBody().setOutput(appcResponse);
234
235             kieSession.insert(dmaapResponse);
236         }
237     }
238
239     /**
240      * This method is used to simulate event messages from DCAE that start the control loop (onset
241      * message) or end the control loop (abatement message).
242      *
243      * @param policy the controlLoopName comes from the policy
244      * @param requestId the requestId for this event
245      * @param status could be onset or abated
246      */
247     protected void sendEvent(ControlLoopPolicy policy, UUID requestId,
248         ControlLoopEventStatus status) {
249         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
250         event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
251         event.setRequestId(requestId);
252         event.setTarget("generic-vnf.vnf-name");
253         event.setClosedLoopAlarmStart(Instant.now());
254         event.setAai(new HashMap<>());
255         event.getAai().put("generic-vnf.vnf-name", "testGenericVnfName");
256         event.setClosedLoopEventStatus(status);
257         kieSession.insert(event);
258     }
259
260     protected void sendEvent(ControlLoopPolicy policy, UUID requestId,
261         ControlLoopEventStatus status, String vnfName, boolean isEnriched) {
262         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
263         event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
264         event.setRequestId(requestId);
265         event.setTarget("generic-vnf.vnf-name");
266         event.setTargetType(ControlLoopTargetType.VNF);
267         event.setClosedLoopAlarmStart(Instant.now());
268         event.setAai(new HashMap<>());
269         event.getAai().put("generic-vnf.vnf-name", vnfName);
270         event.getAai().put("vserver.vserver-name", vnfName);
271         if (isEnriched) {
272             event.getAai().put("generic-vnf.in-maint", "false");
273             event.getAai().put("generic-vnf.is-closed-loop-disabled", "false");
274             event.getAai().put("generic-vnf.orchestration-status", "Created");
275             event.getAai().put("generic-vnf.prov-status", "ACTIVE");
276             event.getAai().put("generic-vnf.resource-version", "1");
277             event.getAai().put("generic-vnf.service-id", "e8cb8968-5411-478b-906a-f28747de72cd");
278             event.getAai().put("generic-vnf.vnf-id", "63b31229-9a3a-444f-9159-04ce2dca3be9");
279             event.getAai().put("generic-vnf.vnf-type", "vCPEInfraService10/vCPEInfraService10 0");
280         }
281         event.setClosedLoopEventStatus(status);
282         kieSession.insert(event);
283     }
284 }