2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.datarouter.schema;
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;
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.Version;
37 public class OxmConfigTranslator extends ConfigTranslator {
38 public OxmConfigTranslator(SchemaLocationsBean bean) {
43 public Map<Version, List<String>> getNodeFiles() {
44 String nodeDirectory = bean.getNodeDirectory();
45 if (nodeDirectory == null) {
46 throw new ServiceConfigurationError(
47 "Node(s) directory is empty in the schema location bean (" + bean.getSchemaConfigLocation() + ")");
50 return getVersionMap(Paths.get(nodeDirectory), "*_v*.xml");
51 } catch (IOException e) {
52 throw new ServiceConfigurationError("Failed to read node(s) directory " + getPath(nodeDirectory), e);
57 public Map<Version, List<String>> getEdgeFiles() {
58 String edgeDirectory = bean.getEdgeDirectory();
59 if (edgeDirectory == null) {
60 throw new ServiceConfigurationError(
61 "Edge(s) directory is empty in the schema location bean (" + bean.getSchemaConfigLocation() + ")");
64 return getVersionMap(Paths.get(edgeDirectory), "*_v*.json");
65 } catch (IOException e) {
66 throw new ServiceConfigurationError("Failed to read edge(s) directory " + getPath(edgeDirectory), e);
70 private String getPath(String nodeDirectory) {
71 return Paths.get(nodeDirectory).toAbsolutePath().toString();
75 * Creates a map containing each OXM Version and the matching OXM file path(s)
77 * @param folderPath the folder/directory containing the OXM files
79 * @return a new Map object (may be empty)
80 * @throws IOException if there is a problem reading the specified directory path
82 private Map<Version, List<String>> getVersionMap(Path folderPath, String globPattern) throws IOException {
83 final PathMatcher filter = folderPath.getFileSystem().getPathMatcher("glob:**/" + globPattern);
84 try (final Stream<Path> stream = Files.list(folderPath)) {
85 return stream.filter(filter::matches).map(Path::toString).filter(p -> getVersionFromPath(p) != null)
86 .collect(Collectors.groupingBy(this::getVersionFromPath));
90 private Version getVersionFromPath(String pathName) {
91 String version = "V" + pathName.replaceAll("^.*\\/", "").replaceAll("\\D+", "");
93 return Version.valueOf(version);
94 } catch (IllegalArgumentException e) {