diff options
Diffstat (limited to 'src/main/java/org/onap/aai/graphgraph/ModelExporter.java')
-rw-r--r-- | src/main/java/org/onap/aai/graphgraph/ModelExporter.java | 84 |
1 files changed, 45 insertions, 39 deletions
diff --git a/src/main/java/org/onap/aai/graphgraph/ModelExporter.java b/src/main/java/org/onap/aai/graphgraph/ModelExporter.java index d0db27e..1c433fe 100644 --- a/src/main/java/org/onap/aai/graphgraph/ModelExporter.java +++ b/src/main/java/org/onap/aai/graphgraph/ModelExporter.java @@ -20,20 +20,6 @@ package org.onap.aai.graphgraph; import com.google.common.collect.Multimap; -import java.io.FileWriter; -import java.io.IOException; -import java.io.StringWriter; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.stream.Collectors; -import java.util.stream.Stream; import org.apache.tinkerpop.gremlin.structure.Direction; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; @@ -42,6 +28,7 @@ import org.apache.velocity.runtime.RuntimeConstants; import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; import org.apache.velocity.tools.generic.EscapeTool; import org.eclipse.jetty.util.StringUtil; +import org.onap.aai.edges.EdgeIngestor; import org.onap.aai.edges.EdgeRule; import org.onap.aai.edges.enums.DirectionNotation; import org.onap.aai.edges.enums.EdgeField; @@ -54,19 +41,38 @@ import org.onap.aai.introspection.Introspector; import org.onap.aai.schema.enums.ObjectMetadata; import org.onap.aai.schema.enums.PropertyMetadata; import org.onap.aai.setup.SchemaVersion; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import java.io.FileWriter; +import java.io.IOException; +import java.io.StringWriter; +import java.util.*; +import java.util.Map.Entry; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +@Service public class ModelExporter { private static final String AAIMODEL_UML_FILENAME = "aaimodel.uml"; private static final String VELOCITY_TEMPLATE_FILENAME = "model_export.vm"; private static final boolean OXM_ENABLED = false; private static final String camelCaseRegex = "(?=[A-Z][a-z])"; - private static Map<String, Introspector> allEntities; + private Map<String, Introspector> allEntities; + + @Autowired + private MoxyLoaderRepository moxyLoaderRepository; + + @Autowired + private EdgeIngestor edgeIngestor; - private static Multimap<String, EdgeRule> getEdgeRules(String schemaVersion) { + private Multimap<String, EdgeRule> getEdgeRules(String schemaVersion) { try { - Multimap<String, EdgeRule> allRules = App.edgeIngestor.getAllRules(new SchemaVersion(schemaVersion)); - allEntities = App.moxyLoaders.get(schemaVersion).getAllObjects(); + Multimap<String, EdgeRule> allRules = edgeIngestor.getAllRules(new SchemaVersion(schemaVersion)); + allEntities = moxyLoaderRepository.getMoxyLoaders().get(schemaVersion).getAllObjects(); if (OXM_ENABLED) { addOxmRelationships(allRules, allEntities); } @@ -78,10 +84,10 @@ public class ModelExporter { return null; } - private static void addOxmRelationships( + private void addOxmRelationships( Multimap<String, EdgeRule> allRules, - Map<String, Introspector> allEntities - ) { + Map<String, Introspector> allEntities) { + for (Entry<String, Introspector> currentParent : allEntities.entrySet()) { currentParent.getValue().getProperties().stream() .filter(allEntities::containsKey) @@ -95,7 +101,7 @@ public class ModelExporter { } } - private static EdgeRule createEdgeRule(String parent, String child) { + private EdgeRule createEdgeRule(String parent, String child) { Map<String, String> edgeRuleProps = new HashMap<>(); edgeRuleProps.put(EdgeField.FROM.toString(), child); edgeRuleProps.put(EdgeField.TO.toString(), parent); @@ -111,8 +117,8 @@ public class ModelExporter { return new EdgeRule(edgeRuleProps); } - static String exportModel(String schemaVersion) { - Map<String, Introspector> allObjects = App.moxyLoaders.get(schemaVersion).getAllObjects(); + public String exportModel(String schemaVersion) { + Map<String, Introspector> allObjects = moxyLoaderRepository.getMoxyLoaders().get(schemaVersion).getAllObjects(); Template t = initVelocity(); VelocityContext context = populateVelocityContext(schemaVersion, allObjects); StringWriter writer = new StringWriter(); @@ -120,7 +126,7 @@ public class ModelExporter { return writer.toString(); } - static void writeExportedModel(String result) { + public void writeExportedModel(String result) { try { FileWriter fw = new FileWriter(AAIMODEL_UML_FILENAME); fw.write(result); @@ -130,10 +136,10 @@ public class ModelExporter { } } - private static VelocityContext populateVelocityContext( + private VelocityContext populateVelocityContext( String schemaVersion, - Map<String, Introspector> allObjects - ) { + Map<String, Introspector> allObjects) { + VelocityContext context = new VelocityContext(); Multimap<String, EdgeRule> edgeRules = getEdgeRules(schemaVersion); Set<VelocityEntity> entityList = createEntityList(edgeRules); @@ -146,7 +152,7 @@ public class ModelExporter { return context; } - private static Template initVelocity() { + private Template initVelocity() { VelocityEngine ve = new VelocityEngine(); ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); @@ -154,11 +160,11 @@ public class ModelExporter { return ve.getTemplate(VELOCITY_TEMPLATE_FILENAME); } - private static void updateEntities( + private void updateEntities( Set<VelocityEntity> entityList, Set<VelocityAssociation> associationsList, - Map<String, Introspector> allObjects - ) { + Map<String, Introspector> allObjects) { + entityList.forEach(e -> { List<VelocityAssociation> associations = associationsList.stream() .filter(a -> a.getFromEntityId().equals(e.getId())).collect( @@ -172,7 +178,7 @@ public class ModelExporter { e -> e.setProperties(getPropertiesForEntity(allObjects.get(e.getName()), entityList))); } - private static void updateNeighbour( + private void updateNeighbour( Set<VelocityEntity> entityList, List<VelocityAssociation> associations ) { associations.forEach(ass -> { @@ -182,7 +188,7 @@ public class ModelExporter { }); } - private static Set<VelocityEntityProperty> getPropertiesForEntity( + private Set<VelocityEntityProperty> getPropertiesForEntity( Introspector introspector, Set<VelocityEntity> entityList) { return introspector.getProperties().stream() @@ -195,7 +201,7 @@ public class ModelExporter { Collectors.toSet()); } - private static Set<VelocityEntity> createEntityList( + private Set<VelocityEntity> createEntityList( Multimap<String, EdgeRule> edgeRules) { return Objects.requireNonNull(edgeRules).values().stream() .flatMap(er -> Stream.of(er.getFrom(), er.getTo())) @@ -203,7 +209,7 @@ public class ModelExporter { .collect(Collectors.toSet()); } - private static Set<VelocityAssociation> createVelocityAssociations( + private Set<VelocityAssociation> createVelocityAssociations( Set<VelocityEntity> entities, Multimap<String, EdgeRule> edgeRules) { return edgeRules.values().stream().flatMap(er -> { @@ -224,7 +230,7 @@ public class ModelExporter { }).collect(Collectors.toSet()); } - private static VelocityAssociation createVelocityAssociation( + private VelocityAssociation createVelocityAssociation( Set<VelocityEntity> entities, String from, String to, String label, String description, String multiplicity, String contains) { Optional<VelocityEntity> fromEntity = entities.stream() .filter(ent -> ent.getName().equals(from)).findFirst(); @@ -261,7 +267,7 @@ public class ModelExporter { return null; } - private static String shortenLabel(String label) { + private String shortenLabel(String label) { if (label.contains(".")) { String[] split = label.split("\\."); return split[split.length - 1]; @@ -270,7 +276,7 @@ public class ModelExporter { return label; } - private static VelocityEntity findVelocityEntity(String entityName, Set<VelocityEntity> entities) { + private VelocityEntity findVelocityEntity(String entityName, Set<VelocityEntity> entities) { if (entityName.startsWith("java.lang")) { return null; } |