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