49c8b1fe2c43ff358abb104cd3e50175f44154da
[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.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;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.Map;
35
36 /**
37  * An UrlMapperFactory is used to produced {@link UrlMapper}.
38  */
39 public class UrlMapperFactory {
40
41     private static final Logger logger = LoggerFactory.getLogger(UrlMapperFactory.class);
42
43     /**
44      * Returns UrlMapper instance
45      * @param mappingFilePath path to file with mappings
46      * @param schemasPath path to local schemas repository
47      * @return {@link UrlMapper}
48      */
49     UrlMapper getUrlMapper(String mappingFilePath, String schemasPath) {
50         Map<String, String> mappings = new HashMap<>();
51         try {
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);
58         }
59
60         return new UrlMapper(mappings);
61     }
62
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<>();
67
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);
72             } else {
73                 logger.warn("Mapping for publicURL ({}) will not be added to validator.", mapping.get("publicURL"));
74             }
75         }
76         return mappings;
77     }
78
79     private boolean isMappingValid(String schemasPath, String localURL) throws IOException {
80         String schemaRelativePath = schemasPath + File.separator + localURL;
81         return doesLocalFileExist(schemaRelativePath) && isFileValidSchema(schemaRelativePath);
82     }
83
84     private boolean isFileValidSchema(String schemaRelativePath) throws IOException {
85         String schemaContent = new FileReader(schemaRelativePath).getContent();
86         return isNotEmpty(schemaContent, schemaRelativePath) && isYaml(schemaContent, schemaRelativePath);
87     }
88
89     private boolean isNotEmpty(String schemaContent, String schemaRelativePath) {
90         if (schemaContent.isEmpty()) {
91             logger.warn("Schema file is empty. Schema path: {}", schemaRelativePath);
92             return false;
93         }
94         return true;
95     }
96
97     private boolean isYaml(String schemaContent, String schemaRelativePath) throws IOException {
98         ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory()).findAndRegisterModules();
99         try {
100             yamlMapper.readTree(schemaContent);
101         } catch (JsonParseException e) {
102             logger.warn("Schema has incorrect YAML structure. Schema path: {}", schemaRelativePath);
103             return false;
104         }
105         return true;
106     }
107
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);
112             return false;
113         }
114         return true;
115     }
116 }