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.core.JsonParseException;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 import java.io.IOException;
32 import java.util.Collections;
33 import java.util.HashMap;
37 * An UrlMapperFactory is used to produced {@link UrlMapper}.
39 public class UrlMapperFactory {
41 private static final Logger logger = LoggerFactory.getLogger(UrlMapperFactory.class);
44 * Returns UrlMapper instance
45 * @param mappingFilePath path to file with mappings
46 * @param schemasPath path to local schemas repository
47 * @return {@link UrlMapper}
49 UrlMapper getUrlMapper(String mappingFilePath, String schemasPath) {
50 Map<String, String> mappings = new HashMap<>();
52 mappings = readMappingFile(mappingFilePath, schemasPath);
53 } catch (IOException ex) {
54 logger.warn("Unable to read mapping file. Mapping file path: {}", mappingFilePath);
55 } catch (NullPointerException ex) {
56 mappings = Collections.emptyMap();
57 logger.warn("Schema mapping file has incorrect format. Mapping file path: {}", mappingFilePath);
60 return new UrlMapper(mappings);
63 private Map<String, String> readMappingFile(String mappingFilePath, String schemasPath) throws IOException {
64 ObjectMapper objectMapper = new ObjectMapper();
65 FileReader fileReader = new FileReader(mappingFilePath);
66 Map<String, String> mappings = new HashMap<>();
68 for (JsonNode mapping : objectMapper.readTree(fileReader.getContent())) {
69 String localURL = mapping.get("localURL").asText();
70 if (isMappingValid(schemasPath, localURL)) {
71 mappings.put(mapping.get("publicURL").asText(), localURL);
73 logger.warn("Mapping for publicURL ({}) will not be added to validator.", mapping.get("publicURL"));
79 private boolean isMappingValid(String schemasPath, String localURL) throws IOException {
80 String schemaRelativePath = schemasPath + File.separator + localURL;
81 return doesLocalFileExist(schemaRelativePath) && isFileValidSchema(schemaRelativePath);
84 private boolean isFileValidSchema(String schemaRelativePath) throws IOException {
85 String schemaContent = new FileReader(schemaRelativePath).getContent();
86 return isNotEmpty(schemaContent, schemaRelativePath) && isYaml(schemaContent, schemaRelativePath);
89 private boolean isNotEmpty(String schemaContent, String schemaRelativePath) {
90 if (schemaContent.isEmpty()) {
91 logger.warn("Schema file is empty. Schema path: {}", schemaRelativePath);
97 private boolean isYaml(String schemaContent, String schemaRelativePath) throws IOException {
98 ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory()).findAndRegisterModules();
100 yamlMapper.readTree(schemaContent);
101 } catch (JsonParseException e) {
102 logger.warn("Schema has incorrect YAML structure. Schema path: {}", schemaRelativePath);
108 private boolean doesLocalFileExist(String schemaRelativePath) {
109 FileReader fileReader = new FileReader(schemaRelativePath);
110 if (!fileReader.doesFileExists()) {
111 logger.warn("Local schema resource missing. Schema file with path {} has not been found.", schemaRelativePath);