Merge "Remove drools PDP dependency"
[policy/models.git] / models-interactions / model-actors / actor.appclcm / src / test / java / org / onap / policy / controlloop / actor / appclcm / AppcLcmActorServiceProviderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * AppcServiceProviderTest
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controlloop.actor.appclcm;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.fail;
28
29 import java.time.Instant;
30 import java.util.AbstractMap;
31 import java.util.HashMap;
32 import java.util.UUID;
33
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.policy.aai.util.AaiException;
38 import org.onap.policy.appclcm.LcmCommonHeader;
39 import org.onap.policy.appclcm.LcmRequest;
40 import org.onap.policy.appclcm.LcmRequestWrapper;
41 import org.onap.policy.appclcm.LcmResponse;
42 import org.onap.policy.appclcm.LcmResponseWrapper;
43 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
44 import org.onap.policy.controlloop.ControlLoopEventStatus;
45 import org.onap.policy.controlloop.ControlLoopOperation;
46 import org.onap.policy.controlloop.ControlLoopTargetType;
47 import org.onap.policy.controlloop.VirtualControlLoopEvent;
48 import org.onap.policy.controlloop.policy.Policy;
49 import org.onap.policy.controlloop.policy.PolicyResult;
50 import org.onap.policy.controlloop.policy.Target;
51 import org.onap.policy.controlloop.policy.TargetType;
52 import org.onap.policy.simulators.Util;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56
57 public class AppcLcmActorServiceProviderTest {
58
59     private static final Logger logger = LoggerFactory.getLogger(AppcLcmActorServiceProviderTest.class);
60
61     private static final VirtualControlLoopEvent onsetEvent;
62     private static final ControlLoopOperation operation;
63     private static final Policy policy;
64     private static final LcmResponseWrapper dmaapResponse;
65
66     private static final String RECIPE_RESTART = "Restart";
67     private static final String RECIPE_REBUILD = "Rebuild";
68     private static final String RECIPE_MIGRATE = "Migrate";
69
70     static {
71         /*
72          * Construct an onset with an AAI subtag containing generic-vnf.vnf-id and a target type of
73          * VM.
74          */
75         onsetEvent = new VirtualControlLoopEvent();
76         onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
77         onsetEvent.setRequestId(UUID.randomUUID());
78         onsetEvent.setClosedLoopEventClient("tca.instance00001");
79         onsetEvent.setTargetType(ControlLoopTargetType.VM);
80         onsetEvent.setTarget("generic-vnf.vnf-name");
81         onsetEvent.setFrom("DCAE");
82         onsetEvent.setClosedLoopAlarmStart(Instant.now());
83         onsetEvent.setAai(new HashMap<>());
84         onsetEvent.getAai().put("generic-vnf.vnf-name", "fw0001vm001fw001");
85         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
86
87         /* Construct an operation with an APPC actor and restart operation. */
88         operation = new ControlLoopOperation();
89         operation.setActor("APPC");
90         operation.setOperation("Restart");
91         operation.setTarget("VM");
92         operation.setEnd(Instant.now());
93         operation.setSubRequestId("1");
94
95         /* Construct a policy specifying to restart vm. */
96         policy = new Policy();
97         policy.setName("Restart the VM");
98         policy.setDescription("Upon getting the trigger event, restart the VM");
99         policy.setActor("APPC");
100         policy.setTarget(new Target(TargetType.VNF));
101         policy.setRecipe("Restart");
102         policy.setPayload(null);
103         policy.setRetry(2);
104         policy.setTimeout(300);
105
106         /* A sample DMAAP request wrapper. */
107         LcmRequestWrapper dmaapRequest = new LcmRequestWrapper();
108         dmaapRequest.setCorrelationId(onsetEvent.getRequestId().toString() + "-" + "1");
109         dmaapRequest.setRpcName(policy.getRecipe().toLowerCase());
110         dmaapRequest.setType("request");
111
112         /* A sample DMAAP response wrapper */
113         dmaapResponse = new LcmResponseWrapper();
114         dmaapResponse.setCorrelationId(onsetEvent.getRequestId().toString() + "-" + "1");
115         dmaapResponse.setRpcName(policy.getRecipe().toLowerCase());
116         dmaapResponse.setType("response");
117
118         /* A sample APPC LCM request. */
119         LcmRequest appcRequest = new LcmRequest();
120
121         /* The following code constructs a sample APPC LCM Request */
122         appcRequest.setAction("restart");
123
124         HashMap<String, String> actionIdentifiers = new HashMap<>();
125         actionIdentifiers.put("vnf-id", "trial-vnf-003");
126
127         appcRequest.setActionIdentifiers(actionIdentifiers);
128
129         LcmCommonHeader commonHeader = new LcmCommonHeader();
130         commonHeader.setRequestId(onsetEvent.getRequestId());
131         commonHeader.setSubRequestId("1");
132         commonHeader.setOriginatorId(onsetEvent.getRequestId().toString());
133
134         appcRequest.setCommonHeader(commonHeader);
135
136         appcRequest.setPayload(null);
137
138         dmaapRequest.setBody(appcRequest);
139
140         /* The following code constructs a sample APPC LCM Response */
141         LcmResponse appcResponse = new LcmResponse(appcRequest);
142         appcResponse.getStatus().setCode(400);
143         appcResponse.getStatus().setMessage("Restart Successful");
144
145         dmaapResponse.setBody(appcResponse);
146     }
147
148     /**
149      * Set up before test class.
150      */
151     @BeforeClass
152     public static void setUpSimulator() {
153         try {
154             Util.buildAaiSim();
155         } catch (Exception e) {
156             fail(e.getMessage());
157         }
158     }
159
160     /**
161      * Tear down after test class.
162      */
163     @AfterClass
164     public static void tearDownSimulator() {
165         HttpServletServer.factory.destroy();
166     }
167
168     /**
169      * A test to construct an APPC LCM restart request.
170      */
171     @Test
172     public void constructRestartRequestTest() {
173
174         LcmRequestWrapper dmaapRequest =
175                 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
176
177         /* The service provider must return a non null DMAAP request wrapper */
178         assertNotNull(dmaapRequest);
179
180         /* The DMAAP wrapper's type field must be request */
181         assertEquals("request", dmaapRequest.getType());
182
183         /* The DMAAP wrapper's body field cannot be null */
184         assertNotNull(dmaapRequest.getBody());
185
186         LcmRequest appcRequest = dmaapRequest.getBody();
187
188         /* A common header is required and cannot be null */
189         assertNotNull(appcRequest.getCommonHeader());
190         assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
191
192         /* An action is required and cannot be null */
193         assertNotNull(appcRequest.getAction());
194         assertEquals("Restart", appcRequest.getAction());
195
196         /* Action Identifiers are required and cannot be null */
197         assertNotNull(appcRequest.getActionIdentifiers());
198         assertNotNull(appcRequest.getActionIdentifiers().get("vnf-id"));
199         assertEquals("vnf01", appcRequest.getActionIdentifiers().get("vnf-id"));
200
201         logger.debug("APPC Request: \n" + appcRequest.toString());
202     }
203
204     /**
205      * A test to process a successful APPC restart response.
206      */
207     @Test
208     public void processRestartResponseSuccessTest() {
209         AbstractMap.SimpleEntry<PolicyResult, String> result =
210                 AppcLcmActorServiceProvider.processResponse(dmaapResponse);
211         assertEquals(PolicyResult.SUCCESS, result.getKey());
212         assertEquals("Restart Successful", result.getValue());
213     }
214
215     /**
216      * A test to map APPC response results to corresponding Policy results.
217      */
218     @Test
219     public void appcToPolicyResultTest() {
220
221         AbstractMap.SimpleEntry<PolicyResult, String> result;
222
223         /* If APPC accepts, PolicyResult is null */
224         dmaapResponse.getBody().getStatus().setCode(100);
225         dmaapResponse.getBody().getStatus().setMessage("ACCEPTED");
226         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
227         assertNull(result.getKey());
228
229         /* If APPC is successful, PolicyResult is success */
230         dmaapResponse.getBody().getStatus().setCode(400);
231         dmaapResponse.getBody().getStatus().setMessage("SUCCESS");
232         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
233         assertEquals(PolicyResult.SUCCESS, result.getKey());
234
235         /* If APPC returns an error, PolicyResult is failure exception */
236         dmaapResponse.getBody().getStatus().setCode(200);
237         dmaapResponse.getBody().getStatus().setMessage("ERROR");
238         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
239         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
240
241         /* If APPC rejects, PolicyResult is failure exception */
242         dmaapResponse.getBody().getStatus().setCode(300);
243         dmaapResponse.getBody().getStatus().setMessage("REJECT");
244         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
245         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
246
247         /* Test multiple reject codes */
248         dmaapResponse.getBody().getStatus().setCode(306);
249         dmaapResponse.getBody().getStatus().setMessage("REJECT");
250         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
251         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
252
253         dmaapResponse.getBody().getStatus().setCode(313);
254         dmaapResponse.getBody().getStatus().setMessage("REJECT");
255         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
256         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
257
258         /* If APPC returns failure, PolicyResult is failure */
259         dmaapResponse.getBody().getStatus().setCode(401);
260         dmaapResponse.getBody().getStatus().setMessage("FAILURE");
261         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
262         assertEquals(PolicyResult.FAILURE, result.getKey());
263
264         /* Test multiple failure codes */
265         dmaapResponse.getBody().getStatus().setCode(406);
266         dmaapResponse.getBody().getStatus().setMessage("FAILURE");
267         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
268         assertEquals(PolicyResult.FAILURE, result.getKey());
269
270         dmaapResponse.getBody().getStatus().setCode(450);
271         dmaapResponse.getBody().getStatus().setMessage("FAILURE");
272         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
273         assertEquals(PolicyResult.FAILURE, result.getKey());
274
275         /* If APPC returns partial success, PolicyResult is failure exception */
276         dmaapResponse.getBody().getStatus().setCode(500);
277         dmaapResponse.getBody().getStatus().setMessage("PARTIAL SUCCESS");
278         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
279         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
280
281         /* If APPC returns partial failure, PolicyResult is failure exception */
282         dmaapResponse.getBody().getStatus().setCode(501);
283         dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
284         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
285         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
286
287         /* Test multiple partial failure codes */
288         dmaapResponse.getBody().getStatus().setCode(599);
289         dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
290         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
291         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
292
293         dmaapResponse.getBody().getStatus().setCode(550);
294         dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
295         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
296         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
297
298         /* If APPC code is unknown to Policy, PolicyResult is failure exception */
299         dmaapResponse.getBody().getStatus().setCode(700);
300         dmaapResponse.getBody().getStatus().setMessage("UNKNOWN");
301         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
302         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
303     }
304
305     /**
306      * This test ensures that that if the the source entity is also the target entity, the source
307      * will be used for the APPC request.
308      */
309     @Test
310     public void sourceIsTargetTest() {
311         String resourceId = "82194af1-3c2c-485a-8f44-420e22a9eaa4";
312         String targetVnfId = null;
313         try {
314             targetVnfId = AppcLcmActorServiceProvider.vnfNamedQuery(resourceId, "vnf01",
315                     "http://localhost:6666", "AAI", "AAI");
316         } catch (AaiException e) {
317             logger.warn(e.toString());
318             fail("no vnf-id found");
319         }
320         assertNotNull(targetVnfId);
321         assertEquals("vnf01", targetVnfId);
322     }
323
324     /**
325      * THis test exercises getters not exercised in other tests.
326      */
327     @Test
328     public void testMethods() {
329         AppcLcmActorServiceProvider sp = new AppcLcmActorServiceProvider();
330
331         assertEquals("APPC", sp.actor());
332         assertEquals(4, sp.recipes().size());
333         assertEquals("VM", sp.recipeTargets("Restart").get(0));
334         assertEquals("vm-id", sp.recipePayloads("Restart").get(0));
335     }
336
337     @Test
338     public void payloadNotPassedWhenNotSupportedByRecipe() {
339         //given
340         Policy migratePolicy = constructPolicyWithRecipe(RECIPE_MIGRATE);
341         Policy rebuildPolicy = constructPolicyWithRecipe(RECIPE_REBUILD);
342         Policy restartPolicy = constructPolicyWithRecipe(RECIPE_RESTART);
343
344         // when
345         LcmRequestWrapper migrateRequest =
346             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, migratePolicy, "vnf01");
347         LcmRequestWrapper rebuildRequest =
348             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, rebuildPolicy, "vnf01");
349         LcmRequestWrapper restartRequest =
350             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, restartPolicy, "vnf01");
351
352         // then
353         assertNull(migrateRequest.getBody().getPayload());
354         assertNull(rebuildRequest.getBody().getPayload());
355         assertNull(restartRequest.getBody().getPayload());
356     }
357
358     @Test
359     public void payloadNotPassedWhenNotSuppliedOrEmpty() {
360         //given
361         Policy noPayloadPolicy = constructHealthCheckPolicyWithPayload(null);
362         Policy emptyPayloadPolicy = constructHealthCheckPolicyWithPayload(new HashMap<>());
363
364         // when
365         LcmRequestWrapper noPayloadRequest =
366             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, noPayloadPolicy, "vnf01");
367         LcmRequestWrapper emptyPayloadRequest =
368             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, emptyPayloadPolicy, "vnf01");
369
370
371         // then
372         assertNull(noPayloadRequest.getBody().getPayload());
373         assertNull(emptyPayloadRequest.getBody().getPayload());
374     }
375
376     @Test
377     public void payloadParsedProperlyForSinglePayloadParameter() {
378         // given
379         HashMap<String, String> payload = new HashMap<>();
380         payload.put("requestParameters", "{\"host-ip-address\":\"10.183.37.25\"}");
381         Policy otherPolicy = constructHealthCheckPolicyWithPayload(payload);
382
383         // when
384         LcmRequestWrapper dmaapRequest =
385             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, otherPolicy, "vnf01");
386
387         // then
388         assertEquals(dmaapRequest.getBody().getPayload(),
389             "{\"requestParameters\": {\"host-ip-address\":\"10.183.37.25\"}}");
390     }
391
392
393     @Test
394     public void payloadParsedProperlyForMultiplePayloadParameters() {
395         // given
396         HashMap<String, String> payload = new HashMap<>();
397         payload.put("requestParameters", "{\"host-ip-address\":\"10.183.37.25\"}");
398         payload.put("configurationParameters", "[{\"ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[9]\","
399             + "\"oam-ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[16]\","
400             + "\"enabled\":\"$.vf-module-topology.vf-module-parameters.param[23]\"}]");
401         Policy otherPolicy = constructHealthCheckPolicyWithPayload(payload);
402
403         // when
404         LcmRequestWrapper dmaapRequest =
405             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, otherPolicy, "vnf01");
406
407         // then
408         assertEquals(dmaapRequest.getBody().getPayload(),
409             "{\"requestParameters\": "
410                 + "{\"host-ip-address\":\"10.183.37.25\"},"
411                 + "\"configurationParameters\": "
412                 + "[{\"ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[9]\","
413                 + "\"oam-ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[16]\","
414                 + "\"enabled\":\"$.vf-module-topology.vf-module-parameters.param[23]\"}]"
415                 + "}");
416     }
417
418     private Policy constructHealthCheckPolicyWithPayload(HashMap<String, String> payload) {
419         return constructHealthCheckPolicyWithPayloadAndRecipe(payload, "Health-Check");
420     }
421
422     private Policy constructPolicyWithRecipe(String recipe) {
423         return constructHealthCheckPolicyWithPayloadAndRecipe(null, recipe);
424     }
425
426     private Policy constructHealthCheckPolicyWithPayloadAndRecipe(HashMap<String, String> payload, String recipe) {
427         Policy otherPolicy = new Policy();
428         otherPolicy.setName("Perform health check");
429         otherPolicy.setDescription("Upon getting the trigger event, perform health check");
430         otherPolicy.setActor("APPC");
431         otherPolicy.setTarget(new Target(TargetType.VNF));
432         otherPolicy.setRecipe(recipe);
433         otherPolicy.setPayload(payload);
434         otherPolicy.setRetry(2);
435         otherPolicy.setTimeout(300);
436         return otherPolicy;
437     }
438 }