cf5360202da3b816ba0672c3363ebabfa4bf63dd
[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.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;
52
53 public class AppcLcmServiceProviderTest {
54     
55     private static final Logger logger = LoggerFactory.getLogger(AppcLcmServiceProviderTest.class);
56     
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;
62
63     static {
64         /* 
65          * Construct an onset with an AAI subtag containing
66          * generic-vnf.vnf-id and a target type of VM.
67          */
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);
79
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");
87         
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);
96         policy.setRetry(2);
97         policy.setTimeout(300);
98
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");
104
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");
110         
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");
115         
116         /* A sample APPC LCM request. */
117         LCMRequest appcRequest = new LCMRequest();
118         
119         /* The following code constructs a sample APPC LCM Request */
120         appcRequest.setAction("restart");
121         
122         HashMap<String, String> actionIdentifiers = new HashMap<>();
123         actionIdentifiers.put("vnf-id", "trial-vnf-003");
124         
125         appcRequest.setActionIdentifiers(actionIdentifiers);
126         
127         LCMCommonHeader commonHeader = new LCMCommonHeader();
128         commonHeader.setRequestId(onsetEvent.getRequestID());
129         commonHeader.setSubRequestId("1");
130         commonHeader.setOriginatorId(onsetEvent.getRequestID().toString());
131         
132         appcRequest.setCommonHeader(commonHeader);
133         
134         appcRequest.setPayload(null);
135
136         dmaapRequest.setBody(appcRequest);
137
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");
142
143         dmaapResponse.setBody(appcResponse);
144     }
145     
146     @BeforeClass
147     public static void setUpSimulator() {
148         try {
149             Util.buildAaiSim();
150         } catch (Exception e) {
151             fail(e.getMessage());
152         }
153     }
154
155     @AfterClass
156     public static void tearDownSimulator() {
157         HttpServletServer.factory.destroy();
158     }
159     
160     /**
161      * A test to construct an APPC LCM restart request.
162      */
163     @Test
164     public void constructRestartRequestTest() {
165         
166         LCMRequestWrapper dmaapRequest =  AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
167
168         /* The service provider must return a non null DMAAP request wrapper */
169         assertNotNull(dmaapRequest);
170
171         /* The DMAAP wrapper's type field must be request */
172         assertEquals("request", dmaapRequest.getType());
173         
174         /* The DMAAP wrapper's body field cannot be null */
175         assertNotNull(dmaapRequest.getBody());
176
177         LCMRequest appcRequest = dmaapRequest.getBody();
178         
179         /* A common header is required and cannot be null */
180         assertNotNull(appcRequest.getCommonHeader());
181         assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestID());
182
183         /* An action is required and cannot be null */
184         assertNotNull(appcRequest.getAction());
185         assertEquals("Restart", appcRequest.getAction());
186
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"));
191         
192         logger.debug("APPC Request: \n" + appcRequest.toString());
193     }
194
195     /**
196      * A test to process a successful APPC restart response.
197      */
198     @Test
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());
204     }
205     
206     /**
207      * A test to map APPC response results to corresponding Policy results
208      */
209     @Test
210     public void appcToPolicyResultTest() {
211         
212         AbstractMap.SimpleEntry<PolicyResult, String> result;
213         
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());
219         
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());
225         
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());
231         
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());
237         
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());
243         
244         dmaapResponse.getBody().getStatus().setCode(313);
245         dmaapResponse.getBody().getStatus().setMessage("REJECT");
246         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
247         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
248         
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());
254         
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());
260         
261         dmaapResponse.getBody().getStatus().setCode(450);
262         dmaapResponse.getBody().getStatus().setMessage("FAILURE");
263         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
264         assertEquals(PolicyResult.FAILURE, result.getKey());
265         
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());
271         
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());
277         
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());
283         
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());
288         
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());
294     }
295     
296     /**
297      * This test ensures that that if the the source entity
298      * is also the target entity, the source will be used for
299      * the APPC request
300      */
301     @Test
302     public void sourceIsTargetTest() {
303         String resourceId = "82194af1-3c2c-485a-8f44-420e22a9eaa4";
304         String targetVnfId = null;
305         try {
306             targetVnfId = AppcLcmActorServiceProvider.vnfNamedQuery(resourceId, "vnf01");
307         } catch (AAIException e) {
308             logger.warn(e.toString());
309             fail("no vnf-id found");
310         }
311         assertNotNull(targetVnfId);
312         assertEquals("vnf01", targetVnfId);
313     }
314     
315     /**
316      * THis test exercises getters not exercised in other tests
317      */
318     @Test
319     public void testMethods() {
320                 AppcLcmActorServiceProvider sp = new AppcLcmActorServiceProvider();
321         
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));
326     }
327 }