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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.controlloop.actor.appclcm;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
28 import java.time.Instant;
29 import java.util.AbstractMap;
30 import java.util.HashMap;
31 import java.util.UUID;
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;
54 public class AppcLcmActorServiceProviderTest {
56 private static final String VNF01 = "vnf01";
58 private static final String VNF_ID_KEY = "vnf-id";
60 private static final String REJECT = "REJECT";
62 private static final String PARTIAL_FAILURE = "PARTIAL FAILURE";
64 private static final String FAILURE = "FAILURE";
66 private static final Logger logger = LoggerFactory.getLogger(AppcLcmActorServiceProviderTest.class);
68 private static final VirtualControlLoopEvent onsetEvent;
69 private static final ControlLoopOperation operation;
70 private static final Policy policy;
71 private static final AppcLcmDmaapWrapper dmaapResponse;
73 private static final String RECIPE_RESTART = "Restart";
74 private static final String RECIPE_REBUILD = "Rebuild";
75 private static final String RECIPE_MIGRATE = "Migrate";
79 * Construct an onset with an AAI subtag containing generic-vnf.vnf-id and a target type of VM.
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);
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");
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);
110 policy.setTimeout(300);
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");
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");
124 /* A sample APPC LCM request. */
125 AppcLcmInput appcRequest = new AppcLcmInput();
127 /* The following code constructs a sample APPC LCM Request */
128 appcRequest.setAction("restart");
130 HashMap<String, String> actionIdentifiers = new HashMap<>();
131 actionIdentifiers.put(VNF_ID_KEY, "trial-vnf-003");
133 appcRequest.setActionIdentifiers(actionIdentifiers);
135 AppcLcmCommonHeader commonHeader = new AppcLcmCommonHeader();
136 commonHeader.setRequestId(onsetEvent.getRequestId());
137 commonHeader.setSubRequestId("1");
138 commonHeader.setOriginatorId(onsetEvent.getRequestId().toString());
140 appcRequest.setCommonHeader(commonHeader);
142 appcRequest.setPayload(null);
144 AppcLcmBody appcBody = new AppcLcmBody();
145 appcBody.setInput(appcRequest);
147 dmaapRequest.setBody(appcBody);
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");
154 appcBody.setOutput(appcResponse);
156 dmaapResponse.setBody(appcBody);
160 * Set up before test class.
162 * @throws Exception if an error occurs
165 public static void setUpSimulator() throws Exception {
170 * Tear down after test class.
173 public static void tearDownSimulator() {
174 HttpServletServerFactoryInstance.getServerFactory().destroy();
178 * A test to construct an APPC LCM restart request.
181 public void constructRestartRequestTest() {
183 AppcLcmDmaapWrapper dmaapRequest =
184 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, policy, VNF01);
186 /* The service provider must return a non null DMAAP request wrapper */
187 assertNotNull(dmaapRequest);
189 /* The DMAAP wrapper's type field must be request */
190 assertEquals("request", dmaapRequest.getType());
192 /* The DMAAP wrapper's body field cannot be null */
193 assertNotNull(dmaapRequest.getBody());
195 AppcLcmInput appcRequest = dmaapRequest.getBody().getInput();
197 /* A common header is required and cannot be null */
198 assertNotNull(appcRequest.getCommonHeader());
199 assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
201 /* An action is required and cannot be null */
202 assertNotNull(appcRequest.getAction());
203 assertEquals(RECIPE_RESTART, appcRequest.getAction());
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));
210 logger.debug("APPC Request: \n" + appcRequest.toString());
214 * A test to process a successful APPC restart response.
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());
225 * A test to assert that a null pointer exception is thrown if the APPC response body is null.
227 @Test(expected = NullPointerException.class)
228 public void processNullBodyResponseTest() {
229 AppcLcmActorServiceProvider.processResponse(new AppcLcmDmaapWrapper());
233 * A test to assert that a null pointer exception is thrown if the APPC response output is null.
235 @Test(expected = NullPointerException.class)
236 public void processNullOutputResponseTest() {
237 AppcLcmDmaapWrapper dmaapWrapper = new AppcLcmDmaapWrapper();
238 dmaapWrapper.setBody(new AppcLcmBody());
239 AppcLcmActorServiceProvider.processResponse(dmaapWrapper);
243 * A test to map APPC response results to corresponding Policy results.
246 public void appcToPolicyResultTest() {
248 AbstractMap.SimpleEntry<PolicyResult, String> result;
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
333 * This test exercises getters not exercised in other tests.
336 public void testMethods() {
337 AppcLcmActorServiceProvider sp = new AppcLcmActorServiceProvider();
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));
346 public void testPayloadNotPassedWhenNotSupportedByRecipe() {
348 Policy migratePolicy = constructPolicyWithRecipe(RECIPE_MIGRATE);
349 Policy rebuildPolicy = constructPolicyWithRecipe(RECIPE_REBUILD);
350 Policy restartPolicy = constructPolicyWithRecipe(RECIPE_RESTART);
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);
361 assertNull(migrateRequest.getBody().getInput().getPayload());
362 assertNull(rebuildRequest.getBody().getInput().getPayload());
363 assertNull(restartRequest.getBody().getInput().getPayload());
367 public void testPayloadNotPassedWhenNotSuppliedOrEmpty() {
369 Policy noPayloadPolicy = constructHealthCheckPolicyWithPayload(null);
370 Policy emptyPayloadPolicy = constructHealthCheckPolicyWithPayload(new HashMap<>());
373 AppcLcmDmaapWrapper noPayloadRequest =
374 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, noPayloadPolicy, VNF01);
375 AppcLcmDmaapWrapper emptyPayloadRequest =
376 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, emptyPayloadPolicy, VNF01);
379 assertNull(noPayloadRequest.getBody().getInput().getPayload());
380 assertNull(emptyPayloadRequest.getBody().getInput().getPayload());
384 public void testPayloadParsedProperlyForSinglePayloadParameter() {
386 HashMap<String, String> payload = new HashMap<>();
387 payload.put("requestParameters", "{\"host-ip-address\":\"10.183.37.25\"}");
388 Policy otherPolicy = constructHealthCheckPolicyWithPayload(payload);
391 AppcLcmDmaapWrapper dmaapRequest =
392 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, otherPolicy, VNF01);
395 assertEquals("{\"requestParameters\": {\"host-ip-address\":\"10.183.37.25\"}}",
396 dmaapRequest.getBody().getInput().getPayload());
400 public void testPayloadParsedProperlyForMultiplePayloadParameters() {
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);
411 AppcLcmDmaapWrapper dmaapRequest =
412 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, otherPolicy, VNF01);
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]\"}]" + "}");
422 private Policy constructHealthCheckPolicyWithPayload(HashMap<String, String> payload) {
423 return constructHealthCheckPolicyWithPayloadAndRecipe(payload, "Health-Check");
426 private Policy constructPolicyWithRecipe(String recipe) {
427 return constructHealthCheckPolicyWithPayloadAndRecipe(null, recipe);
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);