aboutsummaryrefslogtreecommitdiffstats
path: root/app-c/appc/appc-provider/appc-provider-bundle/src/main/java/org/openecomp/appc/provider/lcm/util/ValidationService.java
blob: fe2fdc1424be5f23cbfd41b45edca0d5ee247380 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : APP-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 						reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.appc.provider.lcm.util;

import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160108.Action;
import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160108.common.header.CommonHeader;
import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160108.status.Status;
import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160108.status.StatusBuilder;
import org.openecomp.appc.Constants;
import org.openecomp.appc.configuration.Configuration;
import org.openecomp.appc.configuration.ConfigurationFactory;
import org.openecomp.appc.executor.objects.LCMCommandStatus;
import org.openecomp.appc.executor.objects.Params;
import org.openecomp.appc.i18n.Msg;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.att.eelf.i18n.EELFResourceManager;

import javax.swing.*;



public class ValidationService {

    private static class ValidationServiceHolder {
        private static final ValidationService INSTANCE = new ValidationService();
    }

    private final EELFLogger logger = EELFManager.getInstance().getLogger(ValidationService.class);

    private Configuration configuration = ConfigurationFactory.getConfiguration();

    public static ValidationService getInstance(){
        return ValidationServiceHolder.INSTANCE;
    }

    public Status validateInput (CommonHeader commonHeader, Action action , String rpcName) {
        String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
        String reason ;
        StringBuilder paramName = new StringBuilder("");
        if (!isEmpty(commonHeader) && !isEmpty(commonHeader.getApiVer())&& !isEmpty(commonHeader.getTimestamp()) && !isEmpty(commonHeader.getRequestId()) && !isEmpty(action) && !isEmpty(commonHeader.getOriginatorId())){
            if(!action.toString().equalsIgnoreCase(rpcName)) logger.warn("action in input request '" + action.toString() + "' is different from endpoint '" + rpcName + "'");
            return null;
        } else{
            if(isEmpty(commonHeader)){
                paramName.append("common-header");
            }else{
                if (isEmpty(commonHeader.getApiVer())) paramName.append("api-ver");
                if (isEmpty(commonHeader.getTimestamp())) paramName.append(isEmpty(paramName) ? "timestamp" : " , timestamp" );
                if (isEmpty(commonHeader.getRequestId())) paramName.append(isEmpty(paramName)  ? "request-id" : " , request-id" );
                if (isEmpty(commonHeader.getOriginatorId())) paramName.append(isEmpty(paramName)  ? "originator-id" : " , originator-id" );
            }
            if (isEmpty(action)) paramName.append(isEmpty(paramName)  ? "action" : " , action" );
        }



        reason = EELFResourceManager.format(Msg.NULL_OR_INVALID_ARGUMENT, appName, rpcName, paramName.toString() , "");
        logger.info("Mandatory parameter/s" + paramName.toString() + " is/are missing");
        logger.error(reason);
        LCMCommandStatus lcmCommandStatus = LCMCommandStatus.MISSING_MANDATORY_PARAMETER;
        Params params = new Params().addParam("paramName", paramName.toString());
        StatusBuilder status = new StatusBuilder();
        status.setCode(lcmCommandStatus.getResponseCode());
        status.setMessage(lcmCommandStatus.getFormattedMessage(params));
        return status.build();
    }

    private boolean isEmpty (Object object){
        return (null == object || "".equalsIgnoreCase(object.toString()));
    }
}