e67f0ebcd193b76f59b02afff413c21302409c48
[dcaegen2/services/sdk.git] /
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dcaegen2.services.sdk.services.external.schema.manager.service;
22
23 import com.fasterxml.jackson.databind.JsonNode;
24 import org.onap.dcaegen2.services.sdk.services.external.schema.manager.model.SchemaReference;
25 import org.onap.dcaegen2.services.sdk.services.external.schema.manager.exception.IncorrectInternalFileReferenceException;
26 import org.openapi4j.core.exception.ResolutionException;
27 import org.openapi4j.schema.validator.v3.SchemaValidator;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import java.io.IOException;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 class ValidatorCache {
36
37     private static final Logger logger = LoggerFactory.getLogger(ValidatorCache.class);
38     private final SchemaReferenceMapper schemaReferenceMapper;
39     private final Map<String, SchemaValidator> cache;
40
41     ValidatorCache(SchemaReferenceMapper schemaReferenceMapper) {
42         this.schemaReferenceMapper = schemaReferenceMapper;
43         this.cache = new HashMap<>();
44     }
45
46     SchemaReferenceMapper getSchemaReferenceMapper() {
47         return schemaReferenceMapper;
48     }
49
50     synchronized SchemaValidator resolveValidator(JsonNode event, String schemaRefPath) throws IOException {
51         SchemaReference schemaReference = resolveSchemaReference(event, schemaRefPath);
52         schemaReference = schemaReferenceMapper.mapToLocalSchema(schemaReference);
53         SchemaValidator validator = cache.get(schemaReference.getUrl());
54         if (!isValidatorCached(validator)) {
55             validator = createNewValidator(schemaReference);
56         }
57         return validator;
58     }
59
60     private SchemaReference resolveSchemaReference(JsonNode event, String schemaRefPath) {
61         String publicSchemaReference = JsonFragmentRetriever.getFragment(event, schemaRefPath).asText();
62         SchemaReferenceResolver schemaReferenceResolver = new SchemaReferenceResolver(publicSchemaReference);
63         return new SchemaReference(schemaReferenceResolver);
64     }
65
66     private boolean isValidatorCached(SchemaValidator validator) {
67         return validator != null;
68     }
69
70     private SchemaValidator createNewValidator(SchemaReference schemaReference) throws IOException {
71         logger.info("Creating new stndDefined schema validator");
72         JsonNode schemaRefNode = SchemaReferenceJsonGenerator.getSchemaReferenceJson(schemaReference);
73         SchemaValidator schemaValidator = handleValidatorCreation(schemaRefNode);
74         cacheSchemaValidator(schemaReference.getFullSchemaReference(), schemaValidator);
75         return schemaValidator;
76     }
77
78     private SchemaValidator handleValidatorCreation(JsonNode schemaNode) {
79         try {
80             return new SchemaValidator("StndDefinedSchemaValidator", schemaNode);
81         } catch (ResolutionException e) {
82             throw new IncorrectInternalFileReferenceException("Schema reference refer to existing file, " +
83                     "but internal reference (after #) is incorrect. " + e.getMessage());
84         }
85     }
86
87     private void cacheSchemaValidator(String localSchemaReference, SchemaValidator schemaValidator) {
88         logger.info("Adding new stndDefined schema validator to cache");
89         cache.put(localSchemaReference, schemaValidator);
90     }
91 }