aboutsummaryrefslogtreecommitdiffstats
path: root/services/external-schema-manager/src/main/java/org/onap/dcaegen2/services/sdk/services/external/schema/manager/service/StndDefinedValidator.java
blob: 0b34339493bcf7805856cdc9981ce7614e05df15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * ============LICENSE_START=======================================================
 * DCAEGEN2-SERVICES-SDK
 * ================================================================================
 * Copyright (C) 2020 Nokia. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.dcaegen2.services.sdk.services.external.schema.manager.service;

import com.fasterxml.jackson.databind.JsonNode;
import org.onap.dcaegen2.services.sdk.services.external.schema.manager.exception.IncorrectInternalFileReferenceException;
import org.onap.dcaegen2.services.sdk.services.external.schema.manager.exception.NoLocalReferenceException;
import org.openapi4j.core.validation.ValidationException;
import org.openapi4j.schema.validator.v3.SchemaValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;

public class StndDefinedValidator {
    private static final Logger logger = LoggerFactory.getLogger(StndDefinedValidator.class);
    private final String schemaRefPath;
    private final String stndDefinedDataPath;
    private final ValidatorCache validatorCache;

    private StndDefinedValidator(String schemaRefPath, String stndDefinedDataPath, ValidatorCache validatorCache) {
        this.schemaRefPath = schemaRefPath;
        this.stndDefinedDataPath = stndDefinedDataPath;
        this.validatorCache = validatorCache;
    }

    ValidatorCache getValidatorCache() {
        return validatorCache;
    }

    /**
     * Validates incoming event
     *
     * @param event as JsonNode
     * @return validation result
     * @throws IncorrectInternalFileReferenceException when reference to part of openApi yaml file with schemas is incorrect.
     * @throws NoLocalReferenceException when mapping for public url is not present in schema mapping file.
     */
    public boolean validate(JsonNode event) throws IncorrectInternalFileReferenceException, NoLocalReferenceException {
        boolean validationResult = false;
        try {
            JsonNode stndDefinedData = JsonFragmentRetriever.getFragment(event, this.stndDefinedDataPath);
            SchemaValidator schemaValidator = validatorCache.resolveValidator(event, schemaRefPath);
            schemaValidator.validate(stndDefinedData);
            logger.info("Validation of stndDefinedDomain has been successful");
            validationResult = true;
        } catch (ValidationException ex) {
            logger.error(String.valueOf(ex.results()));
        } catch (IOException ex){
            logger.error("Schema reference has invalid characters", ex);
        }
        return validationResult;
    }

    public static final class ValidatorBuilder {

        public static final String DEFAULT_MAPPING_FILE_PATH = "etc/externalRepo/schema-map.json";
        public static final String DEFAULT_SCHEMA_REF_PATH = "/event/stndDefinedFields/schemaReference";
        public static final String DEFAULT_STND_DEFINED_DATA_PATH = "/event/stndDefinedFields/data";
        public static final String DEFAULT_SCHEMAS_PATH = "etc/externalRepo";

        private String mappingFilePath = DEFAULT_MAPPING_FILE_PATH;
        private String schemaRefPath = DEFAULT_SCHEMA_REF_PATH;
        private String stndDefinedDataPath = DEFAULT_STND_DEFINED_DATA_PATH;
        private String schemasPath = DEFAULT_SCHEMAS_PATH;

        /**
         * @param mappingFilePath relative path to the file with mappings of schemas from the context in which
         *                        the application is running.
         * @return builder reference
         * @implNote example mapping file:
         * [
         * {
         * "publicURL": "http://localhost:8080/external1",
         * "localURL": "rel-16.4/2020-07-10-3GPP_TS28532_FaultMNS.yaml"
         * }
         * ]
         * @implNote default mapping file path: "etc/externalRepo/schema-map.json"
         */
        public ValidatorBuilder mappingFilePath(String mappingFilePath) {
            this.mappingFilePath = mappingFilePath;
            return this;
        }

        /**
         * @param schemaRefPath schema reference path in json.
         * @return builder reference
         * @implNote default: "/event/stndDefinedFields/schemaReference"
         */
        public ValidatorBuilder schemaRefPath(String schemaRefPath) {
            this.schemaRefPath = schemaRefPath;
            return this;
        }

        /**
         * @param stndDefinedDataPath path to stndDefined data in json.
         * @return builder reference
         * @implNote default: "/event/stndDefinedFields/data"
         */
        public ValidatorBuilder stndDefinedDataPath(String stndDefinedDataPath) {
            this.stndDefinedDataPath = stndDefinedDataPath;
            return this;
        }

        /**
         * @param schemasPath relative path to schemas directory from the context in which the application is running.
         * @return builder reference
         * @implNote default: "etc/externalRepo"
         */
        public ValidatorBuilder schemasPath(String schemasPath) {
            this.schemasPath = new File(schemasPath).getAbsolutePath();
            return this;
        }

        /**
         * Builds stndDefined Validator. May log warnings when:
         * - schema mapping file does not exist
         * - schema mapping file has invalid format
         * - any of schema files does not exist
         * - any of schema files has invalid yaml format
         * - any of schema files is empty
         *
         * @return stndDefinedValidator with cached schemas
         */
        public StndDefinedValidator build() {
            UrlMapper urlMapper = new UrlMapperFactory().getUrlMapper(mappingFilePath, schemasPath);
            SchemaReferenceMapper schemaReferenceMapper = new SchemaReferenceMapper(urlMapper, schemasPath);
            ValidatorCache validatorCache = new ValidatorCache(schemaReferenceMapper);
            return new StndDefinedValidator(schemaRefPath, stndDefinedDataPath, validatorCache);
        }
    }
}