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