fix constructor ref to a non existing bean
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / config / oxm / OxmConfigTranslator.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.aai.sparky.config.oxm;
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.Arrays;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.ServiceConfigurationError;
32 import java.util.stream.Collectors;
33 import java.util.stream.Stream;
34
35 import org.onap.aai.cl.api.Logger;
36 import org.onap.aai.cl.eelf.LoggerFactory;
37 import org.onap.aai.setup.ConfigTranslator;
38 import org.onap.aai.setup.SchemaLocationsBean;
39 import org.onap.aai.setup.Version;
40 import org.onap.aai.sparky.logging.AaiUiMsgs;
41 import org.onap.aai.sparky.util.NodeUtils;
42
43 /**
44  * Determine which OXM and edge rules files to return based on the latest Version
45  *
46  */
47 public class OxmConfigTranslator extends ConfigTranslator {
48
49     private static final Logger LOG = LoggerFactory.getInstance().getLogger(OxmConfigTranslator.class);
50     
51     public OxmConfigTranslator(SchemaLocationsBean bean) {
52         super(bean);
53     }
54
55     @Override
56     public Map<Version, List<String>> getNodeFiles() {
57         String nodeDirectory = bean.getNodeDirectory();
58         if (nodeDirectory == null) {
59             throw new ServiceConfigurationError(
60                     "Node(s) directory is empty in the schema location bean (" + bean.getSchemaConfigLocation() + ")");
61         }
62         try {
63             return getVersionMap(Paths.get(nodeDirectory), "*_v*.xml");
64         } catch (IOException e) {
65             throw new ServiceConfigurationError("Failed to read node(s) directory " + getPath(nodeDirectory), e);
66         }
67     } 
68
69     @Override
70     public Map<Version, List<String>> getEdgeFiles() {
71         String edgeDirectory = bean.getEdgeDirectory();
72         if (edgeDirectory == null) {
73             throw new ServiceConfigurationError(
74                     "Edge(s) directory is empty in the schema location bean (" + bean.getSchemaConfigLocation() + ")");
75         }
76         try {
77             return getVersionMap(Paths.get(edgeDirectory), "*_v*.json");
78         } catch (IOException e) {
79             throw new ServiceConfigurationError("Failed to read edge(s) directory " + getPath(edgeDirectory), e);
80         }
81     }
82
83     private String getPath(String nodeDirectory) {
84         return Paths.get(nodeDirectory).toAbsolutePath().toString();
85     }
86
87     /**
88      * Creates a map containing each OXM Version and the matching OXM file path(s)
89      *
90      * @param folderPath the folder/directory containing the OXM files
91      * @param fileSuffix
92      * @return a new Map object (may be empty)
93      * @throws IOException if there is a problem reading the specified directory path
94      */
95     private Map<Version, List<String>> getVersionMap(Path folderPath, String globPattern) throws IOException {
96         final PathMatcher filter = folderPath.getFileSystem().getPathMatcher("glob:**/" + globPattern);
97         try (final Stream<Path> stream = Files.list(folderPath)) {
98             return stream.filter(filter::matches).map(Path::toString).filter(p -> getVersionFromPath(p) != null)
99                     .collect(Collectors.groupingBy(this::getVersionFromPath));
100         }
101     }
102
103     private Version getVersionFromPath(String pathName) {
104
105         String version = "V" + NodeUtils.extractOxmVersionFromPath(pathName);
106
107         try {
108             return Version.valueOf(version);
109         } catch (IllegalArgumentException e) {
110             LOG.error(AaiUiMsgs.ERROR_GENERIC, "Failed to find OXM version '" + version
111                     + "' from Version enumeration value set = " + Arrays.asList(Version.values()) + ".");
112         }
113
114         return null;
115     }
116 }