aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/schema
diff options
context:
space:
mode:
authorBansal, Nitin (nb121v) <nitin.bansal@amdocs.com>2018-11-07 10:36:38 -0500
committerBansal, Nitin (nb121v) <nitin.bansal@amdocs.com>2018-11-07 10:37:47 -0500
commitdee6c34b3e5d3b40bc74fc73fafc1d116126c7fc (patch)
treec5428449b7b6618f5aba94e3f1db65f2b1e37030 /src/main/java/org/onap/schema
parentf83c10c4279f88bccecc195811c21acb2d6d0811 (diff)
Add swagger docs for gizmo APIs
Improve the way node type is resolved from OXM jaxbContext. Issue-ID: AAI-1882 Change-Id: Ia72fea0ac286dc96bb3b29d117b241b58b7eece7 Signed-off-by: Bansal, Nitin (nb121v) <nitin.bansal@amdocs.com>
Diffstat (limited to 'src/main/java/org/onap/schema')
-rw-r--r--src/main/java/org/onap/schema/OxmModelLoader.java66
-rw-r--r--src/main/java/org/onap/schema/validation/OxmModelValidator.java26
2 files changed, 76 insertions, 16 deletions
diff --git a/src/main/java/org/onap/schema/OxmModelLoader.java b/src/main/java/org/onap/schema/OxmModelLoader.java
index 8f55602..948914c 100644
--- a/src/main/java/org/onap/schema/OxmModelLoader.java
+++ b/src/main/java/org/onap/schema/OxmModelLoader.java
@@ -22,12 +22,18 @@ package org.onap.schema;
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;
@@ -38,10 +44,14 @@ import org.onap.crud.exception.CrudException;
import org.onap.crud.logging.CrudServiceMsgs;
import org.onap.schema.util.SchemaIngestPropertyReader;
+import com.google.common.base.CaseFormat;
+
public class OxmModelLoader {
private static Map<String, DynamicJAXBContext> versionContextMap =
new ConcurrentHashMap<>();
+
+ private static Map<String, HashMap<String, DynamicType>> xmlElementLookup = new ConcurrentHashMap<String, HashMap<String, DynamicType>>();
static final Pattern p = Pattern.compile("aai_oxm_(.*).xml");
static final Pattern versionPattern = Pattern.compile("V(\\d*)");
@@ -82,6 +92,7 @@ public class OxmModelLoader {
private static synchronized void loadModel(String oxmVersion, DynamicJAXBContext jaxbContext) {
versionContextMap.put(oxmVersion, jaxbContext);
+ loadXmlLookupMap(oxmVersion, jaxbContext);
logger.info(CrudServiceMsgs.LOADED_OXM_FILE, oxmVersion);
}
@@ -192,4 +203,59 @@ public class OxmModelLoader {
public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
OxmModelLoader.versionContextMap = versionContextMap;
}
+
+
+ public static void loadXmlLookupMap(String version, DynamicJAXBContext jaxbContext ) {
+
+ @SuppressWarnings("rawtypes")
+ List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
+ HashMap<String,DynamicType> types = new HashMap<String,DynamicType>();
+
+ 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);
+
+ //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);
+ }
+
+ 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;
+
+ }
}
diff --git a/src/main/java/org/onap/schema/validation/OxmModelValidator.java b/src/main/java/org/onap/schema/validation/OxmModelValidator.java
index d317678..9da835e 100644
--- a/src/main/java/org/onap/schema/validation/OxmModelValidator.java
+++ b/src/main/java/org/onap/schema/validation/OxmModelValidator.java
@@ -88,8 +88,7 @@ public class OxmModelValidator {
logger.error(CrudServiceMsgs.OXM_LOAD_ERROR, OXM_LOAD_ERROR + ": " + version);
throw new CrudException(OXM_LOAD_ERROR + ": " + version, Status.NOT_FOUND);
}
- final DynamicType modelObjectType = jaxbContext.getDynamicType(
- CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, type)));
+ final DynamicType modelObjectType = OxmModelLoader.getDynamicTypeForVersion(version, type);
final DynamicType reservedObjectType = jaxbContext.getDynamicType("ReservedPropNames");
for (String key : filter.keySet()) {
@@ -137,8 +136,7 @@ public class OxmModelValidator {
}
// Determine if the Object part is a collection type in the model
// definition
- final DynamicType modelObjectType = jaxbContext.getDynamicType(
- CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, type)));
+ final DynamicType modelObjectType = OxmModelLoader.getDynamicTypeForVersion(version, type);
if (modelObjectType == null) {
logger.error(CrudServiceMsgs.INVALID_OXM_FILE, "Object of collection type not found: " + type);
@@ -168,10 +166,8 @@ public class OxmModelValidator {
try {
type = resolveCollectionType(version, type);
DynamicJAXBContext jaxbContext = OxmModelLoader.getContextForVersion(version);
- String modelObjectClass = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL,
- CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, type));
- final DynamicType modelObjectType = jaxbContext.getDynamicType(modelObjectClass);
+ final DynamicType modelObjectType = OxmModelLoader.getDynamicTypeForVersion(version, type);
final DynamicType reservedType = jaxbContext.getDynamicType("ReservedPropNames");
Set<Map.Entry<String, JsonElement>> payloadEntriesSet = properties.getAsJsonObject().entrySet();
@@ -252,10 +248,8 @@ public class OxmModelValidator {
try {
type = resolveCollectionType(version, type);
DynamicJAXBContext jaxbContext = OxmModelLoader.getContextForVersion(version);
- String modelObjectClass = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL,
- CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, type));
-
- final DynamicType modelObjectType = jaxbContext.getDynamicType(modelObjectClass);
+
+ final DynamicType modelObjectType = OxmModelLoader.getDynamicTypeForVersion(version, type);
final DynamicType reservedType = jaxbContext.getDynamicType("ReservedPropNames");
Set<Map.Entry<String, JsonElement>> payloadEntriesSet = properties.getAsJsonObject().entrySet();
@@ -326,11 +320,11 @@ public class OxmModelValidator {
try {
DynamicJAXBContext jaxbContext = OxmModelLoader.getContextForVersion(version);
- String modelObjectClass = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL,
- CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL,
- vertex.getProperties().get(Metadata.NODE_TYPE.propertyName()) != null
- ? vertex.getProperties().get(Metadata.NODE_TYPE.propertyName()).toString() : vertex.getType()));
- final DynamicType modelObjectType = jaxbContext.getDynamicType(modelObjectClass);
+
+ final DynamicType modelObjectType = OxmModelLoader.getDynamicTypeForVersion(version,
+ vertex.getProperties().get(Metadata.NODE_TYPE.propertyName()) != null
+ ? vertex.getProperties().get(Metadata.NODE_TYPE.propertyName()).toString()
+ : vertex.getType());
final DynamicType reservedObjectType = jaxbContext.getDynamicType("ReservedPropNames");
for (String key : vertex.getProperties().keySet()) {