7c0f1882a17d2822684a80ea489ebea32b3486b6
[policy/drools-applications.git] /
1 /*-
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.appclcm;
22
23 import static org.junit.Assert.*;
24
25 import java.time.Instant;
26 import java.util.AbstractMap;
27 import java.util.HashMap;
28 import java.util.UUID;
29
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;
53
54 public class AppcLcmServiceProviderTest {
55     
56     private static final Logger logger = LoggerFactory.getLogger(AppcLcmServiceProviderTest.class);
57     
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;
64
65     static {
66         /* 
67          * Construct an onset with an AAI subtag containing
68          * generic-vnf.vnf-id and a target type of VM.
69          */
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;
81
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";
89         
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);
98         policy.setRetry(2);
99         policy.setTimeout(300);
100         
101         /* Construct a mock A&AI response */
102         aaiResponse = new AAIGETVnfResponse();
103         aaiResponse.vnfID = "vnf01";
104
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");
110
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");
116         
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");
121         
122         /* A sample APPC LCM request. */
123         LCMRequest appcRequest = new LCMRequest();
124         
125         /* The following code constructs a sample APPC LCM Request */
126         appcRequest.setAction("restart");
127         
128         HashMap<String, String> actionIdentifiers = new HashMap<>();
129         actionIdentifiers.put("vnf-id", "trial-vnf-003");
130         
131         appcRequest.setActionIdentifiers(actionIdentifiers);
132         
133         LCMCommonHeader commonHeader = new LCMCommonHeader();
134         commonHeader.setRequestId(onsetEvent.requestID);
135         commonHeader.setSubRequestId("1");
136         commonHeader.setOriginatorId(onsetEvent.requestID.toString());
137         
138         appcRequest.setCommonHeader(commonHeader);
139         
140         appcRequest.setPayload(null);
141
142         dmaapRequest.setBody(appcRequest);
143
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");
148
149         dmaapResponse.setBody(appcResponse);
150     }
151     
152     @BeforeClass
153     public static void setUpSimulator() {
154         try {
155             Util.buildAaiSim();
156         } catch (Exception e) {
157             fail(e.getMessage());
158         }
159     }
160
161     @AfterClass
162     public static void tearDownSimulator() {
163         HttpServletServer.factory.destroy();
164     }
165     
166     /**
167      * A test to construct an APPC LCM restart request.
168      */
169     @Test
170     public void constructRestartRequestTest() {
171         
172         LCMRequestWrapper dmaapRequest = null;
173         try {
174             dmaapRequest = AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, policy, aaiResponse);
175         } catch (AAIException e) {
176             logger.warn(e.toString());
177             fail("no vnfid found");
178         }
179
180         /* The service provider must return a non null DMAAP request wrapper */
181         assertNotNull(dmaapRequest);
182
183         /* The DMAAP wrapper's type field must be request */
184         assertEquals(dmaapRequest.getType(), "request");
185         
186         /* The DMAAP wrapper's body field cannot be null */
187         assertNotNull(dmaapRequest.getBody());
188
189         LCMRequest appcRequest = dmaapRequest.getBody();
190         
191         /* A common header is required and cannot be null */
192         assertNotNull(appcRequest.getCommonHeader());
193         assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.requestID);
194
195         /* An action is required and cannot be null */
196         assertNotNull(appcRequest.getAction());
197         assertEquals(appcRequest.getAction(), "Restart");
198
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");
203         
204         logger.debug("APPC Request: \n" + appcRequest.toString());
205     }
206
207     /**
208      * A test to process a successful APPC restart response.
209      */
210     @Test
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");
216     }
217     
218     /**
219      * A test to map APPC response results to corresponding Policy results
220      */
221     @Test
222     public void appcToPolicyResultTest() {
223         
224         AbstractMap.SimpleEntry<PolicyResult, String> result;
225         
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);
231         
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);
237         
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);
243         
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);
249         
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);
255         
256         dmaapResponse.getBody().getStatus().setCode(313);
257         dmaapResponse.getBody().getStatus().setMessage("REJECT");
258         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
259         assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
260         
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);
266         
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);
272         
273         dmaapResponse.getBody().getStatus().setCode(450);
274         dmaapResponse.getBody().getStatus().setMessage("FAILURE");
275         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
276         assertEquals(result.getKey(), PolicyResult.FAILURE);
277         
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);
283         
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);
289         
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);
295         
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);
300         
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);
306     }
307     
308     /**
309      * This test ensures that that if the the source entity
310      * is also the target entity, the source will be used for
311      * the APPC request
312      */
313     @Test
314     public void sourceIsTargetTest() {
315         String resourceId = "82194af1-3c2c-485a-8f44-420e22a9eaa4";
316         String targetVnfId = null;
317         try {
318             targetVnfId = AppcLcmActorServiceProvider.vnfNamedQuery(resourceId, aaiResponse.vnfID);
319         } catch (AAIException e) {
320             logger.warn(e.toString());
321             fail("no vnf-id found");
322         }
323         assertNotNull(targetVnfId);
324         assertEquals(targetVnfId, aaiResponse.vnfID);
325     }
326 }