Upgrade version of aai-common
[aai/spike.git] / src / main / java / org / onap / aai / spike / schema / OXMModelLoader.java
index 5d35017..7f41d8c 100644 (file)
@@ -20,6 +20,7 @@
  */
 package org.onap.aai.spike.schema;
 
+import com.google.common.base.CaseFormat;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -28,58 +29,64 @@ 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.aai.spike.exception.SpikeException;
 import org.onap.aai.spike.logging.SpikeMsgs;
-import org.onap.aai.spike.util.SchemaIngestPropertiesReader;
-import com.google.common.base.CaseFormat;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
 
 /**
  * This class contains all of the logic for importing OXM model schemas from the available OXM
  * schema files.
  */
+@Component
 public class OXMModelLoader {
 
-    private static Map<String, DynamicJAXBContext> versionContextMap =
-            new ConcurrentHashMap<String, DynamicJAXBContext>();
-    private static Map<String, HashMap<String, DynamicType>> xmlElementLookup  = new ConcurrentHashMap<String, HashMap<String, DynamicType>>();
-  
-    final static Pattern p = Pattern.compile("aai_oxm_(.*).xml");
-    final static Pattern versionPattern = Pattern.compile("V(\\d*)");
+    private static ConfigTranslator configTranslator;
+    private static NodeIngestor nodeIngestor;
+
+    private static Map<String, DynamicJAXBContext> versionContextMap = new ConcurrentHashMap<>();
+    private static Map<String, HashMap<String, DynamicType>> xmlElementLookup = new ConcurrentHashMap<>();
+
+    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() {}
+
+    /**
+     * This constructor presents an awkward marrying of Spring bean creation and static method use. This
+     * is technical debt that will need fixing.
+     *
+     * @param configTranslator contains schema versions configuration
+     * @param nodeIngestor provides DynamicJAXBContext for the OXM version
+     */
+    @Autowired
+    public OXMModelLoader(ConfigTranslator configTranslator, NodeIngestor nodeIngestor) {
+        OXMModelLoader.configTranslator = configTranslator;
+        OXMModelLoader.nodeIngestor = nodeIngestor;
+    }
+
     /**
      * 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()) {
+        for (SchemaVersion oxmVersion : configTranslator.getSchemaVersions().getVersions()) {
             DynamicJAXBContext jaxbContext = nodeIngestor.getContextForVersion(oxmVersion);
             if (jaxbContext != null) {
                 loadModel(oxmVersion.toString(), jaxbContext);
@@ -87,7 +94,6 @@ public class OXMModelLoader {
         }
     }
 
-
     private synchronized static void loadModel(String oxmVersion, DynamicJAXBContext jaxbContext) {
         versionContextMap.put(oxmVersion, jaxbContext);
         loadXmlLookupMap(oxmVersion, jaxbContext);
@@ -167,57 +173,57 @@ public class OXMModelLoader {
         OXMModelLoader.versionContextMap = versionContextMap;
     }
 
-  public static void loadXmlLookupMap(String version, DynamicJAXBContext jaxbContext) {
+    public static void loadXmlLookupMap(String version, DynamicJAXBContext jaxbContext) {
 
-    @SuppressWarnings("rawtypes")
-    List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
-    HashMap<String, DynamicType> types = new HashMap<String, DynamicType>();
+        @SuppressWarnings("rawtypes")
+        List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
+        HashMap<String, DynamicType> types = new HashMap<String, DynamicType>();
 
-    for (@SuppressWarnings("rawtypes")
-    Descriptor desc : descriptorsList) {
+        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 SpikeException {
-
-    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(SpikeMsgs.OXM_LOAD_ERROR, "Error loading oxm model: " + version);
-      throw new SpikeException("Error loading oxm model: " + version);
+            DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
+            String entityName = desc.getDefaultRootElement();
+            types.put(entityName, entity);
+        }
+        xmlElementLookup.put(version, types);
     }
 
-    // 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);
+    public static DynamicType getDynamicTypeForVersion(String version, String type) throws SpikeException {
 
-    if (xmlElementLookup.containsKey(version)) {
-    if (dynamicType == null) {
-      // Try to lookup by xml root element by exact match
-      dynamicType = xmlElementLookup.get(version).get(type);
-    }
+        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(SpikeMsgs.OXM_LOAD_ERROR, "Error loading oxm model: " + version);
+            throw new SpikeException("Error loading oxm model: " + version);
+        }
 
-    if (dynamicType == null) {
-      // Try to lookup by xml root element by lowercase
-      dynamicType = xmlElementLookup.get(version).get(type.toLowerCase());
-    }
+        // 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 (dynamicType == null) {
-      // Direct lookup as java-type name
-      dynamicType = versionContextMap.get(version).getDynamicType(type);
-    }
-    }
+        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);
+            }
+        }
 
-    return dynamicType;
-  }
+        return dynamicType;
+    }
 
     /**
      * Retrieves the list of all Loaded OXM versions.
@@ -238,7 +244,7 @@ public class OXMModelLoader {
         }
         List<String> versions = new ArrayList<String>();
         for (String versionKey : versionContextMap.keySet()) {
-            Matcher matcher = versionPattern.matcher(versionKey.toUpperCase());
+            Matcher matcher = versionPattern.matcher(versionKey);
             if (matcher.find()) {
                 versions.add("V" + matcher.group(1));
             }