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;
36 public class UrlMapperFactory {
38 private static final Logger logger = LoggerFactory.getLogger(UrlMapperFactory.class);
40 UrlMapper getUrlMapper(String mappingFilePath, String schemasPath) {
41 Map<String, String> mappings = new HashMap<>();
43 mappings = readMappingFile(mappingFilePath, schemasPath);
44 } catch (IOException ex) {
45 logger.warn("Unable to read mapping file. Mapping file path: {}", mappingFilePath);
46 } catch (NullPointerException ex) {
47 mappings = Collections.emptyMap();
48 logger.warn("Schema mapping file has incorrect format. Mapping file path: {}", mappingFilePath);
51 return new UrlMapper(mappings);
54 private Map<String, String> readMappingFile(String mappingFilePath, String schemasPath) throws IOException {
55 ObjectMapper objectMapper = new ObjectMapper();
56 FileReader fileReader = new FileReader(mappingFilePath);
57 Map<String, String> mappings = new HashMap<>();
59 for (JsonNode mapping : objectMapper.readTree(fileReader.readFile())) {
60 String localURL = mapping.get("localURL").asText();
61 if (isMappingValid(schemasPath, localURL)) {
62 mappings.put(mapping.get("publicURL").asText(), localURL);
64 logger.warn("Mapping for publicURL ({}) will not be added to validator.", mapping.get("publicURL"));
70 private boolean isMappingValid(String schemasPath, String localURL) throws IOException {
71 String schemaRelativePath = schemasPath + File.separator + localURL;
72 return doesLocalFileExist(schemaRelativePath) && isFileValidSchema(schemaRelativePath);
75 private boolean isFileValidSchema(String schemaRelativePath) throws IOException {
76 String schemaContent = new FileReader(schemaRelativePath).readFile();
77 return isNotEmpty(schemaContent, schemaRelativePath) && isYaml(schemaContent, schemaRelativePath);
80 private boolean isNotEmpty(String schemaContent, String schemaRelativePath) {
81 if (schemaContent.isEmpty()) {
82 logger.warn("Schema file is empty. Schema path: {}", schemaRelativePath);
88 private boolean isYaml(String schemaContent, String schemaRelativePath) throws IOException {
89 ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory()).findAndRegisterModules();
91 yamlMapper.readTree(schemaContent);
92 } catch (JsonParseException e) {
93 logger.warn("Schema has incorrect YAML structure. Schema path: {}", schemaRelativePath);
99 private boolean doesLocalFileExist(String schemaRelativePath) {
100 FileReader fileReader = new FileReader(schemaRelativePath);
101 if (!fileReader.doesFileExists()) {
102 logger.warn("Local schema resource missing. Schema file with path {} has not been found.", schemaRelativePath);