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(), "vnf-id");
 
 131         if (validatedStatus == null) {
 
 132             validatedStatus = validateExcludedActIds(actionIdentifiers, EnumSet.of(ACTID_KEYS.VNF_ID));
 
 135         return validatedStatus;
 
 139      * Validate input as well as VSERVER 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 validateVserverId(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
 
 147         Status validatedStatus = validateInput(commonHeader, action, actionIdentifiers);
 
 148         if (validatedStatus != null) {
 
 149             return validatedStatus;
 
 152         validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVserverId(), "vserver-id");
 
 153         if (validatedStatus == null) {
 
 154             validatedStatus = validateExcludedActIds(actionIdentifiers, EnumSet.of(ACTID_KEYS.VSERVER_ID));
 
 157         return validatedStatus;
 
 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.
 
 166     Status validateMustHaveParamValue(String value, String keyName) {
 
 167         Status validatedStatus = null;
 
 168         if (StringUtils.isEmpty(value)) {
 
 170                 validatedStatus = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, keyName);
 
 172                 validatedStatus = buildStatusForErrorMsg(LCMCommandStatus.INVALID_INPUT_PARAMETER, keyName);
 
 175         return validatedStatus;
 
 179      * Validate the excluded Action Identifier to ensure they do not exist.
 
 180      * Set Status if any error occurs.
 
 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.
 
 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)) {
 
 195                 case SERVICE_INSTANCE_ID:
 
 196                     append = actionIdentifiers.getServiceInstanceId() != null;
 
 199                     append = actionIdentifiers.getVfModuleId() != null;
 
 202                     append = actionIdentifiers.getVserverId() != null;
 
 205                     append = actionIdentifiers.getVnfcName() != null;
 
 208                     append = actionIdentifiers.getVnfId() != null;
 
 215                 names.append(key.getKeyName()).append(DELIMITER_COMMA);
 
 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());
 
 226         return validatedStatus;
 
 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.
 
 237     RequestHandlerInput getRequestHandlerInput(CommonHeader commonHeader,
 
 238                                                ActionIdentifiers actionIdentifiers,
 
 240                                                String callerClassName) {
 
 243             RequestInputBuilder requestInputBuilder = new RequestInputBuilder().requestContext()
 
 244                 .commonHeader(commonHeader)
 
 245                 .actionIdentifiers(actionIdentifiers)
 
 246                 .action(expectedAction.name())
 
 248             if (payload != null) {
 
 249                 requestInputBuilder = requestInputBuilder.payload(payload);
 
 251             return requestInputBuilder.build();
 
 252         } catch (ParseException e) {
 
 253             status = buildStatusWithParseException(e);
 
 255             LoggingUtils.logErrorMessage(
 
 256                 LoggingConstants.TargetNames.APPC_PROVIDER,
 
 257                 String.format(COMMON_ERROR_MESSAGE_TEMPLATE, expectedAction, e.getMessage()),
 
 264      * Execute the action through RequestExecutor
 
 265      * @param requestHandlerInput contains everything about the action
 
 267     RequestHandlerOutput executeAction(RequestHandlerInput requestHandlerInput) {
 
 268         RequestHandlerOutput requestHandlerOutput = null;
 
 269         if (requestHandlerInput == null) {
 
 270             status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR,
 
 271                 "executeAction with null RequestHandlerInput");
 
 273             RequestExecutor requestExecutor = new RequestExecutor();
 
 274             requestHandlerOutput = requestExecutor.executeRequest(requestHandlerInput);
 
 275             status = buildStatusWithDispatcherOutput(requestHandlerOutput);
 
 277         return requestHandlerOutput;