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.openecomp.appc.provider.lcm.util;
27 import org.opendaylight.yang.gen.v1.org.openecomp.appc.lcm.rev160108.Action;
28 import org.opendaylight.yang.gen.v1.org.openecomp.appc.lcm.rev160108.common.header.CommonHeader;
29 import org.opendaylight.yang.gen.v1.org.openecomp.appc.lcm.rev160108.status.Status;
30 import org.opendaylight.yang.gen.v1.org.openecomp.appc.lcm.rev160108.status.StatusBuilder;
31 import org.openecomp.appc.Constants;
32 import org.openecomp.appc.configuration.Configuration;
33 import org.openecomp.appc.configuration.ConfigurationFactory;
34 import org.openecomp.appc.executor.objects.LCMCommandStatus;
35 import org.openecomp.appc.executor.objects.Params;
36 import org.openecomp.appc.i18n.Msg;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39 import com.att.eelf.i18n.EELFResourceManager;
45 public class ValidationService {
47 private static class ValidationServiceHolder {
48 private static final ValidationService INSTANCE = new ValidationService();
51 private final EELFLogger logger = EELFManager.getInstance().getLogger(ValidationService.class);
53 private Configuration configuration = ConfigurationFactory.getConfiguration();
55 public static ValidationService getInstance(){
56 return ValidationServiceHolder.INSTANCE;
59 public Status validateInput (CommonHeader commonHeader, Action action , String rpcName) {
60 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
62 StringBuilder paramName = new StringBuilder("");
63 if (!isEmpty(commonHeader) && !isEmpty(commonHeader.getApiVer())&& !isEmpty(commonHeader.getTimestamp()) && !isEmpty(commonHeader.getRequestId()) && !isEmpty(action) && !isEmpty(commonHeader.getOriginatorId())){
64 if(!action.toString().equalsIgnoreCase(rpcName)) logger.warn("action in input request '" + action.toString() + "' is different from endpoint '" + rpcName + "'");
67 if(isEmpty(commonHeader)){
68 paramName.append("common-header");
70 if (isEmpty(commonHeader.getApiVer())) paramName.append("api-ver");
71 if (isEmpty(commonHeader.getTimestamp())) paramName.append(isEmpty(paramName) ? "timestamp" : " , timestamp" );
72 if (isEmpty(commonHeader.getRequestId())) paramName.append(isEmpty(paramName) ? "request-id" : " , request-id" );
73 if (isEmpty(commonHeader.getOriginatorId())) paramName.append(isEmpty(paramName) ? "originator-id" : " , originator-id" );
75 if (isEmpty(action)) paramName.append(isEmpty(paramName) ? "action" : " , action" );
80 reason = EELFResourceManager.format(Msg.NULL_OR_INVALID_ARGUMENT, appName, rpcName, paramName.toString() , "");
81 logger.info("Mandatory parameter/s" + paramName.toString() + " is/are missing");
83 LCMCommandStatus lcmCommandStatus = LCMCommandStatus.MISSING_MANDATORY_PARAMETER;
84 Params params = new Params().addParam("paramName", paramName.toString());
85 StatusBuilder status = new StatusBuilder();
86 status.setCode(lcmCommandStatus.getResponseCode());
87 status.setMessage(lcmCommandStatus.getFormattedMessage(params));
88 return status.build();
91 private boolean isEmpty (Object object){
92 return (null == object || "".equalsIgnoreCase(object.toString()));