Upgrade version of aai-common
[aai/gizmo.git] / src / main / java / org / onap / schema / OxmModelConfigTranslator.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.schema;
22
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.PathMatcher;
27 import java.nio.file.Paths;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.ServiceConfigurationError;
31 import java.util.stream.Collectors;
32 import java.util.stream.Stream;
33 import org.onap.aai.setup.ConfigTranslator;
34 import org.onap.aai.setup.SchemaLocationsBean;
35 import org.onap.aai.setup.SchemaVersion;
36 import org.onap.aai.setup.SchemaVersions;
37
38 public class OxmModelConfigTranslator extends ConfigTranslator {
39
40     public OxmModelConfigTranslator(SchemaLocationsBean bean, SchemaVersions schemaVersions) {
41         super(bean, schemaVersions);
42     }
43
44     @Override
45     public Map<SchemaVersion, List<String>> getNodeFiles() {
46         String nodeDirectory = bean.getNodeDirectory();
47         if (nodeDirectory == null) {
48             throw new ServiceConfigurationError(
49                     "Node(s) directory is empty in the schema location bean (" + bean.getSchemaConfigLocation() + ")");
50         }
51         try {
52             return getVersionMap(Paths.get(nodeDirectory), "*_v*.xml");
53         } catch (IOException e) {
54             throw new ServiceConfigurationError("Failed to read node(s) directory " + getPath(nodeDirectory), e);
55         }
56     }
57
58     @Override
59     public Map<SchemaVersion, List<String>> getEdgeFiles() {
60         String edgeDirectory = bean.getEdgeDirectory();
61         if (edgeDirectory == null) {
62             throw new ServiceConfigurationError(
63                     "Edge(s) directory is empty in the schema location bean (" + bean.getSchemaConfigLocation() + ")");
64         }
65         try {
66             return getVersionMap(Paths.get(edgeDirectory), "*_v*.json");
67         } catch (IOException e) {
68             throw new ServiceConfigurationError("Failed to read edge(s) directory " + getPath(edgeDirectory), e);
69         }
70     }
71
72     private String getPath(String nodeDirectory) {
73         return Paths.get(nodeDirectory).toAbsolutePath().toString();
74     }
75
76     /**
77      * Creates a map containing each OXM Version and the matching OXM file path(s)
78      *
79      * @param folderPath the folder/directory containing the OXM files
80      * @param fileSuffix
81      * @return a new Map object (may be empty)
82      * @throws IOException if there is a problem reading the specified directory path
83      */
84     private Map<SchemaVersion, List<String>> getVersionMap(Path folderPath, String globPattern) throws IOException {
85         final PathMatcher filter = folderPath.getFileSystem().getPathMatcher("glob:**/" + globPattern);
86         try (final Stream<Path> stream = Files.list(folderPath)) {
87             return stream.filter(filter::matches).map(Path::toString).filter(p -> getVersionFromPath(p) != null)
88                     .collect(Collectors.groupingBy(this::getVersionFromPath));
89         }
90     }
91
92     private SchemaVersion getVersionFromPath(String pathName) {
93         String version = "V" + pathName.replaceAll("^.*\\/", "").replaceAll("\\D+", "");
94         try {
95             SchemaVersion schemaVersion = schemaVersions.getVersions().stream()
96                     .filter(s -> s.toString().equalsIgnoreCase(version)).findAny().orElse(null);
97             return schemaVersion;
98         } catch (IllegalArgumentException e) {
99             return null;
100         }
101     }
102
103 }