34a0730c8e0df4562f733078e408dabac914215a
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.provider.lcm.util;
24
25 import org.opendaylight.yang.gen.v1.org.openecomp.appc.lcm.rev160108.Action;
26 import org.opendaylight.yang.gen.v1.org.openecomp.appc.lcm.rev160108.common.header.CommonHeader;
27 import org.opendaylight.yang.gen.v1.org.openecomp.appc.lcm.rev160108.status.Status;
28 import org.opendaylight.yang.gen.v1.org.openecomp.appc.lcm.rev160108.status.StatusBuilder;
29 import org.openecomp.appc.Constants;
30 import org.openecomp.appc.configuration.Configuration;
31 import org.openecomp.appc.configuration.ConfigurationFactory;
32 import org.openecomp.appc.executor.objects.LCMCommandStatus;
33 import org.openecomp.appc.executor.objects.Params;
34 import org.openecomp.appc.i18n.Msg;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import com.att.eelf.i18n.EELFResourceManager;
38
39 import javax.swing.*;
40
41
42
43 public class ValidationService {
44
45     private static class ValidationServiceHolder {
46         private static final ValidationService INSTANCE = new ValidationService();
47     }
48
49     private final EELFLogger logger = EELFManager.getInstance().getLogger(ValidationService.class);
50
51     private Configuration configuration = ConfigurationFactory.getConfiguration();
52
53     public static ValidationService getInstance(){
54         return ValidationServiceHolder.INSTANCE;
55     }
56
57     public Status validateInput (CommonHeader commonHeader, Action action , String rpcName) {
58         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
59         String reason ;
60         StringBuilder paramName = new StringBuilder("");
61         if (!isEmpty(commonHeader) && !isEmpty(commonHeader.getApiVer())&& !isEmpty(commonHeader.getTimestamp()) && !isEmpty(commonHeader.getRequestId()) && !isEmpty(action) && !isEmpty(commonHeader.getOriginatorId())){
62             if(!action.toString().equalsIgnoreCase(rpcName)) logger.warn("action in input request '" + action.toString() + "' is different from endpoint '" + rpcName + "'");
63             return null;
64         } else{
65             if(isEmpty(commonHeader)){
66                 paramName.append("common-header");
67             }else{
68                 if (isEmpty(commonHeader.getApiVer())) paramName.append("api-ver");
69                 if (isEmpty(commonHeader.getTimestamp())) paramName.append(isEmpty(paramName) ? "timestamp" : " , timestamp" );
70                 if (isEmpty(commonHeader.getRequestId())) paramName.append(isEmpty(paramName)  ? "request-id" : " , request-id" );
71                 if (isEmpty(commonHeader.getOriginatorId())) paramName.append(isEmpty(paramName)  ? "originator-id" : " , originator-id" );
72             }
73             if (isEmpty(action)) paramName.append(isEmpty(paramName)  ? "action" : " , action" );
74         }
75
76
77
78         reason = EELFResourceManager.format(Msg.NULL_OR_INVALID_ARGUMENT, appName, rpcName, paramName.toString() , "");
79         logger.info("Mandatory parameter/s" + paramName.toString() + " is/are missing");
80         logger.error(reason);
81         LCMCommandStatus lcmCommandStatus = LCMCommandStatus.MISSING_MANDATORY_PARAMETER;
82         Params params = new Params().addParam("paramName", paramName.toString());
83         StatusBuilder status = new StatusBuilder();
84         status.setCode(lcmCommandStatus.getResponseCode());
85         status.setMessage(lcmCommandStatus.getFormattedMessage(params));
86         return status.build();
87     }
88
89     private boolean isEmpty (Object object){
90         return (null == object || "".equalsIgnoreCase(object.toString()));
91     }
92 }