diff options
Diffstat (limited to 'src/main/java/org')
4 files changed, 141 insertions, 26 deletions
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<String, EdgeRule> getEdgeRules(String schemaVersion) { try { Multimap<String, EdgeRule> allRules = App.edgeIngestor.getAllRules(new SchemaVersion(schemaVersion)); - Map<String, Introspector> allEntities = App.moxyLoaders.get("v16").getAllObjects(); - for (Entry<String, Introspector> 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<String, Introspector> 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<String, EdgeRule> allRules, + Map<String, Introspector> allEntities) { + for (Entry<String, Introspector> 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<String, String> edgeRuleProps = new HashMap<>(); edgeRuleProps.put(EdgeField.FROM.toString(), child); @@ -133,13 +144,33 @@ public class ModelExporter { List<VelocityAssociation> 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<VelocityEntity> entityList, List<VelocityAssociation> associations) { + associations.forEach(ass -> { + VelocityEntity velocityEntity = entityList.stream() + .filter(e -> e.getId().equals(ass.getToEntityId())).findFirst().get(); + velocityEntity.addNeighbours(ass); }); } + private static Set<VelocityEntityProperty> getPropertiesForEntity(Introspector introspector, + Set<VelocityEntity> entityList) { + return introspector.getProperties().stream() + .map(p -> new VelocityEntityProperty( + p, + introspector.getType(p), + findVelocityEntity(introspector.getType(p), entityList))) + .collect( + Collectors.toSet()); + } + private static Set<VelocityEntity> createEntityList( Multimap<String, EdgeRule> edgeRules) { return Objects.requireNonNull(edgeRules).values().stream() @@ -170,16 +201,43 @@ public class ModelExporter { private static VelocityAssociation createVelocityAssociation(Set<VelocityEntity> 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<VelocityEntity> 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<VelocityEntity> 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 @@ -95,10 +95,21 @@ public class VelocityAssociation extends VelocityId { } @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<VelocityAssociation> neighbours; - private Set<String> properties; + private List<VelocityAssociation> neighbours = new LinkedList<>(); + private Set<VelocityEntityProperty> properties; - public Set<String> getProperties() { + public Set<VelocityEntityProperty> getProperties() { return properties; } - public void setProperties(Set<String> properties) { + public void setProperties(Set<VelocityEntityProperty> 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() + + '}'; + } +} |