6657c2fddb3eb16b9efe37bfa835f083a0f0a6f0
[appc.git] /
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(), "vnf-id");
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 VSERVER 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 validateVserverId(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.getVserverId(), "vserver-id");
153         if (validatedStatus == null) {
154             validatedStatus = validateExcludedActIds(actionIdentifiers, EnumSet.of(ACTID_KEYS.VSERVER_ID));
155         }
156
157         return validatedStatus;
158     }
159
160     /**
161      * Validate a value of the must have parameter
162      * @param value   the value of the parameter
163      * @param keyName the key name of the parameter
164      * @return null if validation passed, otherwise, return Status with validation failure details.
165      */
166     Status validateMustHaveParamValue(String value, String keyName) {
167         Status validatedStatus = null;
168         if (StringUtils.isEmpty(value)) {
169             if (value == null) {
170                 validatedStatus = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, keyName);
171             } else {
172                 validatedStatus = buildStatusForErrorMsg(LCMCommandStatus.INVALID_INPUT_PARAMETER, keyName);
173             }
174         }
175         return validatedStatus;
176     }
177
178     /**
179      * Validate the excluded Action Identifier to ensure they do not exist.
180      * Set Status if any error occurs.
181      *
182      * @param actionIdentifiers of the to be validated object
183      * @param exclusionKeys of a list of ACTID_KEYS should be ignored in this validation
184      * @return null if validation passed, otherwise, return Status with validation failure details.
185      */
186     Status validateExcludedActIds(ActionIdentifiers actionIdentifiers, EnumSet<ACTID_KEYS> exclusionKeys) {
187         StringBuilder names = new StringBuilder();
188         boolean append = false;
189         for (ACTID_KEYS key : ACTID_KEYS.values()) {
190             if (exclusionKeys.contains(key)) {
191                 continue;
192             }
193
194             switch (key) {
195                 case SERVICE_INSTANCE_ID:
196                     append = actionIdentifiers.getServiceInstanceId() != null;
197                     break;
198                 case VF_MODULE_ID:
199                     append = actionIdentifiers.getVfModuleId() != null;
200                     break;
201                 case VSERVER_ID:
202                     append = actionIdentifiers.getVserverId() != null;
203                     break;
204                 case VNFC_NAME:
205                     append = actionIdentifiers.getVnfcName() != null;
206                     break;
207                 case VNF_ID:
208                     append = actionIdentifiers.getVnfId() != null;
209                     break;
210                 default:
211                     append = false;
212             }
213
214             if (append) {
215                 names.append(key.getKeyName()).append(DELIMITER_COMMA);
216             }
217         }
218
219         Status validatedStatus = null;
220         int namesLength = names.length();
221         if (namesLength != 0) {
222             names.setLength(namesLength - 1);
223             validatedStatus = buildStatusForErrorMsg(LCMCommandStatus.INVALID_INPUT_PARAMETER, names.toString());
224         }
225
226         return validatedStatus;
227     }
228
229     /**
230      * Get RequestHandlerInput
231      * @param commonHeader of the input
232      * @param actionIdentifiers of the input
233      * @param payload of the input
234      * @param callerClassName String of this.getClass().getName() of the call class
235      * @return the newly built RequestHandlerInput if no error occured, otherwise, return null.
236      */
237     RequestHandlerInput getRequestHandlerInput(CommonHeader commonHeader,
238                                                ActionIdentifiers actionIdentifiers,
239                                                Payload payload,
240                                                String callerClassName) {
241
242         try {
243             RequestInputBuilder requestInputBuilder = new RequestInputBuilder().requestContext()
244                 .commonHeader(commonHeader)
245                 .actionIdentifiers(actionIdentifiers)
246                 .action(expectedAction.name())
247                 .rpcName(rpcName);
248             if (payload != null) {
249                 requestInputBuilder = requestInputBuilder.payload(payload);
250             }
251             return requestInputBuilder.build();
252         } catch (ParseException e) {
253             status = buildStatusWithParseException(e);
254
255             LoggingUtils.logErrorMessage(
256                 LoggingConstants.TargetNames.APPC_PROVIDER,
257                 String.format(COMMON_ERROR_MESSAGE_TEMPLATE, expectedAction, e.getMessage()),
258                 callerClassName);
259         }
260         return null;
261     }
262
263     /**
264      * Execute the action through RequestExecutor
265      * @param requestHandlerInput contains everything about the action
266      */
267     RequestHandlerOutput executeAction(RequestHandlerInput requestHandlerInput) {
268         RequestHandlerOutput requestHandlerOutput = null;
269         if (requestHandlerInput == null) {
270             status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR,
271                 "executeAction with null RequestHandlerInput");
272         } else {
273             RequestExecutor requestExecutor = new RequestExecutor();
274             requestHandlerOutput = requestExecutor.executeRequest(requestHandlerInput);
275             status = buildStatusWithDispatcherOutput(requestHandlerOutput);
276         }
277         return requestHandlerOutput;
278     }
279 }