diff options
author | Bansal, Nitin (nb121v) <nitin.bansal@amdocs.com> | 2017-08-02 14:12:16 -0400 |
---|---|---|
committer | Bansal, Nitin (nb121v) <nitin.bansal@amdocs.com> | 2017-08-02 14:19:52 -0400 |
commit | 02780597dbdf84a3b7d9d399a8e7da18a83da074 (patch) | |
tree | cc76f0d139d9cc9c8617c9817d6a7ed87bf0cad1 | |
parent | 6cb28e580a3d3e308de77b2e7b239cae3912dd7f (diff) |
[AAI-125] DataRouter to consume aai-schema jar
Change-Id: I7f0a5d78f662b591b87f1f6d86c511e00eef6fd5
Signed-off-by: Bansal, Nitin (nb121v) <nitin.bansal@amdocs.com>
6 files changed, 65 insertions, 69 deletions
@@ -58,7 +58,11 @@ <version>3.1.200</version> <scope>provided</scope> </dependency> - + <dependency> + <groupId>org.onap.aai.aai-common</groupId> + <artifactId>aai-schema</artifactId> + <version>1.1.0-SNAPSHOT</version> + </dependency> <dependency> <groupId>org.onap.aai.router-core</groupId> <artifactId>router-core</artifactId> diff --git a/src/main/java/org/openecomp/datarouter/logging/DataRouterMsgs.java b/src/main/java/org/openecomp/datarouter/logging/DataRouterMsgs.java index 6206b63..ae68fc7 100644 --- a/src/main/java/org/openecomp/datarouter/logging/DataRouterMsgs.java +++ b/src/main/java/org/openecomp/datarouter/logging/DataRouterMsgs.java @@ -156,6 +156,14 @@ public enum DataRouterMsgs implements LogMessageEnum { INVALID_OXM_FILE, INVALID_OXM_DIR, + LOAD_OXM_ERROR, + /** + * Successfully loaded schema: {0} + * + * <p>Arguments: + * {0} = oxm filename + */ + LOADED_OXM_FILE, /** * Arguments: {0} = origin payload diff --git a/src/main/java/org/openecomp/datarouter/policy/EntityEventPolicy.java b/src/main/java/org/openecomp/datarouter/policy/EntityEventPolicy.java index 4938bdd..8ccf1a2 100644 --- a/src/main/java/org/openecomp/datarouter/policy/EntityEventPolicy.java +++ b/src/main/java/org/openecomp/datarouter/policy/EntityEventPolicy.java @@ -25,6 +25,7 @@ package org.openecomp.datarouter.policy; import java.io.BufferedReader; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; @@ -134,7 +135,7 @@ public class EntityEventPolicy implements Processor { SUCCESS, PARTIAL_SUCCESS, FAILURE; }; - public EntityEventPolicy(EntityEventPolicyConfig config) { + public EntityEventPolicy(EntityEventPolicyConfig config) throws FileNotFoundException { LoggerFactory loggerFactoryInstance = LoggerFactory.getInstance(); logger = loggerFactoryInstance.getLogger(EntityEventPolicy.class.getName()); metricsLogger = loggerFactoryInstance.getMetricsLogger(EntityEventPolicy.class.getName()); diff --git a/src/main/java/org/openecomp/datarouter/util/OxmModelLoader.java b/src/main/java/org/openecomp/datarouter/util/OxmModelLoader.java index 2919ba2..c242d29 100644 --- a/src/main/java/org/openecomp/datarouter/util/OxmModelLoader.java +++ b/src/main/java/org/openecomp/datarouter/util/OxmModelLoader.java @@ -24,18 +24,14 @@ */ package org.openecomp.datarouter.util; -import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; -import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Timer; -import java.util.TimerTask; import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -50,11 +46,13 @@ import org.openecomp.cl.eelf.LoggerFactory; import org.openecomp.datarouter.logging.DataRouterMsgs; import org.openecomp.datarouter.util.ExternalOxmModelProcessor; +import org.springframework.core.io.Resource; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.core.io.support.ResourcePatternResolver; public class OxmModelLoader { - private static Map<String, DynamicJAXBContext> versionContextMap = new ConcurrentHashMap<String, DynamicJAXBContext>(); - private static Map<String, Timer> timers = new ConcurrentHashMap<String, Timer>(); + private static Map<String, DynamicJAXBContext> versionContextMap = new ConcurrentHashMap<String, DynamicJAXBContext>(); private static List<ExternalOxmModelProcessor> oxmModelProcessorRegistry = new ArrayList<ExternalOxmModelProcessor>(); final static Pattern p = Pattern.compile("aai_oxm_(.*).xml"); @@ -63,62 +61,44 @@ public class OxmModelLoader { private static org.openecomp.cl.api.Logger logger = LoggerFactory.getInstance() .getLogger(OxmModelLoader.class.getName()); - public synchronized static void loadModels() { + public synchronized static void loadModels() throws FileNotFoundException { + + ClassLoader cl = OxmModelLoader.class.getClassLoader(); + ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl); + Resource[] resources; + try { + resources = resolver.getResources("classpath*:/oxm/aai_oxm*.xml"); + } catch (IOException ex) { + logger.error(DataRouterMsgs.LOAD_OXM_ERROR, ex.getMessage()); + throw new FileNotFoundException("Unable to load OXM models from schema path : /oxm/aai_oxm*.xml"); + } + + if (resources.length == 0) { + logger.error(DataRouterMsgs.LOAD_OXM_ERROR, "No OXM schema files found on classpath"); + throw new FileNotFoundException("Unable to load OXM models from schema path : /oxm/aai_oxm*.xml"); + } + + for (Resource resource : resources) { + Matcher matcher = p.matcher(resource.getFilename()); + + if (matcher.matches()) { + try { + OxmModelLoader.loadModel(matcher.group(1), resource.getFilename(),resource.getInputStream()); + } catch (Exception e) { + logger.error(DataRouterMsgs.LOAD_OXM_ERROR, "Failed to load " + resource.getFilename() + + ": " + e.getMessage()); + } + } + } + - File[] listOfFiles = new File(DataRouterConstants.DR_HOME_MODEL).listFiles(); - - if (listOfFiles != null) { - for (File file : listOfFiles) { - if (file.isFile()) { - Matcher m = p.matcher(file.getName()); - if (m.matches()) { - try { - OxmModelLoader.loadModel(m.group(1), file); - } catch (Exception e) { - logger.error(DataRouterMsgs.INVALID_OXM_FILE, file.getName(), e.getMessage()); - } - } - - } - } - } else { - logger.error(DataRouterMsgs.INVALID_OXM_DIR, DataRouterConstants.DR_HOME_MODEL); - } - - } - private static void addtimer(String version,File file){ - TimerTask task = null; - task = new FileWatcher( - file) { - protected void onChange(File file) { - // here we implement the onChange - logger.info(DataRouterMsgs.FILE_CHANGED, file.getName()); - - try { - OxmModelLoader.loadModel(version,file); - } catch (Exception e) { - e.printStackTrace(); - } - - } - }; - - if (!timers.containsKey(version)) { - Timer timer = new Timer("oxm-"+version); - timer.schedule(task, new Date(), 10000); - timers.put(version, timer); - - } - } - - private synchronized static void loadModel(String version,File file) throws JAXBException, FileNotFoundException { + - - InputStream iStream = new FileInputStream(file); + private synchronized static void loadModel(String version,String resourceName,InputStream inputStream) throws JAXBException, FileNotFoundException { Map<String, Object> properties = new HashMap<String, Object>(); - properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream); + properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, inputStream); final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory .createContextFromOXM(Thread.currentThread().getContextClassLoader(), properties); versionContextMap.put(version, jaxbContext); @@ -126,20 +106,16 @@ public class OxmModelLoader { for ( ExternalOxmModelProcessor processor : oxmModelProcessorRegistry ) { processor.onOxmVersionChange(Version.valueOf(version), jaxbContext ); } - } - addtimer(version,file); - + } + logger.info(DataRouterMsgs.LOADED_OXM_FILE, resourceName); } public static DynamicJAXBContext getContextForVersion(String version) throws Exception { if (versionContextMap == null || versionContextMap.isEmpty()) { loadModels(); - } else if (!versionContextMap.containsKey(version)) { - try { - loadModel(version,new File (DataRouterConstants.DR_HOME_MODEL + "aai_oxm_" + version + ".xml")); - } catch (Exception e) { + } else if (!versionContextMap.containsKey(version)) { throw new Exception(Status.NOT_FOUND.toString()); - } + } return versionContextMap.get(version); diff --git a/src/main/java/org/openecomp/datarouter/util/Version.java b/src/main/java/org/openecomp/datarouter/util/Version.java index a0b3e5f..3a5d9aa 100644 --- a/src/main/java/org/openecomp/datarouter/util/Version.java +++ b/src/main/java/org/openecomp/datarouter/util/Version.java @@ -25,5 +25,5 @@ package org.openecomp.datarouter.util; public enum Version { - v2, v3, v4, v5, v6, v7, v8, v9, v10; + v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20; }
\ No newline at end of file diff --git a/src/main/resources/logging/DataRouterMsgs.properties b/src/main/resources/logging/DataRouterMsgs.properties index fbb24c4..fff262c 100644 --- a/src/main/resources/logging/DataRouterMsgs.properties +++ b/src/main/resources/logging/DataRouterMsgs.properties @@ -96,6 +96,10 @@ ROUTING_RESPONSE=\ OP_TIME=\ DR0013I|\ {0} took {1} ms. + +LOADED_OXM_FILE=\ + DR0014I|\ + Successfully loaded schema: {0} STARTUP_FAILURE=\ DR2001E|\ @@ -164,3 +168,6 @@ JSON_CONVERSION_ERROR=\ FAIL_TO_CREATE_UPDATE_DOC=\ DR3015E|\ Failed to create or update document in index {0}. Cause: {1} +LOAD_OXM_ERROR=\ + DR5000E|\ + Unable to load Oxm model: {0}\ |