2 * ============LICENSE_START=======================================================
3 * AppcServiceProviderTest
4 * ================================================================================
5 * Copyright (C) 2017-2018 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;
27 import static org.junit.Assert.fail;
29 import java.time.Instant;
30 import java.util.AbstractMap;
31 import java.util.HashMap;
32 import java.util.UUID;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.policy.aai.util.AaiException;
38 import org.onap.policy.appclcm.LcmCommonHeader;
39 import org.onap.policy.appclcm.LcmRequest;
40 import org.onap.policy.appclcm.LcmRequestWrapper;
41 import org.onap.policy.appclcm.LcmResponse;
42 import org.onap.policy.appclcm.LcmResponseWrapper;
43 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
44 import org.onap.policy.controlloop.ControlLoopEventStatus;
45 import org.onap.policy.controlloop.ControlLoopOperation;
46 import org.onap.policy.controlloop.ControlLoopTargetType;
47 import org.onap.policy.controlloop.VirtualControlLoopEvent;
48 import org.onap.policy.controlloop.policy.Policy;
49 import org.onap.policy.controlloop.policy.PolicyResult;
50 import org.onap.policy.controlloop.policy.Target;
51 import org.onap.policy.controlloop.policy.TargetType;
52 import org.onap.policy.simulators.Util;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
57 public class AppcLcmActorServiceProviderTest {
59 private static final Logger logger = LoggerFactory.getLogger(AppcLcmActorServiceProviderTest.class);
61 private static final VirtualControlLoopEvent onsetEvent;
62 private static final ControlLoopOperation operation;
63 private static final Policy policy;
64 private static final LcmResponseWrapper dmaapResponse;
66 private static final String RECIPE_RESTART = "Restart";
67 private static final String RECIPE_REBUILD = "Rebuild";
68 private static final String RECIPE_MIGRATE = "Migrate";
72 * Construct an onset with an AAI subtag containing generic-vnf.vnf-id and a target type of
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);
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");
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);
104 policy.setTimeout(300);
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");
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");
118 /* A sample APPC LCM request. */
119 LcmRequest appcRequest = new LcmRequest();
121 /* The following code constructs a sample APPC LCM Request */
122 appcRequest.setAction("restart");
124 HashMap<String, String> actionIdentifiers = new HashMap<>();
125 actionIdentifiers.put("vnf-id", "trial-vnf-003");
127 appcRequest.setActionIdentifiers(actionIdentifiers);
129 LcmCommonHeader commonHeader = new LcmCommonHeader();
130 commonHeader.setRequestId(onsetEvent.getRequestId());
131 commonHeader.setSubRequestId("1");
132 commonHeader.setOriginatorId(onsetEvent.getRequestId().toString());
134 appcRequest.setCommonHeader(commonHeader);
136 appcRequest.setPayload(null);
138 dmaapRequest.setBody(appcRequest);
140 /* The following code constructs a sample APPC LCM Response */
141 LcmResponse appcResponse = new LcmResponse(appcRequest);
142 appcResponse.getStatus().setCode(400);
143 appcResponse.getStatus().setMessage("Restart Successful");
145 dmaapResponse.setBody(appcResponse);
149 * Set up before test class.
152 public static void setUpSimulator() {
155 } catch (Exception e) {
156 fail(e.getMessage());
161 * Tear down after test class.
164 public static void tearDownSimulator() {
165 HttpServletServer.factory.destroy();
169 * A test to construct an APPC LCM restart request.
172 public void constructRestartRequestTest() {
174 LcmRequestWrapper dmaapRequest =
175 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
177 /* The service provider must return a non null DMAAP request wrapper */
178 assertNotNull(dmaapRequest);
180 /* The DMAAP wrapper's type field must be request */
181 assertEquals("request", dmaapRequest.getType());
183 /* The DMAAP wrapper's body field cannot be null */
184 assertNotNull(dmaapRequest.getBody());
186 LcmRequest appcRequest = dmaapRequest.getBody();
188 /* A common header is required and cannot be null */
189 assertNotNull(appcRequest.getCommonHeader());
190 assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
192 /* An action is required and cannot be null */
193 assertNotNull(appcRequest.getAction());
194 assertEquals("Restart", appcRequest.getAction());
196 /* Action Identifiers are required and cannot be null */
197 assertNotNull(appcRequest.getActionIdentifiers());
198 assertNotNull(appcRequest.getActionIdentifiers().get("vnf-id"));
199 assertEquals("vnf01", appcRequest.getActionIdentifiers().get("vnf-id"));
201 logger.debug("APPC Request: \n" + appcRequest.toString());
205 * A test to process a successful APPC restart response.
208 public void processRestartResponseSuccessTest() {
209 AbstractMap.SimpleEntry<PolicyResult, String> result =
210 AppcLcmActorServiceProvider.processResponse(dmaapResponse);
211 assertEquals(PolicyResult.SUCCESS, result.getKey());
212 assertEquals("Restart Successful", result.getValue());
216 * A test to map APPC response results to corresponding Policy results.
219 public void appcToPolicyResultTest() {
221 AbstractMap.SimpleEntry<PolicyResult, String> result;
223 /* If APPC accepts, PolicyResult is null */
224 dmaapResponse.getBody().getStatus().setCode(100);
225 dmaapResponse.getBody().getStatus().setMessage("ACCEPTED");
226 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
227 assertNull(result.getKey());
229 /* If APPC is successful, PolicyResult is success */
230 dmaapResponse.getBody().getStatus().setCode(400);
231 dmaapResponse.getBody().getStatus().setMessage("SUCCESS");
232 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
233 assertEquals(PolicyResult.SUCCESS, result.getKey());
235 /* If APPC returns an error, PolicyResult is failure exception */
236 dmaapResponse.getBody().getStatus().setCode(200);
237 dmaapResponse.getBody().getStatus().setMessage("ERROR");
238 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
239 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
241 /* If APPC rejects, PolicyResult is failure exception */
242 dmaapResponse.getBody().getStatus().setCode(300);
243 dmaapResponse.getBody().getStatus().setMessage("REJECT");
244 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
245 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
247 /* Test multiple reject codes */
248 dmaapResponse.getBody().getStatus().setCode(306);
249 dmaapResponse.getBody().getStatus().setMessage("REJECT");
250 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
251 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
253 dmaapResponse.getBody().getStatus().setCode(313);
254 dmaapResponse.getBody().getStatus().setMessage("REJECT");
255 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
256 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
258 /* If APPC returns failure, PolicyResult is failure */
259 dmaapResponse.getBody().getStatus().setCode(401);
260 dmaapResponse.getBody().getStatus().setMessage("FAILURE");
261 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
262 assertEquals(PolicyResult.FAILURE, result.getKey());
264 /* Test multiple failure codes */
265 dmaapResponse.getBody().getStatus().setCode(406);
266 dmaapResponse.getBody().getStatus().setMessage("FAILURE");
267 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
268 assertEquals(PolicyResult.FAILURE, result.getKey());
270 dmaapResponse.getBody().getStatus().setCode(450);
271 dmaapResponse.getBody().getStatus().setMessage("FAILURE");
272 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
273 assertEquals(PolicyResult.FAILURE, result.getKey());
275 /* If APPC returns partial success, PolicyResult is failure exception */
276 dmaapResponse.getBody().getStatus().setCode(500);
277 dmaapResponse.getBody().getStatus().setMessage("PARTIAL SUCCESS");
278 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
279 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
281 /* If APPC returns partial failure, PolicyResult is failure exception */
282 dmaapResponse.getBody().getStatus().setCode(501);
283 dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
284 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
285 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
287 /* Test multiple partial failure codes */
288 dmaapResponse.getBody().getStatus().setCode(599);
289 dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
290 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
291 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
293 dmaapResponse.getBody().getStatus().setCode(550);
294 dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
295 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
296 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
298 /* If APPC code is unknown to Policy, PolicyResult is failure exception */
299 dmaapResponse.getBody().getStatus().setCode(700);
300 dmaapResponse.getBody().getStatus().setMessage("UNKNOWN");
301 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
302 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
306 * This test ensures that that if the the source entity is also the target entity, the source
307 * will be used for the APPC request.
310 public void sourceIsTargetTest() {
311 String resourceId = "82194af1-3c2c-485a-8f44-420e22a9eaa4";
312 String targetVnfId = null;
314 targetVnfId = AppcLcmActorServiceProvider.vnfNamedQuery(resourceId, "vnf01",
315 "http://localhost:6666", "AAI", "AAI");
316 } catch (AaiException e) {
317 logger.warn(e.toString());
318 fail("no vnf-id found");
320 assertNotNull(targetVnfId);
321 assertEquals("vnf01", targetVnfId);
325 * THis test exercises getters not exercised in other tests.
328 public void testMethods() {
329 AppcLcmActorServiceProvider sp = new AppcLcmActorServiceProvider();
331 assertEquals("APPC", sp.actor());
332 assertEquals(4, sp.recipes().size());
333 assertEquals("VM", sp.recipeTargets("Restart").get(0));
334 assertEquals("vm-id", sp.recipePayloads("Restart").get(0));
338 public void payloadNotPassedWhenNotSupportedByRecipe() {
340 Policy migratePolicy = constructPolicyWithRecipe(RECIPE_MIGRATE);
341 Policy rebuildPolicy = constructPolicyWithRecipe(RECIPE_REBUILD);
342 Policy restartPolicy = constructPolicyWithRecipe(RECIPE_RESTART);
345 LcmRequestWrapper migrateRequest =
346 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, migratePolicy, "vnf01");
347 LcmRequestWrapper rebuildRequest =
348 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, rebuildPolicy, "vnf01");
349 LcmRequestWrapper restartRequest =
350 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, restartPolicy, "vnf01");
353 assertNull(migrateRequest.getBody().getPayload());
354 assertNull(rebuildRequest.getBody().getPayload());
355 assertNull(restartRequest.getBody().getPayload());
359 public void payloadNotPassedWhenNotSuppliedOrEmpty() {
361 Policy noPayloadPolicy = constructHealthCheckPolicyWithPayload(null);
362 Policy emptyPayloadPolicy = constructHealthCheckPolicyWithPayload(new HashMap<>());
365 LcmRequestWrapper noPayloadRequest =
366 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, noPayloadPolicy, "vnf01");
367 LcmRequestWrapper emptyPayloadRequest =
368 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, emptyPayloadPolicy, "vnf01");
372 assertNull(noPayloadRequest.getBody().getPayload());
373 assertNull(emptyPayloadRequest.getBody().getPayload());
377 public void payloadParsedProperlyForSinglePayloadParameter() {
379 HashMap<String, String> payload = new HashMap<>();
380 payload.put("requestParameters", "{\"host-ip-address\":\"10.183.37.25\"}");
381 Policy otherPolicy = constructHealthCheckPolicyWithPayload(payload);
384 LcmRequestWrapper dmaapRequest =
385 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, otherPolicy, "vnf01");
388 assertEquals(dmaapRequest.getBody().getPayload(),
389 "{\"requestParameters\": {\"host-ip-address\":\"10.183.37.25\"}}");
394 public void payloadParsedProperlyForMultiplePayloadParameters() {
396 HashMap<String, String> payload = new HashMap<>();
397 payload.put("requestParameters", "{\"host-ip-address\":\"10.183.37.25\"}");
398 payload.put("configurationParameters", "[{\"ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[9]\","
399 + "\"oam-ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[16]\","
400 + "\"enabled\":\"$.vf-module-topology.vf-module-parameters.param[23]\"}]");
401 Policy otherPolicy = constructHealthCheckPolicyWithPayload(payload);
404 LcmRequestWrapper dmaapRequest =
405 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, otherPolicy, "vnf01");
408 assertEquals(dmaapRequest.getBody().getPayload(),
409 "{\"requestParameters\": "
410 + "{\"host-ip-address\":\"10.183.37.25\"},"
411 + "\"configurationParameters\": "
412 + "[{\"ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[9]\","
413 + "\"oam-ip-addr\":\"$.vf-module-topology.vf-module-parameters.param[16]\","
414 + "\"enabled\":\"$.vf-module-topology.vf-module-parameters.param[23]\"}]"
418 private Policy constructHealthCheckPolicyWithPayload(HashMap<String, String> payload) {
419 return constructHealthCheckPolicyWithPayloadAndRecipe(payload, "Health-Check");
422 private Policy constructPolicyWithRecipe(String recipe) {
423 return constructHealthCheckPolicyWithPayloadAndRecipe(null, recipe);
426 private Policy constructHealthCheckPolicyWithPayloadAndRecipe(HashMap<String, String> payload, String recipe) {
427 Policy otherPolicy = new Policy();
428 otherPolicy.setName("Perform health check");
429 otherPolicy.setDescription("Upon getting the trigger event, perform health check");
430 otherPolicy.setActor("APPC");
431 otherPolicy.setTarget(new Target(TargetType.VNF));
432 otherPolicy.setRecipe(recipe);
433 otherPolicy.setPayload(payload);
434 otherPolicy.setRetry(2);
435 otherPolicy.setTimeout(300);