Fix sonar issues for APPC
[appc.git] / appc-inbound / appc-design-services / provider / src / main / java / org / onap / appc / design / dbervices / RequestValidator.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.design.dbervices;
26
27 import org.onap.appc.design.services.util.DesignServiceConstants;
28
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31 import com.fasterxml.jackson.databind.JsonNode;
32 import com.fasterxml.jackson.databind.ObjectMapper;
33
34 public class RequestValidator {
35     
36     private static final EELFLogger log = EELFManager.getInstance().getLogger(RequestValidator.class);
37     public static void validate(String action, String payload) throws Exception {
38         log.info("payload"  +  payload);
39         ObjectMapper objectMapper = new ObjectMapper();
40         JsonNode payloadObject = objectMapper.readTree(payload);
41         log.info("payloadObject"  +   payloadObject.get(DesignServiceConstants.ARTIFACT_CONTENTS));
42         
43         String errorString = null;
44         switch (action) {
45         case DesignServiceConstants.GETDESIGNS:
46             if(payloadObject.get(DesignServiceConstants.USER_ID) == null || payloadObject.get(DesignServiceConstants.USER_ID).textValue().isEmpty())
47                 errorString =     DesignServiceConstants.USER_ID;
48             break;
49         case DesignServiceConstants.GETARTIFACT:
50             if(payloadObject.get(DesignServiceConstants.VNF_TYPE) == null || payloadObject.get(DesignServiceConstants.VNF_TYPE).textValue().isEmpty())
51                 errorString =     DesignServiceConstants.VNF_TYPE;
52             else if(payloadObject.get(DesignServiceConstants.ARTIFACT_TYPE) == null || payloadObject.get(DesignServiceConstants.ARTIFACT_TYPE).textValue().isEmpty())
53                 errorString = DesignServiceConstants.ARTIFACT_TYPE;
54             else if(payloadObject.get(DesignServiceConstants.ARTIFACT_NAME) == null || payloadObject.get(DesignServiceConstants.ARTIFACT_NAME).textValue().isEmpty())
55                 errorString = DesignServiceConstants.ARTIFACT_NAME;
56             break;
57         case DesignServiceConstants.GETSTATUS:
58             if(payloadObject.get(DesignServiceConstants.USER_ID) == null || payloadObject.get(DesignServiceConstants.USER_ID).textValue().isEmpty())
59                 errorString =     DesignServiceConstants.USER_ID;
60             else if(payloadObject.get(DesignServiceConstants.VNF_TYPE) == null || payloadObject.get(DesignServiceConstants.VNF_TYPE).textValue().isEmpty())
61                 errorString =     DesignServiceConstants.VNF_TYPE;
62             break;
63         case DesignServiceConstants.SETSTATUS:
64             if(payloadObject.get(DesignServiceConstants.USER_ID) == null || payloadObject.get(DesignServiceConstants.USER_ID).textValue().isEmpty())
65                 errorString =     DesignServiceConstants.USER_ID;
66             else if(payloadObject.get(DesignServiceConstants.VNF_TYPE) == null || payloadObject.get(DesignServiceConstants.VNF_TYPE).textValue().isEmpty())
67                 errorString =     DesignServiceConstants.VNF_TYPE;
68             else if(payloadObject.get(DesignServiceConstants.ACTION) == null || payloadObject.get(DesignServiceConstants.ACTION).textValue().isEmpty())
69                 errorString =     DesignServiceConstants.ACTION;
70             else if(payloadObject.get(DesignServiceConstants.ARTIFACT_TYPE) == null || payloadObject.get(DesignServiceConstants.ARTIFACT_TYPE).textValue().isEmpty())
71                 errorString = DesignServiceConstants.ARTIFACT_TYPE;
72             else if(payloadObject.get(DesignServiceConstants.STATUS) == null || payloadObject.get(DesignServiceConstants.STATUS).textValue().isEmpty())
73                 errorString = DesignServiceConstants.STATUS;
74             break;            
75         case DesignServiceConstants.UPLOADARTIFACT:
76             if(payloadObject.get(DesignServiceConstants.ARTIFACT_NAME) == null || payloadObject.get(DesignServiceConstants.ARTIFACT_NAME).textValue().isEmpty())
77                 errorString =     DesignServiceConstants.ARTIFACT_NAME;
78             else if(! payloadObject.get(DesignServiceConstants.ARTIFACT_NAME).textValue().contains("reference")){
79                 if(payloadObject.get(DesignServiceConstants.ACTION) == null || payloadObject.get(DesignServiceConstants.ACTION).textValue().isEmpty())
80                     errorString =     DesignServiceConstants.ACTION;
81             }
82             else if(payloadObject.get(DesignServiceConstants.ARTIFACT_VERSOIN) == null || payloadObject.get(DesignServiceConstants.ARTIFACT_VERSOIN).textValue().isEmpty())
83                 errorString =     DesignServiceConstants.ARTIFACT_VERSOIN;
84             else if(payloadObject.get(DesignServiceConstants.ARTIFACT_CONTENTS) == null)
85                 errorString =     DesignServiceConstants.ARTIFACT_CONTENTS;
86             else if(payloadObject.get(DesignServiceConstants.ARTIFACT_TYPE) == null || payloadObject.get(DesignServiceConstants.ARTIFACT_TYPE).textValue().isEmpty())
87                 errorString = DesignServiceConstants.ARTIFACT_TYPE;
88             
89             else if(payloadObject.get(DesignServiceConstants.VNF_TYPE) == null || payloadObject.get(DesignServiceConstants.VNF_TYPE).textValue().isEmpty())
90                 errorString = DesignServiceConstants.VNF_TYPE;
91         
92
93             break;
94         case DesignServiceConstants.SETPROTOCOLREFERENCE:
95             if(payloadObject.get(DesignServiceConstants.ACTION) == null || payloadObject.get(DesignServiceConstants.ACTION).textValue().isEmpty())
96                 errorString =     DesignServiceConstants.ACTION;
97             else if(payloadObject.get(DesignServiceConstants.ACTION_LEVEL) == null || payloadObject.get(DesignServiceConstants.ACTION_LEVEL).textValue().isEmpty())
98                 errorString =     DesignServiceConstants.ACTION_LEVEL;
99             else if(payloadObject.get(DesignServiceConstants.VNF_TYPE) == null || payloadObject.get(DesignServiceConstants.VNF_TYPE).textValue().isEmpty())
100                 errorString =     DesignServiceConstants.VNF_TYPE;
101             else if(payloadObject.get(DesignServiceConstants.PROTOCOL) == null || payloadObject.get(DesignServiceConstants.PROTOCOL).textValue().isEmpty())
102                 errorString =     DesignServiceConstants.PROTOCOL;
103             
104         case DesignServiceConstants.SETINCART:
105             if(payloadObject.get(DesignServiceConstants.ACTION) == null || payloadObject.get(DesignServiceConstants.ACTION).textValue().isEmpty())
106                 errorString =     DesignServiceConstants.ACTION;
107             else if(payloadObject.get(DesignServiceConstants.ACTION_LEVEL) == null || payloadObject.get(DesignServiceConstants.ACTION_LEVEL).textValue().isEmpty())
108                 errorString =     DesignServiceConstants.ACTION_LEVEL;
109             else if(payloadObject.get(DesignServiceConstants.VNF_TYPE) == null || payloadObject.get(DesignServiceConstants.VNF_TYPE).textValue().isEmpty())
110                 errorString =     DesignServiceConstants.VNF_TYPE;
111             else if(payloadObject.get(DesignServiceConstants.PROTOCOL) == null || payloadObject.get(DesignServiceConstants.PROTOCOL).textValue().isEmpty())
112                 errorString =     DesignServiceConstants.PROTOCOL;            
113             break;
114         default: 
115             throw new Exception(" Action " + action + " not found while processing request ");            
116
117         }
118         if(errorString != null)
119             throw new Exception(" Missing input parameter :-" + errorString + " -:");
120
121     }
122
123 }
124
125