Fix Retries for Policies
[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 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.Test;
31 import org.onap.policy.appclcm.LCMCommonHeader;
32 import org.onap.policy.appclcm.LCMRequest;
33 import org.onap.policy.appclcm.LCMRequestWrapper;
34 import org.onap.policy.appclcm.LCMResponse;
35 import org.onap.policy.appclcm.LCMResponseWrapper;
36 import org.onap.policy.controlloop.ControlLoopEventStatus;
37 import org.onap.policy.controlloop.ControlLoopOperation;
38 import org.onap.policy.controlloop.ControlLoopTargetType;
39 import org.onap.policy.controlloop.VirtualControlLoopEvent;
40 import org.onap.policy.controlloop.policy.Policy;
41 import org.onap.policy.controlloop.policy.PolicyResult;
42 import org.onap.policy.controlloop.policy.Target;
43 import org.onap.policy.controlloop.policy.TargetType;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 public class AppcLcmServiceProviderTest {
48     
49     private static final Logger logger = LoggerFactory.getLogger(AppcLcmServiceProviderTest.class);
50     
51     private static VirtualControlLoopEvent onsetEvent;
52     private static ControlLoopOperation operation;
53     private static Policy policy;
54     private static LCMRequestWrapper dmaapRequest;
55     private static LCMResponseWrapper dmaapResponse;
56
57     static {
58         /* 
59          * Construct an onset with an AAI subtag containing
60          * generic-vnf.vnf-id and a target type of VM.
61          */
62         onsetEvent = new VirtualControlLoopEvent();
63         onsetEvent.closedLoopControlName = "closedLoopControlName-Test";
64         onsetEvent.requestID = UUID.randomUUID();
65         onsetEvent.closedLoopEventClient = "tca.instance00001";
66         onsetEvent.target_type = ControlLoopTargetType.VM;
67         onsetEvent.target = "generic-vnf.vnf-id";
68         onsetEvent.from = "DCAE";
69         onsetEvent.closedLoopAlarmStart = Instant.now();
70         onsetEvent.AAI = new HashMap<>();
71         onsetEvent.AAI.put("generic-vnf.vnf-id", "fw0001vm001fw001");
72         onsetEvent.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
73
74         /* Construct an operation with an APPC actor and restart operation. */
75         operation = new ControlLoopOperation();
76         operation.actor = "APPC";
77         operation.operation = "Restart";
78         operation.target = "VM";
79         operation.end = Instant.now();
80         operation.subRequestId = "1";
81         
82         /* Construct a policy specifying to restart vm. */
83         policy = new Policy();
84         policy.setName("Restart the VM");
85         policy.setDescription("Upon getting the trigger event, restart the VM");
86         policy.setActor("APPC");
87         policy.setTarget(new Target(TargetType.VM));
88         policy.setRecipe("Restart");
89         policy.setPayload(null);
90         policy.setRetry(2);
91         policy.setTimeout(300);
92
93         /* A sample DMAAP request wrapper. */
94         dmaapRequest = new LCMRequestWrapper();
95         dmaapRequest.setCorrelationId(onsetEvent.requestID.toString() + "-" + "1");
96         dmaapRequest.setRpcName(policy.getRecipe().toLowerCase());
97         dmaapRequest.setType("request");
98
99         /* A sample DMAAP response wrapper */
100         dmaapResponse = new LCMResponseWrapper();
101         dmaapResponse.setCorrelationId(onsetEvent.requestID.toString() + "-" + "1");
102         dmaapResponse.setRpcName(policy.getRecipe().toLowerCase());
103         dmaapResponse.setType("response");
104
105         /* A sample APPC LCM request. */
106         LCMRequest appcRequest = new LCMRequest();
107         
108         /* The following code constructs a sample APPC LCM Request */
109         appcRequest.setAction("restart");
110         
111         HashMap<String, String> actionIdentifiers = new HashMap<>();
112         actionIdentifiers.put("vnf-id", "trial-vnf-003");
113         
114         appcRequest.setActionIdentifiers(actionIdentifiers);
115         
116         LCMCommonHeader commonHeader = new LCMCommonHeader();
117         commonHeader.setRequestId(onsetEvent.requestID);
118         commonHeader.setSubRequestId("1");
119         commonHeader.setOriginatorId(onsetEvent.requestID.toString());
120         
121         appcRequest.setCommonHeader(commonHeader);
122         
123         appcRequest.setPayload(null);
124
125         dmaapRequest.setBody(appcRequest);
126
127         /* The following code constructs a sample APPC LCM Response */
128         LCMResponse appcResponse = new LCMResponse(appcRequest);
129         appcResponse.getStatus().setCode(400);
130         appcResponse.getStatus().setMessage("Restart Successful");
131
132         dmaapResponse.setBody(appcResponse);
133     }
134     
135     /**
136      * A test to construct an APPC LCM restart request.
137      */
138     @Test
139     public void constructRestartRequestTest() {
140         
141         LCMRequestWrapper dmaapRequest = AppcLcmActorServiceProvider.constructRequest(onsetEvent, operation, policy);
142
143         /* The service provider must return a non null DMAAP request wrapper */
144         assertNotNull(dmaapRequest);
145
146         /* The DMAAP wrapper's type field must be request */
147         assertEquals(dmaapRequest.getType(), "request");
148         
149         /* The DMAAP wrapper's body field cannot be null */
150         assertNotNull(dmaapRequest.getBody());
151
152         LCMRequest appcRequest = dmaapRequest.getBody();
153         
154         /* A common header is required and cannot be null */
155         assertNotNull(appcRequest.getCommonHeader());
156         assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.requestID);
157
158         /* An action is required and cannot be null */
159         assertNotNull(appcRequest.getAction());
160         assertEquals(appcRequest.getAction(), "Restart");
161
162         /* Action Identifiers are required and cannot be null */
163         assertNotNull(appcRequest.getActionIdentifiers());
164         assertNotNull(appcRequest.getActionIdentifiers().get("vnf-id"));
165         
166         logger.debug("APPC Request: \n" + appcRequest.toString());
167     }
168
169     /**
170      * A test to process a successful APPC restart response.
171      */
172     @Test
173     public void processRestartResponseSuccessTest() {
174         AbstractMap.SimpleEntry<PolicyResult, String> result = AppcLcmActorServiceProvider
175                 .processResponse(dmaapResponse);
176         assertEquals(result.getKey(), PolicyResult.SUCCESS);
177         assertEquals(result.getValue(), "Restart Successful");
178     }
179     
180     /**
181      * A test to map APPC response results to corresponding Policy results
182      */
183     @Test
184     public void appcToPolicyResultTest() {
185         
186         AbstractMap.SimpleEntry<PolicyResult, String> result;
187         
188         /* If APPC accepts, PolicyResult is null */
189         dmaapResponse.getBody().getStatus().setCode(100);
190         dmaapResponse.getBody().getStatus().setMessage("ACCEPTED");
191         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
192         assertEquals(result.getKey(), null);
193         
194         /* If APPC is successful, PolicyResult is success */
195         dmaapResponse.getBody().getStatus().setCode(400);
196         dmaapResponse.getBody().getStatus().setMessage("SUCCESS");
197         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
198         assertEquals(result.getKey(), PolicyResult.SUCCESS);
199         
200         /* If APPC returns an error, PolicyResult is failure exception */
201         dmaapResponse.getBody().getStatus().setCode(200);
202         dmaapResponse.getBody().getStatus().setMessage("ERROR");
203         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
204         assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
205         
206         /* If APPC rejects, PolicyResult is failure exception */
207         dmaapResponse.getBody().getStatus().setCode(300);
208         dmaapResponse.getBody().getStatus().setMessage("REJECT");
209         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
210         assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
211         
212         /* Test multiple reject codes */
213         dmaapResponse.getBody().getStatus().setCode(306);
214         dmaapResponse.getBody().getStatus().setMessage("REJECT");
215         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
216         assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
217         
218         dmaapResponse.getBody().getStatus().setCode(313);
219         dmaapResponse.getBody().getStatus().setMessage("REJECT");
220         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
221         assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
222         
223         /* If APPC returns failure, PolicyResult is failure */
224         dmaapResponse.getBody().getStatus().setCode(401);
225         dmaapResponse.getBody().getStatus().setMessage("FAILURE");
226         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
227         assertEquals(result.getKey(), PolicyResult.FAILURE);
228         
229         /* Test multiple failure codes */
230         dmaapResponse.getBody().getStatus().setCode(406);
231         dmaapResponse.getBody().getStatus().setMessage("FAILURE");
232         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
233         assertEquals(result.getKey(), PolicyResult.FAILURE);
234         
235         dmaapResponse.getBody().getStatus().setCode(450);
236         dmaapResponse.getBody().getStatus().setMessage("FAILURE");
237         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
238         assertEquals(result.getKey(), PolicyResult.FAILURE);
239         
240         /* If APPC returns partial success, PolicyResult is failure exception */
241         dmaapResponse.getBody().getStatus().setCode(500);
242         dmaapResponse.getBody().getStatus().setMessage("PARTIAL SUCCESS");
243         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
244         assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
245         
246         /* If APPC returns partial failure, PolicyResult is failure exception */
247         dmaapResponse.getBody().getStatus().setCode(501);
248         dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
249         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
250         assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
251         
252         /* Test multiple partial failure codes */
253         dmaapResponse.getBody().getStatus().setCode(599);
254         dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
255         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
256         assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
257         
258         dmaapResponse.getBody().getStatus().setCode(550);
259         dmaapResponse.getBody().getStatus().setMessage("PARTIAL FAILURE");
260         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
261         assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
262         
263         /* If APPC code is unknown to Policy, PolicyResult is failure exception */
264         dmaapResponse.getBody().getStatus().setCode(700);
265         dmaapResponse.getBody().getStatus().setMessage("UNKNOWN");
266         result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
267         assertEquals(result.getKey(), PolicyResult.FAILURE_EXCEPTION);
268     }
269     
270
271 }