2 * ============LICENSE_START=======================================================
3 * AppcServiceProviderTest
4 * ================================================================================
5 * Copyright (C) 2017 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.*;
25 import java.time.Instant;
26 import java.util.AbstractMap;
27 import java.util.HashMap;
28 import java.util.UUID;
30 import org.junit.AfterClass;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.onap.policy.aai.util.AAIException;
34 import org.onap.policy.appclcm.LCMCommonHeader;
35 import org.onap.policy.appclcm.LCMRequest;
36 import org.onap.policy.appclcm.LCMRequestWrapper;
37 import org.onap.policy.appclcm.LCMResponse;
38 import org.onap.policy.appclcm.LCMResponseWrapper;
39 import org.onap.policy.controlloop.ControlLoopEventStatus;
40 import org.onap.policy.controlloop.ControlLoopOperation;
41 import org.onap.policy.controlloop.ControlLoopTargetType;
42 import org.onap.policy.controlloop.VirtualControlLoopEvent;
43 import org.onap.policy.controlloop.policy.Policy;
44 import org.onap.policy.controlloop.policy.PolicyResult;
45 import org.onap.policy.controlloop.policy.Target;
46 import org.onap.policy.controlloop.policy.TargetType;
47 import org.onap.policy.drools.http.server.HttpServletServer;
48 import org.onap.policy.drools.system.PolicyEngine;
49 import org.onap.policy.simulators.Util;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 public class AppcLcmServiceProviderTest {
55 private static final Logger logger = LoggerFactory.getLogger(AppcLcmServiceProviderTest.class);
57 private static VirtualControlLoopEvent onsetEvent;
58 private static ControlLoopOperation operation;
59 private static Policy policy;
60 private static LCMRequestWrapper dmaapRequest;
61 private static LCMResponseWrapper dmaapResponse;
65 * Construct an onset with an AAI subtag containing
66 * generic-vnf.vnf-id and a target type of VM.
68 onsetEvent = new VirtualControlLoopEvent();
69 onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
70 onsetEvent.setRequestID(UUID.randomUUID());
71 onsetEvent.setClosedLoopEventClient("tca.instance00001");
72 onsetEvent.setTargetType(ControlLoopTargetType.VM);
73 onsetEvent.setTarget("generic-vnf.vnf-name");
74 onsetEvent.setFrom("DCAE");
75 onsetEvent.setClosedLoopAlarmStart(Instant.now());
76 onsetEvent.setAAI(new HashMap<>());
77 onsetEvent.getAAI().put("generic-vnf.vnf-name", "fw0001vm001fw001");
78 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
80 /* Construct an operation with an APPC actor and restart operation. */
81 operation = new ControlLoopOperation();
82 operation.setActor("APPC");
83 operation.setOperation("Restart");
84 operation.setTarget("VM");
85 operation.setEnd(Instant.now());
86 operation.setSubRequestId("1");
88 /* Construct a policy specifying to restart vm. */
89 policy = new Policy();
90 policy.setName("Restart the VM");
91 policy.setDescription("Upon getting the trigger event, restart the VM");
92 policy.setActor("APPC");
93 policy.setTarget(new Target(TargetType.VNF));
94 policy.setRecipe("Restart");
95 policy.setPayload(null);
97 policy.setTimeout(300);
99 /* A sample DMAAP request wrapper. */
100 dmaapRequest = new LCMRequestWrapper();
101 dmaapRequest.setCorrelationId(onsetEvent.getRequestID().toString() + "-" + "1");
102 dmaapRequest.setRpcName(policy.getRecipe().toLowerCase());
103 dmaapRequest.setType("request");
105 /* A sample DMAAP response wrapper */
106 dmaapResponse = new LCMResponseWrapper();
107 dmaapResponse.setCorrelationId(onsetEvent.getRequestID().toString() + "-" + "1");
108 dmaapResponse.setRpcName(policy.getRecipe().toLowerCase());
109 dmaapResponse.setType("response");
111 /* Set environment properties */
112 PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
113 PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI");
114 PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
116 /* A sample APPC LCM request. */
117 LCMRequest appcRequest = new LCMRequest();
119 /* The following code constructs a sample APPC LCM Request */
120 appcRequest.setAction("restart");
122 HashMap<String, String> actionIdentifiers = new HashMap<>();
123 actionIdentifiers.put("vnf-id", "trial-vnf-003");
125 appcRequest.setActionIdentifiers(actionIdentifiers);
127 LCMCommonHeader commonHeader = new LCMCommonHeader();
128 commonHeader.setRequestId(onsetEvent.getRequestID());
129 commonHeader.setSubRequestId("1");
130 commonHeader.setOriginatorId(onsetEvent.getRequestID().toString());
132 appcRequest.setCommonHeader(commonHeader);
134 appcRequest.setPayload(null);
136 dmaapRequest.setBody(appcRequest);
138 /* The following code constructs a sample APPC LCM Response */
139 LCMResponse appcResponse = new LCMResponse(appcRequest);
140 appcResponse.getStatus().setCode(400);
141 appcResponse.getStatus().setMessage("Restart Successful");
143 dmaapResponse.setBody(appcResponse);
147 public static void setUpSimulator() {
150 } catch (Exception e) {
151 fail(e.getMessage());
156 public static void tearDownSimulator() {
157 HttpServletServer.factory.destroy();
161 * A test to construct an APPC LCM restart request.
164 public void constructRestartRequestTest() {
166 LCMRequestWrapper dmaapRequest = AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
168 /* The service provider must return a non null DMAAP request wrapper */
169 assertNotNull(dmaapRequest);
171 /* The DMAAP wrapper's type field must be request */
172 assertEquals("request", dmaapRequest.getType());
174 /* The DMAAP wrapper's body field cannot be null */
175 assertNotNull(dmaapRequest.getBody());
177 LCMRequest appcRequest = dmaapRequest.getBody();
179 /* A common header is required and cannot be null */
180 assertNotNull(appcRequest.getCommonHeader());
181 assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestID());
183 /* An action is required and cannot be null */
184 assertNotNull(appcRequest.getAction());
185 assertEquals("Restart", appcRequest.getAction());
187 /* Action Identifiers are required and cannot be null */
188 assertNotNull(appcRequest.getActionIdentifiers());
189 assertNotNull(appcRequest.getActionIdentifiers().get("vnf-id"));
190 assertEquals("vnf01", appcRequest.getActionIdentifiers().get("vnf-id"));
192 logger.debug("APPC Request: \n" + appcRequest.toString());
196 * A test to process a successful APPC restart response.
199 public void processRestartResponseSuccessTest() {
200 AbstractMap.SimpleEntry<PolicyResult, String> result = AppcLcmActorServiceProvider
201 .processResponse(dmaapResponse);
202 assertEquals(PolicyResult.SUCCESS, result.getKey());
203 assertEquals("Restart Successful", result.getValue());
207 * A test to map APPC response results to corresponding Policy results
210 public void appcToPolicyResultTest() {
212 AbstractMap.SimpleEntry<PolicyResult, String> result;
214 /* If APPC accepts, PolicyResult is null */
215 dmaapResponse.getBody().getStatus().setCode(100);
216 dmaapResponse.getBody().getStatus().setMessage("ACCEPTED");
217 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
218 assertEquals(null, result.getKey());
220 /* If APPC is successful, PolicyResult is success */
221 dmaapResponse.getBody().getStatus().setCode(400);
222 dmaapResponse.getBody().getStatus().setMessage("SUCCESS");
223 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
224 assertEquals(PolicyResult.SUCCESS, result.getKey());
226 /* If APPC returns an error, PolicyResult is failure exception */
227 dmaapResponse.getBody().getStatus().setCode(200);
228 dmaapResponse.getBody().getStatus().setMessage("ERROR");
229 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
230 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
232 /* If APPC rejects, PolicyResult is failure exception */
233 dmaapResponse.getBody().getStatus().setCode(300);
234 dmaapResponse.getBody().getStatus().setMessage("REJECT");
235 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
236 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
238 /* Test multiple reject codes */
239 dmaapResponse.getBody().getStatus().setCode(306);
240 dmaapResponse.getBody().getStatus().setMessage("REJECT");
241 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
242 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
244 dmaapResponse.getBody().getStatus().setCode(313);
245 dmaapResponse.getBody().getStatus().setMessage("REJECT");
246 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
247 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
249 /* If APPC returns failure, PolicyResult is failure */
250 dmaapResponse.getBody().getStatus().setCode(401);
251 dmaapResponse.getBody().getStatus().setMessage("FAILURE");
252 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
253 assertEquals(PolicyResult.FAILURE, result.getKey());
255 /* Test multiple failure codes */
256 dmaapResponse.getBody().getStatus().setCode(406);
257 dmaapResponse.getBody().getStatus().setMessage("FAILURE");
258 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
259 assertEquals(PolicyResult.FAILURE, result.getKey());
261 dmaapResponse.getBody().getStatus().setCode(450);
262 dmaapResponse.getBody().getStatus().setMessage("FAILURE");
263 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
264 assertEquals(PolicyResult.FAILURE, result.getKey());
266 /* If APPC returns partial success, PolicyResult is failure exception */
267 dmaapResponse.getBody().getStatus().setCode(500);
268 dmaapResponse.getBody().getStatus().setMessage("PARTIAL SUCCESS");
269 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
270 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
272 /* If APPC returns partial failure, PolicyResult is failure exception */
273 dmaapResponse.getBody().getStatus().setCode(501);
274 dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
275 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
276 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
278 /* Test multiple partial failure codes */
279 dmaapResponse.getBody().getStatus().setCode(599);
280 dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
281 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
282 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
284 dmaapResponse.getBody().getStatus().setCode(550);
285 dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
286 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
287 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
289 /* If APPC code is unknown to Policy, PolicyResult is failure exception */
290 dmaapResponse.getBody().getStatus().setCode(700);
291 dmaapResponse.getBody().getStatus().setMessage("UNKNOWN");
292 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
293 assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
297 * This test ensures that that if the the source entity
298 * is also the target entity, the source will be used for
302 public void sourceIsTargetTest() {
303 String resourceId = "82194af1-3c2c-485a-8f44-420e22a9eaa4";
304 String targetVnfId = null;
306 targetVnfId = AppcLcmActorServiceProvider.vnfNamedQuery(resourceId, "vnf01");
307 } catch (AAIException e) {
308 logger.warn(e.toString());
309 fail("no vnf-id found");
311 assertNotNull(targetVnfId);
312 assertEquals("vnf01", targetVnfId);
316 * THis test exercises getters not exercised in other tests
319 public void testMethods() {
320 AppcLcmActorServiceProvider sp = new AppcLcmActorServiceProvider();
322 assertEquals("APPC", sp.actor());
323 assertEquals(4, sp.recipes().size());
324 assertEquals("VM", sp.recipeTargets("Restart").get(0));
325 assertEquals("vm-id", sp.recipePayloads("Restart").get(0));