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.AAIGETVnfResponse;
34 import org.onap.policy.aai.util.AAIException;
35 import org.onap.policy.appclcm.LCMCommonHeader;
36 import org.onap.policy.appclcm.LCMRequest;
37 import org.onap.policy.appclcm.LCMRequestWrapper;
38 import org.onap.policy.appclcm.LCMResponse;
39 import org.onap.policy.appclcm.LCMResponseWrapper;
40 import org.onap.policy.controlloop.ControlLoopEventStatus;
41 import org.onap.policy.controlloop.ControlLoopOperation;
42 import org.onap.policy.controlloop.ControlLoopTargetType;
43 import org.onap.policy.controlloop.VirtualControlLoopEvent;
44 import org.onap.policy.controlloop.policy.Policy;
45 import org.onap.policy.controlloop.policy.PolicyResult;
46 import org.onap.policy.controlloop.policy.Target;
47 import org.onap.policy.controlloop.policy.TargetType;
48 import org.onap.policy.drools.http.server.HttpServletServer;
49 import org.onap.policy.drools.system.PolicyEngine;
50 import org.onap.policy.simulators.Util;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 public class AppcLcmServiceProviderTest {
56 private static final Logger logger = LoggerFactory.getLogger(AppcLcmServiceProviderTest.class);
58 private static VirtualControlLoopEvent onsetEvent;
59 private static ControlLoopOperation operation;
60 private static Policy policy;
61 private static AAIGETVnfResponse aaiResponse;
62 private static LCMRequestWrapper dmaapRequest;
63 private static LCMResponseWrapper dmaapResponse;
67 * Construct an onset with an AAI subtag containing
68 * generic-vnf.vnf-id and a target type of VM.
70 onsetEvent = new VirtualControlLoopEvent();
71 onsetEvent.closedLoopControlName = "closedLoopControlName-Test";
72 onsetEvent.requestID = UUID.randomUUID();
73 onsetEvent.closedLoopEventClient = "tca.instance00001";
74 onsetEvent.target_type = ControlLoopTargetType.VM;
75 onsetEvent.target = "generic-vnf.vnf-name";
76 onsetEvent.from = "DCAE";
77 onsetEvent.closedLoopAlarmStart = Instant.now();
78 onsetEvent.AAI = new HashMap<>();
79 onsetEvent.AAI.put("generic-vnf.vnf-name", "fw0001vm001fw001");
80 onsetEvent.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
82 /* Construct an operation with an APPC actor and restart operation. */
83 operation = new ControlLoopOperation();
84 operation.actor = "APPC";
85 operation.operation = "Restart";
86 operation.target = "VM";
87 operation.end = Instant.now();
88 operation.subRequestId = "1";
90 /* Construct a policy specifying to restart vm. */
91 policy = new Policy();
92 policy.setName("Restart the VM");
93 policy.setDescription("Upon getting the trigger event, restart the VM");
94 policy.setActor("APPC");
95 policy.setTarget(new Target(TargetType.VNF));
96 policy.setRecipe("Restart");
97 policy.setPayload(null);
99 policy.setTimeout(300);
101 /* Construct a mock A&AI response */
102 aaiResponse = new AAIGETVnfResponse();
103 aaiResponse.vnfID = "vnf01";
105 /* A sample DMAAP request wrapper. */
106 dmaapRequest = new LCMRequestWrapper();
107 dmaapRequest.setCorrelationId(onsetEvent.requestID.toString() + "-" + "1");
108 dmaapRequest.setRpcName(policy.getRecipe().toLowerCase());
109 dmaapRequest.setType("request");
111 /* A sample DMAAP response wrapper */
112 dmaapResponse = new LCMResponseWrapper();
113 dmaapResponse.setCorrelationId(onsetEvent.requestID.toString() + "-" + "1");
114 dmaapResponse.setRpcName(policy.getRecipe().toLowerCase());
115 dmaapResponse.setType("response");
117 /* Set environment properties */
118 PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
119 PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI");
120 PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
122 /* A sample APPC LCM request. */
123 LCMRequest appcRequest = new LCMRequest();
125 /* The following code constructs a sample APPC LCM Request */
126 appcRequest.setAction("restart");
128 HashMap<String, String> actionIdentifiers = new HashMap<>();
129 actionIdentifiers.put("vnf-id", "trial-vnf-003");
131 appcRequest.setActionIdentifiers(actionIdentifiers);
133 LCMCommonHeader commonHeader = new LCMCommonHeader();
134 commonHeader.setRequestId(onsetEvent.requestID);
135 commonHeader.setSubRequestId("1");
136 commonHeader.setOriginatorId(onsetEvent.requestID.toString());
138 appcRequest.setCommonHeader(commonHeader);
140 appcRequest.setPayload(null);
142 dmaapRequest.setBody(appcRequest);
144 /* The following code constructs a sample APPC LCM Response */
145 LCMResponse appcResponse = new LCMResponse(appcRequest);
146 appcResponse.getStatus().setCode(400);
147 appcResponse.getStatus().setMessage("Restart Successful");
149 dmaapResponse.setBody(appcResponse);
153 public static void setUpSimulator() {
156 } catch (Exception e) {
157 fail(e.getMessage());
162 public static void tearDownSimulator() {
163 HttpServletServer.factory.destroy();
167 * A test to construct an APPC LCM restart request.
170 public void constructRestartRequestTest() {
172 LCMRequestWrapper dmaapRequest = null;
174 dmaapRequest = AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, policy, aaiResponse);
175 } catch (AAIException e) {
176 logger.warn(e.toString());
177 fail("no vnfid found");
180 /* The service provider must return a non null DMAAP request wrapper */
181 assertNotNull(dmaapRequest);
183 /* The DMAAP wrapper's type field must be request */
184 assertEquals(dmaapRequest.getType(), "request");
186 /* The DMAAP wrapper's body field cannot be null */
187 assertNotNull(dmaapRequest.getBody());
189 LCMRequest appcRequest = dmaapRequest.getBody();
191 /* A common header is required and cannot be null */
192 assertNotNull(appcRequest.getCommonHeader());
193 assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.requestID);
195 /* An action is required and cannot be null */
196 assertNotNull(appcRequest.getAction());
197 assertEquals(appcRequest.getAction(), "Restart");
199 /* Action Identifiers are required and cannot be null */
200 assertNotNull(appcRequest.getActionIdentifiers());
201 assertNotNull(appcRequest.getActionIdentifiers().get("vnf-id"));
202 assertEquals(appcRequest.getActionIdentifiers().get("vnf-id"), "vnf01");
204 logger.debug("APPC Request: \n" + appcRequest.toString());
208 * A test to process a successful APPC restart response.
211 public void processRestartResponseSuccessTest() {
212 AbstractMap.SimpleEntry<PolicyResult, String> result = AppcLcmActorServiceProvider
213 .processResponse(dmaapResponse);
214 assertEquals(result.getKey(), PolicyResult.SUCCESS);
215 assertEquals(result.getValue(), "Restart Successful");
219 * A test to map APPC response results to corresponding Policy results
222 public void appcToPolicyResultTest() {
224 AbstractMap.SimpleEntry<PolicyResult, String> result;
226 /* If APPC accepts, PolicyResult is null */
227 dmaapResponse.getBody().getStatus().setCode(100);
228 dmaapResponse.getBody().getStatus().setMessage("ACCEPTED");
229 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
230 assertEquals(result.getKey(), null);
232 /* If APPC is successful, PolicyResult is success */
233 dmaapResponse.getBody().getStatus().setCode(400);
234 dmaapResponse.getBody().getStatus().setMessage("SUCCESS");
235 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
236 assertEquals(result.getKey(), PolicyResult.SUCCESS);
238 /* If APPC returns an error, PolicyResult is failure exception */
239 dmaapResponse.getBody().getStatus().setCode(200);
240 dmaapResponse.getBody().getStatus().setMessage("ERROR");
241 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
242 assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
244 /* If APPC rejects, PolicyResult is failure exception */
245 dmaapResponse.getBody().getStatus().setCode(300);
246 dmaapResponse.getBody().getStatus().setMessage("REJECT");
247 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
248 assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
250 /* Test multiple reject codes */
251 dmaapResponse.getBody().getStatus().setCode(306);
252 dmaapResponse.getBody().getStatus().setMessage("REJECT");
253 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
254 assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
256 dmaapResponse.getBody().getStatus().setCode(313);
257 dmaapResponse.getBody().getStatus().setMessage("REJECT");
258 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
259 assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
261 /* If APPC returns failure, PolicyResult is failure */
262 dmaapResponse.getBody().getStatus().setCode(401);
263 dmaapResponse.getBody().getStatus().setMessage("FAILURE");
264 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
265 assertEquals(result.getKey(), PolicyResult.FAILURE);
267 /* Test multiple failure codes */
268 dmaapResponse.getBody().getStatus().setCode(406);
269 dmaapResponse.getBody().getStatus().setMessage("FAILURE");
270 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
271 assertEquals(result.getKey(), PolicyResult.FAILURE);
273 dmaapResponse.getBody().getStatus().setCode(450);
274 dmaapResponse.getBody().getStatus().setMessage("FAILURE");
275 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
276 assertEquals(result.getKey(), PolicyResult.FAILURE);
278 /* If APPC returns partial success, PolicyResult is failure exception */
279 dmaapResponse.getBody().getStatus().setCode(500);
280 dmaapResponse.getBody().getStatus().setMessage("PARTIAL SUCCESS");
281 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
282 assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
284 /* If APPC returns partial failure, PolicyResult is failure exception */
285 dmaapResponse.getBody().getStatus().setCode(501);
286 dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
287 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
288 assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
290 /* Test multiple partial failure codes */
291 dmaapResponse.getBody().getStatus().setCode(599);
292 dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
293 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
294 assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
296 dmaapResponse.getBody().getStatus().setCode(550);
297 dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
298 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
299 assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
301 /* If APPC code is unknown to Policy, PolicyResult is failure exception */
302 dmaapResponse.getBody().getStatus().setCode(700);
303 dmaapResponse.getBody().getStatus().setMessage("UNKNOWN");
304 result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
305 assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
309 * This test ensures that that if the the source entity
310 * is also the target entity, the source will be used for
314 public void sourceIsTargetTest() {
315 String resourceId = "82194af1-3c2c-485a-8f44-420e22a9eaa4";
316 String targetVnfId = null;
318 targetVnfId = AppcLcmActorServiceProvider.vnfNamedQuery(resourceId, aaiResponse.vnfID);
319 } catch (AAIException e) {
320 logger.warn(e.toString());
321 fail("no vnf-id found");
323 assertNotNull(targetVnfId);
324 assertEquals(targetVnfId, aaiResponse.vnfID);