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 = null;
168 dmaapRequest = AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
169 } catch (AAIException e) {
170 logger.warn(e.toString());
171 fail("no vnfid found");
174 /* The service provider must return a non null DMAAP request wrapper */
175 assertNotNull(dmaapRequest);
177 /* The DMAAP wrapper's type field must be request */
178 assertEquals(dmaapRequest.getType(), "request");
180 /* The DMAAP wrapper's body field cannot be null */
181 assertNotNull(dmaapRequest.getBody());
183 LCMRequest appcRequest = dmaapRequest.getBody();
185 /* A common header is required and cannot be null */
186 assertNotNull(appcRequest.getCommonHeader());
187 assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestID());
189 /* An action is required and cannot be null */
190 assertNotNull(appcRequest.getAction());
191 assertEquals(appcRequest.getAction(), "Restart");
193 /* Action Identifiers are required and cannot be null */
194 assertNotNull(appcRequest.getActionIdentifiers());
195 assertNotNull(appcRequest.getActionIdentifiers().get("vnf-id"));
196 assertEquals(appcRequest.getActionIdentifiers().get("vnf-id"), "vnf01");
198 logger.debug("APPC Request: \n" + appcRequest.toString());
202 * A test to process a successful APPC restart response.
205 public void processRestartResponseSuccessTest() {
206 AbstractMap.SimpleEntry<PolicyResult, String> result = AppcLcmActorServiceProvider
207 .processResponse(dmaapResponse);
208 assertEquals(result.getKey(), PolicyResult.SUCCESS);
209 assertEquals(result.getValue(), "Restart Successful");
213 * A test to map APPC response results to corresponding Policy results
216 public void appcToPolicyResultTest() {
218 AbstractMap.SimpleEntry<PolicyResult, String> result;
220 /* If APPC accepts, PolicyResult is null */
221 dmaapResponse.getBody().getStatus().setCode(100);
222 dmaapResponse.getBody().getStatus().setMessage("ACCEPTED");
223 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
224 assertEquals(result.getKey(), null);
226 /* If APPC is successful, PolicyResult is success */
227 dmaapResponse.getBody().getStatus().setCode(400);
228 dmaapResponse.getBody().getStatus().setMessage("SUCCESS");
229 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
230 assertEquals(result.getKey(), PolicyResult.SUCCESS);
232 /* If APPC returns an error, PolicyResult is failure exception */
233 dmaapResponse.getBody().getStatus().setCode(200);
234 dmaapResponse.getBody().getStatus().setMessage("ERROR");
235 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
236 assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
238 /* If APPC rejects, PolicyResult is failure exception */
239 dmaapResponse.getBody().getStatus().setCode(300);
240 dmaapResponse.getBody().getStatus().setMessage("REJECT");
241 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
242 assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
244 /* Test multiple reject codes */
245 dmaapResponse.getBody().getStatus().setCode(306);
246 dmaapResponse.getBody().getStatus().setMessage("REJECT");
247 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
248 assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
250 dmaapResponse.getBody().getStatus().setCode(313);
251 dmaapResponse.getBody().getStatus().setMessage("REJECT");
252 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
253 assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
255 /* If APPC returns failure, PolicyResult is failure */
256 dmaapResponse.getBody().getStatus().setCode(401);
257 dmaapResponse.getBody().getStatus().setMessage("FAILURE");
258 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
259 assertEquals(result.getKey(), PolicyResult.FAILURE);
261 /* Test multiple failure codes */
262 dmaapResponse.getBody().getStatus().setCode(406);
263 dmaapResponse.getBody().getStatus().setMessage("FAILURE");
264 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
265 assertEquals(result.getKey(), PolicyResult.FAILURE);
267 dmaapResponse.getBody().getStatus().setCode(450);
268 dmaapResponse.getBody().getStatus().setMessage("FAILURE");
269 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
270 assertEquals(result.getKey(), PolicyResult.FAILURE);
272 /* If APPC returns partial success, PolicyResult is failure exception */
273 dmaapResponse.getBody().getStatus().setCode(500);
274 dmaapResponse.getBody().getStatus().setMessage("PARTIAL SUCCESS");
275 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
276 assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
278 /* If APPC returns partial failure, PolicyResult is failure exception */
279 dmaapResponse.getBody().getStatus().setCode(501);
280 dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
281 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
282 assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
284 /* Test multiple partial failure codes */
285 dmaapResponse.getBody().getStatus().setCode(599);
286 dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
287 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
288 assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
290 dmaapResponse.getBody().getStatus().setCode(550);
291 dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
292 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
293 assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
295 /* If APPC code is unknown to Policy, PolicyResult is failure exception */
296 dmaapResponse.getBody().getStatus().setCode(700);
297 dmaapResponse.getBody().getStatus().setMessage("UNKNOWN");
298 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
299 assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
303 * This test ensures that that if the the source entity
304 * is also the target entity, the source will be used for
308 public void sourceIsTargetTest() {
309 String resourceId = "82194af1-3c2c-485a-8f44-420e22a9eaa4";
310 String targetVnfId = null;
312 targetVnfId = AppcLcmActorServiceProvider.vnfNamedQuery(resourceId, "vnf01");
313 } catch (AAIException e) {
314 logger.warn(e.toString());
315 fail("no vnf-id found");
317 assertNotNull(targetVnfId);
318 assertEquals(targetVnfId, "vnf01");