From 9ae8ae65c0aff6b8fd4e94460b44af33cf15aa50 Mon Sep 17 00:00:00 2001 From: Ravi Geda Date: Wed, 28 Nov 2018 18:14:33 +0000 Subject: Upgrade version of aai-common Update OXM Model Loader and Edge Rules Loader to use the 1810 schema ingestion mechanism. Update tests accordingly. Change-Id: I979951fcdcaf901c508c30d770b83dfa3d52bde4 Issue-ID: AAI-1952 Signed-off-by: Ravi Geda --- .../org/onap/schema/EdgePropsConfiguration.java | 38 +++++ src/main/java/org/onap/schema/EdgeRulesLoader.java | 76 +++++---- .../org/onap/schema/OxmModelConfigTranslator.java | 19 ++- src/main/java/org/onap/schema/OxmModelLoader.java | 187 ++++++++++----------- .../schema/util/SchemaIngestPropertyReader.java | 117 ------------- 5 files changed, 181 insertions(+), 256 deletions(-) create mode 100644 src/main/java/org/onap/schema/EdgePropsConfiguration.java delete mode 100644 src/main/java/org/onap/schema/util/SchemaIngestPropertyReader.java (limited to 'src/main/java/org/onap/schema') diff --git a/src/main/java/org/onap/schema/EdgePropsConfiguration.java b/src/main/java/org/onap/schema/EdgePropsConfiguration.java new file mode 100644 index 0000000..728fa26 --- /dev/null +++ b/src/main/java/org/onap/schema/EdgePropsConfiguration.java @@ -0,0 +1,38 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.schema; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class EdgePropsConfiguration { + + @Value("${edgePropsDir}") + private String edgePropsDir; + + public String getEdgePropsDir() { + return edgePropsDir; + } + + public void setEdgePropsDir(String edgePropsDir) { + this.edgePropsDir = edgePropsDir; + } +} diff --git a/src/main/java/org/onap/schema/EdgeRulesLoader.java b/src/main/java/org/onap/schema/EdgeRulesLoader.java index 7f08949..2c139a7 100644 --- a/src/main/java/org/onap/schema/EdgeRulesLoader.java +++ b/src/main/java/org/onap/schema/EdgeRulesLoader.java @@ -31,35 +31,48 @@ import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; + import javax.ws.rs.core.Response.Status; + import org.apache.commons.io.IOUtils; import org.onap.aai.cl.eelf.LoggerFactory; import org.onap.aai.edges.EdgeIngestor; import org.onap.aai.edges.EdgeRule; import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; import org.onap.aai.setup.ConfigTranslator; -import org.onap.aai.setup.SchemaLocationsBean; -import org.onap.aai.setup.Version; +import org.onap.aai.setup.SchemaVersion; import org.onap.crud.exception.CrudException; import org.onap.crud.logging.CrudServiceMsgs; -import org.onap.schema.util.SchemaIngestPropertyReader; +import org.springframework.beans.factory.annotation.Autowired; + import com.google.common.collect.Multimap; public class EdgeRulesLoader { + + private static ConfigTranslator configTranslator; + private static EdgeIngestor edgeIngestor; + + private static EdgePropsConfiguration edgePropsConfiguration; private static Map versionContextMap = new ConcurrentHashMap<> (); - static final Pattern versionPattern = Pattern.compile ( "V(\\d*)" ); + static final Pattern versionPattern = Pattern.compile("(?i)v(\\d*)"); static final String propsPrefix = "edge_properties_"; static final String propsSuffix = ".json"; final static Pattern propsFilePattern = Pattern.compile ( propsPrefix + "(.*)" + propsSuffix ); - final static Pattern propsVersionPattern = Pattern.compile ( "v\\d*" ); + final static Pattern propsVersionPattern = Pattern.compile("(?i)v(\\d*)"); private static org.onap.aai.cl.api.Logger logger = LoggerFactory.getInstance ().getLogger ( EdgeRulesLoader.class.getName () ); - private EdgeRulesLoader () { + private EdgeRulesLoader () { } + + @Autowired + public EdgeRulesLoader(ConfigTranslator configTranslator, EdgeIngestor edgeIngestor, EdgePropsConfiguration edgePropsConfiguration) { + EdgeRulesLoader.configTranslator = configTranslator; + EdgeRulesLoader.edgeIngestor = edgeIngestor; + EdgeRulesLoader.edgePropsConfiguration = edgePropsConfiguration; } /** @@ -68,20 +81,17 @@ public class EdgeRulesLoader { * @throws CrudException */ public static synchronized void loadModels () throws CrudException { - SchemaIngestPropertyReader schemaIngestPropertyReader = new SchemaIngestPropertyReader (); - SchemaLocationsBean schemaLocationsBean = new SchemaLocationsBean (); - schemaLocationsBean.setEdgeDirectory ( schemaIngestPropertyReader.getEdgeDir () ); - ConfigTranslator configTranslator = new OxmModelConfigTranslator ( schemaLocationsBean ); - EdgeIngestor edgeIngestor = new EdgeIngestor ( configTranslator ); - Map propFiles = edgePropertyFiles(schemaIngestPropertyReader); - - if (logger.isDebugEnabled ()) { - logger.debug ( "Loading DB Edge Rules" ); + Map propFiles = edgePropertyFiles(edgePropsConfiguration); + + if (logger.isDebugEnabled()) { + logger.debug("Loading DB Edge Rules"); } - for (String version : OxmModelLoader.getLoadedOXMVersions ()) { + for (String version : OxmModelLoader.getLoadedOXMVersions()) { try { - loadModel ( Version.valueOf ( version ), edgeIngestor, propFiles ); + SchemaVersion schemaVersion = configTranslator.getSchemaVersions().getVersions().stream() + .filter(s -> s.toString().equalsIgnoreCase(version)).findAny().orElse(null); + loadModel(schemaVersion, edgeIngestor, propFiles); } catch (IOException | EdgeRuleNotFoundException e) { throw new CrudException(e.getMessage (), e); } @@ -95,20 +105,17 @@ public class EdgeRulesLoader { */ public static synchronized void loadModels ( String v ) throws CrudException { - SchemaIngestPropertyReader schemaIngestPropertyReader = new SchemaIngestPropertyReader (); - SchemaLocationsBean schemaLocationsBean = new SchemaLocationsBean (); - schemaLocationsBean.setEdgeDirectory ( schemaIngestPropertyReader.getEdgeDir () ); - ConfigTranslator configTranslator = new OxmModelConfigTranslator ( schemaLocationsBean ); - EdgeIngestor edgeIngestor = new EdgeIngestor ( configTranslator ); - String version = v.toUpperCase (); - Map propFiles = edgePropertyFiles(schemaIngestPropertyReader); - - if (logger.isDebugEnabled ()) { - logger.debug ( "Loading DB Edge Rules " ); + Map propFiles = edgePropertyFiles(edgePropsConfiguration); + + if (logger.isDebugEnabled()) { + logger.debug("Loading DB Edge Rules "); } try { - loadModel ( Version.valueOf ( version ), edgeIngestor, propFiles ); + SchemaVersion schemaVersion = configTranslator.getSchemaVersions().getVersions().stream() + .filter(s -> s.toString().equalsIgnoreCase(v)).findAny().orElse(null); + + loadModel(schemaVersion, edgeIngestor, propFiles); } catch (IOException | EdgeRuleNotFoundException e) { throw new CrudException(e.getMessage (), Status.INTERNAL_SERVER_ERROR); } @@ -175,7 +182,7 @@ public class EdgeRulesLoader { String latestVersionStr = null; for (String versionKey : versionContextMap.keySet ()) { - Matcher matcher = versionPattern.matcher ( versionKey.toUpperCase () ); + Matcher matcher = versionPattern.matcher(versionKey); if (matcher.find ()) { int currentVersion = Integer.parseInt ( matcher.group ( 1 ) ); @@ -199,8 +206,8 @@ public class EdgeRulesLoader { versionContextMap = new ConcurrentHashMap<> (); } - private static synchronized void loadModel ( Version version, EdgeIngestor edgeIngestor, Map props) - throws IOException, CrudException, EdgeRuleNotFoundException { + private static synchronized void loadModel(SchemaVersion version, EdgeIngestor edgeIngestor, + Map props) throws IOException, CrudException, EdgeRuleNotFoundException { Multimap edges = edgeIngestor.getAllRules ( version ); String edgeProps; @@ -216,11 +223,12 @@ public class EdgeRulesLoader { } } - private static Map edgePropertyFiles ( SchemaIngestPropertyReader dir ) throws CrudException { - Map propsFiles = Arrays.stream ( new File ( dir.getEdgePropsDir () ) + private static Map edgePropertyFiles ( EdgePropsConfiguration edgePropsConfiguration ) throws CrudException { + Map propsFiles = Arrays.stream ( new File ( edgePropsConfiguration.getEdgePropsDir () ) .listFiles ( ( d, name ) -> propsFilePattern.matcher ( name ).matches () ) ) .collect ( Collectors.toMap ( new Function () { - public String apply ( File f ) { + @Override + public String apply ( File f ) { Matcher m1 = propsVersionPattern.matcher ( f.getName () ); m1.find (); return m1.group ( 0 ); diff --git a/src/main/java/org/onap/schema/OxmModelConfigTranslator.java b/src/main/java/org/onap/schema/OxmModelConfigTranslator.java index f6b9e70..cb82ed0 100644 --- a/src/main/java/org/onap/schema/OxmModelConfigTranslator.java +++ b/src/main/java/org/onap/schema/OxmModelConfigTranslator.java @@ -32,16 +32,17 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import org.onap.aai.setup.ConfigTranslator; import org.onap.aai.setup.SchemaLocationsBean; -import org.onap.aai.setup.Version; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; public class OxmModelConfigTranslator extends ConfigTranslator { - public OxmModelConfigTranslator(SchemaLocationsBean bean) { - super(bean); + public OxmModelConfigTranslator(SchemaLocationsBean bean, SchemaVersions schemaVersions) { + super(bean, schemaVersions); } @Override - public Map> getNodeFiles() { + public Map> getNodeFiles() { String nodeDirectory = bean.getNodeDirectory(); if (nodeDirectory == null) { throw new ServiceConfigurationError( @@ -55,7 +56,7 @@ public class OxmModelConfigTranslator extends ConfigTranslator { } @Override - public Map> getEdgeFiles() { + public Map> getEdgeFiles() { String edgeDirectory = bean.getEdgeDirectory(); if (edgeDirectory == null) { throw new ServiceConfigurationError( @@ -80,7 +81,7 @@ public class OxmModelConfigTranslator extends ConfigTranslator { * @return a new Map object (may be empty) * @throws IOException if there is a problem reading the specified directory path */ - private Map> getVersionMap(Path folderPath, String globPattern) throws IOException { + private Map> getVersionMap(Path folderPath, String globPattern) throws IOException { final PathMatcher filter = folderPath.getFileSystem().getPathMatcher("glob:**/" + globPattern); try (final Stream stream = Files.list(folderPath)) { return stream.filter(filter::matches).map(Path::toString).filter(p -> getVersionFromPath(p) != null) @@ -88,10 +89,12 @@ public class OxmModelConfigTranslator extends ConfigTranslator { } } - private Version getVersionFromPath(String pathName) { + private SchemaVersion getVersionFromPath(String pathName) { String version = "V" + pathName.replaceAll("^.*\\/", "").replaceAll("\\D+", ""); try { - return Version.valueOf(version); + SchemaVersion schemaVersion = schemaVersions.getVersions().stream() + .filter(s -> s.toString().equalsIgnoreCase(version)).findAny().orElse(null); + return schemaVersion; } catch (IllegalArgumentException e) { return null; } diff --git a/src/main/java/org/onap/schema/OxmModelLoader.java b/src/main/java/org/onap/schema/OxmModelLoader.java index 948914c..12f8f09 100644 --- a/src/main/java/org/onap/schema/OxmModelLoader.java +++ b/src/main/java/org/onap/schema/OxmModelLoader.java @@ -20,46 +20,48 @@ */ package org.onap.schema; +import com.google.common.base.CaseFormat; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; - import javax.ws.rs.core.Response.Status; - import org.eclipse.persistence.dynamic.DynamicType; import org.eclipse.persistence.internal.oxm.mappings.Descriptor; import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext; import org.onap.aai.cl.eelf.LoggerFactory; import org.onap.aai.nodes.NodeIngestor; import org.onap.aai.setup.ConfigTranslator; -import org.onap.aai.setup.SchemaLocationsBean; -import org.onap.aai.setup.Version; +import org.onap.aai.setup.SchemaVersion; import org.onap.crud.exception.CrudException; import org.onap.crud.logging.CrudServiceMsgs; -import org.onap.schema.util.SchemaIngestPropertyReader; - -import com.google.common.base.CaseFormat; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +@Component public class OxmModelLoader { - private static Map versionContextMap = - new ConcurrentHashMap<>(); + private static ConfigTranslator configTranslator; + private static NodeIngestor nodeIngestor; - private static Map> xmlElementLookup = new ConcurrentHashMap>(); + private static Map versionContextMap = new ConcurrentHashMap<>(); + private static Map> xmlElementLookup = new ConcurrentHashMap<>(); - static final Pattern p = Pattern.compile("aai_oxm_(.*).xml"); - static final Pattern versionPattern = Pattern.compile("V(\\d*)"); + final static Pattern versionPattern = Pattern.compile("(?i)v(\\d*)"); private static org.onap.aai.cl.api.Logger logger = LoggerFactory.getInstance().getLogger(OxmModelLoader.class.getName()); - private OxmModelLoader() { + private OxmModelLoader() { } + + @Autowired + public OxmModelLoader(ConfigTranslator configTranslator, NodeIngestor nodeIngestor) { + OxmModelLoader.configTranslator = configTranslator; + OxmModelLoader.nodeIngestor = nodeIngestor; } /** @@ -69,28 +71,22 @@ public class OxmModelLoader { * @throws IOException * */ - public static synchronized void loadModels() throws CrudException { - SchemaIngestPropertyReader schemaIngestPropertyReader = new SchemaIngestPropertyReader(); - - SchemaLocationsBean schemaLocationsBean = new SchemaLocationsBean(); - schemaLocationsBean.setNodeDirectory(schemaIngestPropertyReader.getNodeDir()); - ConfigTranslator configTranslator = new OxmModelConfigTranslator(schemaLocationsBean); - NodeIngestor nodeIngestor = new NodeIngestor(configTranslator); + public synchronized static void loadModels() throws CrudException { if (logger.isDebugEnabled()) { logger.debug("Loading OXM Models"); } - for (Version oxmVersion : Version.values()) { + for (SchemaVersion oxmVersion : configTranslator.getSchemaVersions().getVersions()) { DynamicJAXBContext jaxbContext = nodeIngestor.getContextForVersion(oxmVersion); if (jaxbContext != null) { - loadModel(oxmVersion.toString().toLowerCase(), jaxbContext); + loadModel(oxmVersion.toString(), jaxbContext); } } } - private static synchronized void loadModel(String oxmVersion, DynamicJAXBContext jaxbContext) { + private synchronized static void loadModel(String oxmVersion, DynamicJAXBContext jaxbContext) { versionContextMap.put(oxmVersion, jaxbContext); loadXmlLookupMap(oxmVersion, jaxbContext); logger.info(CrudServiceMsgs.LOADED_OXM_FILE, oxmVersion); @@ -111,7 +107,7 @@ public class OxmModelLoader { if (versionContextMap == null || versionContextMap.isEmpty()) { loadModels(); } else if (!versionContextMap.containsKey(version)) { - logger.error(CrudServiceMsgs.OXM_LOAD_ERROR, "Error loading oxm model: " + version); + logger.error(CrudServiceMsgs.OXM_LOAD_ERROR, "Error loading oxm model: " + version); throw new CrudException("Error loading oxm model: " + version, Status.INTERNAL_SERVER_ERROR); } @@ -128,8 +124,7 @@ public class OxmModelLoader { // If there are still no models available, then there's not much we can do... if (versionContextMap.isEmpty()) { logger.error(CrudServiceMsgs.OXM_LOAD_ERROR, "No available OXM schemas to get latest version for."); - throw new CrudException("No available OXM schemas to get latest version for.", - Status.INTERNAL_SERVER_ERROR); + throw new CrudException("No available OXM schemas to get latest version for.", Status.INTERNAL_SERVER_ERROR); } // Iterate over the available model versions to determine which is the most @@ -138,10 +133,10 @@ public class OxmModelLoader { String latestVersionStr = null; for (String versionKey : versionContextMap.keySet()) { - Matcher matcher = versionPattern.matcher(versionKey.toUpperCase()); + Matcher matcher = versionPattern.matcher(versionKey); if (matcher.find()) { - int currentVersion = Integer.parseInt(matcher.group(1)); + int currentVersion = Integer.valueOf(matcher.group(1)); if ((latestVersion == null) || (currentVersion > latestVersion)) { latestVersion = currentVersion; @@ -153,38 +148,6 @@ public class OxmModelLoader { return latestVersionStr; } - /** - * Retrieves the list of all Loaded OXM versions. - * - * @return - A List of Strings of all loaded OXM versions. - * - * @throws CrudException - */ - public static List getLoadedOXMVersions() throws CrudException { - - // If we haven't already loaded in the available OXM models, then do so now. - if (versionContextMap == null || versionContextMap.isEmpty()) { - loadModels(); - } - - // If there are still no models available, then there's not much we can do... - if (versionContextMap.isEmpty()) { - logger.error(CrudServiceMsgs.OXM_LOAD_ERROR, "No available OXM schemas to get versions for."); - throw new CrudException("No available OXM schemas to get versions for.", - Status.INTERNAL_SERVER_ERROR); - } - - List versions = new ArrayList (); - for (String versionKey : versionContextMap.keySet()) { - - Matcher matcher = versionPattern.matcher(versionKey.toUpperCase()); - if (matcher.find()) { - versions.add ( "V" + matcher.group ( 1 ) ); - } - } - return versions; - } - /** * Retrieves the map of all JAXB context objects that have been created by importing the * available OXM model schemas. @@ -206,56 +169,86 @@ public class OxmModelLoader { public static void loadXmlLookupMap(String version, DynamicJAXBContext jaxbContext ) { - - @SuppressWarnings("rawtypes") - List descriptorsList = jaxbContext.getXMLContext().getDescriptors(); - HashMap types = new HashMap(); - for (@SuppressWarnings("rawtypes") - Descriptor desc : descriptorsList) { + @SuppressWarnings("rawtypes") + List descriptorsList = jaxbContext.getXMLContext().getDescriptors(); + HashMap types = new HashMap(); - DynamicType entity = jaxbContext.getDynamicType(desc.getAlias()); - String entityName = desc.getDefaultRootElement(); - types.put(entityName, entity); - } - xmlElementLookup.put(version, types); + for (@SuppressWarnings("rawtypes") + Descriptor desc : descriptorsList) { + + DynamicType entity = jaxbContext.getDynamicType(desc.getAlias()); + String entityName = desc.getDefaultRootElement(); + types.put(entityName, entity); + } + xmlElementLookup.put(version, types); } public static DynamicType getDynamicTypeForVersion(String version, String type) throws CrudException { - DynamicType dynamicType; - // If we haven't already loaded in the available OXM models, then do so now. - if (versionContextMap == null || versionContextMap.isEmpty()) { - loadModels(); - } else if (!versionContextMap.containsKey(version)) { - logger.error(CrudServiceMsgs.OXM_LOAD_ERROR, "Error loading oxm model: " + version); - throw new CrudException("Error loading oxm model: " + version, Status.INTERNAL_SERVER_ERROR); - } - - // First try to match the Java-type based on hyphen to camel case translation - String javaTypeName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, type); - dynamicType = versionContextMap.get(version).getDynamicType(javaTypeName); + DynamicType dynamicType; + // If we haven't already loaded in the available OXM models, then do so now. + if (versionContextMap == null || versionContextMap.isEmpty()) { + loadModels(); + } else if (!versionContextMap.containsKey(version)) { + logger.error(CrudServiceMsgs.OXM_LOAD_ERROR, "Error loading oxm model: " + version); + throw new CrudException("Error loading oxm model: " + version, Status.INTERNAL_SERVER_ERROR); + } - //Attempt to lookup in xml elements - if (xmlElementLookup.containsKey(version)) { - if (dynamicType == null) { - // Try to lookup by xml root element by exact match - dynamicType = xmlElementLookup.get(version).get(type); + // First try to match the Java-type based on hyphen to camel case + // translation + String javaTypeName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, type); + dynamicType = versionContextMap.get(version).getDynamicType(javaTypeName); + + if (xmlElementLookup.containsKey(version)) { + if (dynamicType == null) { + // Try to lookup by xml root element by exact match + dynamicType = xmlElementLookup.get(version).get(type); + } + + if (dynamicType == null) { + // Try to lookup by xml root element by lowercase + dynamicType = xmlElementLookup.get(version).get(type.toLowerCase()); + } + + if (dynamicType == null) { + // Direct lookup as java-type name + dynamicType = versionContextMap.get(version).getDynamicType(type); + } } - if (dynamicType == null) { - // Try to lookup by xml root element by lowercase - dynamicType = xmlElementLookup.get(version).get(type.toLowerCase()); + return dynamicType; + } + + /** + * Retrieves the list of all Loaded OXM versions. + * + * @return - A List of Strings of all loaded OXM versions. + * + * @throws CrudException + */ + public static List getLoadedOXMVersions() throws CrudException { + + // If we haven't already loaded in the available OXM models, then do so now. + if (versionContextMap == null || versionContextMap.isEmpty()) { + loadModels(); } - if (dynamicType == null) { - // Direct lookup as java-type name - dynamicType = versionContextMap.get(version).getDynamicType(type); + // If there are still no models available, then there's not much we can do... + if (versionContextMap.isEmpty()) { + logger.error(CrudServiceMsgs.OXM_LOAD_ERROR, "No available OXM schemas to get versions for."); + throw new CrudException("No available OXM schemas to get versions for.", Status.INTERNAL_SERVER_ERROR); } - } - return dynamicType; + List versions = new ArrayList(); + for (String versionKey : versionContextMap.keySet()) { + Matcher matcher = versionPattern.matcher(versionKey); + if (matcher.find()) { + versions.add ("V"+ matcher.group(1)); + } + } + return versions; } } diff --git a/src/main/java/org/onap/schema/util/SchemaIngestPropertyReader.java b/src/main/java/org/onap/schema/util/SchemaIngestPropertyReader.java deleted file mode 100644 index ca5fa6b..0000000 --- a/src/main/java/org/onap/schema/util/SchemaIngestPropertyReader.java +++ /dev/null @@ -1,117 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 Amdocs - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.onap.schema.util; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URISyntaxException; -import java.nio.file.Files; -import java.nio.file.NoSuchFileException; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Properties; -import org.onap.aai.cl.eelf.LoggerFactory; -import org.onap.crud.exception.CrudException; -import org.onap.crud.logging.CrudServiceMsgs; - -public class SchemaIngestPropertyReader { - - private static final String SCHEMA_INGEST_PROPERTIES_FILE = "schemaIngest.properties"; - - private static final String SCHEMA_INGEST_PROPERTIES_LOCATION = - System.getProperty("CONFIG_HOME") + "/" + SCHEMA_INGEST_PROPERTIES_FILE; - - private static org.onap.aai.cl.api.Logger logger = - LoggerFactory.getInstance().getLogger(SchemaIngestPropertyReader.class.getName()); - - /** - * Gets the location of the OXM - * - * @return - * @throws CrudException - */ - public String getNodeDir() throws CrudException { - - return getProps ().getProperty("nodeDir"); - } - - /** - * Gets the location of the Edge Rules - * - * @return - * @throws CrudException - */ - public String getEdgeDir() throws CrudException { - - return getProps ().getProperty("edgeDir"); - } - - /** - * Gets the location of the Edge Properties - * - * @return - * @throws CrudException - */ - public String getEdgePropsDir() throws CrudException { - - return getProps ().getProperty("edgePropsDir"); - } - - private Properties getProps() throws CrudException { - - Properties prop = new Properties(); - try { - prop = loadFromFile(SCHEMA_INGEST_PROPERTIES_LOCATION); - } catch (NoSuchFileException e) { - // if file not found, try via classpath - try { - prop = loadFromClasspath(SCHEMA_INGEST_PROPERTIES_FILE); - } catch (URISyntaxException | IOException e1) { - logger.error(CrudServiceMsgs.SCHEMA_INGEST_LOAD_ERROR, e1.getMessage()); - throw new CrudException("Failed to load schemaIngest.properties", e1); - } - } catch (IOException e) { - logger.error(CrudServiceMsgs.SCHEMA_INGEST_LOAD_ERROR, e.getMessage()); - throw new CrudException("Failed to load schemaIngest.properties", e); - } - return prop; - } - - private Properties loadFromFile(String filename) throws IOException { - Path configLocation = Paths.get(filename); - try (InputStream stream = Files.newInputStream(configLocation)) { - return loadProperties(stream); - } - } - - private Properties loadFromClasspath(String resourceName) throws URISyntaxException, IOException { - Path path = Paths.get(ClassLoader.getSystemResource(resourceName).toURI()); - try (InputStream stream = Files.newInputStream(path)) { - return loadProperties(stream); - } - } - - private Properties loadProperties(InputStream stream) throws IOException { - Properties config = new Properties(); - config.load(stream); - return config; - } -} \ No newline at end of file -- cgit 1.2.3-korg