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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actor.appclcm;
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;
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.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;
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 /* 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");
123 /* A sample APPC LCM request. */
124 LcmRequest appcRequest = new LcmRequest();
126 /* The following code constructs a sample APPC LCM Request */
127 appcRequest.setAction("restart");
129 HashMap<String, String> actionIdentifiers = new HashMap<>();
130 actionIdentifiers.put("vnf-id", "trial-vnf-003");
132 appcRequest.setActionIdentifiers(actionIdentifiers);
134 LcmCommonHeader commonHeader = new LcmCommonHeader();
135 commonHeader.setRequestId(onsetEvent.getRequestId());
136 commonHeader.setSubRequestId("1");
137 commonHeader.setOriginatorId(onsetEvent.getRequestId().toString());
139 appcRequest.setCommonHeader(commonHeader);
141 appcRequest.setPayload(null);
143 dmaapRequest.setBody(appcRequest);
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");
150 dmaapResponse.setBody(appcResponse);
154 * Set up before test class.
157 public static void setUpSimulator() {
160 } catch (Exception e) {
161 fail(e.getMessage());
166 * Tear down after test class.
169 public static void tearDownSimulator() {
170 HttpServletServer.factory.destroy();
174 * A test to construct an APPC LCM restart request.
177 public void constructRestartRequestTest() {
179 LcmRequestWrapper dmaapRequest =
180 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
182 /* The service provider must return a non null DMAAP request wrapper */
183 assertNotNull(dmaapRequest);
185 /* The DMAAP wrapper's type field must be request */
186 assertEquals("request", dmaapRequest.getType());
188 /* The DMAAP wrapper's body field cannot be null */
189 assertNotNull(dmaapRequest.getBody());
191 LcmRequest appcRequest = dmaapRequest.getBody();
193 /* A common header is required and cannot be null */
194 assertNotNull(appcRequest.getCommonHeader());
195 assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
197 /* An action is required and cannot be null */
198 assertNotNull(appcRequest.getAction());
199 assertEquals("Restart", appcRequest.getAction());
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"));
206 logger.debug("APPC Request: \n" + appcRequest.toString());
210 * A test to process a successful APPC restart response.
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());
221 * A test to map APPC response results to corresponding Policy results.
224 public void appcToPolicyResultTest() {
226 AbstractMap.SimpleEntry<PolicyResult, String> result;
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());
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());
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());
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());
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());
258 dmaapResponse.getBody().getStatus().setCode(313);
259 dmaapResponse.getBody().getStatus().setMessage("REJECT");
260 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
261 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
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());
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());
275 dmaapResponse.getBody().getStatus().setCode(450);
276 dmaapResponse.getBody().getStatus().setMessage("FAILURE");
277 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
278 assertEquals(PolicyResult.FAILURE, result.getKey());
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());
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());
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());
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());
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());
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.
315 public void sourceIsTargetTest() {
316 String resourceId = "82194af1-3c2c-485a-8f44-420e22a9eaa4";
317 String targetVnfId = null;
319 targetVnfId = AppcLcmActorServiceProvider.vnfNamedQuery(resourceId, "vnf01");
320 } catch (AaiException e) {
321 logger.warn(e.toString());
322 fail("no vnf-id found");
324 assertNotNull(targetVnfId);
325 assertEquals("vnf01", targetVnfId);
329 * THis test exercises getters not exercised in other tests.
332 public void testMethods() {
333 AppcLcmActorServiceProvider sp = new AppcLcmActorServiceProvider();
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));
342 public void payloadNotPassedWhenNotSupportedByRecipe() {
344 Policy migratePolicy = constructPolicyWithRecipe(RECIPE_MIGRATE);
345 Policy rebuildPolicy = constructPolicyWithRecipe(RECIPE_REBUILD);
346 Policy restartPolicy = constructPolicyWithRecipe(RECIPE_RESTART);
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");
357 assertNull(migrateRequest.getBody().getPayload());
358 assertNull(rebuildRequest.getBody().getPayload());
359 assertNull(restartRequest.getBody().getPayload());
363 public void payloadNotPassedWhenNotSuppliedOrEmpty() {
365 Policy noPayloadPolicy = constructHealthCheckPolicyWithPayload(null);
366 Policy emptyPayloadPolicy = constructHealthCheckPolicyWithPayload(new HashMap<>());
369 LcmRequestWrapper noPayloadRequest =
370 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, noPayloadPolicy, "vnf01");
371 LcmRequestWrapper emptyPayloadRequest =
372 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, emptyPayloadPolicy, "vnf01");
376 assertNull(noPayloadRequest.getBody().getPayload());
377 assertNull(emptyPayloadRequest.getBody().getPayload());
381 public void payloadParsedProperlyForSinglePayloadParameter() {
383 HashMap<String, String> payload = new HashMap<>();
384 payload.put("requestParameters", "{\"host-ip-address\":\"10.183.37.25\"}");
385 Policy otherPolicy = constructHealthCheckPolicyWithPayload(payload);
388 LcmRequestWrapper dmaapRequest =
389 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, otherPolicy, "vnf01");
392 assertEquals(dmaapRequest.getBody().getPayload(),
393 "{\"requestParameters\": {\"host-ip-address\":\"10.183.37.25\"}}");
398 public void payloadParsedProperlyForMultiplePayloadParameters() {
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);
408 LcmRequestWrapper dmaapRequest =
409 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, otherPolicy, "vnf01");
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]\"}]"
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);