Update license header in appc-provider files
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / onap / appc / provider / lcm / util / ValidationService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.provider.lcm.util;
25
26 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Action;
27 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.common.header.CommonHeader;
28 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.status.Status;
29 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.status.StatusBuilder;
30 import org.onap.appc.Constants;
31 import org.onap.appc.configuration.Configuration;
32 import org.onap.appc.configuration.ConfigurationFactory;
33 import org.onap.appc.executor.objects.LCMCommandStatus;
34 import org.onap.appc.executor.objects.Params;
35 import org.onap.appc.i18n.Msg;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38 import com.att.eelf.i18n.EELFResourceManager;
39
40 public class ValidationService {
41
42     private static class ValidationServiceHolder {
43         private static final ValidationService INSTANCE = new ValidationService();
44     }
45
46     private final EELFLogger logger = EELFManager.getInstance().getLogger(ValidationService.class);
47     private Configuration configuration = ConfigurationFactory.getConfiguration();
48
49     public static ValidationService getInstance(){
50         return ValidationServiceHolder.INSTANCE;
51     }
52
53     public Status validateInput(CommonHeader commonHeader, Action action , String rpcName) {
54         if (!isEmpty(commonHeader)
55             && !isEmpty(commonHeader.getApiVer())
56             && !isEmpty(commonHeader.getTimestamp())
57             && !isEmpty(commonHeader.getRequestId())
58             && !isEmpty(commonHeader.getOriginatorId())
59             && !isEmpty(action)) {
60             return null;
61         }
62
63         StringBuilder paramName = new StringBuilder();
64         if(isEmpty(commonHeader)){
65             paramName.append("common-header");
66         } else {
67             if (isEmpty(commonHeader.getApiVer())) {
68                 paramName.append("api-ver");
69             }
70             if (isEmpty(commonHeader.getTimestamp())) {
71                 paramName.append(isEmpty(paramName) ? "timestamp" : " , timestamp");
72             }
73             if (isEmpty(commonHeader.getRequestId())) {
74                 paramName.append(isEmpty(paramName)  ? "request-id" : " , request-id");
75             }
76             if (isEmpty(commonHeader.getOriginatorId())) {
77                 paramName.append(isEmpty(paramName)  ? "originator-id" : " , originator-id");
78             }
79         }
80         if (isEmpty(action)) {
81             paramName.append(isEmpty(paramName)  ? "action" : " , action");
82         }
83
84         logger.info("Mandatory parameter/s" + paramName.toString() + " is/are missing");
85         String reason = EELFResourceManager.format(
86             Msg.NULL_OR_INVALID_ARGUMENT,
87             configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME),
88             rpcName,
89             paramName.toString(),
90             "");
91         logger.error(reason);
92
93         LCMCommandStatus lcmCommandStatus = LCMCommandStatus.MISSING_MANDATORY_PARAMETER;
94         Params params = new Params().addParam("paramName", paramName.toString());
95
96         StatusBuilder status = new StatusBuilder();
97         status.setCode(lcmCommandStatus.getResponseCode());
98         status.setMessage(lcmCommandStatus.getFormattedMessage(params));
99         return status.build();
100     }
101
102     private boolean isEmpty(Object object) {
103         return null == object || "".equalsIgnoreCase(object.toString());
104     }
105 }