2 * ============LICENSE_START=======================================================
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
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.schemaif.oxm;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.util.Arrays;
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;
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;
48 import com.google.common.collect.Multimap;
51 public class OxmEdgeRulesLoader {
53 private static Translator translator;
54 private static EdgeIngestor edgeIngestor;
56 private static EdgePropsConfiguration edgePropsConfiguration;
58 private static Map<String, RelationshipSchema> versionContextMap = new ConcurrentHashMap<>();
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*");
66 private static Logger logger =
67 LoggerFactory.getInstance().getLogger(OxmEdgeRulesLoader.class.getName());
69 private OxmEdgeRulesLoader() {}
72 * This constructor presents an awkward marrying of Spring bean creation and static method use. This
73 * is technical debt that will need fixing.
75 * @param translator contains schema versions configuration
76 * @param edgeIngestor provides edge rules
77 * @param edgePropsConfiguration edge property validation configuration
80 public OxmEdgeRulesLoader(Translator translator, EdgeIngestor edgeIngestor,
81 EdgePropsConfiguration edgePropsConfiguration) {
82 OxmEdgeRulesLoader.translator = translator;
83 OxmEdgeRulesLoader.edgeIngestor = edgeIngestor;
84 OxmEdgeRulesLoader.edgePropsConfiguration = edgePropsConfiguration;
88 * Finds all DB Edge Rules and Edge Properties files for all OXM models.
90 * @throws SchemaProviderException
91 * @throws SchemaProviderException
93 public static synchronized void loadModels() throws SchemaProviderException {
94 Map<String, File> propFiles = edgePropertyFiles(edgePropsConfiguration);
96 if (logger.isDebugEnabled()) {
97 logger.debug("Loading DB Edge Rules");
100 for (String version : OxmSchemaLoader.getLoadedOXMVersions()) {
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);
112 * Loads DB Edge Rules and Edge Properties for a given version.
114 * @throws SchemaProviderException
117 public static synchronized void loadModels(String v) throws SchemaProviderException {
118 Map<String, File> propFiles = edgePropertyFiles(edgePropsConfiguration);
120 if (logger.isDebugEnabled()) {
121 logger.debug("Loading DB Edge Rules ");
125 SchemaVersion schemaVersion = translator.getSchemaVersions().getVersions().stream()
126 .filter(s -> s.toString().equalsIgnoreCase(v)).findAny().orElse(null);
128 loadModel(schemaVersion, edgeIngestor, propFiles);
129 } catch (IOException | EdgeRuleNotFoundException e) {
130 throw new SchemaProviderException(e.getMessage());
135 * Retrieves the DB Edge Rule relationship schema for a given version.
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
141 public static RelationshipSchema getSchemaForVersion(String version) throws SchemaProviderException {
143 // If we haven't already loaded in the available OXM models, then do so now.
144 if (versionContextMap == null || versionContextMap.isEmpty()) {
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);
151 return versionContextMap.get(version);
155 * Retrieves the DB Edge Rule relationship schema for all loaded OXM versions.
157 * @return - A Map of the OXM version and it's corresponding RelationshipSchema of the DB Edge Rule.
158 * @throws SchemaProviderException
160 public static Map<String, RelationshipSchema> getSchemas() throws SchemaProviderException {
162 // If we haven't already loaded in the available OXM models, then do so now.
163 if (versionContextMap == null || versionContextMap.isEmpty()) {
166 return versionContextMap;
170 * Returns the latest available DB Edge Rule version.
172 * @return - A Map of the OXM version and it's corresponding RelationshipSchema of the DB Edge Rule.
173 * @throws SchemaProviderException
175 public static String getLatestSchemaVersion() throws SchemaProviderException {
177 // If we haven't already loaded in the available OXM models, then do so now.
178 if (versionContextMap == null || versionContextMap.isEmpty()) {
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.");
188 // Iterate over the available model versions to determine which is the most
190 Integer latestVersion = null;
191 String latestVersionStr = null;
192 for (String versionKey : versionContextMap.keySet()) {
194 Matcher matcher = versionPattern.matcher(versionKey);
195 if (matcher.find()) {
197 int currentVersion = Integer.parseInt(matcher.group(1));
199 if ((latestVersion == null) || (currentVersion > latestVersion)) {
200 latestVersion = currentVersion;
201 latestVersionStr = versionKey;
206 return latestVersionStr;
210 * Reset the loaded DB Edge Rule schemas
213 public static void resetSchemaVersionContext() {
214 versionContextMap = new ConcurrentHashMap<>();
217 private static synchronized void loadModel(SchemaVersion version, EdgeIngestor edgeIngestor,
218 Map<String, File> props) throws IOException, SchemaProviderException, EdgeRuleNotFoundException {
220 Multimap<String, EdgeRule> edges = edgeIngestor.getAllRules(version);
222 if (props.get(version.toString().toLowerCase()) != null) {
223 edgeProps = IOUtils.toString(new FileInputStream(props.get(version.toString().toLowerCase())), "UTF-8");
225 throw new FileNotFoundException("The Edge Properties file for OXM version " + version + "was not found.");
228 RelationshipSchema rs = new RelationshipSchema(edges, edgeProps);
229 versionContextMap.put(version.toString().toLowerCase(), rs);
230 logger.info(SchemaProviderMsgs.LOADED_DB_RULE_FILE, version.toString());
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>() {
241 public String apply(File f) {
242 Matcher m1 = propsVersionPattern.matcher(f.getName());