summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKajur, Harish (vk250x) <vk250x@att.com>2019-04-07 20:18:24 -0400
committerKajur, Harish (vk250x) <vk250x@att.com>2019-04-07 20:18:24 -0400
commit86115147f09f2f2a0702a82c1895e13ff7f37131 (patch)
treebed838c28320e0dc580ac06e8a15dbbc6ceeba99
parent08c1bd4d78a0504e7531876869a7ae0cd9c448df (diff)
Optimize the areas where its creating extra memory
After doing some analysis using profiler found that the most cases where memory is being spent when doing an GET is during the conversion from one case to another case and instead cached that during the start of the application Issue-ID: AAI-2331 Change-Id: I291d5f953d4158daca293198cf6fc7f5cf86d25d Signed-off-by: Kajur, Harish (vk250x) <vk250x@att.com>
-rw-r--r--aai-core/src/main/java/org/onap/aai/introspection/Introspector.java22
-rw-r--r--aai-core/src/main/java/org/onap/aai/introspection/MoxyLoader.java47
-rw-r--r--aai-core/src/main/java/org/onap/aai/introspection/MoxyStrategy.java99
-rw-r--r--aai-schema-ingest/src/main/java/org/onap/aai/nodes/CaseFormatStore.java105
-rw-r--r--aai-schema-ingest/src/main/java/org/onap/aai/nodes/NodeIngestor.java39
-rw-r--r--aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaVersion.java24
6 files changed, 224 insertions, 112 deletions
diff --git a/aai-core/src/main/java/org/onap/aai/introspection/Introspector.java b/aai-core/src/main/java/org/onap/aai/introspection/Introspector.java
index 269d6330..21299f79 100644
--- a/aai-core/src/main/java/org/onap/aai/introspection/Introspector.java
+++ b/aai-core/src/main/java/org/onap/aai/introspection/Introspector.java
@@ -24,9 +24,12 @@ import com.att.eelf.configuration.EELFManager;
import com.google.common.base.CaseFormat;
import org.apache.commons.lang.ClassUtils;
import org.eclipse.persistence.exceptions.DynamicException;
+import org.onap.aai.config.SpringContextAware;
import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
import org.onap.aai.logging.ErrorLogHelper;
import org.onap.aai.logging.LogFormatTools;
+import org.onap.aai.nodes.CaseFormatStore;
+import org.onap.aai.nodes.NodeIngestor;
import org.onap.aai.restcore.MediaType;
import org.onap.aai.schema.enums.ObjectMetadata;
import org.onap.aai.schema.enums.PropertyMetadata;
@@ -50,13 +53,26 @@ public abstract class Introspector implements Cloneable {
private Set<String> uniqueProperties = null;
private Set<String> indexedProperties = null;
private Set<String> allKeys = null;
+
+ protected CaseFormatStore caseFormatStore = null;
+ protected NodeIngestor nodeIngestor;
+
protected Introspector(Object obj) {
+ this.nodeIngestor = SpringContextAware.getBean(NodeIngestor.class);
+ this.caseFormatStore = nodeIngestor.getCaseFormatStore();
}
public abstract boolean hasProperty(String name);
protected String convertPropertyName (String name) {
- return CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, name);
+ return caseFormatStore
+ .fromLowerHyphenToLowerCamel(name)
+ .orElseGet(
+ () -> {
+ LOGGER.debug("Unable to find {} in the store from lower hyphen to lower camel", name);
+ return CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, name);
+ }
+ );
}
protected abstract Object get(String name);
@@ -198,7 +214,7 @@ public abstract class Introspector implements Cloneable {
* @param obj the value to be set
* @return
*/
- public void setValue(String name, Object obj) throws IllegalArgumentException {
+ public void setValue(String name, Object obj) {
Object box = this.castValueAccordingToSchema(name, obj);
name = convertPropertyName(name);
@@ -558,8 +574,6 @@ public abstract class Introspector implements Cloneable {
public abstract String marshal(MarshallerProperties properties);
- public abstract Object clone();
-
public abstract Object getUnderlyingObject();
public String marshal(boolean formatted) {
diff --git a/aai-core/src/main/java/org/onap/aai/introspection/MoxyLoader.java b/aai-core/src/main/java/org/onap/aai/introspection/MoxyLoader.java
index fa52d62f..35583d7c 100644
--- a/aai-core/src/main/java/org/onap/aai/introspection/MoxyLoader.java
+++ b/aai-core/src/main/java/org/onap/aai/introspection/MoxyLoader.java
@@ -32,12 +32,12 @@ import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
import org.onap.aai.introspection.exceptions.AAIUnmarshallingException;
import org.onap.aai.logging.ErrorLogHelper;
import org.onap.aai.logging.LogFormatTools;
+import org.onap.aai.nodes.CaseFormatStore;
import org.onap.aai.nodes.NodeIngestor;
import org.onap.aai.restcore.MediaType;
import org.onap.aai.schema.enums.ObjectMetadata;
import org.onap.aai.setup.SchemaVersion;
import org.onap.aai.workarounds.NamingExceptions;
-import org.springframework.stereotype.Component;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
@@ -49,19 +49,22 @@ import java.util.stream.Collectors;
public class MoxyLoader extends Loader {
+ private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(MoxyLoader.class);
+
private DynamicJAXBContext jaxbContext = null;
- private EELFLogger LOGGER = EELFManager.getInstance().getLogger(MoxyLoader.class);
private Map<String, Introspector> allObjs = null;
private Map<SchemaVersion, MoxyLoader> moxyLoaderFactory;
private NodeIngestor nodeIngestor;
+ private CaseFormatStore caseFormatStore;
private Set<String> namedProps;
public MoxyLoader(SchemaVersion version, NodeIngestor nodeIngestor) {
super(version, ModelType.MOXY);
this.nodeIngestor = nodeIngestor;
+ this.caseFormatStore = nodeIngestor.getCaseFormatStore();
process(version);
}
@@ -79,6 +82,16 @@ public class MoxyLoader extends Loader {
return IntrospectorFactory.newInstance(ModelType.MOXY, objectFromName(name));
}
+ private boolean containsUpperCase(String str){
+
+ for(int i = 0; i < str.length(); i++){
+ if(Character.isUpperCase(str.charAt(i))){
+ return true;
+ }
+ }
+
+ return false;
+ }
/**
* {@inheritDoc}
*/
@@ -92,10 +105,17 @@ public class MoxyLoader extends Loader {
final String upperCamel;
//Contains any uppercase, then assume it's upper camel
- if (name.matches(".*[A-Z].*")) {
+ if (containsUpperCase(name)) {
upperCamel = sanitizedName;
} else {
- upperCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, sanitizedName);
+ upperCamel = caseFormatStore
+ .fromLowerHyphenToUpperCamel(sanitizedName)
+ .orElseGet(
+ () -> {
+ LOGGER.debug("Unable to find {} in the store for lower hyphen to upper camel", sanitizedName);
+ return CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, sanitizedName);
+ }
+ );
}
try {
@@ -153,7 +173,7 @@ public class MoxyLoader extends Loader {
if (this.allObjs != null) {
return allObjs;
} else {
- ImmutableMap.Builder<String, Introspector> map = new ImmutableMap.Builder<String, Introspector>();
+ ImmutableMap.Builder<String, Introspector> map = new ImmutableMap.Builder<>();
Set<String> objs = objectsInVersion();
for (String objName : objs) {
try {
@@ -178,7 +198,6 @@ public class MoxyLoader extends Loader {
LOGGER.warn("Exception while enumerating objects for API version " + getVersion() + " (returning partial results) " + LogFormatTools.getStackTop(e));
}
- //result.remove("EdgePropNames");
return result;
}
@@ -199,20 +218,4 @@ public class MoxyLoader extends Loader {
public DynamicJAXBContext getJAXBContext() {
return this.jaxbContext;
}
-
- /*
- * Im keeping this for now - Just in case
- */
- /*private static class Helper {
- private static final Map<SchemaVersion, MoxyLoader> INSTANCEMAP = new ConcurrentHashMap<>();
-
- private Helper() {}
-
- private static MoxyLoader getLoaderBySchemaVersion(SchemaVersion v) {
- if (!INSTANCEMAP.containsKey(v)) {
- INSTANCEMAP.put(v, new MoxyLoader(v, nodeIngestor));
- }
- return INSTANCEMAP.get(v);
- }
- }*/
}
diff --git a/aai-core/src/main/java/org/onap/aai/introspection/MoxyStrategy.java b/aai-core/src/main/java/org/onap/aai/introspection/MoxyStrategy.java
index f0d487e8..455e7846 100644
--- a/aai-core/src/main/java/org/onap/aai/introspection/MoxyStrategy.java
+++ b/aai-core/src/main/java/org/onap/aai/introspection/MoxyStrategy.java
@@ -23,35 +23,31 @@ import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.google.common.base.CaseFormat;
import com.google.common.base.Joiner;
-
-
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.dynamic.DynamicEntity;
import org.eclipse.persistence.dynamic.DynamicType;
import org.eclipse.persistence.exceptions.DynamicException;
-import org.eclipse.persistence.jaxb.UnmarshallerProperties;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.oxm.XMLField;
import org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping;
import org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping;
import org.onap.aai.config.SpringContextAware;
+import org.onap.aai.logging.LogFormatTools;
+import org.onap.aai.nodes.CaseFormatStore;
import org.onap.aai.nodes.NodeIngestor;
import org.onap.aai.restcore.MediaType;
import org.onap.aai.schema.enums.ObjectMetadata;
import org.onap.aai.schema.enums.PropertyMetadata;
import org.onap.aai.setup.SchemaVersion;
import org.springframework.web.util.UriUtils;
+
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
-import javax.xml.bind.Unmarshaller;
-import javax.xml.transform.stream.StreamSource;
-import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.Map.Entry;
-import java.util.stream.Collectors;
public class MoxyStrategy extends Introspector {
@@ -60,8 +56,6 @@ public class MoxyStrategy extends Introspector {
private DynamicType internalType = null;
private DynamicJAXBContext jaxbContext = null;
private ClassDescriptor cd = null;
- private Marshaller marshaller = null;
- private Unmarshaller unmarshaller = null;
private SchemaVersion version = null;
private Set<String> properties = null;
private Set<String> keys = null;
@@ -69,14 +63,11 @@ public class MoxyStrategy extends Introspector {
private boolean isInitialized = false;
- private NodeIngestor nodeIngestor;
-
protected MoxyStrategy(Object obj) {
super(obj);
/* must look up the correct jaxbcontext for this object */
className = MoxyStrategy.class.getSimpleName();
internalObject = (DynamicEntity)obj;
- nodeIngestor = SpringContextAware.getBean(NodeIngestor.class);
version = nodeIngestor.getVersionFromClassName(internalObject.getClass().getName());
super.loader = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(getModelType(), version);
jaxbContext = nodeIngestor.getContextForVersion(version);
@@ -84,15 +75,6 @@ public class MoxyStrategy extends Introspector {
internalType = jaxbContext.getDynamicType(simpleName);
cd = internalType.getDescriptor();
- try {
- marshaller = jaxbContext.createMarshaller();
-
- unmarshaller = jaxbContext.createUnmarshaller();
-
- } catch (JAXBException e) {
-
- }
-
}
private void init() {
@@ -100,21 +82,26 @@ public class MoxyStrategy extends Introspector {
Set<String> props = new LinkedHashSet<>();
for (String s : internalType.getPropertiesNames()) {
- props.add(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, s));
+ String value = caseFormatStore
+ .fromLowerCamelToLowerHyphen(s)
+ .orElseGet(
+ () -> {
+ LOGGER.debug("Unable to find {} in the store from lower camel to lower hyphen", s);
+ return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, s);
+ }
+ );
+ props.add(value);
}
props = Collections.unmodifiableSet(props);
this.properties = props;
Set<String> requiredProps = new LinkedHashSet<>();
- requiredProps = new LinkedHashSet<>();
for (DatabaseMapping dm : cd.getMappings()) {
if (dm.getField() instanceof XMLField) {
XMLField x = (XMLField)dm.getField();
- if (x != null) {
- if (x.isRequired()) {
- requiredProps.add(this.removeXPathDescriptor(x.getName()));
- }
+ if (x != null && x.isRequired()) {
+ requiredProps.add(this.removeXPathDescriptor(x.getName()));
}
}
}
@@ -145,7 +132,7 @@ public class MoxyStrategy extends Introspector {
}
@Override
- public void set(String name, Object obj) throws IllegalArgumentException {
+ public void set(String name, Object obj){
internalObject.set(name, obj);
}
@@ -257,10 +244,26 @@ public class MoxyStrategy extends Introspector {
public String getChildName() {
String className = internalObject.getClass().getSimpleName();
- String lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, className);
+ String lowerHyphen = caseFormatStore
+ .fromUpperCamelToLowerHyphen(className)
+ .orElseGet(
+ () -> {
+ LOGGER.debug("Unable to find {} in the store for upper camel to lower hyphen", className);
+ return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, className);
+ }
+ );
if (this.isContainer()) {
- lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,this.getGenericTypeClass(this.getProperties().iterator().next()).getSimpleName());
+ String upperCamel = this.getGenericTypeClass(this.getProperties().iterator().next()).getSimpleName();
+
+ lowerHyphen = caseFormatStore
+ .fromUpperCamelToLowerHyphen(upperCamel)
+ .orElseGet(
+ () -> {
+ LOGGER.debug("Unable to find {} in the store for upper camel to lower hyphen", upperCamel);
+ return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, upperCamel);
+ }
+ );
}
return lowerHyphen;
@@ -269,14 +272,12 @@ public class MoxyStrategy extends Introspector {
@Override
public String getName() {
String className = internalObject.getClass().getSimpleName();
- String lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, className);
- /*
- if (this.isContainer()) {
- lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,this.getGenericTypeClass(this.getProperties().get(0)).getSimpleName());
- }*/
-
-
- return lowerHyphen;
+ return caseFormatStore
+ .fromUpperCamelToLowerHyphen(className)
+ .orElseGet(() -> {
+ LOGGER.debug("Unable to find {} in the store for upper camel to lower hyphen", className);
+ return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, className);
+ });
}
@Override
@@ -313,7 +314,6 @@ public class MoxyStrategy extends Introspector {
@Override
public String preProcessKey (String key) {
String result = "";
- //String trimmedRestURI = restURI.replaceAll("/[\\w\\-]+?/[\\w\\-]+?$", "");
String[] split = key.split("/");
int i = 0;
for (i = split.length-1; i >= 0; i--) {
@@ -334,6 +334,7 @@ public class MoxyStrategy extends Introspector {
public String marshal(MarshallerProperties properties) {
StringWriter result = new StringWriter();
try {
+ Marshaller marshaller = jaxbContext.createMarshaller();
if (properties.getMediaType().equals(MediaType.APPLICATION_JSON_TYPE)) {
marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_INCLUDE_ROOT, properties.getIncludeRoot());
@@ -344,31 +345,13 @@ public class MoxyStrategy extends Introspector {
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, properties.getFormatted());
marshaller.marshal(this.internalObject, result);
} catch (JAXBException e) {
- //e.printStackTrace();
+ LOGGER.warn("Encountered an jaxb exception during marshalling ", LogFormatTools.getStackTop(e));
}
return result.toString();
}
@Override
- public Object clone() {
- Object result = null;
- try {
- unmarshaller = jaxbContext.createUnmarshaller();
-
- unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
- unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
- unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
-
- result = unmarshaller.unmarshal(new StreamSource(new StringReader(this.marshal(true))), this.internalObject.getClass()).getValue();
- } catch (JAXBException e) {
- // TODO Auto-generated catch block
- //e.printStackTrace();
- }
- result = IntrospectorFactory.newInstance(getModelType(), result);
- return result;
- }
- @Override
public ModelType getModelType() {
return ModelType.MOXY;
}
diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/nodes/CaseFormatStore.java b/aai-schema-ingest/src/main/java/org/onap/aai/nodes/CaseFormatStore.java
new file mode 100644
index 00000000..69a153da
--- /dev/null
+++ b/aai-schema-ingest/src/main/java/org/onap/aai/nodes/CaseFormatStore.java
@@ -0,0 +1,105 @@
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2019 AT&T Intellectual Property. 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=========================================================
+ */
+package org.onap.aai.nodes;
+
+import com.google.common.base.CaseFormat;
+import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * CaseFormatStore stores the converted strings from
+ * lower hyphen (example-object) to lower camel case (exampleObject)
+ * so it avoids the creation of the object for every single request
+ * and cause an issue with taking too much memory just for the conversion
+ */
+public class CaseFormatStore {
+
+ private final Map<String, String> lowerHyphenToLowerCamel = new HashMap<>();
+ private final Map<String, String> lowerHyphenToUpperCamel = new HashMap<>();
+ private final Map<String, String> lowerCamelToLowerHyphen = new HashMap<>();
+ private final Map<String, String> upperCamelToLowerHyphen = new HashMap<>();
+
+ CaseFormatStore(){}
+
+ /**
+ * Parses the document and creates a lower camel case string
+ * upper camel string, lower hyphen and lower camel case
+ *
+ * @param doc Takes an xml document and adds it to the hash maps as appropriate
+ */
+ void parse(Document doc){
+
+ // Get the xml-root-element and add those nodes
+ // with the attribute name and it to the hashmaps
+ // For the attribute with name, it is going to be lower-hyphen
+ // If the attribute is javaAttribute then it will be lower camel case
+ NodeList list = doc.getElementsByTagName("xml-root-element");
+ addCaseFormatForNodesAndProperties(list, "name");
+
+ list = doc.getElementsByTagName("xml-element");
+ addCaseFormatForNodesAndProperties(list, "java-attribute");
+
+ list = doc.getElementsByTagName("xml-any-element");
+ addCaseFormatForNodesAndProperties(list, "java-attribute");
+ }
+
+ private void addCaseFormatForNodesAndProperties(NodeList list, String attributeName) {
+ for (int i = 0; i < list.getLength(); i++) {
+
+ String lowerCamel = null;
+ String lowerHyphen = null;
+
+ if ("java-attribute".equals(attributeName)) {
+ lowerCamel = list.item(i).getAttributes().getNamedItem(attributeName).getNodeValue();
+ lowerHyphen = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, lowerCamel);
+ } else {
+ lowerHyphen = list.item(i).getAttributes().getNamedItem(attributeName).getNodeValue();
+ lowerCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, lowerHyphen);
+ }
+
+ String upperCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, lowerHyphen);
+ lowerHyphenToLowerCamel.put(lowerHyphen, lowerCamel);
+ lowerHyphenToUpperCamel.put(lowerHyphen, upperCamel);
+ upperCamelToLowerHyphen.put(upperCamel, lowerHyphen);
+ lowerCamelToLowerHyphen.put(lowerCamel, lowerHyphen);
+ }
+ }
+
+ public Optional<String> fromLowerHyphenToLowerCamel(String value){
+ return Optional.ofNullable(lowerHyphenToLowerCamel.get(value));
+ }
+
+ public Optional<String> fromLowerHyphenToUpperCamel(String value){
+ return Optional.ofNullable(lowerHyphenToUpperCamel.get(value));
+ }
+
+ public Optional<String> fromUpperCamelToLowerHyphen(String value){
+ return Optional.ofNullable(upperCamelToLowerHyphen.get(value));
+ }
+
+ public Optional<String> fromLowerCamelToLowerHyphen(String value){
+ return Optional.ofNullable(lowerCamelToLowerHyphen.get(value));
+ }
+
+}
diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/nodes/NodeIngestor.java b/aai-schema-ingest/src/main/java/org/onap/aai/nodes/NodeIngestor.java
index 69cd51ab..82991f06 100644
--- a/aai-schema-ingest/src/main/java/org/onap/aai/nodes/NodeIngestor.java
+++ b/aai-schema-ingest/src/main/java/org/onap/aai/nodes/NodeIngestor.java
@@ -62,14 +62,14 @@ public class NodeIngestor {
private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(NodeIngestor.class);
private static final Pattern classNamePattern = Pattern.compile("\\.(v\\d+)\\.");
- Map<SchemaVersion, List<String>> filesToIngest;
- private Map<SchemaVersion, DynamicJAXBContext> versionContextMap = new TreeMap<>();
- private Map<SchemaVersion, Set<String>> typesPerVersion = new TreeMap<>();
- private Map<SchemaVersion, Document> schemaPerVersion = new TreeMap<>();
+ private Map<SchemaVersion, DynamicJAXBContext> versionContextMap = new HashMap<>();
+ private Map<SchemaVersion, Set<String>> typesPerVersion = new HashMap<>();
+ private Map<SchemaVersion, Document> schemaPerVersion = new HashMap<>();
private String localSchema;
private SchemaVersions schemaVersions;
private Set<Translator> translators;
-
+
+ private CaseFormatStore caseFormatStore;
//TODO : See if you can get rid of InputStream resets
/**
* Instantiates the NodeIngestor bean.
@@ -82,6 +82,7 @@ public class NodeIngestor {
public NodeIngestor(Set<Translator> translatorSet) {
LOGGER.debug("Local Schema files will be fetched");
this.translators = translatorSet;
+ this.caseFormatStore = new CaseFormatStore();
}
@PostConstruct
@@ -128,7 +129,7 @@ public class NodeIngestor {
final DynamicJAXBContext ctx = ingest(inputStreams);
versionContextMap.put(version, ctx);
- typesPerVersion.put(version, getAllNodeTypes(inputStreams));
+ setAllTypesAndProperties(version, inputStreams);
schemaPerVersion.put(version, createCombinedSchema(inputStreams, version, retrieveLocalSchema));
}
} catch (JAXBException | ParserConfigurationException | SAXException | IOException e) {
@@ -152,8 +153,7 @@ public class NodeIngestor {
return DynamicJAXBContextFactory.createContextFromOXM(this.getClass().getClassLoader(), properties);
}
- private Set<String> getAllNodeTypes(List<InputStream> inputStreams) throws ParserConfigurationException, SAXException, IOException {
- //Reset the InputStream to reset the offset to inital position
+ private void setAllTypesAndProperties(SchemaVersion version, List<InputStream> inputStreams) throws ParserConfigurationException, IOException, SAXException {
Set<String> types = new HashSet<>();
final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
@@ -164,15 +164,20 @@ public class NodeIngestor {
inputStream.reset();
final Document doc = docBuilder.parse(inputStream);
final NodeList list = doc.getElementsByTagName("java-type");
-
- for (int i = 0; i < list.getLength(); i++) {
- String type = list.item(i).getAttributes().getNamedItem("name").getNodeValue();
- types.add(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, type));
- }
+ getAllNodeTypes(list, types);
+ caseFormatStore.parse(doc);
}
- LOGGER.debug("Types size" + types.size());
- return types;
+ LOGGER.debug("Types size {}", types.size());
+ typesPerVersion.put(version, types);
+ }
+
+ private void getAllNodeTypes(NodeList list, Set<String> types){
+
+ for (int i = 0; i < list.getLength(); i++) {
+ String type = list.item(i).getAttributes().getNamedItem("name").getNodeValue();
+ types.add(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, type));
+ }
}
private Document createCombinedSchema(List<InputStream> inputStreams, SchemaVersion version, boolean localSchema) throws ParserConfigurationException, SAXException, IOException {
@@ -274,4 +279,8 @@ public class NodeIngestor {
"</xml-bindings>";
return new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8));
}
+
+ public CaseFormatStore getCaseFormatStore(){
+ return caseFormatStore;
+ }
}
diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaVersion.java b/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaVersion.java
index a1a40e69..8143b5e1 100644
--- a/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaVersion.java
+++ b/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaVersion.java
@@ -21,20 +21,23 @@ package org.onap.aai.setup;
import org.onap.aai.validation.AAISchemaValidationException;
+import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SchemaVersion implements Comparable<SchemaVersion> {
- public static final Pattern VERSION_PATTERN = Pattern.compile("v[1-9][0-9]*");
+ public static final Pattern VERSION_PATTERN = Pattern.compile("v([1-9][0-9]*)");
- private final String value;
+ private final Integer value;
public SchemaVersion(String value){
- if(!VERSION_PATTERN.matcher(value).matches()){
+ Matcher matcher = VERSION_PATTERN.matcher(value);
+
+ if(!matcher.find()){
throw new AAISchemaValidationException("Invalid Schema Version " + value + ", value doesn't match the expected regex: " + VERSION_PATTERN);
+ } else {
+ this.value = Integer.parseInt(matcher.group(1));
}
-
- this.value = value;
}
@Override
@@ -44,6 +47,7 @@ public class SchemaVersion implements Comparable<SchemaVersion> {
@Override
public boolean equals(Object other){
+
if(this == other){
return true;
}
@@ -62,7 +66,7 @@ public class SchemaVersion implements Comparable<SchemaVersion> {
@Override
public String toString(){
- return value;
+ return String.valueOf("v" + value);
}
@Override
@@ -72,12 +76,6 @@ public class SchemaVersion implements Comparable<SchemaVersion> {
return -1;
}
- // Requires to convert to integer to match the past behavior
- // Otherwise the string comparison of versions aren't working as expected
-
- Integer tVal = Integer.parseInt(this.value.replaceAll("v", ""));
- Integer oVal = Integer.parseInt(o.value.replaceAll("v", ""));
-
- return tVal.compareTo(oVal);
+ return this.value.compareTo(o.value);
}
}