APPC CDT Future timestamp issue fix -new HTTP req
[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-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.design.dbervices;
25
26 import static org.onap.appc.design.services.util.DesignServiceConstants.ACTION;
27 import static org.onap.appc.design.services.util.DesignServiceConstants.ACTION_LEVEL;
28 import static org.onap.appc.design.services.util.DesignServiceConstants.ARTIFACT_CONTENTS;
29 import static org.onap.appc.design.services.util.DesignServiceConstants.ARTIFACT_NAME;
30 import static org.onap.appc.design.services.util.DesignServiceConstants.ARTIFACT_TYPE;
31 import static org.onap.appc.design.services.util.DesignServiceConstants.ARTIFACT_VERSOIN;
32 import static org.onap.appc.design.services.util.DesignServiceConstants.GETARTIFACT;
33 import static org.onap.appc.design.services.util.DesignServiceConstants.GETAPPCTIMESTAMPUTC;
34 import static org.onap.appc.design.services.util.DesignServiceConstants.GETDESIGNS;
35 import static org.onap.appc.design.services.util.DesignServiceConstants.GETSTATUS;
36 import static org.onap.appc.design.services.util.DesignServiceConstants.PROTOCOL;
37 import static org.onap.appc.design.services.util.DesignServiceConstants.SETINCART;
38 import static org.onap.appc.design.services.util.DesignServiceConstants.SETPROTOCOLREFERENCE;
39 import static org.onap.appc.design.services.util.DesignServiceConstants.SETSTATUS;
40 import static org.onap.appc.design.services.util.DesignServiceConstants.STATUS;
41 import static org.onap.appc.design.services.util.DesignServiceConstants.UPLOADARTIFACT;
42 import static org.onap.appc.design.services.util.DesignServiceConstants.USER_ID;
43 import static org.onap.appc.design.services.util.DesignServiceConstants.VNF_TYPE;
44
45 import com.att.eelf.configuration.EELFLogger;
46 import com.att.eelf.configuration.EELFManager;
47 import com.fasterxml.jackson.databind.JsonNode;
48 import com.fasterxml.jackson.databind.ObjectMapper;
49 import java.io.IOException;
50
51 public class RequestValidator {
52
53     private static final EELFLogger log = EELFManager.getInstance().getLogger(RequestValidator.class);
54
55     private RequestValidator() {
56     }
57
58     public static void validate(String action, String payload) throws RequestValidationException, IOException {
59         log.info("validate: action:"  +  action );
60         log.info("validate: payload:" + payload);
61         ObjectMapper objectMapper = new ObjectMapper();
62         JsonNode payloadObject = objectMapper.readTree(payload);
63         log.info("payloadObject:" + payloadObject.get(ARTIFACT_CONTENTS));
64
65         String errorString= null;
66         switch (action) {
67             case GETDESIGNS:
68                 errorString = resolveGetDesignsErrorString(payloadObject);
69                 break;
70             case GETAPPCTIMESTAMPUTC:
71                 log.info("validate: No payload validation needed for Timestamp.");
72                 break;
73             case GETARTIFACT:
74                 errorString = resolveGetArtifactErrorString(payloadObject);
75                 break;
76             case GETSTATUS:
77                 errorString = resolveGetStatusErrorString(payloadObject);
78                 break;
79             case SETSTATUS:
80                 errorString = resolveSetStatusErrorString(payloadObject);
81                 break;
82             case UPLOADARTIFACT:
83                 errorString = resolveUploadArtifactErrorString(payloadObject);
84                 break;
85             case SETPROTOCOLREFERENCE:
86             case SETINCART:
87                 errorString = resolveErrorString(payloadObject);
88                 break;
89             default:
90                 throw new RequestValidationException(" Action " + action + " not found while processing request ");
91         }
92         checkForErrorString(errorString);
93     }
94
95     private static void checkForErrorString(String errorString) throws RequestValidationException {
96         if (errorString != null) {
97             throw new RequestValidationException(" Missing input parameter :-" + errorString + " -:");
98         }
99     }
100
101     private static String resolveGetDesignsErrorString(JsonNode payloadObject) {
102         return nullOrEmpty(payloadObject, USER_ID) ? USER_ID : null;
103     }
104
105     private static String resolveErrorString(JsonNode payloadObject) {
106         if (nullOrEmpty(payloadObject, ACTION)) {
107             return ACTION;
108         } else if (nullOrEmpty(payloadObject, ACTION_LEVEL)) {
109             return ACTION_LEVEL;
110         } else if (nullOrEmpty(payloadObject, VNF_TYPE)) {
111             return VNF_TYPE;
112         } else if (nullOrEmpty(payloadObject, PROTOCOL)) {
113             return PROTOCOL;
114         }
115         return null;
116     }
117
118     private static String resolveUploadArtifactErrorString(JsonNode payloadObject) {
119         if (nullOrEmpty(payloadObject, ARTIFACT_NAME)) {
120             return ARTIFACT_NAME;
121         } else if (doesNotContainReference(payloadObject)) {
122             return ACTION;
123         } else if (nullOrEmpty(payloadObject, ARTIFACT_VERSOIN)) {
124             return ARTIFACT_VERSOIN;
125         } else if (payloadObject.get(ARTIFACT_CONTENTS) == null) {
126             return ARTIFACT_CONTENTS;
127         } else if (nullOrEmpty(payloadObject, ARTIFACT_TYPE)) {
128             return ARTIFACT_TYPE;
129         } else if (nullOrEmpty(payloadObject, VNF_TYPE)) {
130             return VNF_TYPE;
131         }
132         return null;
133     }
134
135     private static boolean doesNotContainReference(JsonNode payloadObject) {
136         return
137             !payloadObject.get(ARTIFACT_NAME).textValue().contains("reference")
138                 && nullOrEmpty(payloadObject, ARTIFACT_NAME);
139     }
140
141     private static String resolveSetStatusErrorString(JsonNode payloadObject) {
142         if (nullOrEmpty(payloadObject, USER_ID)) {
143             return USER_ID;
144         } else if (nullOrEmpty(payloadObject, VNF_TYPE)) {
145             return VNF_TYPE;
146         } else if (nullOrEmpty(payloadObject, ACTION)) {
147             return ACTION;
148         } else if (nullOrEmpty(payloadObject, ARTIFACT_TYPE)) {
149             return ARTIFACT_TYPE;
150         } else if (nullOrEmpty(payloadObject, STATUS)) {
151             return STATUS;
152         }
153         return null;
154     }
155
156     private static String resolveGetStatusErrorString(JsonNode payloadObject) {
157         if (nullOrEmpty(payloadObject, USER_ID)) {
158             return USER_ID;
159         } else if (nullOrEmpty(payloadObject, VNF_TYPE)) {
160             return VNF_TYPE;
161         }
162         return null;
163     }
164
165     private static String resolveGetArtifactErrorString(JsonNode payloadObject) {
166         if (nullOrEmpty(payloadObject, VNF_TYPE)) {
167             return VNF_TYPE;
168         } else if (nullOrEmpty(payloadObject, ARTIFACT_TYPE)) {
169             return ARTIFACT_TYPE;
170         } else if (nullOrEmpty(payloadObject, ARTIFACT_NAME)) {
171             return ARTIFACT_NAME;
172         }
173         return null;
174     }
175
176     private static boolean nullOrEmpty(JsonNode payloadObject, String param) {
177         return payloadObject.get(param) == null || payloadObject
178             .get(param).textValue().isEmpty();
179     }
180
181 }
182
183