79bcf153ffbc36056b1c42ac7afede41eebb4963
[policy/drools-applications.git] / controlloop / common / 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  * ================================================================================
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.controlloop.actor.appclcm;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.fail;
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.aai.util.AaiException;
37 import org.onap.policy.appclcm.LcmCommonHeader;
38 import org.onap.policy.appclcm.LcmRequest;
39 import org.onap.policy.appclcm.LcmRequestWrapper;
40 import org.onap.policy.appclcm.LcmResponse;
41 import org.onap.policy.appclcm.LcmResponseWrapper;
42 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
43 import org.onap.policy.controlloop.ControlLoopEventStatus;
44 import org.onap.policy.controlloop.ControlLoopOperation;
45 import org.onap.policy.controlloop.ControlLoopTargetType;
46 import org.onap.policy.controlloop.VirtualControlLoopEvent;
47 import org.onap.policy.controlloop.policy.Policy;
48 import org.onap.policy.controlloop.policy.PolicyResult;
49 import org.onap.policy.controlloop.policy.Target;
50 import org.onap.policy.controlloop.policy.TargetType;
51 import org.onap.policy.drools.system.PolicyEngine;
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         /* Set environment properties */
119         PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
120         PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI");
121         PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
122
123         /* A sample APPC LCM request. */
124         LcmRequest appcRequest = new LcmRequest();
125
126         /* The following code constructs a sample APPC LCM Request */
127         appcRequest.setAction("restart");
128
129         HashMap<String, String> actionIdentifiers = new HashMap<>();
130         actionIdentifiers.put("vnf-id", "trial-vnf-003");
131
132         appcRequest.setActionIdentifiers(actionIdentifiers);
133
134         LcmCommonHeader commonHeader = new LcmCommonHeader();
135         commonHeader.setRequestId(onsetEvent.getRequestId());
136         commonHeader.setSubRequestId("1");
137         commonHeader.setOriginatorId(onsetEvent.getRequestId().toString());
138
139         appcRequest.setCommonHeader(commonHeader);
140
141         appcRequest.setPayload(null);
142
143         dmaapRequest.setBody(appcRequest);
144
145         /* The following code constructs a sample APPC LCM Response */
146         LcmResponse appcResponse = new LcmResponse(appcRequest);
147         appcResponse.getStatus().setCode(400);
148         appcResponse.getStatus().setMessage("Restart Successful");
149
150         dmaapResponse.setBody(appcResponse);
151     }
152
153     /**
154      * Set up before test class.
155      */
156     @BeforeClass
157     public static void setUpSimulator() {
158         try {
159             Util.buildAaiSim();
160         } catch (Exception e) {
161             fail(e.getMessage());
162         }
163     }
164
165     /**
166      * Tear down after test class.
167      */
168     @AfterClass
169     public static void tearDownSimulator() {
170         HttpServletServer.factory.destroy();
171     }
172
173     /**
174      * A test to construct an APPC LCM restart request.
175      */
176     @Test
177     public void constructRestartRequestTest() {
178
179         LcmRequestWrapper dmaapRequest =
180                 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
181
182         /* The service provider must return a non null DMAAP request wrapper */
183         assertNotNull(dmaapRequest);
184
185         /* The DMAAP wrapper's type field must be request */
186         assertEquals("request", dmaapRequest.getType());
187
188         /* The DMAAP wrapper's body field cannot be null */
189         assertNotNull(dmaapRequest.getBody());
190
191         LcmRequest appcRequest = dmaapRequest.getBody();
192
193         /* A common header is required and cannot be null */
194         assertNotNull(appcRequest.getCommonHeader());
195         assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
196
197         /* An action is required and cannot be null */
198         assertNotNull(appcRequest.getAction());
199         assertEquals("Restart", appcRequest.getAction());
200
201         /* Action Identifiers are required and cannot be null */
202         assertNotNull(appcRequest.getActionIdentifiers());
203         assertNotNull(appcRequest.getActionIdentifiers().get("vnf-id"));
204         assertEquals("vnf01", appcRequest.getActionIdentifiers().get("vnf-id"));
205
206         logger.debug("APPC Request: \n" + appcRequest.toString());
207     }
208
209     /**
210      * A test to process a successful APPC restart response.
211      */
212     @Test
213     public void processRestartResponseSuccessTest() {
214         AbstractMap.SimpleEntry<PolicyResult, String> result =
215                 AppcLcmActorServiceProvider.processResponse(dmaapResponse);
216         assertEquals(PolicyResult.SUCCESS, result.getKey());
217         assertEquals("Restart Successful", result.getValue());
218     }
219
220     /**
221      * A test to map APPC response results to corresponding Policy results.
222      */
223     @Test
224     public void appcToPolicyResultTest() {
225
226         AbstractMap.SimpleEntry<PolicyResult, String> result;
227
228         /* If APPC accepts, PolicyResult is null */
229         dmaapResponse.getBody().getStatus().setCode(100);
230         dmaapResponse.getBody().getStatus().setMessage("ACCEPTED");
231         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
232         assertNull(result.getKey());
233
234         /* If APPC is successful, PolicyResult is success */
235         dmaapResponse.getBody().getStatus().setCode(400);
236         dmaapResponse.getBody().getStatus().setMessage("SUCCESS");
237         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
238         assertEquals(PolicyResult.SUCCESS, result.getKey());
239
240         /* If APPC returns an error, PolicyResult is failure exception */
241         dmaapResponse.getBody().getStatus().setCode(200);
242         dmaapResponse.getBody().getStatus().setMessage("ERROR");
243         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
244         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
245
246         /* If APPC rejects, PolicyResult is failure exception */
247         dmaapResponse.getBody().getStatus().setCode(300);
248         dmaapResponse.getBody().getStatus().setMessage("REJECT");
249         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
250         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
251
252         /* Test multiple reject codes */
253         dmaapResponse.getBody().getStatus().setCode(306);
254         dmaapResponse.getBody().getStatus().setMessage("REJECT");
255         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
256         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
257
258         dmaapResponse.getBody().getStatus().setCode(313);
259         dmaapResponse.getBody().getStatus().setMessage("REJECT");
260         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
261         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
262
263         /* If APPC returns failure, PolicyResult is failure */
264         dmaapResponse.getBody().getStatus().setCode(401);
265         dmaapResponse.getBody().getStatus().setMessage("FAILURE");
266         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
267         assertEquals(PolicyResult.FAILURE, result.getKey());
268
269         /* Test multiple failure codes */
270         dmaapResponse.getBody().getStatus().setCode(406);
271         dmaapResponse.getBody().getStatus().setMessage("FAILURE");
272         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
273         assertEquals(PolicyResult.FAILURE, result.getKey());
274
275         dmaapResponse.getBody().getStatus().setCode(450);
276         dmaapResponse.getBody().getStatus().setMessage("FAILURE");
277         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
278         assertEquals(PolicyResult.FAILURE, result.getKey());
279
280         /* If APPC returns partial success, PolicyResult is failure exception */
281         dmaapResponse.getBody().getStatus().setCode(500);
282         dmaapResponse.getBody().getStatus().setMessage("PARTIAL SUCCESS");
283         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
284         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
285
286         /* If APPC returns partial failure, PolicyResult is failure exception */
287         dmaapResponse.getBody().getStatus().setCode(501);
288         dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
289         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
290         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
291
292         /* Test multiple partial failure codes */
293         dmaapResponse.getBody().getStatus().setCode(599);
294         dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
295         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
296         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
297
298         dmaapResponse.getBody().getStatus().setCode(550);
299         dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
300         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
301         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
302
303         /* If APPC code is unknown to Policy, PolicyResult is failure exception */
304         dmaapResponse.getBody().getStatus().setCode(700);
305         dmaapResponse.getBody().getStatus().setMessage("UNKNOWN");
306         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
307         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
308     }
309
310     /**
311      * This test ensures that that if the the source entity is also the target entity, the source
312      * will be used for the APPC request.
313      */
314     @Test
315     public void sourceIsTargetTest() {
316         String resourceId = "82194af1-3c2c-485a-8f44-420e22a9eaa4";
317         String targetVnfId = null;
318         try {
319             targetVnfId = AppcLcmActorServiceProvider.vnfNamedQuery(resourceId, "vnf01");
320         } catch (AaiException e) {
321             logger.warn(e.toString());
322             fail("no vnf-id found");
323         }
324         assertNotNull(targetVnfId);
325         assertEquals("vnf01", targetVnfId);
326     }
327
328     /**
329      * THis test exercises getters not exercised in other tests.
330      */
331     @Test
332     public void testMethods() {
333         AppcLcmActorServiceProvider sp = new AppcLcmActorServiceProvider();
334
335         assertEquals("APPC", sp.actor());
336         assertEquals(4, sp.recipes().size());
337         assertEquals("VM", sp.recipeTargets("Restart").get(0));
338         assertEquals("vm-id", sp.recipePayloads("Restart").get(0));
339     }
340
341     @Test
342     public void payloadNotPassedWhenNotSupportedByRecipe() {
343         //given
344         Policy migratePolicy = constructPolicyWithRecipe(RECIPE_MIGRATE);
345         Policy rebuildPolicy = constructPolicyWithRecipe(RECIPE_REBUILD);
346         Policy restartPolicy = constructPolicyWithRecipe(RECIPE_RESTART);
347
348         // when
349         LcmRequestWrapper migrateRequest =
350             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, migratePolicy, "vnf01");
351         LcmRequestWrapper rebuildRequest =
352             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, rebuildPolicy, "vnf01");
353         LcmRequestWrapper restartRequest =
354             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, restartPolicy, "vnf01");
355
356         // then
357         assertNull(migrateRequest.getBody().getPayload());
358         assertNull(rebuildRequest.getBody().getPayload());
359         assertNull(restartRequest.getBody().getPayload());
360     }
361
362     @Test
363     public void payloadNotPassedWhenNotSuppliedOrEmpty() {
364         //given
365         Policy noPayloadPolicy = constructHealthCheckPolicyWithPayload(null);
366         Policy emptyPayloadPolicy = constructHealthCheckPolicyWithPayload(new HashMap<>());
367
368         // when
369         LcmRequestWrapper noPayloadRequest =
370             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, noPayloadPolicy, "vnf01");
371         LcmRequestWrapper emptyPayloadRequest =
372             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, emptyPayloadPolicy, "vnf01");
373
374
375         // then
376         assertNull(noPayloadRequest.getBody().getPayload());
377         assertNull(emptyPayloadRequest.getBody().getPayload());
378     }
379
380     @Test
381     public void payloadParsedProperlyForSinglePayloadParameter() {
382         // given
383         HashMap<String, String> payload = new HashMap<>();
384         payload.put("requestParameters", "{\"host-ip-address\":\"10.183.37.25\"}");
385         Policy otherPolicy = constructHealthCheckPolicyWithPayload(payload);
386
387         // when
388         LcmRequestWrapper dmaapRequest =
389             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, otherPolicy, "vnf01");
390
391         // then
392         assertEquals(dmaapRequest.getBody().getPayload(),
393             "{\"requestParameters\": {\"host-ip-address\":\"10.183.37.25\"}}");
394     }
395
396
397     @Test
398     public void payloadParsedProperlyForMultiplePayloadParameters() {
399         // given
400         HashMap<String, String> payload = new HashMap<>();
401         payload.put("requestParameters", "{\"host-ip-address\":\"10.183.37.25\"}");
402         payload.put("configurationParameters", "[{\"ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[9]\","
403             + "\"oam-ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[16]\","
404             + "\"enabled\":\"$.vf-module-topology.vf-module-parameters.param[23]\"}]");
405         Policy otherPolicy = constructHealthCheckPolicyWithPayload(payload);
406
407         // when
408         LcmRequestWrapper dmaapRequest =
409             AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, otherPolicy, "vnf01");
410
411         // then
412         assertEquals(dmaapRequest.getBody().getPayload(),
413             "{\"requestParameters\": "
414                 + "{\"host-ip-address\":\"10.183.37.25\"},"
415                 + "\"configurationParameters\": "
416                 + "[{\"ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[9]\","
417                 + "\"oam-ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[16]\","
418                 + "\"enabled\":\"$.vf-module-topology.vf-module-parameters.param[23]\"}]"
419                 + "}");
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 }