2 * Copyright 2017 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.cli.fw.schema;
19 import com.fasterxml.jackson.databind.ObjectMapper;
20 import net.minidev.json.JSONObject;
22 import org.onap.cli.fw.error.OnapCommandInvalidSchema;
24 import static org.onap.cli.fw.conf.Constants.*;
27 import java.io.IOException;
28 import java.util.HashSet;
29 import java.util.List;
31 import java.util.Map.Entry;
36 * Schema Validation impl.
39 public class SchemaValidator extends AbstractSchemaValidate {
46 * @throws OnapCommandInvalidSchema
49 public SchemaValidator(File schemaFile) throws OnapCommandInvalidSchema {
53 public SchemaValidator(String schemaFile) throws OnapCommandInvalidSchema {
58 protected void validateSpecificSchema(SchemaType type) throws OnapCommandInvalidSchema {
59 if (type.equals(SchemaType.HTTP)) {
60 validateHttpParameters();
64 @SuppressWarnings("unchecked")
65 private void validateHttpParameters() {
67 Map<String, Object> httpMap = (Map<String, Object>) yamlMap.get(HTTP);
69 if (httpMap == null || httpMap.isEmpty()) {
70 schemaErrors.add(HTTP_SECTION_EMPTY);
74 validateMandatoryParams(httpMap, HTTP_SECTIONS, HTTP_MANDATORY_SECTIONS, PARAMETERS);
76 Map<String, Object> requestMap = (Map<String, Object>) httpMap.get(REQUEST);
78 if (requestMap != null && !requestMap.isEmpty()) {
79 validateHttpRequestParams(requestMap);
81 schemaErrors.add(SchemaValidate.emptySection(REQUEST));
84 List<Object> requestSuccessCodes = (List<Object>) httpMap.get(SUCCESS_CODES);
85 if (requestSuccessCodes != null && !requestSuccessCodes.isEmpty()) {
86 validateHttpSccessCodes(requestSuccessCodes);
88 schemaErrors.add(SchemaValidate.emptySection(SUCCESS_CODES));
91 Map<String, Object> resultMap = (Map<String, Object>) httpMap.get(RESULT_MAP);
93 if (resultMap != null && !resultMap.isEmpty()) {
94 validateHttpResultMapping(resultMap);
97 Object object = httpMap.get(SAMPLE_RESPONSE);
99 if (object instanceof String) {
100 schemaErrors.add(HTTP_SAMPLE_RESPONSE_FAILED_PARSING);
102 validateSampleResponse((Map<String, Object>) object);
107 private void validateHttpRequestParams(Map<String, Object> requestMap) {
109 if (requestMap == null || requestMap.isEmpty()) {
112 // validate mandatory parameters
113 validateMandatoryParams(requestMap, HTTP_REQUEST_PARAMS, HTTP_REQUEST_MANDATORY_PARAMS, REQUEST);
115 // Validate method types
116 String method = (String) requestMap.get(METHOD);
117 if (method != null && !method.isEmpty()) {
118 if (!HTTP_METHODS.contains(method.toLowerCase())) {
119 schemaErrors.add(SchemaValidate.invalidType(REQUEST, METHOD, HTTP_METHODS));
122 schemaErrors.add("Http request method cann't be null or empty");
125 Set<String> requestParams = getRequestParams();
127 // validate uriParams
128 Set<String> uriParams = validateHttpUri(requestMap);
131 Set<String> bodyParams = validateHttpBody(requestMap);
134 Set<String> headerParams = validateHttpHeaders(requestMap);
137 Set<String> queryParams = validateHttpQueries(requestMap);
139 for (String declaredParam : requestParams) {
140 if (!uriParams.contains(declaredParam) && !bodyParams.contains(declaredParam)
141 && !headerParams.contains(declaredParam) && !queryParams.contains(declaredParam)) {
142 schemaErrors.add(SchemaValidate.parameterNotMapped(declaredParam));
146 Set<String> totalParams = new HashSet<>();
147 totalParams.addAll(uriParams);
148 totalParams.addAll(bodyParams);
149 totalParams.addAll(queryParams);
150 totalParams.addAll(headerParams);
152 for (String definedParam : totalParams) {
153 if (!requestParams.contains(definedParam)) {
154 if (uriParams.contains(definedParam)) {
155 schemaErrors.add(SchemaValidate.invalidRequestParam(URI, definedParam));
156 } else if (bodyParams.contains(definedParam)) {
157 schemaErrors.add(SchemaValidate.invalidRequestParam(BODY, definedParam));
158 } else if (queryParams.contains(definedParam)) {
159 schemaErrors.add(SchemaValidate.invalidRequestParam(QUERIES, definedParam));
160 } else if (headerParams.contains(definedParam)) {
161 schemaErrors.add(SchemaValidate.invalidRequestParam(HEADERS, definedParam));
168 private Set<String> validateHttpUri(Map<String, Object> requestMap) {
169 Set<String> uriParamNames = new HashSet<>();
170 String uri = (String) requestMap.get(URI);
171 if (uri == null || uri.isEmpty()) {
172 schemaErrors.add(SchemaValidate.emptySection(URI));
173 return uriParamNames;
175 parseParameters(uri, uriParamNames);
176 return uriParamNames;
179 @SuppressWarnings("unchecked")
180 private Set<String> validateHttpHeaders(Map<String, Object> requestMap) {
182 Map<String, Object> headers = (Map<String, Object>) requestMap.get(HEADERS);
183 Set<String> headerParamNames = new HashSet<>();
184 if (headers != null) {
185 for (Entry<String, Object> entry : headers.entrySet()) {
186 parseParameters(String.valueOf(entry.getValue()), headerParamNames);
189 return headerParamNames;
192 @SuppressWarnings("unchecked")
193 private Set<String> validateHttpQueries(Map<String, Object> requestMap) {
194 Map<String, Object> queries = (Map<String, Object>) requestMap.get(QUERIES);
195 Set<String> queryParamNames = new HashSet<>();
196 if (queries != null) {
197 for (Entry<String, Object> entry : queries.entrySet()) {
198 parseParameters(String.valueOf(entry.getValue()), queryParamNames);
201 return queryParamNames;
204 private Set<String> validateHttpBody(Map<String, Object> requestMap) {
205 Set<String> bodyParamNames = new HashSet<>();
206 Object bodyString = requestMap.get(BODY);
207 if (bodyString == null) {
208 return bodyParamNames;
211 String body = String.valueOf(bodyString);
212 JSONObject obj = null;
214 obj = new ObjectMapper().readValue(body, JSONObject.class);
215 } catch (IOException e1) { // NOSONAR
216 schemaErrors.add(HTTP_BODY_FAILED_PARSING);
218 if (obj == null || "".equals(obj.toString())) {
219 schemaErrors.add(HTTP_BODY_JSON_EMPTY);
221 parseParameters(body, bodyParamNames);
223 return bodyParamNames;
226 private void parseParameters(String line, Set<String> paramNames) {
229 while (currentIdx < line.length()) {
230 int idxS = line.indexOf("${", currentIdx);
234 int idxE = line.indexOf("}", idxS);
235 String paramName = line.substring(idxS + 2, idxE);
236 paramNames.add(paramName.trim());
238 currentIdx = idxE + 1;
243 private void validateHttpSccessCodes(List<Object> requestSuccessCodes) {
245 for (Object successCode : requestSuccessCodes) {
246 Integer code = (Integer) successCode;
247 if (code < 200 || code >= 300) {
248 schemaErrors.add(HTTP_SUCCESS_CODE_INVALID);
254 private void validateHttpResultMapping(Map<String, Object> resultMap) {
255 Set<String> resultAttributes = getResultAttributes();
257 // Validate if all result attributes are used in the result mapping
258 for (String attribute : resultAttributes) {
259 if (!resultMap.containsKey(attribute)) {
260 schemaErrors.add(SchemaValidate.missingInResultMap(attribute));
264 // Validate if all result mapping keys are defined in the result attributes
265 for (Entry<String, Object> entry : resultMap.entrySet()) {
266 if (!resultAttributes.contains(entry.getKey())) {
267 schemaErrors.add(SchemaValidate.missingInResultAttribute(entry.getKey()));
272 private void validateSampleResponse(Map<String, Object> sampleResponseBodyMap) {
275 Object json = sampleResponseBodyMap.get(BODY);
277 schemaErrors.add(HTTP_SAMPLE_RESPONSE_EMPTY);
280 String jsonString = json.toString();
282 if (jsonString.startsWith("[")) {
283 new ObjectMapper().readValue(jsonString, JSONObject[].class);
285 new ObjectMapper().readValue(jsonString, JSONObject.class);
287 } catch (IOException e1) { // NOSONAR
288 schemaErrors.add(HTTP_SAMPLE_RESPONSE_FAILED_PARSING);