Update license header in appc-inbound files
[appc.git] / appc-inbound / appc-interfaces-service / bundle / src / main / java / org / onap / appc / interfaces / service / executor / RequestValidator.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.interfaces.service.executor;
25
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import org.onap.appc.interfaces.service.utils.ServiceConstants;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.fasterxml.jackson.databind.JsonNode;
34 import com.fasterxml.jackson.databind.ObjectMapper;
35
36 public class RequestValidator {
37
38     private static final Logger log = LoggerFactory.getLogger(RequestValidator.class);
39
40     public static void validate(String action, String requestData, String requestDataType) throws Exception {
41         log.debug("Received validation for action= " + action + " Data :" + requestData);
42         try {
43             if (requestData.isEmpty()) {
44                 throw new Exception("Request Data is Empty");
45             }
46             ObjectMapper objectMapper = new ObjectMapper();
47             JsonNode payloadObject = objectMapper.readTree(requestData);
48             log.info("payloadObject" + payloadObject);
49             if (payloadObject.get(ServiceConstants.VNF) == null)
50                 throw new Exception("VNF-ID is null");
51             String vnfId = payloadObject.get(ServiceConstants.VNF).toString();
52             if (vnfId.isEmpty())
53                 throw new Exception("VNF-ID is blank");
54             if (payloadObject.get(ServiceConstants.CURRENTREQUEST) == null)
55                 throw new Exception("Current request is null");
56             String cRequest = payloadObject.get(ServiceConstants.CURRENTREQUEST).toString();
57             if (cRequest.isEmpty())
58                 throw new Exception("Current Request is blank");
59             JsonNode currentRequest = payloadObject.get(ServiceConstants.CURRENTREQUEST);
60             if (currentRequest.get(ServiceConstants.ACTION) == null)
61                 throw new Exception("Action is null in Current Request");
62             String cRequestAction = currentRequest.get(ServiceConstants.ACTION).toString();
63             if (cRequestAction.isEmpty())
64                 throw new Exception("Action is blank in Current Request");
65             if (currentRequest.get(ServiceConstants.ACTIONIDENTIFIER) == null)
66                 throw new Exception("Action Identifier is null in Current Request");
67             String cRequestActionIdentifier = currentRequest.get(ServiceConstants.ACTIONIDENTIFIER).toString();
68             if (cRequestActionIdentifier.isEmpty())
69                 throw new Exception("Action Identifier is blank in Current Request");
70             } catch (Exception e) {
71             e.printStackTrace();
72             log.debug("Error while validating: " + e.getMessage());
73             throw e;
74         }
75     }
76
77 }