From c1211ee4e71f8fda30f835fc7641e4315ecade02 Mon Sep 17 00:00:00 2001 From: Pavel Paroulek Date: Wed, 21 Aug 2019 21:12:24 +0200 Subject: Adding entity properties, changing associations Adding entity properties with corresponding UML types or association types, switching composition around as it was pointing the opposite way, changing associations so that they don't contain arrows Change-Id: I968c9a446eb147f6432b47fd94e69a2842bc66bb Signed-off-by: Pavel Paroulek Issue-ID: AAI-531 --- .../org/onap/aai/graphgraph/ModelExporter.java | 102 ++++++++++++---- .../graphgraph/velocity/VelocityAssociation.java | 11 ++ .../aai/graphgraph/velocity/VelocityEntity.java | 21 +++- .../velocity/VelocityEntityProperty.java | 33 ++++++ src/main/resources/model_export.vm | 132 +++++++++++++++------ 5 files changed, 238 insertions(+), 61 deletions(-) create mode 100644 src/main/java/org/onap/aai/graphgraph/velocity/VelocityEntityProperty.java (limited to 'src') diff --git a/src/main/java/org/onap/aai/graphgraph/ModelExporter.java b/src/main/java/org/onap/aai/graphgraph/ModelExporter.java index 3180c4c..3716f27 100644 --- a/src/main/java/org/onap/aai/graphgraph/ModelExporter.java +++ b/src/main/java/org/onap/aai/graphgraph/ModelExporter.java @@ -29,6 +29,8 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Objects; 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; @@ -44,28 +46,22 @@ import org.onap.aai.edges.enums.MultiplicityRule; import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; import org.onap.aai.graphgraph.velocity.VelocityAssociation; import org.onap.aai.graphgraph.velocity.VelocityEntity; +import org.onap.aai.graphgraph.velocity.VelocityEntityProperty; import org.onap.aai.introspection.Introspector; import org.onap.aai.setup.SchemaVersion; public class ModelExporter { private static final String AAIMODEL_UML_FILENAME = "aaimodel.uml"; - public static final String VELOCITY_TEMPLATE_FILENAME = "model_export.vm"; - + 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 Multimap getEdgeRules(String schemaVersion) { try { Multimap allRules = App.edgeIngestor.getAllRules(new SchemaVersion(schemaVersion)); - Map allEntities = App.moxyLoaders.get("v16").getAllObjects(); - for (Entry currentParent : allEntities.entrySet()) { - currentParent.getValue().getProperties().stream() - .filter(v -> allEntities.containsKey(v)) - .filter(v -> !currentParent.getKey().equals(v)) - .forEach(v -> { - String key = currentParent.getKey() + "|" + v; - if (!allRules.containsKey(key)) { - allRules.put(key, createEdgeRule(currentParent.getKey(), v)); - } - }); + Map allEntities = App.moxyLoaders.get(schemaVersion).getAllObjects(); + if (OXM_ENABLED) { + addOxmRelationships(allRules, allEntities); } return allRules; } catch (EdgeRuleNotFoundException e) { @@ -75,6 +71,21 @@ public class ModelExporter { return null; } + private static void addOxmRelationships(Multimap allRules, + Map allEntities) { + for (Entry currentParent : allEntities.entrySet()) { + currentParent.getValue().getProperties().stream() + .filter(v -> allEntities.containsKey(v)) + .filter(v -> !currentParent.getKey().equals(v)) + .forEach(v -> { + String key = currentParent.getKey() + "|" + v; + if (!allRules.containsKey(key)) { + allRules.put(key, createEdgeRule(currentParent.getKey(), v)); + } + }); + } + } + private static EdgeRule createEdgeRule(String parent, String child) { Map edgeRuleProps = new HashMap<>(); edgeRuleProps.put(EdgeField.FROM.toString(), child); @@ -133,13 +144,33 @@ public class ModelExporter { List associations = associationsList.stream() .filter(a -> a.getFromEntityId().equals(e.getId())).collect( Collectors.toList()); + updateNeighbour(entityList, associations); + }); - entityList.forEach(entity -> entity.setProperties(allObjects.get(entity.getName()).getProperties())); + entityList.forEach(entity -> entity.setProperties(getPropertiesForEntity(allObjects.get(entity.getName()), entityList))); - e.setNeighbours(associations); + } + + private static void updateNeighbour( + Set entityList, List associations) { + associations.forEach(ass -> { + VelocityEntity velocityEntity = entityList.stream() + .filter(e -> e.getId().equals(ass.getToEntityId())).findFirst().get(); + velocityEntity.addNeighbours(ass); }); } + private static Set getPropertiesForEntity(Introspector introspector, + Set entityList) { + return introspector.getProperties().stream() + .map(p -> new VelocityEntityProperty( + p, + introspector.getType(p), + findVelocityEntity(introspector.getType(p), entityList))) + .collect( + Collectors.toSet()); + } + private static Set createEntityList( Multimap edgeRules) { return Objects.requireNonNull(edgeRules).values().stream() @@ -170,16 +201,43 @@ public class ModelExporter { private static VelocityAssociation createVelocityAssociation(Set entities, String from, String to, String label, String multiplicity) { + boolean composition = isComposition(label); return new VelocityAssociation( - findVelocityEntity(from, entities), - findVelocityEntity(to, entities), - String.format("%s - %s (label: %s)", from, to, label), - multiplicity, - label.equals("org.onap.relationships.inventory.BelongsTo") + entities.stream().filter( ent -> ent.getName().equals(from)).findFirst().get(), + entities.stream().filter( ent -> ent.getName().equals(to)).findFirst().get(), + String.format("%s - %s (label: %s)", from, to, shortenLabel(label)), + multiplicity, + composition ); } - private static VelocityEntity findVelocityEntity(String from, Set entities) { - return entities.stream().filter(e -> e.getName().equals(from)).findFirst().get(); + private static String shortenLabel(String label) { + if (label.contains(".")) { + String[] split = label.split("\\."); + return split[split.length - 1]; + } + + return label; + } + + private static boolean isComposition(String label) { + return label.equals("org.onap.relationships.inventory.BelongsTo"); + } + + private static VelocityEntity findVelocityEntity(String entityName, Set entities) { + if (entityName.startsWith("java.lang")){ + return null; + } + + if ( ! entityName.startsWith("inventory.aai.onap.org")){ + return null; + } + + String[] split = entityName.split("\\."); + String entityNameRoot = split[split.length - 1]; + final Pattern pattern = Pattern.compile(camelCaseRegex); + final Matcher matcher = pattern.matcher(entityNameRoot.substring(1, entityNameRoot.length())); + String finalEntityNameRoot = (entityNameRoot.charAt(0) + matcher.replaceAll("-")).toLowerCase(); + return entities.stream().filter(e -> e.getName().equals(finalEntityNameRoot)).findFirst().orElse(null); } } \ No newline at end of file diff --git a/src/main/java/org/onap/aai/graphgraph/velocity/VelocityAssociation.java b/src/main/java/org/onap/aai/graphgraph/velocity/VelocityAssociation.java index bcc314e..697fad1 100644 --- a/src/main/java/org/onap/aai/graphgraph/velocity/VelocityAssociation.java +++ b/src/main/java/org/onap/aai/graphgraph/velocity/VelocityAssociation.java @@ -94,11 +94,22 @@ public class VelocityAssociation extends VelocityId { return isComposition; } + @Override + public String toString() { + return "VelocityAssociation{" + + "name='" + name + '\'' + + ", fromEntity=" + fromEntity + + ", toEntity=" + toEntity + + '}'; + } + @Override public int hashCode() { int result = name.hashCode(); result = 31 * result + fromEntity.hashCode(); result = 31 * result + toEntity.hashCode(); return result; + + } } diff --git a/src/main/java/org/onap/aai/graphgraph/velocity/VelocityEntity.java b/src/main/java/org/onap/aai/graphgraph/velocity/VelocityEntity.java index 111f67c..39b92d4 100644 --- a/src/main/java/org/onap/aai/graphgraph/velocity/VelocityEntity.java +++ b/src/main/java/org/onap/aai/graphgraph/velocity/VelocityEntity.java @@ -19,19 +19,20 @@ */ package org.onap.aai.graphgraph.velocity; +import java.util.LinkedList; import java.util.List; import java.util.Set; public class VelocityEntity extends VelocityId { private String name; - private List neighbours; - private Set properties; + private List neighbours = new LinkedList<>(); + private Set properties; - public Set getProperties() { + public Set getProperties() { return properties; } - public void setProperties(Set properties) { + public void setProperties(Set properties) { this.properties = properties; } @@ -55,6 +56,11 @@ public class VelocityEntity extends VelocityId { this.neighbours = neighbours; } + public void addNeighbours(VelocityAssociation neighbour) { + neighbours.add(neighbour); + } + + @Override public boolean equals(Object o) { if (this == o) { @@ -73,4 +79,11 @@ public class VelocityEntity extends VelocityId { public int hashCode() { return name.hashCode(); } + + @Override + public String toString() { + return "VelocityEntity{" + + "name='" + name + '\'' + + '}'; + } } diff --git a/src/main/java/org/onap/aai/graphgraph/velocity/VelocityEntityProperty.java b/src/main/java/org/onap/aai/graphgraph/velocity/VelocityEntityProperty.java new file mode 100644 index 0000000..9ee2fbb --- /dev/null +++ b/src/main/java/org/onap/aai/graphgraph/velocity/VelocityEntityProperty.java @@ -0,0 +1,33 @@ +package org.onap.aai.graphgraph.velocity; + +import org.onap.aai.graphgraph.dto.Property; + +public class VelocityEntityProperty extends Property { + + private final VelocityEntity entity; + + public VelocityEntityProperty(String propertyName, String propertyValue, VelocityEntity entity) { + super(propertyName, propertyValue); + this.entity = entity; + } + + public String getEntityId() { + return entity.getId(); + } + + public String getEntityName() { + return entity.getName(); + } + + public boolean hasEntity(){ + return entity != null; + } + + @Override + public String toString() { + return "VelocityEntityProperty{" + + " name=" + getPropertyName() + + " type=" + getPropertyValue() + + '}'; + } +} diff --git a/src/main/resources/model_export.vm b/src/main/resources/model_export.vm index c15805c..184d21a 100644 --- a/src/main/resources/model_export.vm +++ b/src/main/resources/model_export.vm @@ -1,44 +1,106 @@ - + - + - #foreach($association in $associationList) - - -
- - - #if($association.multiplicity == "MANY2ONE" || $association.multiplicity == "MANY2MANY") - - - #else - - - #end - - - #end + #foreach($association in $associationList) + + +
+ + + #if($association.multiplicity == "ONE2MANY" || $association.multiplicity == + "MANY2MANY") + + + #else + + + #end + + #if( ! $association.isComposition) + + #if($association.multiplicity == "MANY2ONE" || $association.multiplicity == + "MANY2MANY") + + + #else + + + #end + + #end + + #end - #foreach($entity in $entityList) - - #foreach($association in $entity.neighbours) - - #if($association.multiplicity == "ONE2MANY" || $association.multiplicity == "MANY2MANY") - - - #else - - - #end - - #end - #foreach($prop in $entity.properties) - + #foreach($entity in $entityList) + + #foreach($association in $entity.neighbours) + #if( $association.isComposition) + + #if($association.multiplicity == "MANY2ONE" || $association.multiplicity == + "MANY2MANY") + + + #else + + + #end + + #end + #end + #foreach($prop in $entity.properties) + #if($prop.propertyValue.contains("java.lang")) + + #if($prop.propertyValue == "java.lang.String") + + #end# + #if($prop.propertyValue == "java.lang.Long") + + #end + #if($prop.propertyValue == "java.lang.Boolean") + + #end + #if($prop.propertyValue == "java.lang.Integer") + + #end + + #elseif(! $prop.hasEntity) + #* *# + #else + + #end + #end + #end - - #end \ No newline at end of file -- cgit 1.2.3-korg