2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.provider.lcm.service;
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;
40 import java.text.ParseException;
41 import java.util.EnumSet;
43 public abstract class AbstractBaseService extends AbstractBaseUtils {
45 * The list of ActionIdentifier keys.<br>
46 * The extra space in the front of the keyName is for better REST API response output.
49 SERVICE_INSTANCE_ID(" service-instance-id"),
50 VF_MODULE_ID(" vf-module-id"),
52 VNFC_NAME(" vnfc-name"),
53 VSERVER_ID(" vserver-id");
57 ACTID_KEYS(String keyName) {
58 this.keyName = keyName;
67 Action expectedAction;
71 protected AbstractBaseService(){};
75 * @param theAction of the expected Action for this service
77 protected AbstractBaseService(Action theAction) {
78 expectedAction = theAction;
79 rpcName = getRpcName(theAction);
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>
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.
94 Status validateInput(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
96 Status validatedStatus = ValidationService.getInstance().validateInput(commonHeader, action, rpcName);
97 if (validatedStatus != null) {
98 return validatedStatus;
102 if (action != expectedAction) {
103 validatedStatus = buildStatusForErrorMsg(LCMCommandStatus.INVALID_INPUT_PARAMETER, "action");
104 return validatedStatus;
108 if (actionIdentifiers == null) {
109 validatedStatus = buildStatusForParamName(
110 LCMCommandStatus.MISSING_MANDATORY_PARAMETER, "action-identifiers");
113 return validatedStatus;
117 * Validate input as well as VNF ID in actionIdentifier
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.
124 Status validateVnfId(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
125 Status validatedStatus = validateInput(commonHeader, action, actionIdentifiers);
126 if (validatedStatus != null) {
127 return validatedStatus;
130 validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVnfId(), ACTID_KEYS.VNF_ID.getKeyName());
131 if (validatedStatus == null) {
132 validatedStatus = validateExcludedActIds(actionIdentifiers, EnumSet.of(ACTID_KEYS.VNF_ID));
135 return validatedStatus;
139 * Validate input as well as VF MODULE ID in actionIdentifier
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.
146 Status validateVfModuleId(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
147 Status validatedStatus = validateInput(commonHeader, action, actionIdentifiers);
148 if (validatedStatus != null) {
149 return validatedStatus;
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));
157 return validatedStatus;
161 * Validate input as well as VSERVER ID in actionIdentifier
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.
168 Status validateVserverId(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
169 Status validatedStatus = validateInput(commonHeader, action, actionIdentifiers);
170 if (validatedStatus != null) {
171 return validatedStatus;
174 validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVserverId(), ACTID_KEYS.VSERVER_ID.getKeyName());
175 if (validatedStatus == null) {
176 validatedStatus = validateExcludedActIds(actionIdentifiers, EnumSet.of(ACTID_KEYS.VSERVER_ID));
179 return validatedStatus;
183 * Validate input as well as VNFC NAME in actionIdentifier
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.
190 Status validateVnfcName(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
191 Status validatedStatus = validateInput(commonHeader, action, actionIdentifiers);
192 if (validatedStatus != null) {
193 return validatedStatus;
196 validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVnfcName(), ACTID_KEYS.VNFC_NAME.getKeyName());
197 if (validatedStatus == null) {
198 validatedStatus = validateExcludedActIds(actionIdentifiers, EnumSet.of(ACTID_KEYS.VNFC_NAME));
201 return validatedStatus;
205 * Validate input as well as VNFC NAME in actionIdentifier
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.
212 Status validateAllVnfActIds(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
213 Status validatedStatus = validateInput(commonHeader, action, actionIdentifiers);
214 if (validatedStatus != null) {
215 return validatedStatus;
218 validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVnfId(), ACTID_KEYS.VNF_ID.getKeyName());
220 if (validatedStatus != null) {
221 return validatedStatus;
224 Status validatedFinalStatus = null;
226 for (ACTID_KEYS key : ACTID_KEYS.values()) {
227 if (key.equals(ACTID_KEYS.SERVICE_INSTANCE_ID) || key.equals(ACTID_KEYS.VNF_ID)) {
230 validatedStatus = null;
233 validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVfModuleId(),
234 ACTID_KEYS.VF_MODULE_ID.getKeyName());
237 validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVserverId(),
238 ACTID_KEYS.VSERVER_ID.getKeyName());
241 validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVnfcName(),
242 ACTID_KEYS.VNFC_NAME.getKeyName());
245 if (validatedStatus == null) {
246 validatedFinalStatus = validateExcludedActIds(actionIdentifiers, EnumSet.of(ACTID_KEYS.VNF_ID, key));
251 return validatedFinalStatus;
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.
260 Status validateMustHaveParamValue(String value, String keyName) {
261 Status validatedStatus = null;
262 if (StringUtils.isEmpty(value)) {
264 validatedStatus = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, keyName);
266 validatedStatus = buildStatusForErrorMsg(LCMCommandStatus.INVALID_INPUT_PARAMETER, keyName);
269 return validatedStatus;
273 * Validate the excluded Action Identifier to ensure they do not exist.
274 * Set Status if any error occurs.
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.
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)) {
289 case SERVICE_INSTANCE_ID:
290 append = actionIdentifiers.getServiceInstanceId() != null;
293 append = actionIdentifiers.getVfModuleId() != null;
296 append = actionIdentifiers.getVserverId() != null;
299 append = actionIdentifiers.getVnfcName() != null;
302 append = actionIdentifiers.getVnfId() != null;
309 names.append(key.getKeyName()).append(DELIMITER_COMMA);
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());
320 return validatedStatus;
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.
331 RequestHandlerInput getRequestHandlerInput(CommonHeader commonHeader,
332 ActionIdentifiers actionIdentifiers,
334 String callerClassName) {
337 RequestInputBuilder requestInputBuilder = new RequestInputBuilder().requestContext()
338 .commonHeader(commonHeader)
339 .actionIdentifiers(actionIdentifiers)
340 .action(expectedAction.name())
342 if (payload != null) {
343 requestInputBuilder = requestInputBuilder.payload(payload);
345 return requestInputBuilder.build();
346 } catch (ParseException e) {
347 status = buildStatusWithParseException(e);
349 LoggingUtils.logErrorMessage(
350 LoggingConstants.TargetNames.APPC_PROVIDER,
351 String.format(COMMON_ERROR_MESSAGE_TEMPLATE, expectedAction, e.getMessage()),
358 * Execute the action through RequestExecutor
359 * @param requestHandlerInput contains everything about the action
361 RequestHandlerOutput executeAction(RequestHandlerInput requestHandlerInput) {
362 RequestHandlerOutput requestHandlerOutput = null;
363 if (requestHandlerInput == null) {
364 status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR,
365 "executeAction with null RequestHandlerInput");
367 RequestExecutor requestExecutor = new RequestExecutor();
368 requestHandlerOutput = requestExecutor.executeRequest(requestHandlerInput);
369 status = buildStatusWithDispatcherOutput(requestHandlerOutput);
371 return requestHandlerOutput;