Added aai-utils and OXM Model Loader functionality 43/9443/2
authorShwetank Dave <shwetank.dave@amdocs.com>
Wed, 30 Aug 2017 14:21:33 +0000 (10:21 -0400)
committerShwetank Dave <shwetank.dave@amdocs.com>
Wed, 30 Aug 2017 16:13:53 +0000 (12:13 -0400)
Issue-ID: AAI-245 
Change-Id: Ic982cc07dd76e8a931d4f725ca4e71f2db7ef8a2
Signed-off-by: Shwetank Dave <shwetank.dave@amdocs.com>
.gitignore
aai-utils/License.txt [new file with mode: 0644]
aai-utils/pom.xml [new file with mode: 0644]
aai-utils/src/main/java/org/onap/aaiutils/oxm/OxmModelLoader.java [new file with mode: 0644]
aai-utils/src/main/java/org/onap/aaiutils/oxm/OxmModelLoaderConstants.java [new file with mode: 0644]
aai-utils/src/main/java/org/onap/aaiutils/oxm/OxmModelLoaderMsgs.java [new file with mode: 0644]
aai-utils/src/main/resources/oxm/OxmModelLoaderMsgs.properties [new file with mode: 0644]
aai-utils/src/test/java/org/onap/aaiutils/oxm/OxmModelLoaderTest.java [new file with mode: 0644]
pom.xml

index b181972..50983ee 100644 (file)
@@ -2,10 +2,11 @@
 .project
 target/
 **/logs/
+**/debug-logs/
 bundleconfig-local/etc/auth/aai-client-cert.p12
 bundleconfig-local/etc/auth/tomcat_keystore
 bundleconfig-local/etc/logback.xml
 /.pydevproject
 /bin/
-/.idea
+*/.idea
 *.iml
