use of org.opennms.features.graphml.model.GraphML in project opennms by OpenNMS.
the class AssetGraphGenerator method generateGraphs.
public GraphML generateGraphs(GeneratorConfig config) {
final LayerDefinitionRepository layerDefinitionRepository = new LayerDefinitionRepository();
final List<LayerDefinition> layerDefinitions = layerDefinitionRepository.getDefinitions(config.getLayerHierarchies());
final List<OnmsNode> nodes = nodeProvider.getNodes(layerDefinitions);
// Define Layers
final List<Layer> layers = layerDefinitions.stream().map(mapping -> mapping.getLayer()).collect(Collectors.toList());
// Add last Layer for Nodes
layers.add(new LayerBuilder().withId("nodes").withNamespace("nodes").withLabel("Nodes").withDescription("The nodes in the hierarchy of the topology").withItemProvider(node -> node).withIdGenerator(IdGenerator.SIMPLE).withSemanticZoomLevel(0).withVertexStatusProvider(true).build());
// Ensure that all elements in the nodes do have values set
layers.forEach(layer -> {
List<OnmsNode> nodeWithNullValues = nodes.stream().filter(n -> layer.getItemProvider().getItem(n) == null).collect(Collectors.toList());
if (!nodeWithNullValues.isEmpty()) {
LOG.debug("Found nodes with null value for layer (id: {}, label: {}). Removing nodes {}", layer.getId(), layer.getLabel(), nodeWithNullValues.stream().map(n -> String.format("(id: %s, label: %s)", n.getId(), n.getLabel())).collect(Collectors.toList()));
nodes.removeAll(nodeWithNullValues);
}
});
// Apply additional filters
final Map<String, Filter> filterMap = new FilterParser().parse(config.getFilters());
final List<LayerDefinition> layersToFilter = layerDefinitionRepository.getDefinitions(filterMap.keySet());
applyFilters(nodes, filterMap, layerDefinitionRepository);
// Start generating the hierarchy
// Overall graphml object
final GraphML graphML = new GraphML();
graphML.setProperty(GraphMLProperties.LABEL, config.getLabel());
graphML.setProperty(GraphMLProperties.BREADCRUMB_STRATEGY, config.getBreadcrumbStrategy());
// Build each Graph
int index = 0;
for (Layer layer : layers) {
GraphMLGraph layerGraph = new GraphMLGraph();
layerGraph.setId(config.getProviderId() + ":" + layer.getId());
layerGraph.setProperty(GraphMLProperties.NAMESPACE, config.getProviderId() + ":" + layer.getNamespace());
layerGraph.setProperty(GraphMLProperties.PREFERRED_LAYOUT, config.getPreferredLayout());
layerGraph.setProperty(GraphMLProperties.LABEL, layer.getLabel());
layerGraph.setProperty(GraphMLProperties.DESCRIPTION, layer.getDescription());
layerGraph.setProperty(GraphMLProperties.FOCUS_STRATEGY, layer.getFocusStrategy().name());
layerGraph.setProperty(GraphMLProperties.SEMANTIC_ZOOM_LEVEL, layer.getSemanticZoomLevel());
layerGraph.setProperty(GraphMLProperties.VERTEX_STATUS_PROVIDER, layer.hasVertexStatusProvider());
// Build layer for nodes
for (OnmsNode eachNode : nodes) {
final Object eachItem = layer.getItemProvider().getItem(eachNode);
if (eachItem != null) {
List<Layer> processedLayers = layers.subList(0, index);
String id = layer.getIdGenerator().generateId(processedLayers, eachNode, layer.getNodeDecorator().getId(eachItem));
if (layerGraph.getNodeById(id) == null) {
GraphMLNode node = new GraphMLNode();
node.setId(id);
layer.getNodeDecorator().decorate(node, eachItem);
layerGraph.addNode(node);
}
}
}
graphML.addGraph(layerGraph);
index++;
}
// Now link all nodes, but only if there are at least 2 layers
if (graphML.getGraphs().size() > 1) {
nodes.forEach(n -> {
List<GraphMLNode> path = getPath(n, graphML.getGraphs(), layers);
if (path.size() != graphML.getGraphs().size()) {
throw new IllegalStateException("");
}
for (int i = 0; i < path.size() - 1; i++) {
GraphMLNode sourceNode = path.get(i);
GraphMLNode targetNode = path.get(i + 1);
GraphMLGraph sourceGraph = graphML.getGraphs().get(i);
String edgeId = String.format("%s_%s", sourceNode.getId(), targetNode.getId());
if (sourceGraph.getEdgeById(edgeId) == null) {
GraphMLEdge edge = new GraphMLEdge();
edge.setId(edgeId);
edge.setSource(sourceNode);
edge.setTarget(targetNode);
sourceGraph.addEdge(edge);
}
}
});
}
return graphML;
}
use of org.opennms.features.graphml.model.GraphML in project opennms by OpenNMS.
the class AssetGraphMLProvider method createAssetTopology.
/**
* Generates and installs a new AssetTopology defined by the config
* @param config
*/
public synchronized void createAssetTopology(GeneratorConfig config) {
Objects.requireNonNull(config);
try {
LOG.debug("Creating Asset Topology providerId: {}, label: {}, config: {}", config.getProviderId(), config.getLabel(), config);
if (graphmlRepository.exists(config.getProviderId())) {
throw new IllegalStateException(String.format("GraphML Provider with id '%s' (label: %s) already exists", config.getProviderId(), config.getLabel()));
}
if (assetGraphDefinitionRepository.exists(config.getProviderId())) {
throw new IllegalStateException(String.format("Asset Graph Definition with id '%s' (label: %s) already exists", config.getProviderId(), config.getLabel()));
}
final GraphML graphML = transactionOperations.execute(status -> new AssetGraphGenerator(nodeProvider).generateGraphs(config));
final GraphmlType graphmlType = GraphMLWriter.convert(graphML);
assetGraphDefinitionRepository.addConfigDefinition(config);
graphmlRepository.save(config.getProviderId(), config.getLabel(), graphmlType);
} catch (Exception ex) {
LOG.error("Could not create Asset Topology", ex);
throw new RuntimeException(ex);
}
}
use of org.opennms.features.graphml.model.GraphML in project opennms by OpenNMS.
the class AssetGraphGeneratorTest method verifyGenerationWithNoHierarchies.
@Test
public void verifyGenerationWithNoHierarchies() throws InvalidGraphException, FileNotFoundException {
// Generate
final GeneratorConfig config = new GeneratorConfig();
config.setProviderId("asset");
config.setLabel("testgraph");
config.setPreferredLayout("Grid Layout");
// empty layers
config.setLayerHierarchies(new ArrayList<>());
// empty filters
config.setFilters(new ArrayList<>());
final AssetGraphGenerator assetGraphGenerator = new AssetGraphGenerator(new TestNodeProvider());
final GraphML graphML = assetGraphGenerator.generateGraphs(config);
// Verify
assertEquals(1, graphML.getGraphs().size());
}
use of org.opennms.features.graphml.model.GraphML in project opennms by OpenNMS.
the class AssetGraphGeneratorTest method verifySimpleGraphGeneration.
@Test
public void verifySimpleGraphGeneration() throws InvalidGraphException {
final NodeProvider nodeProvider = new NodeProvider() {
@Override
public List<OnmsNode> getNodes(List<LayerDefinition> definitions) {
List<OnmsNode> nodes = new ArrayList<>();
nodes.add(new NodeBuilder().withId(1).withLabel("Node 1").withAssets().withRegion("Stuttgart").withBuilding("S1").done().getNode());
nodes.add(new NodeBuilder().withId(2).withLabel("Node 2").withAssets().withRegion("Stuttgart").withBuilding("S2").done().getNode());
nodes.add(new NodeBuilder().withId(3).withLabel("Node 3").withAssets().withRegion("Fulda").withBuilding("F1").done().getNode());
nodes.add(new NodeBuilder().withId(4).withLabel("Node 4").withAssets().withRegion("Frankfurt").withBuilding("F1").done().getNode());
nodes.add(new NodeBuilder().withId(5).withLabel("Node 5").withAssets().withRegion("Frankfurt").withBuilding("F2").done().getNode());
nodes.add(new NodeBuilder().withId(6).withLabel("Node 6").withAssets().withRegion("Frankfurt").done().getNode());
nodes.add(new NodeBuilder().withId(7).withLabel("Node 7").withAssets().withBuilding("F2").done().getNode());
return nodes;
}
};
final GeneratorConfig config = new GeneratorConfig();
config.setLayerHierarchies(Lists.newArrayList(NodeParamLabels.ASSET_REGION, NodeParamLabels.ASSET_BUILDING));
// empty filters
config.setFilters(new ArrayList<>());
final GraphML generatedGraphML = new AssetGraphGenerator(nodeProvider).generateGraphs(config);
final GraphML expectedGraphML = GraphMLReader.read(getClass().getResourceAsStream("/test-graph-simple.xml"));
// for debug generate file
GraphMLWriter.write(generatedGraphML, new File("target/verifySimpleGraphGenerationGenerated.graphml"));
Assert.assertEquals(generatedGraphML, expectedGraphML);
}
use of org.opennms.features.graphml.model.GraphML in project opennms by OpenNMS.
the class AssetGraphGeneratorTest method verifyFilter.
@Test
public void verifyFilter() throws InvalidGraphException {
final NodeProvider nodeProvider = new NodeProvider() {
@Override
public List<OnmsNode> getNodes(List<LayerDefinition> definitions) {
List<OnmsNode> nodes = new ArrayList<>();
nodes.add(new NodeBuilder().withId(1).withLabel("Node 1").withAssets().withRegion("Stuttgart").withBuilding("S1").done().getNode());
nodes.add(new NodeBuilder().withId(2).withLabel("Node 2").withAssets().withRegion("Fulda").withBuilding("F1").done().getNode());
return nodes;
}
};
final GeneratorConfig config = new GeneratorConfig();
config.setLayerHierarchies(Lists.newArrayList(NodeParamLabels.ASSET_REGION));
config.setFilters(Lists.newArrayList(String.format("%s=Stuttgart", NodeParamLabels.ASSET_REGION)));
final GraphML generatedGraphML = new AssetGraphGenerator(nodeProvider).generateGraphs(config);
Assert.assertEquals(2, generatedGraphML.getGraphs().size());
Assert.assertEquals(1, generatedGraphML.getGraphs().get(0).getNodes().size());
Assert.assertEquals(1, generatedGraphML.getGraphs().get(1).getNodes().size());
}
Aggregations