Merge "Add timeout to SO to wait for success"
[policy/drools-applications.git] / controlloop / common / actors / actor.appclcm / src / test / java / org / onap / policy / controlloop / actor / appclcm / AppcLcmServiceProviderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * AppcServiceProviderTest
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
26
27 import java.time.Instant;
28 import java.util.AbstractMap;
29 import java.util.HashMap;
30 import java.util.UUID;
31
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.aai.util.AaiException;
36 import org.onap.policy.appclcm.LCMCommonHeader;
37 import org.onap.policy.appclcm.LCMRequest;
38 import org.onap.policy.appclcm.LCMRequestWrapper;
39 import org.onap.policy.appclcm.LCMResponse;
40 import org.onap.policy.appclcm.LCMResponseWrapper;
41 import org.onap.policy.controlloop.ControlLoopEventStatus;
42 import org.onap.policy.controlloop.ControlLoopOperation;
43 import org.onap.policy.controlloop.ControlLoopTargetType;
44 import org.onap.policy.controlloop.VirtualControlLoopEvent;
45 import org.onap.policy.controlloop.policy.Policy;
46 import org.onap.policy.controlloop.policy.PolicyResult;
47 import org.onap.policy.controlloop.policy.Target;
48 import org.onap.policy.controlloop.policy.TargetType;
49 import org.onap.policy.drools.http.server.HttpServletServer;
50 import org.onap.policy.drools.system.PolicyEngine;
51 import org.onap.policy.simulators.Util;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 public class AppcLcmServiceProviderTest {
56
57     private static final Logger logger = LoggerFactory.getLogger(AppcLcmServiceProviderTest.class);
58
59     private static VirtualControlLoopEvent onsetEvent;
60     private static ControlLoopOperation operation;
61     private static Policy policy;
62     private static LCMRequestWrapper dmaapRequest;
63     private static LCMResponseWrapper dmaapResponse;
64
65     static {
66         /*
67          * Construct an onset with an AAI subtag containing generic-vnf.vnf-id and a target type of
68          * VM.
69          */
70         onsetEvent = new VirtualControlLoopEvent();
71         onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
72         onsetEvent.setRequestID(UUID.randomUUID());
73         onsetEvent.setClosedLoopEventClient("tca.instance00001");
74         onsetEvent.setTargetType(ControlLoopTargetType.VM);
75         onsetEvent.setTarget("generic-vnf.vnf-name");
76         onsetEvent.setFrom("DCAE");
77         onsetEvent.setClosedLoopAlarmStart(Instant.now());
78         onsetEvent.setAAI(new HashMap<>());
79         onsetEvent.getAAI().put("generic-vnf.vnf-name", "fw0001vm001fw001");
80         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
81
82         /* Construct an operation with an APPC actor and restart operation. */
83         operation = new ControlLoopOperation();
84         operation.setActor("APPC");
85         operation.setOperation("Restart");
86         operation.setTarget("VM");
87         operation.setEnd(Instant.now());
88         operation.setSubRequestId("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         /* A sample DMAAP request wrapper. */
102         dmaapRequest = new LCMRequestWrapper();
103         dmaapRequest.setCorrelationId(onsetEvent.getRequestID().toString() + "-" + "1");
104         dmaapRequest.setRpcName(policy.getRecipe().toLowerCase());
105         dmaapRequest.setType("request");
106
107         /* A sample DMAAP response wrapper */
108         dmaapResponse = new LCMResponseWrapper();
109         dmaapResponse.setCorrelationId(onsetEvent.getRequestID().toString() + "-" + "1");
110         dmaapResponse.setRpcName(policy.getRecipe().toLowerCase());
111         dmaapResponse.setType("response");
112
113         /* Set environment properties */
114         PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
115         PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI");
116         PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
117
118         /* A sample APPC LCM request. */
119         LCMRequest appcRequest = new LCMRequest();
120
121         /* The following code constructs a sample APPC LCM Request */
122         appcRequest.setAction("restart");
123
124         HashMap<String, String> actionIdentifiers = new HashMap<>();
125         actionIdentifiers.put("vnf-id", "trial-vnf-003");
126
127         appcRequest.setActionIdentifiers(actionIdentifiers);
128
129         LCMCommonHeader commonHeader = new LCMCommonHeader();
130         commonHeader.setRequestId(onsetEvent.getRequestID());
131         commonHeader.setSubRequestId("1");
132         commonHeader.setOriginatorId(onsetEvent.getRequestID().toString());
133
134         appcRequest.setCommonHeader(commonHeader);
135
136         appcRequest.setPayload(null);
137
138         dmaapRequest.setBody(appcRequest);
139
140         /* The following code constructs a sample APPC LCM Response */
141         LCMResponse appcResponse = new LCMResponse(appcRequest);
142         appcResponse.getStatus().setCode(400);
143         appcResponse.getStatus().setMessage("Restart Successful");
144
145         dmaapResponse.setBody(appcResponse);
146     }
147
148     /**
149      * Set up before test class.
150      */
151     @BeforeClass
152     public static void setUpSimulator() {
153         try {
154             Util.buildAaiSim();
155         } catch (Exception e) {
156             fail(e.getMessage());
157         }
158     }
159
160     /**
161      * Tear down after test class.
162      */
163     @AfterClass
164     public static void tearDownSimulator() {
165         HttpServletServer.factory.destroy();
166     }
167
168     /**
169      * A test to construct an APPC LCM restart request.
170      */
171     @Test
172     public void constructRestartRequestTest() {
173
174         LCMRequestWrapper dmaapRequest =
175                 AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
176
177         /* The service provider must return a non null DMAAP request wrapper */
178         assertNotNull(dmaapRequest);
179
180         /* The DMAAP wrapper's type field must be request */
181         assertEquals("request", dmaapRequest.getType());
182
183         /* The DMAAP wrapper's body field cannot be null */
184         assertNotNull(dmaapRequest.getBody());
185
186         LCMRequest appcRequest = dmaapRequest.getBody();
187
188         /* A common header is required and cannot be null */
189         assertNotNull(appcRequest.getCommonHeader());
190         assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestID());
191
192         /* An action is required and cannot be null */
193         assertNotNull(appcRequest.getAction());
194         assertEquals("Restart", appcRequest.getAction());
195
196         /* Action Identifiers are required and cannot be null */
197         assertNotNull(appcRequest.getActionIdentifiers());
198         assertNotNull(appcRequest.getActionIdentifiers().get("vnf-id"));
199         assertEquals("vnf01", appcRequest.getActionIdentifiers().get("vnf-id"));
200
201         logger.debug("APPC Request: \n" + appcRequest.toString());
202     }
203
204     /**
205      * A test to process a successful APPC restart response.
206      */
207     @Test
208     public void processRestartResponseSuccessTest() {
209         AbstractMap.SimpleEntry<PolicyResult, String> result =
210                 AppcLcmActorServiceProvider.processResponse(dmaapResponse);
211         assertEquals(PolicyResult.SUCCESS, result.getKey());
212         assertEquals("Restart Successful", result.getValue());
213     }
214
215     /**
216      * A test to map APPC response results to corresponding Policy results.
217      */
218     @Test
219     public void appcToPolicyResultTest() {
220
221         AbstractMap.SimpleEntry<PolicyResult, String> result;
222
223         /* If APPC accepts, PolicyResult is null */
224         dmaapResponse.getBody().getStatus().setCode(100);
225         dmaapResponse.getBody().getStatus().setMessage("ACCEPTED");
226         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
227         assertEquals(null, result.getKey());
228
229         /* If APPC is successful, PolicyResult is success */
230         dmaapResponse.getBody().getStatus().setCode(400);
231         dmaapResponse.getBody().getStatus().setMessage("SUCCESS");
232         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
233         assertEquals(PolicyResult.SUCCESS, result.getKey());
234
235         /* If APPC returns an error, PolicyResult is failure exception */
236         dmaapResponse.getBody().getStatus().setCode(200);
237         dmaapResponse.getBody().getStatus().setMessage("ERROR");
238         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
239         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
240
241         /* If APPC rejects, PolicyResult is failure exception */
242         dmaapResponse.getBody().getStatus().setCode(300);
243         dmaapResponse.getBody().getStatus().setMessage("REJECT");
244         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
245         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
246
247         /* Test multiple reject codes */
248         dmaapResponse.getBody().getStatus().setCode(306);
249         dmaapResponse.getBody().getStatus().setMessage("REJECT");
250         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
251         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
252
253         dmaapResponse.getBody().getStatus().setCode(313);
254         dmaapResponse.getBody().getStatus().setMessage("REJECT");
255         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
256         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
257
258         /* If APPC returns failure, PolicyResult is failure */
259         dmaapResponse.getBody().getStatus().setCode(401);
260         dmaapResponse.getBody().getStatus().setMessage("FAILURE");
261         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
262         assertEquals(PolicyResult.FAILURE, result.getKey());
263
264         /* Test multiple failure codes */
265         dmaapResponse.getBody().getStatus().setCode(406);
266         dmaapResponse.getBody().getStatus().setMessage("FAILURE");
267         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
268         assertEquals(PolicyResult.FAILURE, result.getKey());
269
270         dmaapResponse.getBody().getStatus().setCode(450);
271         dmaapResponse.getBody().getStatus().setMessage("FAILURE");
272         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
273         assertEquals(PolicyResult.FAILURE, result.getKey());
274
275         /* If APPC returns partial success, PolicyResult is failure exception */
276         dmaapResponse.getBody().getStatus().setCode(500);
277         dmaapResponse.getBody().getStatus().setMessage("PARTIAL SUCCESS");
278         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
279         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
280
281         /* If APPC returns partial failure, PolicyResult is failure exception */
282         dmaapResponse.getBody().getStatus().setCode(501);
283         dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
284         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
285         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
286
287         /* Test multiple partial failure codes */
288         dmaapResponse.getBody().getStatus().setCode(599);
289         dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
290         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
291         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
292
293         dmaapResponse.getBody().getStatus().setCode(550);
294         dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
295         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
296         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
297
298         /* If APPC code is unknown to Policy, PolicyResult is failure exception */
299         dmaapResponse.getBody().getStatus().setCode(700);
300         dmaapResponse.getBody().getStatus().setMessage("UNKNOWN");
301         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
302         assertEquals(PolicyResult.FAILURE_EXCEPTION, result.getKey());
303     }
304
305     /**
306      * This test ensures that that if the the source entity is also the target entity, the source
307      * will be used for the APPC request.
308      */
309     @Test
310     public void sourceIsTargetTest() {
311         String resourceId = "82194af1-3c2c-485a-8f44-420e22a9eaa4";
312         String targetVnfId = null;
313         try {
314             targetVnfId = AppcLcmActorServiceProvider.vnfNamedQuery(resourceId, "vnf01");
315         } catch (AaiException e) {
316             logger.warn(e.toString());
317             fail("no vnf-id found");
318         }
319         assertNotNull(targetVnfId);
320         assertEquals("vnf01", targetVnfId);
321     }
322
323     /**
324      * THis test exercises getters not exercised in other tests.
325      */
326     @Test
327     public void testMethods() {
328         AppcLcmActorServiceProvider sp = new AppcLcmActorServiceProvider();
329
330         assertEquals("APPC", sp.actor());
331         assertEquals(4, sp.recipes().size());
332         assertEquals("VM", sp.recipeTargets("Restart").get(0));
333         assertEquals("vm-id", sp.recipePayloads("Restart").get(0));
334     }
335 }