2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
 
   7  * ================================================================================
 
   8  * Licensed under the Apache License, Version 2.0 (the "License");
 
   9  * you may not use this file except in compliance with the License.
 
  10  * You may obtain a copy of the License at
 
  12  *      http://www.apache.org/licenses/LICENSE-2.0
 
  14  * Unless required by applicable law or agreed to in writing, software
 
  15  * distributed under the License is distributed on an "AS IS" BASIS,
 
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  17  * See the License for the specific language governing permissions and
 
  18  * limitations under the License.
 
  19  * ============LICENSE_END=========================================================
 
  22 package org.openecomp.appc.provider.lcm.util;
 
  24 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160108.Action;
 
  25 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160108.common.header.CommonHeader;
 
  26 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160108.status.Status;
 
  27 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160108.status.StatusBuilder;
 
  28 import org.openecomp.appc.Constants;
 
  29 import org.openecomp.appc.configuration.Configuration;
 
  30 import org.openecomp.appc.configuration.ConfigurationFactory;
 
  31 import org.openecomp.appc.executor.objects.LCMCommandStatus;
 
  32 import org.openecomp.appc.executor.objects.Params;
 
  33 import org.openecomp.appc.i18n.Msg;
 
  34 import com.att.eelf.configuration.EELFLogger;
 
  35 import com.att.eelf.configuration.EELFManager;
 
  36 import com.att.eelf.i18n.EELFResourceManager;
 
  42 public class ValidationService {
 
  44     private static class ValidationServiceHolder {
 
  45         private static final ValidationService INSTANCE = new ValidationService();
 
  48     private final EELFLogger logger = EELFManager.getInstance().getLogger(ValidationService.class);
 
  50     private Configuration configuration = ConfigurationFactory.getConfiguration();
 
  52     public static ValidationService getInstance(){
 
  53         return ValidationServiceHolder.INSTANCE;
 
  56     public Status validateInput (CommonHeader commonHeader, Action action , String rpcName) {
 
  57         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
 
  59         StringBuilder paramName = new StringBuilder("");
 
  60         if (!isEmpty(commonHeader) && !isEmpty(commonHeader.getApiVer())&& !isEmpty(commonHeader.getTimestamp()) && !isEmpty(commonHeader.getRequestId()) && !isEmpty(action) && !isEmpty(commonHeader.getOriginatorId())){
 
  61             if(!action.toString().equalsIgnoreCase(rpcName)) logger.warn("action in input request '" + action.toString() + "' is different from endpoint '" + rpcName + "'");
 
  64             if(isEmpty(commonHeader)){
 
  65                 paramName.append("common-header");
 
  67                 if (isEmpty(commonHeader.getApiVer())) paramName.append("api-ver");
 
  68                 if (isEmpty(commonHeader.getTimestamp())) paramName.append(isEmpty(paramName) ? "timestamp" : " , timestamp" );
 
  69                 if (isEmpty(commonHeader.getRequestId())) paramName.append(isEmpty(paramName)  ? "request-id" : " , request-id" );
 
  70                 if (isEmpty(commonHeader.getOriginatorId())) paramName.append(isEmpty(paramName)  ? "originator-id" : " , originator-id" );
 
  72             if (isEmpty(action)) paramName.append(isEmpty(paramName)  ? "action" : " , action" );
 
  77         reason = EELFResourceManager.format(Msg.NULL_OR_INVALID_ARGUMENT, appName, rpcName, paramName.toString() , "");
 
  78         logger.info("Mandatory parameter/s" + paramName.toString() + " is/are missing");
 
  80         LCMCommandStatus lcmCommandStatus = LCMCommandStatus.MISSING_MANDATORY_PARAMETER;
 
  81         Params params = new Params().addParam("paramName", paramName.toString());
 
  82         StatusBuilder status = new StatusBuilder();
 
  83         status.setCode(lcmCommandStatus.getResponseCode());
 
  84         status.setMessage(lcmCommandStatus.getFormattedMessage(params));
 
  85         return status.build();
 
  88     private boolean isEmpty (Object object){
 
  89         return (null == object || "".equalsIgnoreCase(object.toString()));