VNFC Support for Ansible actions
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / onap / appc / provider / lcm / service / AbstractBaseService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.provider.lcm.service;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Action;
28 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Payload;
29 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.action.identifiers.ActionIdentifiers;
30 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.common.header.CommonHeader;
31 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.status.Status;
32 import org.onap.appc.executor.objects.LCMCommandStatus;
33 import org.onap.appc.logging.LoggingConstants;
34 import org.onap.appc.logging.LoggingUtils;
35 import org.onap.appc.provider.lcm.util.RequestInputBuilder;
36 import org.onap.appc.provider.lcm.util.ValidationService;
37 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
38 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
39
40 import java.text.ParseException;
41 import java.util.EnumSet;
42
43 public abstract class AbstractBaseService extends AbstractBaseUtils {
44     /**
45      * The list of ActionIdentifier keys.<br>
46      * The extra space in the front of the keyName is for better REST API response output.
47      */
48     enum ACTID_KEYS {
49         SERVICE_INSTANCE_ID(" service-instance-id"),
50         VF_MODULE_ID(" vf-module-id"),
51         VNF_ID(" vnf-id"),
52         VNFC_NAME(" vnfc-name"),
53         VSERVER_ID(" vserver-id");
54
55         String keyName;
56
57         ACTID_KEYS(String keyName) {
58             this.keyName = keyName;
59         }
60
61         String getKeyName() {
62             return keyName;
63         }
64     }
65
66       String rpcName;
67       Action expectedAction;
68
69     Status status;
70
71     protected AbstractBaseService(){};
72     /**
73      * Constructor
74      *
75      * @param theAction of the expected Action for this service
76      */
77     protected AbstractBaseService(Action theAction) {
78         expectedAction = theAction;
79         rpcName = getRpcName(theAction);
80     }
81
82
83     /**
84      * Validate Input: <br>
85      *    - using ValidationService to do common validation <br>
86      *    - validate Action matches the expected Action <br>
87      *    - validate existence of ActionIdentifier <br>
88      *
89      * @param commonHeader      of the input
90      * @param action            of the input
91      * @param actionIdentifiers of the input
92      * @return null if validation passed, otherwise, return Status with validation failure details.
93      */
94     Status validateInput(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
95         // common validation
96         Status validatedStatus = ValidationService.getInstance().validateInput(commonHeader, action, rpcName);
97         if (validatedStatus != null) {
98             return validatedStatus;
99         }
100
101         // action validation
102         if (action != expectedAction) {
103             validatedStatus = buildStatusForErrorMsg(LCMCommandStatus.INVALID_INPUT_PARAMETER, "action");
104             return validatedStatus;
105         }
106
107         // action identifier
108         if (actionIdentifiers == null) {
109             validatedStatus = buildStatusForParamName(
110                 LCMCommandStatus.MISSING_MANDATORY_PARAMETER, "action-identifiers");
111         }
112
113         return validatedStatus;
114     }
115
116     /**
117      * Validate input as well as VNF ID in actionIdentifier
118      *
119      * @param commonHeader      of the input
120      * @param action            of the input
121      * @param actionIdentifiers of the input
122      * @return null if validation passed, otherwise, return Status with validation failure details.
123      */
124     Status validateVnfId(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
125         Status validatedStatus = validateInput(commonHeader, action, actionIdentifiers);
126         if (validatedStatus != null) {
127             return validatedStatus;
128         }
129
130         validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVnfId(), ACTID_KEYS.VNF_ID.getKeyName());
131         if (validatedStatus == null) {
132             validatedStatus = validateExcludedActIds(actionIdentifiers, EnumSet.of(ACTID_KEYS.VNF_ID));
133         }
134
135         return validatedStatus;
136     }
137
138     /**
139      * Validate input as well as VF MODULE ID in actionIdentifier
140      *
141      * @param commonHeader      of the input
142      * @param action            of the input
143      * @param actionIdentifiers of the input
144      * @return null if validation passed, otherwise, return Status with validation failure details.
145      */
146     Status validateVfModuleId(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
147         Status validatedStatus = validateInput(commonHeader, action, actionIdentifiers);
148         if (validatedStatus != null) {
149             return validatedStatus;
150         }
151
152         validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVfModuleId(), ACTID_KEYS.VF_MODULE_ID.getKeyName());
153         if (validatedStatus == null) {
154             validatedStatus = validateExcludedActIds(actionIdentifiers, EnumSet.of(ACTID_KEYS.VF_MODULE_ID));
155         }
156
157         return validatedStatus;
158     }
159
160     /**
161      * Validate input as well as VSERVER ID in actionIdentifier
162      *
163      * @param commonHeader      of the input
164      * @param action            of the input
165      * @param actionIdentifiers of the input
166      * @return null if validation passed, otherwise, return Status with validation failure details.
167      */
168     Status validateVserverId(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
169         Status validatedStatus = validateInput(commonHeader, action, actionIdentifiers);
170         if (validatedStatus != null) {
171             return validatedStatus;
172         }
173
174         validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVserverId(), ACTID_KEYS.VSERVER_ID.getKeyName());
175         if (validatedStatus == null) {
176             validatedStatus = validateExcludedActIds(actionIdentifiers, EnumSet.of(ACTID_KEYS.VSERVER_ID));
177         }
178
179         return validatedStatus;
180     }
181
182     /**
183      * Validate input as well as VNFC NAME in actionIdentifier
184      *
185      * @param commonHeader      of the input
186      * @param action            of the input
187      * @param actionIdentifiers of the input
188      * @return null if validation passed, otherwise, return Status with validation failure details.
189      */
190     Status validateVnfcName(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
191         Status validatedStatus = validateInput(commonHeader, action, actionIdentifiers);
192         if (validatedStatus != null) {
193             return validatedStatus;
194         }
195
196         validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVnfcName(), ACTID_KEYS.VNFC_NAME.getKeyName());
197         if (validatedStatus == null) {
198             validatedStatus = validateExcludedActIds(actionIdentifiers, EnumSet.of(ACTID_KEYS.VNFC_NAME));
199         }
200
201         return validatedStatus;
202     }
203
204     /**
205      * Validate input as well as VNFC NAME in actionIdentifier
206      *
207      * @param commonHeader      of the input
208      * @param action            of the input
209      * @param actionIdentifiers of the input
210      * @return null if validation passed, otherwise, return Status with validation failure details.
211      */
212     Status validateAllVnfActIds(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
213         Status validatedStatus = validateInput(commonHeader, action, actionIdentifiers);
214         if (validatedStatus != null) {
215             return validatedStatus;
216         }
217
218         validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVnfId(), ACTID_KEYS.VNF_ID.getKeyName());
219
220         if (validatedStatus != null) {
221             return validatedStatus;
222         }
223
224         Status validatedFinalStatus = null;
225
226         for (ACTID_KEYS key : ACTID_KEYS.values()) {
227             if (key.equals(ACTID_KEYS.SERVICE_INSTANCE_ID) || key.equals(ACTID_KEYS.VNF_ID)) {
228                 continue;
229             }
230             validatedStatus = null;
231             switch (key) {
232                 case VF_MODULE_ID:
233                     validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVfModuleId(),
234                             ACTID_KEYS.VF_MODULE_ID.getKeyName());
235                     break;
236                 case VSERVER_ID:
237                     validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVserverId(),
238                             ACTID_KEYS.VSERVER_ID.getKeyName());
239                     break;
240                 case VNFC_NAME:
241                     validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVnfcName(),
242                             ACTID_KEYS.VNFC_NAME.getKeyName());
243                     break;
244             }
245             if (validatedStatus == null) {
246                 validatedFinalStatus = validateExcludedActIds(actionIdentifiers, EnumSet.of(ACTID_KEYS.VNF_ID, key));
247                 break;
248             }
249         }
250
251         return validatedFinalStatus;
252     }
253
254     /**
255      * Validate a value of the must have parameter
256      * @param value   the value of the parameter
257      * @param keyName the key name of the parameter
258      * @return null if validation passed, otherwise, return Status with validation failure details.
259      */
260     Status validateMustHaveParamValue(String value, String keyName) {
261         Status validatedStatus = null;
262         if (StringUtils.isEmpty(value)) {
263             if (value == null) {
264                 validatedStatus = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, keyName);
265             } else {
266                 validatedStatus = buildStatusForErrorMsg(LCMCommandStatus.INVALID_INPUT_PARAMETER, keyName);
267             }
268         }
269         return validatedStatus;
270     }
271
272     /**
273      * Validate the excluded Action Identifier to ensure they do not exist.
274      * Set Status if any error occurs.
275      *
276      * @param actionIdentifiers of the to be validated object
277      * @param exclusionKeys of a list of ACTID_KEYS should be ignored in this validation
278      * @return null if validation passed, otherwise, return Status with validation failure details.
279      */
280     Status validateExcludedActIds(ActionIdentifiers actionIdentifiers, EnumSet<ACTID_KEYS> exclusionKeys) {
281         StringBuilder names = new StringBuilder();
282         boolean append = false;
283         for (ACTID_KEYS key : ACTID_KEYS.values()) {
284             if (exclusionKeys.contains(key)) {
285                 continue;
286             }
287
288             switch (key) {
289                 case SERVICE_INSTANCE_ID:
290                     append = actionIdentifiers.getServiceInstanceId() != null;
291                     break;
292                 case VF_MODULE_ID:
293                     append = actionIdentifiers.getVfModuleId() != null;
294                     break;
295                 case VSERVER_ID:
296                     append = actionIdentifiers.getVserverId() != null;
297                     break;
298                 case VNFC_NAME:
299                     append = actionIdentifiers.getVnfcName() != null;
300                     break;
301                 case VNF_ID:
302                     append = actionIdentifiers.getVnfId() != null;
303                     break;
304                 default:
305                     append = false;
306             }
307
308             if (append) {
309                 names.append(key.getKeyName()).append(DELIMITER_COMMA);
310             }
311         }
312
313         Status validatedStatus = null;
314         int namesLength = names.length();
315         if (namesLength != 0) {
316             names.setLength(namesLength - 1);
317             validatedStatus = buildStatusForErrorMsg(LCMCommandStatus.INVALID_INPUT_PARAMETER, names.toString());
318         }
319
320         return validatedStatus;
321     }
322
323     /**
324      * Get RequestHandlerInput
325      * @param commonHeader of the input
326      * @param actionIdentifiers of the input
327      * @param payload of the input
328      * @param callerClassName String of this.getClass().getName() of the call class
329      * @return the newly built RequestHandlerInput if no error occured, otherwise, return null.
330      */
331     RequestHandlerInput getRequestHandlerInput(CommonHeader commonHeader,
332                                                ActionIdentifiers actionIdentifiers,
333                                                Payload payload,
334                                                String callerClassName) {
335
336         try {
337             RequestInputBuilder requestInputBuilder = new RequestInputBuilder().requestContext()
338                 .commonHeader(commonHeader)
339                 .actionIdentifiers(actionIdentifiers)
340                 .action(expectedAction.name())
341                 .rpcName(rpcName);
342             if (payload != null) {
343                 requestInputBuilder = requestInputBuilder.payload(payload);
344             }
345             return requestInputBuilder.build();
346         } catch (ParseException e) {
347             status = buildStatusWithParseException(e);
348
349             LoggingUtils.logErrorMessage(
350                 LoggingConstants.TargetNames.APPC_PROVIDER,
351                 String.format(COMMON_ERROR_MESSAGE_TEMPLATE, expectedAction, e.getMessage()),
352                 callerClassName);
353         }
354         return null;
355     }
356
357     /**
358      * Execute the action through RequestExecutor
359      * @param requestHandlerInput contains everything about the action
360      */
361     RequestHandlerOutput executeAction(RequestHandlerInput requestHandlerInput) {
362         RequestHandlerOutput requestHandlerOutput = null;
363         if (requestHandlerInput == null) {
364             status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR,
365                 "executeAction with null RequestHandlerInput");
366         } else {
367             RequestExecutor requestExecutor = new RequestExecutor();
368             requestHandlerOutput = requestExecutor.executeRequest(requestHandlerInput);
369             status = buildStatusWithDispatcherOutput(requestHandlerOutput);
370         }
371         return requestHandlerOutput;
372     }
373 }