Merge "Remove the apache commons-lang dependency in aai-common"
[aai/aai-common.git] / aai-schema-abstraction / src / main / java / org / onap / aai / schemaif / oxm / OxmEdgeRulesLoader.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2019 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.schemaif.oxm;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.util.Arrays;
28 import java.util.Map;
29 import java.util.concurrent.ConcurrentHashMap;
30 import java.util.function.Function;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33 import java.util.stream.Collectors;
34
35 import org.apache.commons.io.IOUtils;
36 import org.onap.aai.cl.api.Logger;
37 import org.onap.aai.cl.eelf.LoggerFactory;
38 import org.onap.aai.edges.EdgeIngestor;
39 import org.onap.aai.edges.EdgeRule;
40 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
41 import org.onap.aai.schemaif.SchemaProviderException;
42 import org.onap.aai.schemaif.SchemaProviderMsgs;
43 import org.onap.aai.setup.SchemaVersion;
44 import org.onap.aai.setup.Translator;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.stereotype.Component;
47
48 import com.google.common.collect.Multimap;
49
50 @Component
51 public class OxmEdgeRulesLoader {
52
53     private static Translator translator;
54     private static EdgeIngestor edgeIngestor;
55
56     private static EdgePropsConfiguration edgePropsConfiguration;
57
58     private static Map<String, RelationshipSchema> versionContextMap = new ConcurrentHashMap<>();
59
60     static final Pattern versionPattern = Pattern.compile("(?i)v(\\d*)");
61     static final String propsPrefix = "edge_properties_";
62     static final String propsSuffix = ".json";
63     final static Pattern propsFilePattern = Pattern.compile(propsPrefix + "(.*)" + propsSuffix);
64     final static Pattern propsVersionPattern = Pattern.compile("(?i)v\\d*");
65
66     private static Logger logger =
67             LoggerFactory.getInstance().getLogger(OxmEdgeRulesLoader.class.getName());
68
69     private OxmEdgeRulesLoader() {}
70
71     /**
72      * This constructor presents an awkward marrying of Spring bean creation and static method use. This
73      * is technical debt that will need fixing.
74      *
75      * @param translator contains schema versions configuration
76      * @param edgeIngestor provides edge rules
77      * @param edgePropsConfiguration edge property validation configuration
78      */
79     @Autowired
80     public OxmEdgeRulesLoader(Translator translator, EdgeIngestor edgeIngestor,
81             EdgePropsConfiguration edgePropsConfiguration) {
82         OxmEdgeRulesLoader.translator = translator;
83         OxmEdgeRulesLoader.edgeIngestor = edgeIngestor;
84         OxmEdgeRulesLoader.edgePropsConfiguration = edgePropsConfiguration;
85     }
86
87     /**
88      * Finds all DB Edge Rules and Edge Properties files for all OXM models.
89      *
90      * @throws SchemaProviderException
91      * @throws SchemaProviderException 
92      */
93     public static synchronized void loadModels() throws SchemaProviderException {
94         Map<String, File> propFiles = edgePropertyFiles(edgePropsConfiguration);
95
96         if (logger.isDebugEnabled()) {
97             logger.debug("Loading DB Edge Rules");
98         }
99
100         for (String version : OxmSchemaLoader.getLoadedOXMVersions()) {
101             try {
102                 SchemaVersion schemaVersion = translator.getSchemaVersions().getVersions().stream()
103                         .filter(s -> s.toString().equalsIgnoreCase(version)).findAny().orElse(null);
104                 loadModel(schemaVersion, edgeIngestor, propFiles);
105             } catch (IOException | EdgeRuleNotFoundException e) {
106                 throw new SchemaProviderException(e.getMessage(), e);
107             }
108         }
109     }
110
111     /**
112      * Loads DB Edge Rules and Edge Properties for a given version.
113      *
114      * @throws SchemaProviderException
115      */
116
117     public static synchronized void loadModels(String v) throws SchemaProviderException {
118         Map<String, File> propFiles = edgePropertyFiles(edgePropsConfiguration);
119
120         if (logger.isDebugEnabled()) {
121             logger.debug("Loading DB Edge Rules ");
122         }
123
124         try {
125             SchemaVersion schemaVersion = translator.getSchemaVersions().getVersions().stream()
126                     .filter(s -> s.toString().equalsIgnoreCase(v)).findAny().orElse(null);
127
128             loadModel(schemaVersion, edgeIngestor, propFiles);
129         } catch (IOException | EdgeRuleNotFoundException e) {
130             throw new SchemaProviderException(e.getMessage());
131         }
132     }
133
134     /**
135      * Retrieves the DB Edge Rule relationship schema for a given version.
136      *
137      * @param version - The OXM version that we want the DB Edge Rule for.
138      * @return - A RelationshipSchema of the DB Edge Rule for the OXM version.
139      * @throws SchemaProviderException
140      */
141     public static RelationshipSchema getSchemaForVersion(String version) throws SchemaProviderException {
142
143         // If we haven't already loaded in the available OXM models, then do so now.
144         if (versionContextMap == null || versionContextMap.isEmpty()) {
145             loadModels();
146         } else if (!versionContextMap.containsKey(version)) {
147             logger.error(SchemaProviderMsgs.SCHEMA_LOAD_ERROR, "Error loading DB Edge Rules for: " + version);
148             throw new SchemaProviderException("Error loading DB Edge Rules for: " + version);
149         }
150
151         return versionContextMap.get(version);
152     }
153
154     /**
155      * Retrieves the DB Edge Rule relationship schema for all loaded OXM versions.
156      *
157      * @return - A Map of the OXM version and it's corresponding RelationshipSchema of the DB Edge Rule.
158      * @throws SchemaProviderException
159      */
160     public static Map<String, RelationshipSchema> getSchemas() throws SchemaProviderException {
161
162         // If we haven't already loaded in the available OXM models, then do so now.
163         if (versionContextMap == null || versionContextMap.isEmpty()) {
164             loadModels();
165         }
166         return versionContextMap;
167     }
168
169     /**
170      * Returns the latest available DB Edge Rule version.
171      *
172      * @return - A Map of the OXM version and it's corresponding RelationshipSchema of the DB Edge Rule.
173      * @throws SchemaProviderException
174      */
175     public static String getLatestSchemaVersion() throws SchemaProviderException {
176
177         // If we haven't already loaded in the available OXM models, then do so now.
178         if (versionContextMap == null || versionContextMap.isEmpty()) {
179             loadModels();
180         }
181
182         // If there are still no models available, then there's not much we can do...
183         if (versionContextMap.isEmpty()) {
184             logger.error(SchemaProviderMsgs.SCHEMA_LOAD_ERROR, "No available DB Edge Rules to get latest version for.");
185             throw new SchemaProviderException("No available DB Edge Rules to get latest version for.");
186         }
187
188         // Iterate over the available model versions to determine which is the most
189         // recent.
190         Integer latestVersion = null;
191         String latestVersionStr = null;
192         for (String versionKey : versionContextMap.keySet()) {
193
194             Matcher matcher = versionPattern.matcher(versionKey);
195             if (matcher.find()) {
196
197                 int currentVersion = Integer.parseInt(matcher.group(1));
198
199                 if ((latestVersion == null) || (currentVersion > latestVersion)) {
200                     latestVersion = currentVersion;
201                     latestVersionStr = versionKey;
202                 }
203             }
204         }
205
206         return latestVersionStr;
207     }
208
209     /**
210      * Reset the loaded DB Edge Rule schemas
211      *
212      */
213     public static void resetSchemaVersionContext() {
214         versionContextMap = new ConcurrentHashMap<>();
215     }
216
217     private static synchronized void loadModel(SchemaVersion version, EdgeIngestor edgeIngestor,
218             Map<String, File> props) throws IOException, SchemaProviderException, EdgeRuleNotFoundException {
219
220         Multimap<String, EdgeRule> edges = edgeIngestor.getAllRules(version);
221         String edgeProps;
222         if (props.get(version.toString().toLowerCase()) != null) {
223             edgeProps = IOUtils.toString(new FileInputStream(props.get(version.toString().toLowerCase())), "UTF-8");
224         } else {
225             throw new FileNotFoundException("The Edge Properties file for OXM version " + version + "was not found.");
226         }
227         if (edges != null) {
228             RelationshipSchema rs = new RelationshipSchema(edges, edgeProps);
229             versionContextMap.put(version.toString().toLowerCase(), rs);
230             logger.info(SchemaProviderMsgs.LOADED_DB_RULE_FILE, version.toString());
231         }
232     }
233
234     private static Map<String, File> edgePropertyFiles(EdgePropsConfiguration edgePropsConfiguration)
235             throws SchemaProviderException {
236         Map<String, File> propsFiles = Arrays
237                 .stream(new File(edgePropsConfiguration.getEdgePropsDir())
238                         .listFiles((d, name) -> propsFilePattern.matcher(name).matches()))
239                 .collect(Collectors.toMap(new Function<File, String>() {
240                     @Override
241                     public String apply(File f) {
242                         Matcher m1 = propsVersionPattern.matcher(f.getName());
243                         m1.find();
244                         return m1.group(0);
245                     }
246                 }, f -> f));
247         return propsFiles;
248     }
249 }