2 * ============LICENSE_START=======================================================
3 * DCAEGEN2-SERVICES-SDK
4 * ================================================================================
5 * Copyright (C) 2020 Nokia. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dcaegen2.services.sdk.services.external.schema.manager.service;
23 import com.fasterxml.jackson.databind.JsonNode;
24 import org.onap.dcaegen2.services.sdk.services.external.schema.manager.exception.IncorrectInternalFileReferenceException;
25 import org.onap.dcaegen2.services.sdk.services.external.schema.manager.exception.NoLocalReferenceException;
26 import org.openapi4j.core.validation.ValidationException;
27 import org.openapi4j.schema.validator.v3.SchemaValidator;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 import java.io.IOException;
34 public class StndDefinedValidator {
35 private static final Logger logger = LoggerFactory.getLogger(StndDefinedValidator.class);
36 private final String schemaRefPath;
37 private final String stndDefinedDataPath;
38 private final ValidatorCache validatorCache;
40 private StndDefinedValidator(String schemaRefPath, String stndDefinedDataPath, ValidatorCache validatorCache) {
41 this.schemaRefPath = schemaRefPath;
42 this.stndDefinedDataPath = stndDefinedDataPath;
43 this.validatorCache = validatorCache;
46 ValidatorCache getValidatorCache() {
47 return validatorCache;
51 * Validates incoming event
53 * @param event as JsonNode
54 * @return validation result
55 * @throws IncorrectInternalFileReferenceException when reference to part of openApi yaml file with schemas is incorrect.
56 * @throws NoLocalReferenceException when mapping for public url is not present in schema mapping file.
58 public boolean validate(JsonNode event) throws IncorrectInternalFileReferenceException, NoLocalReferenceException {
59 boolean validationResult = false;
61 JsonNode stndDefinedData = JsonFragmentRetriever.getFragment(event, this.stndDefinedDataPath);
62 SchemaValidator schemaValidator = validatorCache.resolveValidator(event, schemaRefPath);
63 schemaValidator.validate(stndDefinedData);
64 logger.info("Validation of stndDefinedDomain has been successful");
65 validationResult = true;
66 } catch (ValidationException ex) {
67 logger.error(String.valueOf(ex.results()));
68 } catch (IOException ex) {
69 logger.error("Schema reference has invalid characters", ex);
71 return validationResult;
74 public static final class ValidatorBuilder {
76 public static final String DEFAULT_MAPPING_FILE_PATH = "etc/externalRepo/schema-map.json";
77 public static final String DEFAULT_SCHEMA_REF_PATH = "/event/stndDefinedFields/schemaReference";
78 public static final String DEFAULT_STND_DEFINED_DATA_PATH = "/event/stndDefinedFields/data";
79 public static final String DEFAULT_SCHEMAS_PATH = "etc/externalRepo";
81 private String mappingFilePath = DEFAULT_MAPPING_FILE_PATH;
82 private String schemaRefPath = DEFAULT_SCHEMA_REF_PATH;
83 private String stndDefinedDataPath = DEFAULT_STND_DEFINED_DATA_PATH;
84 private String schemasPath = DEFAULT_SCHEMAS_PATH;
87 * @param mappingFilePath relative path to the file with mappings of schemas from the context in which
88 * the application is running.
89 * @return builder reference
90 * @implNote example mapping file:
93 * "publicURL": "http://localhost:8080/external1",
94 * "localURL": "rel-16.4/2020-07-10-3GPP_TS28532_FaultMNS.yaml"
97 * @implNote default mapping file path: "etc/externalRepo/schema-map.json"
99 public ValidatorBuilder mappingFilePath(String mappingFilePath) {
100 this.mappingFilePath = mappingFilePath;
105 * @param schemaRefPath schema reference path in json.
106 * @return builder reference
107 * @implNote default: "/event/stndDefinedFields/schemaReference"
109 public ValidatorBuilder schemaRefPath(String schemaRefPath) {
110 this.schemaRefPath = schemaRefPath;
115 * @param stndDefinedDataPath path to stndDefined data in json.
116 * @return builder reference
117 * @implNote default: "/event/stndDefinedFields/data"
119 public ValidatorBuilder stndDefinedDataPath(String stndDefinedDataPath) {
120 this.stndDefinedDataPath = stndDefinedDataPath;
125 * @param schemasPath relative path to schemas directory from the context in which the application is running.
126 * @return builder reference
127 * @implNote default: "etc/externalRepo"
129 public ValidatorBuilder schemasPath(String schemasPath) {
130 this.schemasPath = new File(schemasPath).getAbsolutePath();
135 * Builds stndDefined Validator. May log warnings when:
136 * - schema mapping file does not exist
137 * - schema mapping file has invalid format
138 * - any of schema files does not exist
139 * - any of schema files has invalid yaml format
140 * - any of schema files is empty
142 * @return stndDefinedValidator with cached schemas
144 public StndDefinedValidator build() {
145 UrlMapper urlMapper = new UrlMapperFactory().getUrlMapper(mappingFilePath, schemasPath);
146 SchemaReferenceMapper schemaReferenceMapper = new SchemaReferenceMapper(urlMapper, schemasPath);
147 ValidatorCache validatorCache = new ValidatorCache(schemaReferenceMapper);
148 return new StndDefinedValidator(schemaRefPath, stndDefinedDataPath, validatorCache);