Change nexus values to properties
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / openecomp / appc / provider / lcm / util / ValidationService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.openecomp.appc.provider.lcm.util;
23
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;
37
38 import javax.swing.*;
39
40
41
42 public class ValidationService {
43
44     private static class ValidationServiceHolder {
45         private static final ValidationService INSTANCE = new ValidationService();
46     }
47
48     private final EELFLogger logger = EELFManager.getInstance().getLogger(ValidationService.class);
49
50     private Configuration configuration = ConfigurationFactory.getConfiguration();
51
52     public static ValidationService getInstance(){
53         return ValidationServiceHolder.INSTANCE;
54     }
55
56     public Status validateInput (CommonHeader commonHeader, Action action , String rpcName) {
57         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
58         String reason ;
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 + "'");
62             return null;
63         } else{
64             if(isEmpty(commonHeader)){
65                 paramName.append("common-header");
66             }else{
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" );
71             }
72             if (isEmpty(action)) paramName.append(isEmpty(paramName)  ? "action" : " , action" );
73         }
74
75
76
77         reason = EELFResourceManager.format(Msg.NULL_OR_INVALID_ARGUMENT, appName, rpcName, paramName.toString() , "");
78         logger.info("Mandatory parameter/s" + paramName.toString() + " is/are missing");
79         logger.error(reason);
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();
86     }
87
88     private boolean isEmpty (Object object){
89         return (null == object || "".equalsIgnoreCase(object.toString()));
90     }
91 }