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;
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;
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 LcmResponseWrapper 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
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 LcmRequestWrapper dmaapRequest = new LcmRequestWrapper();
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 LcmResponseWrapper();
121 dmaapResponse.setCorrelationId(onsetEvent.getRequestId().toString() + "-" + "1");
122 dmaapResponse.setRpcName(policy.getRecipe().toLowerCase());
123 dmaapResponse.setType("response");
125 /* A sample APPC LCM request. */
126 LcmRequest appcRequest = new LcmRequest();
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 LcmCommonHeader commonHeader = new LcmCommonHeader();
137 commonHeader.setRequestId(onsetEvent.getRequestId());
138 commonHeader.setSubRequestId("1");
139 commonHeader.setOriginatorId(onsetEvent.getRequestId().toString());
141 appcRequest.setCommonHeader(commonHeader);
143 appcRequest.setPayload(null);
145 dmaapRequest.setBody(appcRequest);
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");
152 dmaapResponse.setBody(appcResponse);
156 * Set up before test class.
157 * @throws Exception if an error occurs
160 public static void setUpSimulator() throws Exception {
165 * Tear down after test class.
168 public static void tearDownSimulator() {
169 HttpServletServer.factory.destroy();
173 * A test to construct an APPC LCM restart request.
176 public void constructRestartRequestTest() {
178 LcmRequestWrapper dmaapRequest =
179 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, policy, VNF01);
181 /* The service provider must return a non null DMAAP request wrapper */
182 assertNotNull(dmaapRequest);
184 /* The DMAAP wrapper's type field must be request */
185 assertEquals("request", dmaapRequest.getType());
187 /* The DMAAP wrapper's body field cannot be null */
188 assertNotNull(dmaapRequest.getBody());
190 LcmRequest appcRequest = dmaapRequest.getBody();
192 /* A common header is required and cannot be null */
193 assertNotNull(appcRequest.getCommonHeader());
194 assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
196 /* An action is required and cannot be null */
197 assertNotNull(appcRequest.getAction());
198 assertEquals(RECIPE_RESTART, appcRequest.getAction());
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));
205 logger.debug("APPC Request: \n" + appcRequest.toString());
209 * A test to process a successful APPC restart response.
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());
220 * A test to map APPC response results to corresponding Policy results.
223 public void appcToPolicyResultTest() {
225 AbstractMap.SimpleEntry<PolicyResult, String> result;
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());
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());
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());
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());
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());
257 dmaapResponse.getBody().getStatus().setCode(313);
258 dmaapResponse.getBody().getStatus().setMessage(REJECT);
259 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
260 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
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());
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());
274 dmaapResponse.getBody().getStatus().setCode(450);
275 dmaapResponse.getBody().getStatus().setMessage(FAILURE);
276 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
277 assertEquals(PolicyResult.FAILURE, result.getKey());
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());
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());
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());
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());
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());
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.
314 public void sourceIsTargetTest() throws Exception {
315 String resourceId = "82194af1-3c2c-485a-8f44-420e22a9eaa4";
316 String targetVnfId = AppcLcmActorServiceProvider.vnfNamedQuery(resourceId, VNF01, "http://localhost:6666",
318 assertNotNull(targetVnfId);
319 assertEquals(VNF01, targetVnfId);
323 * This test exercises getters not exercised in other tests.
326 public void testMethods() {
327 AppcLcmActorServiceProvider sp = new AppcLcmActorServiceProvider();
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));
336 public void payloadNotPassedWhenNotSupportedByRecipe() {
338 Policy migratePolicy = constructPolicyWithRecipe(RECIPE_MIGRATE);
339 Policy rebuildPolicy = constructPolicyWithRecipe(RECIPE_REBUILD);
340 Policy restartPolicy = constructPolicyWithRecipe(RECIPE_RESTART);
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);
351 assertNull(migrateRequest.getBody().getPayload());
352 assertNull(rebuildRequest.getBody().getPayload());
353 assertNull(restartRequest.getBody().getPayload());
357 public void payloadNotPassedWhenNotSuppliedOrEmpty() {
359 Policy noPayloadPolicy = constructHealthCheckPolicyWithPayload(null);
360 Policy emptyPayloadPolicy = constructHealthCheckPolicyWithPayload(new HashMap<>());
363 LcmRequestWrapper noPayloadRequest =
364 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, noPayloadPolicy, VNF01);
365 LcmRequestWrapper emptyPayloadRequest =
366 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, emptyPayloadPolicy, VNF01);
370 assertNull(noPayloadRequest.getBody().getPayload());
371 assertNull(emptyPayloadRequest.getBody().getPayload());
375 public void payloadParsedProperlyForSinglePayloadParameter() {
377 HashMap<String, String> payload = new HashMap<>();
378 payload.put("requestParameters", "{\"host-ip-address\":\"10.183.37.25\"}");
379 Policy otherPolicy = constructHealthCheckPolicyWithPayload(payload);
382 LcmRequestWrapper dmaapRequest =
383 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, otherPolicy, VNF01);
386 assertEquals(dmaapRequest.getBody().getPayload(),
387 "{\"requestParameters\": {\"host-ip-address\":\"10.183.37.25\"}}");
392 public void payloadParsedProperlyForMultiplePayloadParameters() {
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);
402 LcmRequestWrapper dmaapRequest =
403 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, otherPolicy, VNF01);
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]\"}]"
416 private Policy constructHealthCheckPolicyWithPayload(HashMap<String, String> payload) {
417 return constructHealthCheckPolicyWithPayloadAndRecipe(payload, "Health-Check");
420 private Policy constructPolicyWithRecipe(String recipe) {
421 return constructHealthCheckPolicyWithPayloadAndRecipe(null, recipe);
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);