2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.provider.lcm.service;
27 import org.apache.commons.lang.StringUtils;
28 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Action;
29 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Payload;
30 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.action.identifiers.ActionIdentifiers;
31 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.common.header.CommonHeader;
32 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.status.Status;
33 import org.onap.appc.executor.objects.LCMCommandStatus;
34 import org.onap.appc.logging.LoggingConstants;
35 import org.onap.appc.logging.LoggingUtils;
36 import org.onap.appc.provider.lcm.util.RequestInputBuilder;
37 import org.onap.appc.provider.lcm.util.ValidationService;
38 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
39 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
41 import java.text.ParseException;
42 import java.util.EnumSet;
44 public abstract class AbstractBaseService extends AbstractBaseUtils {
46 * The list of ActionIdentifier keys.<br>
47 * The extra space in the front of the keyName is for better REST API response output.
50 SERVICE_INSTANCE_ID(" service-instance-id"),
51 VF_MODULE_ID(" vf-module-id"),
53 VNFC_NAME(" vnfc-name"),
54 VSERVER_ID(" vserver-id");
58 ACTID_KEYS(String keyName) {
59 this.keyName = keyName;
68 Action expectedAction;
72 protected AbstractBaseService(){};
76 * @param theAction of the expected Action for this service
78 protected AbstractBaseService(Action theAction) {
79 expectedAction = theAction;
80 rpcName = getRpcName(theAction);
85 * Validate Input: <br>
86 * - using ValidationService to do common validation <br>
87 * - validate Action matches the expected Action <br>
88 * - validate existence of ActionIdentifier <br>
90 * @param commonHeader of the input
91 * @param action of the input
92 * @param actionIdentifiers of the input
93 * @return null if validation passed, otherwise, return Status with validation failure details.
95 Status validateInput(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
97 Status validatedStatus = ValidationService.getInstance().validateInput(commonHeader, action, rpcName);
98 if (validatedStatus != null) {
99 return validatedStatus;
103 if (action != expectedAction) {
104 validatedStatus = buildStatusForErrorMsg(LCMCommandStatus.INVALID_INPUT_PARAMETER, "action");
105 return validatedStatus;
109 if (actionIdentifiers == null) {
110 validatedStatus = buildStatusForParamName(
111 LCMCommandStatus.MISSING_MANDATORY_PARAMETER, "action-identifiers");
114 return validatedStatus;
118 * Validate input as well as VNF ID in actionIdentifier
120 * @param commonHeader of the input
121 * @param action of the input
122 * @param actionIdentifiers of the input
123 * @return null if validation passed, otherwise, return Status with validation failure details.
125 Status validateVnfId(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
126 Status validatedStatus = validateInput(commonHeader, action, actionIdentifiers);
127 if (validatedStatus != null) {
128 return validatedStatus;
131 validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVnfId(), "vnf-id");
132 if (validatedStatus == null) {
133 validatedStatus = validateExcludedActIds(actionIdentifiers, EnumSet.of(ACTID_KEYS.VNF_ID));
136 return validatedStatus;
140 * Validate input as well as VSERVER ID in actionIdentifier
142 * @param commonHeader of the input
143 * @param action of the input
144 * @param actionIdentifiers of the input
145 * @return null if validation passed, otherwise, return Status with validation failure details.
147 Status validateVserverId(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
148 Status validatedStatus = validateInput(commonHeader, action, actionIdentifiers);
149 if (validatedStatus != null) {
150 return validatedStatus;
153 validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVserverId(), "vserver-id");
154 if (validatedStatus == null) {
155 validatedStatus = validateExcludedActIds(actionIdentifiers, EnumSet.of(ACTID_KEYS.VSERVER_ID));
158 return validatedStatus;
162 * Validate a value of the must have parameter
163 * @param value the value of the parameter
164 * @param keyName the key name of the parameter
165 * @return null if validation passed, otherwise, return Status with validation failure details.
167 Status validateMustHaveParamValue(String value, String keyName) {
168 Status validatedStatus = null;
169 if (StringUtils.isEmpty(value)) {
171 validatedStatus = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, keyName);
173 validatedStatus = buildStatusForErrorMsg(LCMCommandStatus.INVALID_INPUT_PARAMETER, keyName);
176 return validatedStatus;
180 * Validate the excluded Action Identifier to ensure they do not exist.
181 * Set Status if any error occurs.
183 * @param actionIdentifiers of the to be validated object
184 * @param exclusionKeys of a list of ACTID_KEYS should be ignored in this validation
185 * @return null if validation passed, otherwise, return Status with validation failure details.
187 Status validateExcludedActIds(ActionIdentifiers actionIdentifiers, EnumSet<ACTID_KEYS> exclusionKeys) {
188 StringBuilder names = new StringBuilder();
189 boolean append = false;
190 for (ACTID_KEYS key : ACTID_KEYS.values()) {
191 if (exclusionKeys.contains(key)) {
196 case SERVICE_INSTANCE_ID:
197 append = actionIdentifiers.getServiceInstanceId() != null;
200 append = actionIdentifiers.getVfModuleId() != null;
203 append = actionIdentifiers.getVserverId() != null;
206 append = actionIdentifiers.getVnfcName() != null;
209 append = actionIdentifiers.getVnfId() != null;
216 names.append(key.getKeyName()).append(DELIMITER_COMMA);
220 Status validatedStatus = null;
221 int namesLength = names.length();
222 if (namesLength != 0) {
223 names.setLength(namesLength - 1);
224 validatedStatus = buildStatusForErrorMsg(LCMCommandStatus.INVALID_INPUT_PARAMETER, names.toString());
227 return validatedStatus;
231 * Get RequestHandlerInput
232 * @param commonHeader of the input
233 * @param actionIdentifiers of the input
234 * @param payload of the input
235 * @param callerClassName String of this.getClass().getName() of the call class
236 * @return the newly built RequestHandlerInput if no error occured, otherwise, return null.
238 RequestHandlerInput getRequestHandlerInput(CommonHeader commonHeader,
239 ActionIdentifiers actionIdentifiers,
241 String callerClassName) {
244 RequestInputBuilder requestInputBuilder = new RequestInputBuilder().requestContext()
245 .commonHeader(commonHeader)
246 .actionIdentifiers(actionIdentifiers)
247 .action(expectedAction.name())
249 if (payload != null) {
250 requestInputBuilder = requestInputBuilder.payload(payload);
252 return requestInputBuilder.build();
253 } catch (ParseException e) {
254 status = buildStatusWithParseException(e);
256 LoggingUtils.logErrorMessage(
257 LoggingConstants.TargetNames.APPC_PROVIDER,
258 String.format(COMMON_ERROR_MESSAGE_TEMPLATE, expectedAction, e.getMessage()),
265 * Execute the action through RequestExecutor
266 * @param requestHandlerInput contains everything about the action
268 RequestHandlerOutput executeAction(RequestHandlerInput requestHandlerInput) {
269 RequestHandlerOutput requestHandlerOutput = null;
270 if (requestHandlerInput == null) {
271 status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR,
272 "executeAction with null RequestHandlerInput");
274 RequestExecutor requestExecutor = new RequestExecutor();
275 requestHandlerOutput = requestExecutor.executeRequest(requestHandlerInput);
276 status = buildStatusWithDispatcherOutput(requestHandlerOutput);
278 return requestHandlerOutput;