diff options
author | Michael Arrastia <MArrasti@amdocs.com> | 2018-07-12 13:37:43 +0100 |
---|---|---|
committer | Michael Arrastia <MArrasti@amdocs.com> | 2018-07-12 14:01:38 +0100 |
commit | 7d3dcdbff807ba1facb84f94ac39ff91bf410b29 (patch) | |
tree | 0f72ce4d3a15e8c77ec1b0b81865cd942176d454 /src/main/java/org/onap/aai/spike/schema/OXMModelLoader.java | |
parent | 3105617e4686774f98b398ee8d6f8a7bd021f86a (diff) |
Migrate Spike code to ONAP
Move what was originally an Open ECOMP microservice into ONAP.
This is primarily a code move and includes:
- removal of AJSC and replacement with latest version of Spring Boot
- alignment with ONAP standards
- license headers
- package renaming
- FOSSology scan completed
Further work is required to add JJB and OOM.
Change-Id: I305c8407256bf2dbcc816f34f031f92eafd6ef5a
Issue-ID: AAI-1374
Signed-off-by: Michael Arrastia <MArrasti@amdocs.com>
Diffstat (limited to 'src/main/java/org/onap/aai/spike/schema/OXMModelLoader.java')
-rw-r--r-- | src/main/java/org/onap/aai/spike/schema/OXMModelLoader.java | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/src/main/java/org/onap/aai/spike/schema/OXMModelLoader.java b/src/main/java/org/onap/aai/spike/schema/OXMModelLoader.java new file mode 100644 index 0000000..0d174db --- /dev/null +++ b/src/main/java/org/onap/aai/spike/schema/OXMModelLoader.java @@ -0,0 +1,187 @@ +/** + * ============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.aai.spike.schema; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +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.spike.exception.SpikeException; +import org.onap.aai.spike.logging.SpikeMsgs; +import org.onap.aai.spike.util.SchemaIngestPropertiesReader; + +/** + * This class contains all of the logic for importing OXM model schemas from the available OXM + * schema files. + */ +public class OXMModelLoader { + + private static Map<String, DynamicJAXBContext> versionContextMap = + new ConcurrentHashMap<String, DynamicJAXBContext>(); + + final static Pattern p = Pattern.compile("aai_oxm_(.*).xml"); + final static Pattern versionPattern = Pattern.compile("V(\\d*)"); + + private static org.onap.aai.cl.api.Logger logger = + LoggerFactory.getInstance().getLogger(OXMModelLoader.class.getName()); + + /** + * Finds all OXM model files + * + * @throws SpikeException + * @throws IOException + * + */ + public synchronized static void loadModels() throws SpikeException { + SchemaIngestPropertiesReader schemaIngestPropReader = new SchemaIngestPropertiesReader(); + SchemaLocationsBean schemaLocationsBean = new SchemaLocationsBean(); + schemaLocationsBean.setNodeDirectory(schemaIngestPropReader.getNodeDir()); + schemaLocationsBean.setEdgeDirectory(schemaIngestPropReader.getEdgeDir()); + ConfigTranslator configTranslator = new OxmConfigTranslator(schemaLocationsBean); + NodeIngestor nodeIngestor = new NodeIngestor(configTranslator); + + if (logger.isDebugEnabled()) { + logger.debug("Loading OXM Models"); + } + + for (Version oxmVersion : Version.values()) { + DynamicJAXBContext jaxbContext = nodeIngestor.getContextForVersion(oxmVersion); + if (jaxbContext != null) { + loadModel(oxmVersion.toString(), jaxbContext); + } + } + } + + + private synchronized static void loadModel(String oxmVersion, DynamicJAXBContext jaxbContext) { + versionContextMap.put(oxmVersion, jaxbContext); + logger.info(SpikeMsgs.LOADED_OXM_FILE, oxmVersion); + } + + /** + * Retrieves the JAXB context for the specified OXM model version. + * + * @param version - The OXM version that we want the JAXB context for. + * + * @return - A JAXB context derived from the OXM model schema. + * + * @throws SpikeException + */ + public static DynamicJAXBContext getContextForVersion(String version) throws SpikeException { + + // 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)) { + throw new SpikeException("Error loading oxm model: " + version); + } + + return versionContextMap.get(version); + } + + public static String getLatestVersion() throws SpikeException { + + // 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()) { + throw new SpikeException("No available OXM schemas to get latest version for."); + } + + // Iterate over the available model versions to determine which is the most + // recent. + Integer latestVersion = null; + String latestVersionStr = null; + for (String versionKey : versionContextMap.keySet()) { + + Matcher matcher = versionPattern.matcher(versionKey); + if (matcher.find()) { + + int currentVersion = Integer.valueOf(matcher.group(1)); + + if ((latestVersion == null) || (currentVersion > latestVersion)) { + latestVersion = currentVersion; + latestVersionStr = versionKey; + } + } + } + + return latestVersionStr; + } + + /** + * Retrieves the map of all JAXB context objects that have been created by importing the available + * OXM model schemas. + * + * @return - Map of JAXB context objects. + */ + public static Map<String, DynamicJAXBContext> getVersionContextMap() { + return versionContextMap; + } + + /** + * Assigns the map of all JAXB context objects. + * + * @param versionContextMap + */ + public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) { + OXMModelLoader.versionContextMap = versionContextMap; + } + + /** + * Retrieves the list of all Loaded OXM versions. + * + * @return - A List of Strings of all loaded OXM versions. + * + * @throws SpikeException + */ + public static List<String> getLoadedOXMVersions() throws SpikeException { + // 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(SpikeMsgs.OXM_LOAD_ERROR, "No available OXM schemas to get versions for."); + throw new SpikeException("No available OXM schemas to get latest version for."); + } + List<String> versions = new ArrayList<String>(); + for (String versionKey : versionContextMap.keySet()) { + Matcher matcher = versionPattern.matcher(versionKey.toUpperCase()); + if (matcher.find()) { + versions.add("V" + matcher.group(1)); + } + } + return versions; + } +} |