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.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;
31 import java.io.IOException;
32 import java.util.HashMap;
35 class ValidatorCache {
37 private static final Logger logger = LoggerFactory.getLogger(ValidatorCache.class);
38 private final SchemaReferenceMapper schemaReferenceMapper;
39 private final Map<String, SchemaValidator> cache;
41 ValidatorCache(SchemaReferenceMapper schemaReferenceMapper) {
42 this.schemaReferenceMapper = schemaReferenceMapper;
43 this.cache = new HashMap<>();
46 SchemaReferenceMapper getSchemaReferenceMapper() {
47 return schemaReferenceMapper;
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);
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);
66 private boolean isValidatorCached(SchemaValidator validator) {
67 return validator != null;
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;
78 private SchemaValidator handleValidatorCreation(JsonNode schemaNode) {
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());
87 private void cacheSchemaValidator(String localSchemaReference, SchemaValidator schemaValidator) {
88 logger.info("Adding new stndDefined schema validator to cache");
89 cache.put(localSchemaReference, schemaValidator);