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 AppcLcmServiceProviderTest {
59 private static final Logger logger = LoggerFactory.getLogger(AppcLcmServiceProviderTest.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;
68 * Construct an onset with an AAI subtag containing generic-vnf.vnf-id and a target type of
71 onsetEvent = new VirtualControlLoopEvent();
72 onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
73 onsetEvent.setRequestId(UUID.randomUUID());
74 onsetEvent.setClosedLoopEventClient("tca.instance00001");
75 onsetEvent.setTargetType(ControlLoopTargetType.VM);
76 onsetEvent.setTarget("generic-vnf.vnf-name");
77 onsetEvent.setFrom("DCAE");
78 onsetEvent.setClosedLoopAlarmStart(Instant.now());
79 onsetEvent.setAai(new HashMap<>());
80 onsetEvent.getAai().put("generic-vnf.vnf-name", "fw0001vm001fw001");
81 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
83 /* Construct an operation with an APPC actor and restart operation. */
84 operation = new ControlLoopOperation();
85 operation.setActor("APPC");
86 operation.setOperation("Restart");
87 operation.setTarget("VM");
88 operation.setEnd(Instant.now());
89 operation.setSubRequestId("1");
91 /* Construct a policy specifying to restart vm. */
92 policy = new Policy();
93 policy.setName("Restart the VM");
94 policy.setDescription("Upon getting the trigger event, restart the VM");
95 policy.setActor("APPC");
96 policy.setTarget(new Target(TargetType.VNF));
97 policy.setRecipe("Restart");
98 policy.setPayload(null);
100 policy.setTimeout(300);
102 /* A sample DMAAP request wrapper. */
103 LcmRequestWrapper dmaapRequest = new LcmRequestWrapper();
104 dmaapRequest.setCorrelationId(onsetEvent.getRequestId().toString() + "-" + "1");
105 dmaapRequest.setRpcName(policy.getRecipe().toLowerCase());
106 dmaapRequest.setType("request");
108 /* A sample DMAAP response wrapper */
109 dmaapResponse = new LcmResponseWrapper();
110 dmaapResponse.setCorrelationId(onsetEvent.getRequestId().toString() + "-" + "1");
111 dmaapResponse.setRpcName(policy.getRecipe().toLowerCase());
112 dmaapResponse.setType("response");
114 /* Set environment properties */
115 PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
116 PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI");
117 PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
119 /* A sample APPC LCM request. */
120 LcmRequest appcRequest = new LcmRequest();
122 /* The following code constructs a sample APPC LCM Request */
123 appcRequest.setAction("restart");
125 HashMap<String, String> actionIdentifiers = new HashMap<>();
126 actionIdentifiers.put("vnf-id", "trial-vnf-003");
128 appcRequest.setActionIdentifiers(actionIdentifiers);
130 LcmCommonHeader commonHeader = new LcmCommonHeader();
131 commonHeader.setRequestId(onsetEvent.getRequestId());
132 commonHeader.setSubRequestId("1");
133 commonHeader.setOriginatorId(onsetEvent.getRequestId().toString());
135 appcRequest.setCommonHeader(commonHeader);
137 appcRequest.setPayload(null);
139 dmaapRequest.setBody(appcRequest);
141 /* The following code constructs a sample APPC LCM Response */
142 LcmResponse appcResponse = new LcmResponse(appcRequest);
143 appcResponse.getStatus().setCode(400);
144 appcResponse.getStatus().setMessage("Restart Successful");
146 dmaapResponse.setBody(appcResponse);
150 * Set up before test class.
153 public static void setUpSimulator() {
156 } catch (Exception e) {
157 fail(e.getMessage());
162 * Tear down after test class.
165 public static void tearDownSimulator() {
166 HttpServletServer.factory.destroy();
170 * A test to construct an APPC LCM restart request.
173 public void constructRestartRequestTest() {
175 LcmRequestWrapper dmaapRequest =
176 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
178 /* The service provider must return a non null DMAAP request wrapper */
179 assertNotNull(dmaapRequest);
181 /* The DMAAP wrapper's type field must be request */
182 assertEquals("request", dmaapRequest.getType());
184 /* The DMAAP wrapper's body field cannot be null */
185 assertNotNull(dmaapRequest.getBody());
187 LcmRequest appcRequest = dmaapRequest.getBody();
189 /* A common header is required and cannot be null */
190 assertNotNull(appcRequest.getCommonHeader());
191 assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
193 /* An action is required and cannot be null */
194 assertNotNull(appcRequest.getAction());
195 assertEquals("Restart", appcRequest.getAction());
197 /* Action Identifiers are required and cannot be null */
198 assertNotNull(appcRequest.getActionIdentifiers());
199 assertNotNull(appcRequest.getActionIdentifiers().get("vnf-id"));
200 assertEquals("vnf01", appcRequest.getActionIdentifiers().get("vnf-id"));
202 logger.debug("APPC Request: \n" + appcRequest.toString());
206 * A test to process a successful APPC restart response.
209 public void processRestartResponseSuccessTest() {
210 AbstractMap.SimpleEntry<PolicyResult, String> result =
211 AppcLcmActorServiceProvider.processResponse(dmaapResponse);
212 assertEquals(PolicyResult.SUCCESS, result.getKey());
213 assertEquals("Restart Successful", result.getValue());
217 * A test to map APPC response results to corresponding Policy results.
220 public void appcToPolicyResultTest() {
222 AbstractMap.SimpleEntry<PolicyResult, String> result;
224 /* If APPC accepts, PolicyResult is null */
225 dmaapResponse.getBody().getStatus().setCode(100);
226 dmaapResponse.getBody().getStatus().setMessage("ACCEPTED");
227 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
228 assertNull(result.getKey());
230 /* If APPC is successful, PolicyResult is success */
231 dmaapResponse.getBody().getStatus().setCode(400);
232 dmaapResponse.getBody().getStatus().setMessage("SUCCESS");
233 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
234 assertEquals(PolicyResult.SUCCESS, result.getKey());
236 /* If APPC returns an error, PolicyResult is failure exception */
237 dmaapResponse.getBody().getStatus().setCode(200);
238 dmaapResponse.getBody().getStatus().setMessage("ERROR");
239 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
240 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
242 /* If APPC rejects, PolicyResult is failure exception */
243 dmaapResponse.getBody().getStatus().setCode(300);
244 dmaapResponse.getBody().getStatus().setMessage("REJECT");
245 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
246 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
248 /* Test multiple reject codes */
249 dmaapResponse.getBody().getStatus().setCode(306);
250 dmaapResponse.getBody().getStatus().setMessage("REJECT");
251 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
252 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
254 dmaapResponse.getBody().getStatus().setCode(313);
255 dmaapResponse.getBody().getStatus().setMessage("REJECT");
256 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
257 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
259 /* If APPC returns failure, PolicyResult is failure */
260 dmaapResponse.getBody().getStatus().setCode(401);
261 dmaapResponse.getBody().getStatus().setMessage("FAILURE");
262 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
263 assertEquals(PolicyResult.FAILURE, result.getKey());
265 /* Test multiple failure codes */
266 dmaapResponse.getBody().getStatus().setCode(406);
267 dmaapResponse.getBody().getStatus().setMessage("FAILURE");
268 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
269 assertEquals(PolicyResult.FAILURE, result.getKey());
271 dmaapResponse.getBody().getStatus().setCode(450);
272 dmaapResponse.getBody().getStatus().setMessage("FAILURE");
273 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
274 assertEquals(PolicyResult.FAILURE, result.getKey());
276 /* If APPC returns partial success, PolicyResult is failure exception */
277 dmaapResponse.getBody().getStatus().setCode(500);
278 dmaapResponse.getBody().getStatus().setMessage("PARTIAL SUCCESS");
279 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
280 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
282 /* If APPC returns partial failure, PolicyResult is failure exception */
283 dmaapResponse.getBody().getStatus().setCode(501);
284 dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
285 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
286 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
288 /* Test multiple partial failure codes */
289 dmaapResponse.getBody().getStatus().setCode(599);
290 dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
291 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
292 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
294 dmaapResponse.getBody().getStatus().setCode(550);
295 dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
296 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
297 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
299 /* If APPC code is unknown to Policy, PolicyResult is failure exception */
300 dmaapResponse.getBody().getStatus().setCode(700);
301 dmaapResponse.getBody().getStatus().setMessage("UNKNOWN");
302 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
303 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
307 * This test ensures that that if the the source entity is also the target entity, the source
308 * will be used for the APPC request.
311 public void sourceIsTargetTest() {
312 String resourceId = "82194af1-3c2c-485a-8f44-420e22a9eaa4";
313 String targetVnfId = null;
315 targetVnfId = AppcLcmActorServiceProvider.vnfNamedQuery(resourceId, "vnf01");
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));