diff --git a/aai-utils/License.txt b/aai-utils/License.txt
new file mode 100644 (file)
index 0000000..cdf6f18
--- /dev/null
@@ -0,0 +1,21 @@
+============LICENSE_START=======================================================
+org.onap.aai
+================================================================================
+Copyright © 2017 AT&T Intellectual Property.
+Copyright © 2017 Amdocs
+All rights reserved.
+================================================================================
+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=========================================================
+
+ECOMP is a trademark and service mark of AT&T Intellectual Property.
diff --git a/aai-utils/pom.xml b/aai-utils/pom.xml
new file mode 100644 (file)
index 0000000..137b260
--- /dev/null
@@ -0,0 +1,138 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.onap.aai.aai-common</groupId>
+        <artifactId>aai-common</artifactId>
+        <version>1.1.0-SNAPSHOT</version>
+    </parent>
+    <artifactId>aai-utils</artifactId>
+    <name>aai-utils</name>
+    <version>1.1.0-SNAPSHOT</version>
+
+    <properties>
+        <checkstyle.config.location>google_checks.xml</checkstyle.config.location>
+        <sitePath>/content/sites/site/${project.groupId}/${project.artifactId}/${project.version}</sitePath>
+    </properties>
+
+
+    <dependencies>
+
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-databind</artifactId>
+            <version>2.8.7</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpclient</artifactId>
+            <version>4.5.3</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.eclipse.persistence</groupId>
+            <artifactId>eclipselink</artifactId>
+            <version>2.6.2</version>
+        </dependency>
+
+        <!-- Common logging framework -->
+        <dependency>
+            <groupId>org.openecomp.aai.logging-service</groupId>
+            <artifactId>common-logging</artifactId>
+            <version>1.0.0</version>
+        </dependency>
+
+        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-core</artifactId>
+            <version>4.3.10.RELEASE</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.onap.aai.aai-common</groupId>
+            <artifactId>aai-schema</artifactId>
+            <version>1.1.0-SNAPSHOT</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>*</groupId>
+                    <artifactId>*</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.11</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+
+    <build>
+        <plugins>
+            <!-- license plugin -->
+            <plugin>
+                <groupId>com.mycila</groupId>
+                <artifactId>license-maven-plugin</artifactId>
+                <version>3.0</version>
+                <configuration>
+                    <header>License.txt</header>
+                    <includes>
+                        <include>src/main/java/**</include>
+                    </includes>
+                </configuration>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>format</goal>
+                        </goals>
+                        <phase>process-sources</phase>
+                    </execution>
+                </executions>
+            </plugin>
+
+
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-site-plugin</artifactId>
+                <version>3.3</version>
+            </plugin>
+            <plugin>
+                <groupId>org.sonatype.plugins</groupId>
+                <artifactId>nexus-staging-maven-plugin</artifactId>
+            </plugin>
+        </plugins>
+    </build>
+
+    <distributionManagement>
+        <site>
+            <id>ecomp-site</id>
+            <url>dav:${onap.nexus.url}${sitePath}</url>
+        </site>
+    </distributionManagement>
+
+    <reporting>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-checkstyle-plugin</artifactId>
+                <version>2.17</version>
+                <reportSets>
+                    <reportSet>
+                        <reports>
+                            <report>checkstyle</report>
+                        </reports>
+                    </reportSet>
+                </reportSets>
+            </plugin>
+        </plugins>
+    </reporting>
+
+
+</project>
\ No newline at end of file
diff --git a/aai-utils/src/main/java/org/onap/aaiutils/oxm/OxmModelLoader.java b/aai-utils/src/main/java/org/onap/aaiutils/oxm/OxmModelLoader.java
new file mode 100644 (file)
index 0000000..9d9d9ad
--- /dev/null
@@ -0,0 +1,129 @@
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2017 AT&T Intellectual Property.
+ * Copyright © 2017 Amdocs
+ * All rights reserved.
+ * ================================================================================
+ * 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=========================================================
+ *
+ * ECOMP is a trademark and service mark of AT&T Intellectual Property.
+ */
+package org.onap.aaiutils.oxm;
+
+import org.eclipse.persistence.jaxb.JAXBContextProperties;
+import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
+import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
+import org.openecomp.cl.eelf.LoggerFactory;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
+import org.springframework.core.io.support.ResourcePatternResolver;
+
+import java.io.*;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.xml.bind.JAXBException;
+
+
+public class OxmModelLoader {
+
+    final static Pattern p = Pattern.compile("aai_oxm_(.*).xml");
+
+  private static Map<String, DynamicJAXBContext> versionContextMap  = new ConcurrentHashMap();
+
+  private static org.openecomp.cl.api.Logger logger = LoggerFactory.getInstance()
+      .getLogger(OxmModelLoader.class.getName());
+
+  public synchronized static void loadModels() throws Exception {
+    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( OxmModelLoaderMsgs.OXM_LOAD_ERROR, ex.getMessage());
+      throw new FileNotFoundException("OXM files not found on classpath.");
+    }
+
+    if (resources.length == 0) {
+      logger.error(OxmModelLoaderMsgs.OXM_LOAD_ERROR, "No OXM schema files found on classpath");
+      throw new Exception("Failed to load schema");
+    }
+
+    for (Resource resource : resources) {
+      Matcher matcher = p.matcher(resource.getFilename());
+
+      if (matcher.matches()) {
+        try {
+          OxmModelLoader.loadModel(matcher.group(1), resource);
+        } catch (Exception e) {
+          logger.error(OxmModelLoaderMsgs.OXM_LOAD_ERROR, "Failed to load " + resource.getFilename()
+              + ": " + e.getMessage());
+          throw new Exception("Failed to load schema");
+        }
+      }
+    }
+  }
+
+  public static DynamicJAXBContext getContextForVersion(String version) throws Exception {
+    if (versionContextMap == null || versionContextMap.isEmpty()) {
+      loadModels();
+    } else if (!versionContextMap.containsKey(version)) {
+      String filename = OxmModelLoaderConstants.AaiUtils_HOME_MODEL + "aai_oxm_" + version + ".xml";
+      try {
+        loadModel(version, new File(filename));
+      } catch (Exception e) {
+        throw new FileNotFoundException(filename);
+      }
+    }
+
+    return versionContextMap.get(version);
+  }
+
+  public static Map<String, DynamicJAXBContext> getVersionContextMap() {
+    return versionContextMap;
+  }
+
+  public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
+    OxmModelLoader.versionContextMap = versionContextMap;
+  }
+
+  private synchronized static void loadModel ( String version, File file )
+          throws JAXBException, IOException {
+      InputStream inputStream = new FileInputStream ( file );
+      loadModel ( version, file.getName (), inputStream );
+  }
+
+  private synchronized static void loadModel ( String version, Resource resource )
+          throws JAXBException, IOException {
+      InputStream inputStream = resource.getInputStream ();
+      loadModel ( version, resource.getFilename (), inputStream );
+  }
+
+  private synchronized static void loadModel ( String version, String resourceName,
+                                               InputStream inputStream )
+          throws JAXBException, IOException {
+      Map<String, Object> properties = new HashMap<String, Object> ();
+      properties.put ( JAXBContextProperties.OXM_METADATA_SOURCE, inputStream );
+      final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory
+              .createContextFromOXM ( Thread.currentThread ().getContextClassLoader (), properties );
+      versionContextMap.put ( version, jaxbContext );
+      logger.info ( OxmModelLoaderMsgs.LOADED_OXM_FILE, resourceName );
+    }
+
+}
diff --git a/aai-utils/src/main/java/org/onap/aaiutils/oxm/OxmModelLoaderConstants.java b/aai-utils/src/main/java/org/onap/aaiutils/oxm/OxmModelLoaderConstants.java
new file mode 100644 (file)
index 0000000..0f8a54f
--- /dev/null
@@ -0,0 +1,34 @@
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2017 AT&T Intellectual Property.
+ * Copyright © 2017 Amdocs
+ * All rights reserved.
+ * ================================================================================
+ * 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=========================================================
+ *
+ * ECOMP is a trademark and service mark of AT&T Intellectual Property.
+ */
+package org.onap.aaiutils.oxm;
+
+public class OxmModelLoaderConstants {
+
+    public static final String AaiUtils_FILESEP = (System.getProperty ( "file.separator" ) == null) ? "/"
+            : System.getProperty ( "file.separator" );
+
+    public static final String AaiUtils_SPECIFIC_CONFIG = System.getProperty ( "CONFIG_HOME" ) + AaiUtils_FILESEP;
+
+    public static final String AaiUtils_HOME_MODEL = AaiUtils_SPECIFIC_CONFIG + "model" + AaiUtils_FILESEP;
+}
diff --git a/aai-utils/src/main/java/org/onap/aaiutils/oxm/OxmModelLoaderMsgs.java b/aai-utils/src/main/java/org/onap/aaiutils/oxm/OxmModelLoaderMsgs.java
new file mode 100644 (file)
index 0000000..c94e3f1
--- /dev/null
@@ -0,0 +1,64 @@
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2017 AT&T Intellectual Property.
+ * Copyright © 2017 Amdocs
+ * All rights reserved.
+ * ================================================================================
+ * 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=========================================================
+ *
+ * ECOMP is a trademark and service mark of AT&T Intellectual Property.
+ */
+package org.onap.aaiutils.oxm;
+
+import com.att.eelf.i18n.EELFResourceManager;
+import org.openecomp.cl.eelf.LogMessageEnum;
+
+public enum OxmModelLoaderMsgs implements LogMessageEnum {
+
+
+    /**
+     * Invalid Model File
+     * Arguments:
+     * {0} = File/Dir
+     * {1} = error
+     */
+
+    INVALID_OXM_FILE,
+    INVALID_OXM_DIR,
+
+    /**
+     * Unable to load OXM schema: {0}
+     *
+     * <p>Arguments:
+     * {0} = error
+     */
+    OXM_LOAD_ERROR,
+
+    /**
+     * Successfully loaded schema: {0}
+     *
+     * <p>Arguments:
+     * {0} = oxm filename
+     */
+    LOADED_OXM_FILE;
+
+    /**
+     * Static initializer to ensure the resource bundles for this class are loaded...
+     */
+    static {
+        EELFResourceManager.loadMessageBundle("oxm/OxmModelLoaderMsgs");
+    }
+}
diff --git a/aai-utils/src/main/resources/oxm/OxmModelLoaderMsgs.properties b/aai-utils/src/main/resources/oxm/OxmModelLoaderMsgs.properties
new file mode 100644 (file)
index 0000000..d4a5559
--- /dev/null
@@ -0,0 +1,40 @@
+#Resource key=Error Code|Message text|Resolution text |Description text
+#######
+#Newlines can be utilized to add some clarity ensuring continuing line
+#has atleast one leading space
+#ResourceKey=\
+#             CA0000E\
+#             Sample error msg txt\
+#             Sample resolution msg\
+#             Sample description txt
+#
+######
+#Error code classification category
+#000     Info/Debug
+#100    Permission errors
+#200    Availability errors/Timeouts
+#300    Data errors
+#400    Schema Interface type/validation errors
+#500    Business process errors
+#900    Unknown errors
+#
+########################################################################
+
+INVALID_OXM_FILE=\
+            CRD0002I|\
+            Unable to parse schema file: {0} due to error : {1}\
+            
+OXM_FILE_CHANGED=\
+            CRD0003I|\
+            OXM file change detected: {0}\
+        
+INVALID_OXM_DIR=\
+            CRD0005I|\
+            Invalid OXM dir: {0}\
+      
+LOADED_OXM_FILE=\
+            CRD0007I|\
+            Successfully loaded schema: {0}
+OXM_LOAD_ERROR=\
+            CRD0503E|\
+            Unable to load OXM schema: {0}
diff --git a/aai-utils/src/test/java/org/onap/aaiutils/oxm/OxmModelLoaderTest.java b/aai-utils/src/test/java/org/onap/aaiutils/oxm/OxmModelLoaderTest.java
new file mode 100644 (file)
index 0000000..81e5072
--- /dev/null
@@ -0,0 +1,13 @@
+package org.onap.aaiutils.oxm;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class OxmModelLoaderTest {
+    @Test
+    public void loadModels() throws Exception {
+        OxmModelLoader.loadModels();
+        assertTrue( OxmModelLoader.getVersionContextMap().size() > 0);
+    }
+}
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 5636144..d2ca727 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -18,6 +18,7 @@
         <module>aai-annotations</module>
         <module>aai-core</module>
         <module>aai-auth</module>
+           <module>aai-utils</module>
     </modules>
 
     <properties>