Merge "Fix more sonar issues in models: yaml to dao"
[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.LcmCommonHeader;
36 import org.onap.policy.appclcm.LcmRequest;
37 import org.onap.policy.appclcm.LcmRequestWrapper;
38 import org.onap.policy.appclcm.LcmResponse;
39 import org.onap.policy.appclcm.LcmResponseWrapper;
40 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
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 LcmResponseWrapper 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         LcmRequestWrapper dmaapRequest = new LcmRequestWrapper();
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 LcmResponseWrapper();
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         LcmRequest appcRequest = new LcmRequest();
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         LcmCommonHeader commonHeader = new LcmCommonHeader();
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         dmaapRequest.setBody(appcRequest);
146
147         /* The following code constructs a sample APPC LCM Response */
148         LcmResponse appcResponse = new LcmResponse(appcRequest);
149         appcResponse.getStatus().setCode(400);
150         appcResponse.getStatus().setMessage("Restart Successful");
151
152         dmaapResponse.setBody(appcResponse);
153     }
154
155     /**
156      * Set up before test class.
157      * @throws Exception if an error occurs
158      */
159     @BeforeClass
160     public static void setUpSimulator() throws Exception {
161         Util.buildAaiSim();
162     }
163
164     /**
165      * Tear down after test class.
166      */
167     @AfterClass
168     public static void tearDownSimulator() {
169         HttpServletServer.factory.destroy();
170     }
171
172     /**
173      * A test to construct an APPC LCM restart request.
174      */
175     @Test
176     public void constructRestartRequestTest() {
177
178         LcmRequestWrapper dmaapRequest =
179                 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, policy, VNF01);
180
181         /* The service provider must return a non null DMAAP request wrapper */
182         assertNotNull(dmaapRequest);
183
184         /* The DMAAP wrapper's type field must be request */
185         assertEquals("request", dmaapRequest.getType());
186
187         /* The DMAAP wrapper's body field cannot be null */
188         assertNotNull(dmaapRequest.getBody());
189
190         LcmRequest appcRequest = dmaapRequest.getBody();
191
192         /* A common header is required and cannot be null */
193         assertNotNull(appcRequest.getCommonHeader());
194         assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
195
196         /* An action is required and cannot be null */
197         assertNotNull(appcRequest.getAction());
198         assertEquals(RECIPE_RESTART, appcRequest.getAction());
199
200         /* Action Identifiers are required and cannot be null */
201         assertNotNull(appcRequest.getActionIdentifiers());
202         assertNotNull(appcRequest.getActionIdentifiers().get(VNF_ID_KEY));
203         assertEquals(VNF01, appcRequest.getActionIdentifiers().get(VNF_ID_KEY));
204
205         logger.debug("APPC Request: \n" + appcRequest.toString());
206     }
207
208     /**
209      * A test to process a successful APPC restart response.
210      */
211     @Test
212     public void processRestartResponseSuccessTest() {
213         AbstractMap.SimpleEntry<PolicyResult, String> result =
214                 AppcLcmActorServiceProvider.processResponse(dmaapResponse);
215         assertEquals(PolicyResult.SUCCESS, result.getKey());
216         assertEquals("Restart Successful", result.getValue());
217     }
218
219     /**
220      * A test to map APPC response results to corresponding Policy results.
221      */
222     @Test
223     public void appcToPolicyResultTest() {
224
225         AbstractMap.SimpleEntry<PolicyResult, String> result;
226
227         /* If APPC accepts, PolicyResult is null */
228         dmaapResponse.getBody().getStatus().setCode(100);
229         dmaapResponse.getBody().getStatus().setMessage("ACCEPTED");
230         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
231         assertNull(result.getKey());
232
233         /* If APPC is successful, PolicyResult is success */
234         dmaapResponse.getBody().getStatus().setCode(400);
235         dmaapResponse.getBody().getStatus().setMessage("SUCCESS");
236         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
237         assertEquals(PolicyResult.SUCCESS, result.getKey());
238
239         /* If APPC returns an error, PolicyResult is failure exception */
240         dmaapResponse.getBody().getStatus().setCode(200);
241         dmaapResponse.getBody().getStatus().setMessage("ERROR");
242         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
243         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
244
245         /* If APPC rejects, PolicyResult is failure exception */
246         dmaapResponse.getBody().getStatus().setCode(300);
247         dmaapResponse.getBody().getStatus().setMessage(REJECT);
248         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
249         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
250
251         /* Test multiple reject codes */
252         dmaapResponse.getBody().getStatus().setCode(306);
253         dmaapResponse.getBody().getStatus().setMessage(REJECT);
254         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
255         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
256
257         dmaapResponse.getBody().getStatus().setCode(313);
258         dmaapResponse.getBody().getStatus().setMessage(REJECT);
259         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
260         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
261
262         /* If APPC returns failure, PolicyResult is failure */
263         dmaapResponse.getBody().getStatus().setCode(401);
264         dmaapResponse.getBody().getStatus().setMessage(FAILURE);
265         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
266         assertEquals(PolicyResult.FAILURE, result.getKey());
267
268         /* Test multiple failure codes */
269         dmaapResponse.getBody().getStatus().setCode(406);
270         dmaapResponse.getBody().getStatus().setMessage(FAILURE);
271         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
272         assertEquals(PolicyResult.FAILURE, result.getKey());
273
274         dmaapResponse.getBody().getStatus().setCode(450);
275         dmaapResponse.getBody().getStatus().setMessage(FAILURE);
276         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
277         assertEquals(PolicyResult.FAILURE, result.getKey());
278
279         /* If APPC returns partial success, PolicyResult is failure exception */
280         dmaapResponse.getBody().getStatus().setCode(500);
281         dmaapResponse.getBody().getStatus().setMessage("PARTIAL SUCCESS");
282         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
283         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
284
285         /* If APPC returns partial failure, PolicyResult is failure exception */
286         dmaapResponse.getBody().getStatus().setCode(501);
287         dmaapResponse.getBody().getStatus().setMessage(PARTIAL_FAILURE);
288         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
289         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
290
291         /* Test multiple partial failure codes */
292         dmaapResponse.getBody().getStatus().setCode(599);
293         dmaapResponse.getBody().getStatus().setMessage(PARTIAL_FAILURE);
294         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
295         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
296
297         dmaapResponse.getBody().getStatus().setCode(550);
298         dmaapResponse.getBody().getStatus().setMessage(PARTIAL_FAILURE);
299         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
300         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
301
302         /* If APPC code is unknown to Policy, PolicyResult is failure exception */
303         dmaapResponse.getBody().getStatus().setCode(700);
304         dmaapResponse.getBody().getStatus().setMessage("UNKNOWN");
305         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
306         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
307     }
308
309     /*
310      * This test ensures that that if the the source entity is also the target entity, the source
311      * will be used for the APPC request.
312      */
313     @Test
314     public void sourceIsTargetTest() throws Exception {
315         String resourceId = "82194af1-3c2c-485a-8f44-420e22a9eaa4";
316         String targetVnfId = AppcLcmActorServiceProvider.vnfNamedQuery(resourceId, VNF01, "http://localhost:6666",
317                         "AAI", "AAI");
318         assertNotNull(targetVnfId);
319         assertEquals(VNF01, targetVnfId);
320     }
321
322     /*
323      * This test exercises getters not exercised in other tests.
324      */
325     @Test
326     public void testMethods() {
327         AppcLcmActorServiceProvider sp = new AppcLcmActorServiceProvider();
328
329         assertEquals("APPC", sp.actor());
330         assertEquals(4, sp.recipes().size());
331         assertEquals("VM", sp.recipeTargets(RECIPE_RESTART).get(0));
332         assertEquals("vm-id", sp.recipePayloads(RECIPE_RESTART).get(0));
333     }
334
335     @Test
336     public void payloadNotPassedWhenNotSupportedByRecipe() {
337         //given
338         Policy migratePolicy = constructPolicyWithRecipe(RECIPE_MIGRATE);
339         Policy rebuildPolicy = constructPolicyWithRecipe(RECIPE_REBUILD);
340         Policy restartPolicy = constructPolicyWithRecipe(RECIPE_RESTART);
341
342         // when
343         LcmRequestWrapper migrateRequest =
344             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, migratePolicy, VNF01);
345         LcmRequestWrapper rebuildRequest =
346             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, rebuildPolicy, VNF01);
347         LcmRequestWrapper restartRequest =
348             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, restartPolicy, VNF01);
349
350         // then
351         assertNull(migrateRequest.getBody().getPayload());
352         assertNull(rebuildRequest.getBody().getPayload());
353         assertNull(restartRequest.getBody().getPayload());
354     }
355
356     @Test
357     public void payloadNotPassedWhenNotSuppliedOrEmpty() {
358         //given
359         Policy noPayloadPolicy = constructHealthCheckPolicyWithPayload(null);
360         Policy emptyPayloadPolicy = constructHealthCheckPolicyWithPayload(new HashMap<>());
361
362         // when
363         LcmRequestWrapper noPayloadRequest =
364             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, noPayloadPolicy, VNF01);
365         LcmRequestWrapper emptyPayloadRequest =
366             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, emptyPayloadPolicy, VNF01);
367
368
369         // then
370         assertNull(noPayloadRequest.getBody().getPayload());
371         assertNull(emptyPayloadRequest.getBody().getPayload());
372     }
373
374     @Test
375     public void payloadParsedProperlyForSinglePayloadParameter() {
376         // given
377         HashMap<String, String> payload = new HashMap<>();
378         payload.put("requestParameters", "{\"host-ip-address\":\"10.183.37.25\"}");
379         Policy otherPolicy = constructHealthCheckPolicyWithPayload(payload);
380
381         // when
382         LcmRequestWrapper dmaapRequest =
383             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, otherPolicy, VNF01);
384
385         // then
386         assertEquals(dmaapRequest.getBody().getPayload(),
387             "{\"requestParameters\": {\"host-ip-address\":\"10.183.37.25\"}}");
388     }
389
390
391     @Test
392     public void payloadParsedProperlyForMultiplePayloadParameters() {
393         // given
394         HashMap<String, String> payload = new HashMap<>();
395         payload.put("requestParameters", "{\"host-ip-address\":\"10.183.37.25\"}");
396         payload.put("configurationParameters", "[{\"ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[9]\","
397             + "\"oam-ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[16]\","
398             + "\"enabled\":\"$.vf-module-topology.vf-module-parameters.param[23]\"}]");
399         Policy otherPolicy = constructHealthCheckPolicyWithPayload(payload);
400
401         // when
402         LcmRequestWrapper dmaapRequest =
403             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, otherPolicy, VNF01);
404
405         // then
406         assertEquals(dmaapRequest.getBody().getPayload(),
407             "{\"requestParameters\": "
408                 + "{\"host-ip-address\":\"10.183.37.25\"},"
409                 + "\"configurationParameters\": "
410                 + "[{\"ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[9]\","
411                 + "\"oam-ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[16]\","
412                 + "\"enabled\":\"$.vf-module-topology.vf-module-parameters.param[23]\"}]"
413                 + "}");
414     }
415
416     private Policy constructHealthCheckPolicyWithPayload(HashMap<String, String> payload) {
417         return constructHealthCheckPolicyWithPayloadAndRecipe(payload, "Health-Check");
418     }
419
420     private Policy constructPolicyWithRecipe(String recipe) {
421         return constructHealthCheckPolicyWithPayloadAndRecipe(null, recipe);
422     }
423
424     private Policy constructHealthCheckPolicyWithPayloadAndRecipe(HashMap<String, String> payload, String recipe) {
425         Policy otherPolicy = new Policy();
426         otherPolicy.setName("Perform health check");
427         otherPolicy.setDescription("Upon getting the trigger event, perform health check");
428         otherPolicy.setActor("APPC");
429         otherPolicy.setTarget(new Target(TargetType.VNF));
430         otherPolicy.setRecipe(recipe);
431         otherPolicy.setPayload(payload);
432         otherPolicy.setRetry(2);
433         otherPolicy.setTimeout(300);
434         return otherPolicy;
435     }
436 }