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