ef69bc1bcd7350a7b023931768012683019d5f08
[policy/models.git] /
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
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.appclcm.AppcLcmBody;
37 import org.onap.policy.appclcm.AppcLcmCommonHeader;
38 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
39 import org.onap.policy.appclcm.AppcLcmInput;
40 import org.onap.policy.appclcm.AppcLcmOutput;
41 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
42 import org.onap.policy.controlloop.ControlLoopEventStatus;
43 import org.onap.policy.controlloop.ControlLoopOperation;
44 import org.onap.policy.controlloop.ControlLoopTargetType;
45 import org.onap.policy.controlloop.VirtualControlLoopEvent;
46 import org.onap.policy.controlloop.policy.Policy;
47 import org.onap.policy.controlloop.policy.PolicyResult;
48 import org.onap.policy.controlloop.policy.Target;
49 import org.onap.policy.controlloop.policy.TargetType;
50 import org.onap.policy.simulators.Util;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
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 VM.
80          */
81         onsetEvent = new VirtualControlLoopEvent();
82         onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
83         onsetEvent.setRequestId(UUID.randomUUID());
84         onsetEvent.setClosedLoopEventClient("tca.instance00001");
85         onsetEvent.setTargetType(ControlLoopTargetType.VM);
86         onsetEvent.setTarget("generic-vnf.vnf-name");
87         onsetEvent.setFrom("DCAE");
88         onsetEvent.setClosedLoopAlarmStart(Instant.now());
89         onsetEvent.setAai(new HashMap<>());
90         onsetEvent.getAai().put("generic-vnf.vnf-name", "fw0001vm001fw001");
91         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
92
93         /* Construct an operation with an APPC actor and restart operation. */
94         operation = new ControlLoopOperation();
95         operation.setActor("APPC");
96         operation.setOperation(RECIPE_RESTART);
97         operation.setTarget("VM");
98         operation.setEnd(Instant.now());
99         operation.setSubRequestId("1");
100
101         /* Construct a policy specifying to restart vm. */
102         policy = new Policy();
103         policy.setName("Restart the VM");
104         policy.setDescription("Upon getting the trigger event, restart the VM");
105         policy.setActor("APPC");
106         policy.setTarget(new Target(TargetType.VNF));
107         policy.setRecipe(RECIPE_RESTART);
108         policy.setPayload(null);
109         policy.setRetry(2);
110         policy.setTimeout(300);
111
112         /* A sample DMAAP request wrapper. */
113         AppcLcmDmaapWrapper dmaapRequest = new AppcLcmDmaapWrapper();
114         dmaapRequest.setCorrelationId(onsetEvent.getRequestId().toString() + "-" + "1");
115         dmaapRequest.setRpcName(policy.getRecipe().toLowerCase());
116         dmaapRequest.setType("request");
117
118         /* A sample DMAAP response wrapper */
119         dmaapResponse = new AppcLcmDmaapWrapper();
120         dmaapResponse.setCorrelationId(onsetEvent.getRequestId().toString() + "-" + "1");
121         dmaapResponse.setRpcName(policy.getRecipe().toLowerCase());
122         dmaapResponse.setType("response");
123
124         /* A sample APPC LCM request. */
125         AppcLcmInput appcRequest = new AppcLcmInput();
126
127         /* The following code constructs a sample APPC LCM Request */
128         appcRequest.setAction("restart");
129
130         HashMap<String, String> actionIdentifiers = new HashMap<>();
131         actionIdentifiers.put(VNF_ID_KEY, "trial-vnf-003");
132
133         appcRequest.setActionIdentifiers(actionIdentifiers);
134
135         AppcLcmCommonHeader commonHeader = new AppcLcmCommonHeader();
136         commonHeader.setRequestId(onsetEvent.getRequestId());
137         commonHeader.setSubRequestId("1");
138         commonHeader.setOriginatorId(onsetEvent.getRequestId().toString());
139
140         appcRequest.setCommonHeader(commonHeader);
141
142         appcRequest.setPayload(null);
143
144         AppcLcmBody appcBody = new AppcLcmBody();
145         appcBody.setInput(appcRequest);
146
147         dmaapRequest.setBody(appcBody);
148
149         /* The following code constructs a sample APPC LCM Response */
150         AppcLcmOutput appcResponse = new AppcLcmOutput(appcRequest);
151         appcResponse.getStatus().setCode(400);
152         appcResponse.getStatus().setMessage("Restart Successful");
153
154         appcBody.setOutput(appcResponse);
155
156         dmaapResponse.setBody(appcBody);
157     }
158
159     /**
160      * Set up before test class.
161      *
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 the APPC response body is null.
226      */
227     @Test(expected = NullPointerException.class)
228     public void processNullBodyResponseTest() {
229         AppcLcmActorServiceProvider.processResponse(new AppcLcmDmaapWrapper());
230     }
231
232     /**
233      * A test to assert that a null pointer exception is thrown if the APPC response output is null.
234      */
235     @Test(expected = NullPointerException.class)
236     public void processNullOutputResponseTest() {
237         AppcLcmDmaapWrapper dmaapWrapper = new AppcLcmDmaapWrapper();
238         dmaapWrapper.setBody(new AppcLcmBody());
239         AppcLcmActorServiceProvider.processResponse(dmaapWrapper);
240     }
241
242     /**
243      * A test to map APPC response results to corresponding Policy results.
244      */
245     @Test
246     public void appcToPolicyResultTest() {
247
248         AbstractMap.SimpleEntry<PolicyResult, String> result;
249
250         /* If APPC accepts, PolicyResult is null */
251         dmaapResponse.getBody().getOutput().getStatus().setCode(100);
252         dmaapResponse.getBody().getOutput().getStatus().setMessage("ACCEPTED");
253         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
254         assertNull(result.getKey());
255
256         /* If APPC is successful, PolicyResult is success */
257         dmaapResponse.getBody().getOutput().getStatus().setCode(400);
258         dmaapResponse.getBody().getOutput().getStatus().setMessage("SUCCESS");
259         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
260         assertEquals(PolicyResult.SUCCESS, result.getKey());
261
262         /* If APPC returns an error, PolicyResult is failure exception */
263         dmaapResponse.getBody().getOutput().getStatus().setCode(200);
264         dmaapResponse.getBody().getOutput().getStatus().setMessage("ERROR");
265         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
266         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
267
268         /* If APPC rejects, PolicyResult is failure exception */
269         dmaapResponse.getBody().getOutput().getStatus().setCode(300);
270         dmaapResponse.getBody().getOutput().getStatus().setMessage(REJECT);
271         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
272         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
273
274         /* Test multiple reject codes */
275         dmaapResponse.getBody().getOutput().getStatus().setCode(306);
276         dmaapResponse.getBody().getOutput().getStatus().setMessage(REJECT);
277         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
278         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
279
280         dmaapResponse.getBody().getOutput().getStatus().setCode(313);
281         dmaapResponse.getBody().getOutput().getStatus().setMessage(REJECT);
282         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
283         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
284
285         /* If APPC returns failure, PolicyResult is failure */
286         dmaapResponse.getBody().getOutput().getStatus().setCode(401);
287         dmaapResponse.getBody().getOutput().getStatus().setMessage(FAILURE);
288         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
289         assertEquals(PolicyResult.FAILURE, result.getKey());
290
291         /* Test multiple failure codes */
292         dmaapResponse.getBody().getOutput().getStatus().setCode(406);
293         dmaapResponse.getBody().getOutput().getStatus().setMessage(FAILURE);
294         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
295         assertEquals(PolicyResult.FAILURE, result.getKey());
296
297         dmaapResponse.getBody().getOutput().getStatus().setCode(450);
298         dmaapResponse.getBody().getOutput().getStatus().setMessage(FAILURE);
299         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
300         assertEquals(PolicyResult.FAILURE, result.getKey());
301
302         /* If APPC returns partial success, PolicyResult is failure exception */
303         dmaapResponse.getBody().getOutput().getStatus().setCode(500);
304         dmaapResponse.getBody().getOutput().getStatus().setMessage("PARTIAL SUCCESS");
305         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
306         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
307
308         /* If APPC returns partial failure, PolicyResult is failure exception */
309         dmaapResponse.getBody().getOutput().getStatus().setCode(501);
310         dmaapResponse.getBody().getOutput().getStatus().setMessage(PARTIAL_FAILURE);
311         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
312         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
313
314         /* Test multiple partial failure codes */
315         dmaapResponse.getBody().getOutput().getStatus().setCode(599);
316         dmaapResponse.getBody().getOutput().getStatus().setMessage(PARTIAL_FAILURE);
317         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
318         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
319
320         dmaapResponse.getBody().getOutput().getStatus().setCode(550);
321         dmaapResponse.getBody().getOutput().getStatus().setMessage(PARTIAL_FAILURE);
322         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
323         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
324
325         /* If APPC code is unknown to Policy, PolicyResult is failure exception */
326         dmaapResponse.getBody().getOutput().getStatus().setCode(700);
327         dmaapResponse.getBody().getOutput().getStatus().setMessage("UNKNOWN");
328         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
329         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
330     }
331
332     /*
333      * This test exercises getters not exercised in other tests.
334      */
335     @Test
336     public void testMethods() {
337         AppcLcmActorServiceProvider sp = new AppcLcmActorServiceProvider();
338
339         assertEquals("APPC", sp.actor());
340         assertEquals(4, sp.recipes().size());
341         assertEquals("VM", sp.recipeTargets(RECIPE_RESTART).get(0));
342         assertEquals("vm-id", sp.recipePayloads(RECIPE_RESTART).get(0));
343     }
344
345     @Test
346     public void testPayloadNotPassedWhenNotSupportedByRecipe() {
347         // given
348         Policy migratePolicy = constructPolicyWithRecipe(RECIPE_MIGRATE);
349         Policy rebuildPolicy = constructPolicyWithRecipe(RECIPE_REBUILD);
350         Policy restartPolicy = constructPolicyWithRecipe(RECIPE_RESTART);
351
352         // when
353         AppcLcmDmaapWrapper migrateRequest =
354                 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, migratePolicy, VNF01);
355         AppcLcmDmaapWrapper rebuildRequest =
356                 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, rebuildPolicy, VNF01);
357         AppcLcmDmaapWrapper restartRequest =
358                 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, restartPolicy, VNF01);
359
360         // then
361         assertNull(migrateRequest.getBody().getInput().getPayload());
362         assertNull(rebuildRequest.getBody().getInput().getPayload());
363         assertNull(restartRequest.getBody().getInput().getPayload());
364     }
365
366     @Test
367     public void testPayloadNotPassedWhenNotSuppliedOrEmpty() {
368         // given
369         Policy noPayloadPolicy = constructHealthCheckPolicyWithPayload(null);
370         Policy emptyPayloadPolicy = constructHealthCheckPolicyWithPayload(new HashMap<>());
371
372         // when
373         AppcLcmDmaapWrapper noPayloadRequest =
374                 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, noPayloadPolicy, VNF01);
375         AppcLcmDmaapWrapper emptyPayloadRequest =
376                 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, emptyPayloadPolicy, VNF01);
377
378         // then
379         assertNull(noPayloadRequest.getBody().getInput().getPayload());
380         assertNull(emptyPayloadRequest.getBody().getInput().getPayload());
381     }
382
383     @Test
384     public void testPayloadParsedProperlyForSinglePayloadParameter() {
385         // given
386         HashMap<String, String> payload = new HashMap<>();
387         payload.put("requestParameters", "{\"host-ip-address\":\"10.183.37.25\"}");
388         Policy otherPolicy = constructHealthCheckPolicyWithPayload(payload);
389
390         // when
391         AppcLcmDmaapWrapper dmaapRequest =
392                 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, otherPolicy, VNF01);
393
394         // then
395         assertEquals("{\"requestParameters\": {\"host-ip-address\":\"10.183.37.25\"}}",
396                 dmaapRequest.getBody().getInput().getPayload());
397     }
398
399     @Test
400     public void testPayloadParsedProperlyForMultiplePayloadParameters() {
401         // given
402         HashMap<String, String> payload = new HashMap<>();
403         payload.put("requestParameters", "{\"host-ip-address\":\"10.183.37.25\"}");
404         payload.put("configurationParameters",
405                 "[{\"ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[9]\","
406                         + "\"oam-ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[16]\","
407                         + "\"enabled\":\"$.vf-module-topology.vf-module-parameters.param[23]\"}]");
408         Policy otherPolicy = constructHealthCheckPolicyWithPayload(payload);
409
410         // when
411         AppcLcmDmaapWrapper dmaapRequest =
412                 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, otherPolicy, VNF01);
413
414         // then
415         assertEquals(dmaapRequest.getBody().getInput().getPayload(),
416                 "{\"requestParameters\": " + "{\"host-ip-address\":\"10.183.37.25\"}," + "\"configurationParameters\": "
417                         + "[{\"ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[9]\","
418                         + "\"oam-ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[16]\","
419                         + "\"enabled\":\"$.vf-module-topology.vf-module-parameters.param[23]\"}]" + "}");
420     }
421
422     private Policy constructHealthCheckPolicyWithPayload(HashMap<String, String> payload) {
423         return constructHealthCheckPolicyWithPayloadAndRecipe(payload, "Health-Check");
424     }
425
426     private Policy constructPolicyWithRecipe(String recipe) {
427         return constructHealthCheckPolicyWithPayloadAndRecipe(null, recipe);
428     }
429
430     private Policy constructHealthCheckPolicyWithPayloadAndRecipe(HashMap<String, String> payload, String recipe) {
431         Policy otherPolicy = new Policy();
432         otherPolicy.setName("Perform health check");
433         otherPolicy.setDescription("Upon getting the trigger event, perform health check");
434         otherPolicy.setActor("APPC");
435         otherPolicy.setTarget(new Target(TargetType.VNF));
436         otherPolicy.setRecipe(recipe);
437         otherPolicy.setPayload(payload);
438         otherPolicy.setRetry(2);
439         otherPolicy.setTimeout(300);
440         return otherPolicy;
441     }
442 }