diff options
author | Igor Dysko <igor1.dysko@orange.com> | 2021-06-08 12:53:30 +0200 |
---|---|---|
committer | Igor Dysko <igor1.dysko@orange.com> | 2021-06-08 12:53:30 +0200 |
commit | 728886ae57c2b94bd8d49b6fe899b4136da2b6a2 (patch) | |
tree | 5e2cdd74549d3dcd71ad0675d839fef7c73cbff8 /src/main/java | |
parent | 61d1261b57bc1d212be0817cbe0403cccf870b5e (diff) |
Improved loading of schemas
Refactoring of the application. Static method calls were replaced by spring bean calls.
Issue-ID: AAI-3279
Signed-off-by: Igor Dysko <igor1.dysko@orange.com>
Change-Id: I8e22cc7ccf6de4c23a908b0e497f0f9beac9d35c
Diffstat (limited to 'src/main/java')
7 files changed, 200 insertions, 132 deletions
diff --git a/src/main/java/org/onap/aai/graphgraph/App.java b/src/main/java/org/onap/aai/graphgraph/App.java index 3aa326e..638d3f3 100644 --- a/src/main/java/org/onap/aai/graphgraph/App.java +++ b/src/main/java/org/onap/aai/graphgraph/App.java @@ -19,45 +19,21 @@ */ package org.onap.aai.graphgraph; -import static org.onap.aai.graphgraph.ModelExporter.exportModel; -import static org.onap.aai.graphgraph.ModelExporter.writeExportedModel; +import org.apache.commons.io.FileUtils; +import org.onap.aai.restclient.PropertyPasswordConfiguration; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ConfigurableApplicationContext; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.HashMap; -import java.util.Map; import java.util.Objects; -import org.apache.commons.io.FileUtils; -import org.onap.aai.edges.EdgeIngestor; -import org.onap.aai.introspection.MoxyLoader; -import org.onap.aai.nodes.NodeIngestor; -import org.onap.aai.restclient.PropertyPasswordConfiguration; -import org.onap.aai.setup.SchemaVersion; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class App { - public static EdgeIngestor edgeIngestor; - public static Map<String, MoxyLoader> moxyLoaders = new HashMap<>(); - - // TODO - // this should be used properly within Spring as this is a 'static' workaround due - // to some initialization issues. By all means feel free to improve and move it to Spring - public static void loadSchemes(ConfigurableApplicationContext context) { - String version; - for (int i = 10; i <= 20; i++) { - version = "v" + i; - moxyLoaders.put(version, new MoxyLoader( - new SchemaVersion(version), (NodeIngestor) context.getBean("nodeIngestor")) - ); - } - } - public static void main(String[] args) throws IOException { ArgumentParser parser = new ArgumentParser().parseArguments(args); @@ -71,16 +47,15 @@ public class App { if (parser.isRunLocally()) { copyKeystore(app); } - app.addInitializers(new PropertyPasswordConfiguration()); - ConfigurableApplicationContext context = app.run(args); - loadSchemes(context); - edgeIngestor = (EdgeIngestor) context.getBean("edgeIngestor"); + ConfigurableApplicationContext context = app.run(args); if (parser.shouldGenerateUrl()) { - writeExportedModel(exportModel(parser.getSchemaVersion())); - System.exit(0); + ModelExporter modelExporter = context.getBean(ModelExporter.class); + modelExporter.writeExportedModel(modelExporter.exportModel(parser.getSchemaVersion())); + SpringApplication.exit(context); } + } private static void copyKeystore(SpringApplication app) throws IOException { diff --git a/src/main/java/org/onap/aai/graphgraph/Config.java b/src/main/java/org/onap/aai/graphgraph/Config.java index 286d40e..418d16f 100644 --- a/src/main/java/org/onap/aai/graphgraph/Config.java +++ b/src/main/java/org/onap/aai/graphgraph/Config.java @@ -19,15 +19,19 @@ */ package org.onap.aai.graphgraph; -import java.util.Arrays; -import java.util.stream.Collectors; +import org.onap.aai.edges.EdgeIngestor; import org.onap.aai.graphgraph.reader.BasicSchemaReader; import org.onap.aai.graphgraph.reader.SchemaRepository; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + @Configuration @ComponentScan(basePackages = { "org.onap.aai.config", @@ -39,12 +43,23 @@ public class Config { @Value("${schema.version.list}") String schemaVersions; + @Autowired + private MoxyLoaderRepository moxyLoaderRepository; + + @Autowired + private EdgeIngestor edgeIngestor; + @Bean SchemaRepository createSchemaRepository() { return new SchemaRepository( Arrays.stream(schemaVersions.split(",")) - .map(BasicSchemaReader::new) + .map(version -> new BasicSchemaReader(version, moxyLoaderRepository, edgeIngestor)) .collect(Collectors.toList()) ); } + + public List<String> getSchemaVersions() { + return Arrays.asList(schemaVersions.split(",")); + } + } 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; } diff --git a/src/main/java/org/onap/aai/graphgraph/MoxyLoaderRepository.java b/src/main/java/org/onap/aai/graphgraph/MoxyLoaderRepository.java new file mode 100644 index 0000000..d2822d3 --- /dev/null +++ b/src/main/java/org/onap/aai/graphgraph/MoxyLoaderRepository.java @@ -0,0 +1,56 @@ +/* + ============LICENSE_START======================================================= + org.onap.aai + ================================================================================ + Copyright © 2019-2020 Orange 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.graphgraph; + +import org.onap.aai.introspection.MoxyLoader; +import org.onap.aai.nodes.NodeIngestor; +import org.onap.aai.setup.SchemaVersion; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; + +import javax.annotation.PostConstruct; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Repository +public class MoxyLoaderRepository { + + @Autowired + private Config appConfiguration; + + @Autowired + private NodeIngestor nodeIngestor; + + public Map<String, MoxyLoader> moxyLoaders = new HashMap<>(); + + public Map<String, MoxyLoader> getMoxyLoaders() { + return moxyLoaders; + } + + @PostConstruct + public void initMoxyLoaders() { + List<String> schemaVersions = appConfiguration.getSchemaVersions(); + for (String version : schemaVersions) { + moxyLoaders.put(version, new MoxyLoader(new SchemaVersion(version), nodeIngestor)); + } + } + +} diff --git a/src/main/java/org/onap/aai/graphgraph/SchemaResource.java b/src/main/java/org/onap/aai/graphgraph/SchemaResource.java index 34f7dae..326634d 100644 --- a/src/main/java/org/onap/aai/graphgraph/SchemaResource.java +++ b/src/main/java/org/onap/aai/graphgraph/SchemaResource.java @@ -19,45 +19,45 @@ */ package org.onap.aai.graphgraph; -import java.util.Collections; -import java.util.List; -import javax.annotation.Resource; -import org.onap.aai.graphgraph.dto.Graph; -import org.onap.aai.graphgraph.dto.NodeName; -import org.onap.aai.graphgraph.dto.NodeProperty; -import org.onap.aai.graphgraph.dto.Property; -import org.onap.aai.graphgraph.dto.ValidationProblems; +import org.onap.aai.graphgraph.dto.*; import org.onap.aai.graphgraph.reader.SchemaRepository; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.Collections; +import java.util.List; @RestController public class SchemaResource { @Resource - SchemaRepository repository; + private SchemaRepository schemaRepository; + + @Autowired + private ModelExporter modelExporter; + + @Autowired + private SchemaValidator schemaValidator; @RequestMapping("/schemas") public List<String> loadSchemaNames() { - return repository.getAllSchemaNames(); + return schemaRepository.getAllSchemaNames(); } @RequestMapping("/schemas/{schema}/nodes") public List<NodeName> loadVertexNames( @PathVariable("schema") String schemaName, @RequestParam("edgeFilter") String edgeFilter) { - return repository.getSchemaReader(schemaName).getAllVertexNames(edgeFilter); + return schemaRepository.getSchemaReader(schemaName).getAllVertexNames(edgeFilter); } @RequestMapping("/schemas/{schema}/nodes/{node}") public List<NodeProperty> loadProperties( @PathVariable("schema") String schemaName, @PathVariable("node") String node) { - return repository.getSchemaReader(schemaName).getVertexProperties(node); + return schemaRepository.getSchemaReader(schemaName).getVertexProperties(node); } @RequestMapping("/schemas/{schema}/edges") @@ -65,7 +65,7 @@ public class SchemaResource { @PathVariable("schema") String schemaName, @RequestParam("fromNode") String fromNodeName, @RequestParam("toNode") String toNodeName) { - return repository.getSchemaReader(schemaName).getEdgeProperties(fromNodeName, toNodeName, "edgerule"); + return schemaRepository.getSchemaReader(schemaName).getEdgeProperties(fromNodeName, toNodeName, "edgerule"); } @RequestMapping("/schemas/{schema}/graph/basic") @@ -76,7 +76,7 @@ public class SchemaResource { @RequestParam("cousinHops") Integer cousinHops, @RequestParam("childHops") Integer childHops, @RequestParam("edgeFilter") String edgeFilter) { - Graph graph = repository.getSchemaReader(schemaName) + Graph graph = schemaRepository.getSchemaReader(schemaName) .getGraph(initialNodeName, parentHops, cousinHops, childHops, edgeFilter); graph.setPaths(Collections.emptyList()); return graph; @@ -88,20 +88,20 @@ public class SchemaResource { @RequestParam("fromNode") String fromNode, @RequestParam("toNode") String toNode, @RequestParam("edgeFilter") String edgeFilter) { - return repository.getSchemaReader(schemaName).getGraph(fromNode, toNode, edgeFilter); + return schemaRepository.getSchemaReader(schemaName).getGraph(fromNode, toNode, edgeFilter); } @RequestMapping("/schemas/{schema}/validation") public ValidationProblems validateSchema( @PathVariable("schema") String schemaName) { - return new SchemaValidator().validate(schemaName); + return schemaValidator.validate(schemaName); } @RequestMapping(value = "/schemas/{schema}/xmiexport", produces = MediaType.TEXT_XML_VALUE) @ResponseBody public String exportSchema( @PathVariable("schema") String schemaName) { - return ModelExporter.exportModel(schemaName); + return modelExporter.exportModel(schemaName); } } diff --git a/src/main/java/org/onap/aai/graphgraph/SchemaValidator.java b/src/main/java/org/onap/aai/graphgraph/SchemaValidator.java index 73484f7..6ae2afa 100644 --- a/src/main/java/org/onap/aai/graphgraph/SchemaValidator.java +++ b/src/main/java/org/onap/aai/graphgraph/SchemaValidator.java @@ -21,29 +21,37 @@ package org.onap.aai.graphgraph; import io.vavr.Tuple; import io.vavr.Tuple2; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.graphgraph.dto.Edge; +import org.onap.aai.graphgraph.dto.Graph; +import org.onap.aai.graphgraph.dto.ValidationProblems; +import org.onap.aai.graphgraph.reader.BasicSchemaReader; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.onap.aai.graphgraph.dto.Edge; -import org.onap.aai.graphgraph.dto.Graph; -import org.onap.aai.graphgraph.dto.ValidationProblems; -import org.onap.aai.graphgraph.reader.BasicSchemaReader; +@Service public class SchemaValidator { - private Graph edgerules; - private Graph oxm; + @Autowired + private MoxyLoaderRepository moxyLoaderRepository; + + @Autowired + private EdgeIngestor edgeIngestor; public ValidationProblems validate(String schemaVersion) { ValidationProblems validationProblems = new ValidationProblems(); - BasicSchemaReader schema = new BasicSchemaReader(schemaVersion); - oxm = schema.getGraph("all", 0, 0, 0, "Parents"); - edgerules = schema.getGraph("all", 0, 0, 0, "Edgerules"); + BasicSchemaReader schema = new BasicSchemaReader(schemaVersion, moxyLoaderRepository, edgeIngestor); + Graph oxm = schema.getGraph("all", 0, 0, 0, "Parents"); + Graph edgerules = schema.getGraph("all", 0, 0, 0, "Edgerules"); - checkIfDanglingEdgerules(validationProblems); - checkIfObsoleteOxm(validationProblems); + checkIfDanglingEdgerules(validationProblems, edgerules, oxm); + checkIfObsoleteOxm(validationProblems, edgerules, oxm); schema.getSchemaErrors().forEach(validationProblems::addProblem); return validationProblems; } @@ -52,9 +60,9 @@ public class SchemaValidator { * computes nodes connected to relationship-list but not used in edgerules * @param validationProblems */ - private void checkIfObsoleteOxm(ValidationProblems validationProblems) { - Set<String> relationshipListConnected = getAllNodesConnectedToRelationshipList(); - Set<String> nodesInEdgerules = getEdgerulePairs().stream() + private void checkIfObsoleteOxm(ValidationProblems validationProblems, Graph edgerules, Graph oxm) { + Set<String> relationshipListConnected = getAllNodesConnectedToRelationshipList(oxm); + Set<String> nodesInEdgerules = getEdgerulePairs(edgerules).stream() .flatMap(p -> Stream.of(p._1, p._2)) .collect(Collectors.toSet()); relationshipListConnected.removeAll(nodesInEdgerules); @@ -63,7 +71,7 @@ public class SchemaValidator { n))); } - private Set<Tuple2<String, String>> getEdgerulePairs() { + private Set<Tuple2<String, String>> getEdgerulePairs(Graph edgerules) { return edgerules.getEdges().stream() .map(e -> Tuple.of(e.getSource(), e.getTarget())) .collect(Collectors.toSet()); @@ -73,24 +81,24 @@ public class SchemaValidator { * computes edgerules which don't have the necessary connection to relationship-list in OXM * @param validationProblems */ - private void checkIfDanglingEdgerules(ValidationProblems validationProblems) { - Set<Tuple2<String, String>> edgerulePairs = getEdgerulePairs(); - edgerulePairs.removeAll(getOxmPairs()); + private void checkIfDanglingEdgerules(ValidationProblems validationProblems, Graph edgerules, Graph oxm) { + Set<Tuple2<String, String>> edgerulePairs = getEdgerulePairs(edgerules); + edgerulePairs.removeAll(getOxmPairs(oxm)); edgerulePairs.forEach(erp -> validationProblems.addProblem( String.format("%s and %s are associated in edgerules but not in OXM (via relationship-list)", erp._1, erp._2))); } - private Set<Tuple2<String, String>> getOxmPairs() { + private Set<Tuple2<String, String>> getOxmPairs(Graph oxm) { Set<Tuple2<String, String>> pairs = new HashSet<>(); - Set<String> inRelationshipList = getAllNodesConnectedToRelationshipList(); + Set<String> inRelationshipList = getAllNodesConnectedToRelationshipList(oxm); inRelationshipList.forEach(edge1 -> inRelationshipList .forEach(edge2 -> pairs.add(Tuple.of(edge1, edge2)))); return pairs; } - private Set<String> getAllNodesConnectedToRelationshipList() { + private Set<String> getAllNodesConnectedToRelationshipList(Graph oxm) { List<Edge> edges = oxm.getEdges(); Set<String> inRelationshipList = edges.stream() .filter(e -> e.getSource().equals("relationship-list")) diff --git a/src/main/java/org/onap/aai/graphgraph/reader/BasicSchemaReader.java b/src/main/java/org/onap/aai/graphgraph/reader/BasicSchemaReader.java index 1a7d963..d60e955 100644 --- a/src/main/java/org/onap/aai/graphgraph/reader/BasicSchemaReader.java +++ b/src/main/java/org/onap/aai/graphgraph/reader/BasicSchemaReader.java @@ -40,7 +40,7 @@ import org.jgrapht.graph.DefaultDirectedGraph; import org.onap.aai.edges.EdgeIngestor; import org.onap.aai.edges.EdgeRule; import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; -import org.onap.aai.graphgraph.App; +import org.onap.aai.graphgraph.MoxyLoaderRepository; import org.onap.aai.graphgraph.dto.Edge; import org.onap.aai.graphgraph.dto.NodeName; import org.onap.aai.graphgraph.dto.NodeProperty; @@ -56,22 +56,31 @@ public class BasicSchemaReader implements SchemaReader { private EdgeIngestor edgeIngestor; private final String version; private final List<String> schemaErrors = new LinkedList<>(); + private final MoxyLoaderRepository moxyLoaderRepository; - public BasicSchemaReader(String version) { + public BasicSchemaReader(String version, MoxyLoaderRepository moxyLoaderRepository, EdgeIngestor edgeIngestor) { this.version = version; + this.moxyLoaderRepository = moxyLoaderRepository; + this.edgeIngestor = edgeIngestor; } public List<String> getSchemaErrors() { return schemaErrors; } - private void init() { + private void initAllEntitiesAndCreateGraph() { if (allEntities != null) { return; } - allEntities = App.moxyLoaders.get(getSchemaName()).getAllObjects(); - edgeIngestor = App.edgeIngestor; - graph = createGraph(true, true); + + try { + allEntities = moxyLoaderRepository.getMoxyLoaders().get(getSchemaName()).getAllObjects(); + graph = createGraph(true, true); + + } catch (Exception e) { + System.out.println("Failed creation of BasicSchemaReader, version: " + getSchemaName()); + e.printStackTrace(); + } } private Graph<String, MetadataEdge> createGraph(boolean withParentChild, boolean withEdgeRules) { @@ -154,7 +163,7 @@ public class BasicSchemaReader implements SchemaReader { @Override public List<NodeName> getAllVertexNames(String edgeFilter) { - init(); + initAllEntitiesAndCreateGraph(); return createGraph( isParentChildFilter(edgeFilter), @@ -167,8 +176,7 @@ public class BasicSchemaReader implements SchemaReader { @Override public List<NodeProperty> getVertexProperties(String nodeName) { - init(); - + initAllEntitiesAndCreateGraph(); if (!allEntities.containsKey(nodeName)) { return Collections.emptyList(); } @@ -189,7 +197,7 @@ public class BasicSchemaReader implements SchemaReader { @Override public List<Property> getEdgeProperties(String fromNode, String toNode, String type) { - init(); + initAllEntitiesAndCreateGraph(); if (type.equals(EdgeType.EDGE_RULE.getTypeName())) { try { List<EdgeRule> rules = edgeIngestor.getAllRules(new SchemaVersion(getSchemaName())) @@ -240,7 +248,7 @@ public class BasicSchemaReader implements SchemaReader { @Override public org.onap.aai.graphgraph.dto.Graph getGraph( String initialNode, int parentHops, int cousinHops, int childHops, String edgeFilter) { - init(); + initAllEntitiesAndCreateGraph(); Optional<String> anyVertex = graph.vertexSet().stream().findFirst(); if (!anyVertex.isPresent()) { @@ -352,7 +360,7 @@ public class BasicSchemaReader implements SchemaReader { @Override public org.onap.aai.graphgraph.dto.Graph getGraph(String fromNode, String toNode, String edgeFilter) { - init(); + initAllEntitiesAndCreateGraph(); Graph<String, MetadataEdge> tempGraph = createGraph( isParentChildFilter(edgeFilter), isEdgeRulesFilter(edgeFilter)); List<List<NodeName>> paths = new LinkedList<>(); |