2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2020 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;
32 import java.util.stream.Collectors;
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 AppcLcmActorTest {
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(AppcLcmActorTest.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
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);
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");
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);
111 policy.setTimeout(300);
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");
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");
125 /* A sample APPC LCM request. */
126 AppcLcmInput appcRequest = new AppcLcmInput();
128 /* The following code constructs a sample APPC LCM Request */
129 appcRequest.setAction("restart");
131 HashMap<String, String> actionIdentifiers = new HashMap<>();
132 actionIdentifiers.put(VNF_ID_KEY, "trial-vnf-003");
134 appcRequest.setActionIdentifiers(actionIdentifiers);
136 AppcLcmCommonHeader commonHeader = new AppcLcmCommonHeader();
137 commonHeader.setRequestId(onsetEvent.getRequestId());
138 commonHeader.setSubRequestId("1");
139 commonHeader.setOriginatorId(onsetEvent.getRequestId().toString());
141 appcRequest.setCommonHeader(commonHeader);
143 appcRequest.setPayload(null);
145 AppcLcmBody appcBody = new AppcLcmBody();
146 appcBody.setInput(appcRequest);
148 dmaapRequest.setBody(appcBody);
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");
155 appcBody.setOutput(appcResponse);
157 dmaapResponse.setBody(appcBody);
161 * Set up before test class.
163 * @throws Exception if an error occurs
166 public static void setUpSimulator() throws Exception {
171 * Tear down after test class.
174 public static void tearDownSimulator() {
175 HttpServletServerFactoryInstance.getServerFactory().destroy();
179 public void testConstructor() {
180 AppcLcmActor prov = new AppcLcmActor();
181 assertEquals(-1, prov.getSequenceNumber());
183 // verify that it has the operators we expect
184 var expected = AppcLcmConstants.COMBINED_OPERATION_NAMES.stream().sorted().collect(Collectors.toList());
185 var actual = prov.getOperationNames().stream().sorted().collect(Collectors.toList());
187 assertEquals(expected.toString(), actual.toString());
191 * A test to construct an APPC LCM restart request.
194 public void constructRestartRequestTest() {
196 AppcLcmDmaapWrapper dmaapRequest =
197 AppcLcmActor.constructRequest(onsetEvent, operation, policy, VNF01);
199 /* The service provider must return a non null DMAAP request wrapper */
200 assertNotNull(dmaapRequest);
202 /* The DMAAP wrapper's type field must be request */
203 assertEquals("request", dmaapRequest.getType());
205 /* The DMAAP wrapper's body field cannot be null */
206 assertNotNull(dmaapRequest.getBody());
208 AppcLcmInput appcRequest = dmaapRequest.getBody().getInput();
210 /* A common header is required and cannot be null */
211 assertNotNull(appcRequest.getCommonHeader());
212 assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
214 /* An action is required and cannot be null */
215 assertNotNull(appcRequest.getAction());
216 assertEquals(RECIPE_RESTART, appcRequest.getAction());
218 /* Action Identifiers are required and cannot be null */
219 assertNotNull(appcRequest.getActionIdentifiers());
220 assertNotNull(appcRequest.getActionIdentifiers().get(VNF_ID_KEY));
221 assertEquals(VNF01, appcRequest.getActionIdentifiers().get(VNF_ID_KEY));
223 logger.debug("APPC Request: \n" + appcRequest.toString());
227 * A test to process a successful APPC restart response.
230 public void processRestartResponseSuccessTest() {
231 AbstractMap.SimpleEntry<PolicyResult, String> result =
232 AppcLcmActor.processResponse(dmaapResponse);
233 assertEquals(PolicyResult.SUCCESS, result.getKey());
234 assertEquals("Restart Successful", result.getValue());
238 * A test to assert that a null pointer exception is thrown if the APPC response body
241 @Test(expected = NullPointerException.class)
242 public void processNullBodyResponseTest() {
243 AppcLcmActor.processResponse(new AppcLcmDmaapWrapper());
247 * A test to assert that a null pointer exception is thrown if the APPC response
250 @Test(expected = NullPointerException.class)
251 public void processNullOutputResponseTest() {
252 AppcLcmDmaapWrapper dmaapWrapper = new AppcLcmDmaapWrapper();
253 dmaapWrapper.setBody(new AppcLcmBody());
254 AppcLcmActor.processResponse(dmaapWrapper);
258 * A test to map APPC response results to corresponding Policy results.
261 public void appcToPolicyResultTest() {
263 AbstractMap.SimpleEntry<PolicyResult, String> result;
265 /* If APPC accepts, PolicyResult is null */
266 dmaapResponse.getBody().getOutput().getStatus().setCode(100);
267 dmaapResponse.getBody().getOutput().getStatus().setMessage("ACCEPTED");
268 result = AppcLcmActor.processResponse(dmaapResponse);
269 assertNull(result.getKey());
271 /* If APPC is successful, PolicyResult is success */
272 dmaapResponse.getBody().getOutput().getStatus().setCode(400);
273 dmaapResponse.getBody().getOutput().getStatus().setMessage("SUCCESS");
274 result = AppcLcmActor.processResponse(dmaapResponse);
275 assertEquals(PolicyResult.SUCCESS, result.getKey());
277 /* If APPC returns an error, PolicyResult is failure exception */
278 dmaapResponse.getBody().getOutput().getStatus().setCode(200);
279 dmaapResponse.getBody().getOutput().getStatus().setMessage("ERROR");
280 result = AppcLcmActor.processResponse(dmaapResponse);
281 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
283 /* If APPC rejects, PolicyResult is failure exception */
284 dmaapResponse.getBody().getOutput().getStatus().setCode(300);
285 dmaapResponse.getBody().getOutput().getStatus().setMessage(REJECT);
286 result = AppcLcmActor.processResponse(dmaapResponse);
287 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
289 /* Test multiple reject codes */
290 dmaapResponse.getBody().getOutput().getStatus().setCode(306);
291 dmaapResponse.getBody().getOutput().getStatus().setMessage(REJECT);
292 result = AppcLcmActor.processResponse(dmaapResponse);
293 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
295 dmaapResponse.getBody().getOutput().getStatus().setCode(313);
296 dmaapResponse.getBody().getOutput().getStatus().setMessage(REJECT);
297 result = AppcLcmActor.processResponse(dmaapResponse);
298 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
300 /* If APPC returns failure, PolicyResult is failure */
301 dmaapResponse.getBody().getOutput().getStatus().setCode(401);
302 dmaapResponse.getBody().getOutput().getStatus().setMessage(FAILURE);
303 result = AppcLcmActor.processResponse(dmaapResponse);
304 assertEquals(PolicyResult.FAILURE, result.getKey());
306 /* Test multiple failure codes */
307 dmaapResponse.getBody().getOutput().getStatus().setCode(406);
308 dmaapResponse.getBody().getOutput().getStatus().setMessage(FAILURE);
309 result = AppcLcmActor.processResponse(dmaapResponse);
310 assertEquals(PolicyResult.FAILURE, result.getKey());
312 dmaapResponse.getBody().getOutput().getStatus().setCode(450);
313 dmaapResponse.getBody().getOutput().getStatus().setMessage(FAILURE);
314 result = AppcLcmActor.processResponse(dmaapResponse);
315 assertEquals(PolicyResult.FAILURE, result.getKey());
317 /* If APPC returns partial success, PolicyResult is failure exception */
318 dmaapResponse.getBody().getOutput().getStatus().setCode(500);
319 dmaapResponse.getBody().getOutput().getStatus().setMessage("PARTIAL SUCCESS");
320 result = AppcLcmActor.processResponse(dmaapResponse);
321 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
323 /* If APPC returns partial failure, PolicyResult is failure exception */
324 dmaapResponse.getBody().getOutput().getStatus().setCode(501);
325 dmaapResponse.getBody().getOutput().getStatus().setMessage(PARTIAL_FAILURE);
326 result = AppcLcmActor.processResponse(dmaapResponse);
327 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
329 /* Test multiple partial failure codes */
330 dmaapResponse.getBody().getOutput().getStatus().setCode(599);
331 dmaapResponse.getBody().getOutput().getStatus().setMessage(PARTIAL_FAILURE);
332 result = AppcLcmActor.processResponse(dmaapResponse);
333 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
335 dmaapResponse.getBody().getOutput().getStatus().setCode(550);
336 dmaapResponse.getBody().getOutput().getStatus().setMessage(PARTIAL_FAILURE);
337 result = AppcLcmActor.processResponse(dmaapResponse);
338 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
340 /* If APPC code is unknown to Policy, PolicyResult is failure exception */
341 dmaapResponse.getBody().getOutput().getStatus().setCode(700);
342 dmaapResponse.getBody().getOutput().getStatus().setMessage("UNKNOWN");
343 result = AppcLcmActor.processResponse(dmaapResponse);
344 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
348 * This test exercises getters not exercised in other tests.
351 public void testMethods() {
352 AppcLcmActor sp = new AppcLcmActor();
354 assertEquals("APPC", sp.actor());
355 assertEquals(4, sp.recipes().size());
356 assertEquals("VM", sp.recipeTargets(RECIPE_RESTART).get(0));
357 assertEquals("vm-id", sp.recipePayloads(RECIPE_RESTART).get(0));
361 public void testPayloadNotPassedWhenNotSupportedByRecipe() {
363 Policy migratePolicy = constructPolicyWithRecipe(RECIPE_MIGRATE);
364 Policy rebuildPolicy = constructPolicyWithRecipe(RECIPE_REBUILD);
365 Policy restartPolicy = constructPolicyWithRecipe(RECIPE_RESTART);
368 AppcLcmDmaapWrapper migrateRequest =
369 AppcLcmActor.constructRequest(onsetEvent, operation, migratePolicy, VNF01);
370 AppcLcmDmaapWrapper rebuildRequest =
371 AppcLcmActor.constructRequest(onsetEvent, operation, rebuildPolicy, VNF01);
372 AppcLcmDmaapWrapper restartRequest =
373 AppcLcmActor.constructRequest(onsetEvent, operation, restartPolicy, VNF01);
376 assertNull(migrateRequest.getBody().getInput().getPayload());
377 assertNull(rebuildRequest.getBody().getInput().getPayload());
378 assertNull(restartRequest.getBody().getInput().getPayload());
382 public void testPayloadNotPassedWhenNotSuppliedOrEmpty() {
384 Policy noPayloadPolicy = constructHealthCheckPolicyWithPayload(null);
385 Policy emptyPayloadPolicy = constructHealthCheckPolicyWithPayload(new HashMap<>());
388 AppcLcmDmaapWrapper noPayloadRequest =
389 AppcLcmActor.constructRequest(onsetEvent, operation, noPayloadPolicy, VNF01);
390 AppcLcmDmaapWrapper emptyPayloadRequest =
391 AppcLcmActor.constructRequest(onsetEvent, operation, emptyPayloadPolicy, VNF01);
394 assertNull(noPayloadRequest.getBody().getInput().getPayload());
395 assertNull(emptyPayloadRequest.getBody().getInput().getPayload());
399 public void testPayloadParsedProperlyForSinglePayloadParameter() {
401 HashMap<String, String> payload = new HashMap<>();
402 payload.put("requestParameters", "{\"host-ip-address\":\"10.183.37.25\"}");
403 Policy otherPolicy = constructHealthCheckPolicyWithPayload(payload);
406 AppcLcmDmaapWrapper dmaapRequest =
407 AppcLcmActor.constructRequest(onsetEvent, operation, otherPolicy, VNF01);
410 assertEquals("{\"requestParameters\": {\"host-ip-address\":\"10.183.37.25\"}}",
411 dmaapRequest.getBody().getInput().getPayload());
415 public void testPayloadParsedProperlyForMultiplePayloadParameters() {
417 HashMap<String, String> payload = new HashMap<>();
418 payload.put("requestParameters", "{\"host-ip-address\":\"10.183.37.25\"}");
419 payload.put("configurationParameters",
420 "[{\"ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[9]\","
421 + "\"oam-ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[16]\","
422 + "\"enabled\":\"$.vf-module-topology.vf-module-parameters.param[23]\"}]");
423 Policy otherPolicy = constructHealthCheckPolicyWithPayload(payload);
426 AppcLcmDmaapWrapper dmaapRequest =
427 AppcLcmActor.constructRequest(onsetEvent, operation, otherPolicy, VNF01);
430 assertEquals(dmaapRequest.getBody().getInput().getPayload(), "{\"requestParameters\": "
431 + "{\"host-ip-address\":\"10.183.37.25\"}," + "\"configurationParameters\": "
432 + "[{\"ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[9]\","
433 + "\"oam-ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[16]\","
434 + "\"enabled\":\"$.vf-module-topology.vf-module-parameters.param[23]\"}]" + "}");
437 private Policy constructHealthCheckPolicyWithPayload(HashMap<String, String> payload) {
438 return constructHealthCheckPolicyWithPayloadAndRecipe(payload, "Health-Check");
441 private Policy constructPolicyWithRecipe(String recipe) {
442 return constructHealthCheckPolicyWithPayloadAndRecipe(null, recipe);
445 private Policy constructHealthCheckPolicyWithPayloadAndRecipe(HashMap<String, String> payload, String recipe) {
446 Policy otherPolicy = new Policy();
447 otherPolicy.setName("Perform health check");
448 otherPolicy.setDescription("Upon getting the trigger event, perform health check");
449 otherPolicy.setActor("APPC");
450 otherPolicy.setTarget(new Target(TargetType.VNF));
451 otherPolicy.setRecipe(recipe);
452 otherPolicy.setPayload(payload);
453 otherPolicy.setRetry(2);
454 otherPolicy.setTimeout(300);