use of org.opennms.features.graphml.model.GraphMLGraph in project opennms by OpenNMS.
the class GraphMLMetaTopologyProvider method validate.
private void validate(GraphML graphML) throws InvalidGraphException {
final Set<String> graphIds = new HashSet<>();
final Map<String, Set<String>> nodeIdsByNamespace = new HashMap<>();
final Map<String, Set<String>> edgeIdsByNamespace = new HashMap<>();
for (GraphMLGraph eachGraph : graphML.getGraphs()) {
final String ns = eachGraph.getProperty(GraphMLProperties.NAMESPACE);
if (Strings.isNullOrEmpty(ns)) {
throw new InvalidGraphException("No namespace defined on graph with id " + eachGraph.getId());
}
if (graphIds.contains(eachGraph.getId())) {
throw new InvalidGraphException("There already exists a graph with id " + eachGraph.getId());
}
graphIds.add(eachGraph.getId());
for (GraphMLNode eachNode : eachGraph.getNodes()) {
Set<String> nodeIdsInNs = nodeIdsByNamespace.get(ns);
if (nodeIdsInNs == null) {
nodeIdsInNs = new HashSet<>();
nodeIdsByNamespace.put(ns, nodeIdsInNs);
}
if (nodeIdsInNs.contains(eachNode.getId())) {
throw new InvalidGraphException("There already exists a node with id " + eachNode.getId() + " in namespace " + ns);
}
nodeIdsInNs.add(eachNode.getId());
}
for (GraphMLEdge eachEdge : eachGraph.getEdges()) {
Set<String> edgeIdsInNs = edgeIdsByNamespace.get(ns);
if (edgeIdsInNs == null) {
edgeIdsInNs = new HashSet<>();
edgeIdsByNamespace.put(ns, edgeIdsInNs);
}
if (edgeIdsInNs.contains(eachEdge.getId())) {
throw new InvalidGraphException("There already exists an edge with id " + eachEdge.getId() + " in namespace " + ns);
}
edgeIdsInNs.add(eachEdge.getId());
}
}
}
use of org.opennms.features.graphml.model.GraphMLGraph in project opennms by OpenNMS.
the class GraphMLEdgeStatusProviderIT method verify.
@Test
public void verify() throws Exception {
final GraphMLServiceAccessor serviceAccessor = new GraphMLServiceAccessor();
serviceAccessor.setTransactionOperations(transactionOperations);
serviceAccessor.setNodeDao(nodeDao);
serviceAccessor.setSnmpInterfaceDao(snmpInterfaceDao);
serviceAccessor.setMeasurementsService(request -> new QueryResponse());
final GraphMLGraph graph = GraphMLReader.read(getClass().getResourceAsStream("/test-graph2.xml")).getGraphs().get(0);
final GraphMLTopologyProvider topologyProvider = new GraphMLTopologyProvider(null, graph, serviceAccessor);
final GraphMLEdgeStatusProvider provider = new GraphMLEdgeStatusProvider(topologyProvider, new ScriptEngineManager(), serviceAccessor, Paths.get("src", "test", "opennms-home", "etc", "graphml-edge-status"));
assertThat(provider.contributesTo("acme:regions"), is(true));
assertThat(provider.getNamespace(), is("acme:regions"));
// Calculating the status executes some tests defined int the according scripts as a side effect
final EdgeRef edgeRef = topologyProvider.getEdge("acme:regions", "center_north");
final Map<? extends EdgeRef, ? extends Status> status = provider.getStatusForEdges(topologyProvider, ImmutableList.of(edgeRef), new Criteria[0]);
// Checking nodeID creation for vertices with only foreignSource/foreignID set
final VertexRef vertexRef = topologyProvider.getVertex("acme:regions", "west");
assertThat(vertexRef, is(notNullValue()));
assertThat(vertexRef, is(instanceOf(GraphMLVertex.class)));
assertThat(((GraphMLVertex) vertexRef).getNodeID(), is(4));
// Testing status merging from two scripts
assertThat(status, is(notNullValue()));
assertThat(status, is(hasEntry(edgeRef, new GraphMLEdgeStatus().severity(OnmsSeverity.WARNING).style("stroke", "pink").style("stroke-width", "3em"))));
}
use of org.opennms.features.graphml.model.GraphMLGraph 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(LayerDefinition::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.GraphMLGraph in project opennms by OpenNMS.
the class GraphMLMetaTopologyProvider method reload.
public void reload() throws IOException, InvalidGraphException {
graphsByNamespace.clear();
oppositeVertices.clear();
rawGraphsByNamespace.clear();
if (graphMLFile == null) {
LOG.warn("No graph defined");
return;
}
if (!graphMLFile.exists()) {
LOG.warn("No graph found at location " + graphMLFile.toString());
return;
}
try (InputStream input = new FileInputStream(graphMLFile)) {
final GraphML graphML = GraphMLReader.read(input);
validate(graphML);
for (GraphMLGraph eachGraph : graphML.getGraphs()) {
final GraphMLTopologyProvider topoProvider = new GraphMLTopologyProvider(this, eachGraph, m_serviceAccessor);
final VertexHopGraphProvider vertexHopGraphProvider = new VertexHopGraphProvider(topoProvider);
graphsByNamespace.put(topoProvider.getNamespace(), vertexHopGraphProvider);
rawGraphsByNamespace.put(topoProvider.getNamespace(), topoProvider);
}
for (GraphMLGraph eachGraph : graphML.getGraphs()) {
for (org.opennms.features.graphml.model.GraphMLEdge eachEdge : eachGraph.getEdges()) {
final VertexRef sourceVertex = getVertex(eachEdge.getSource());
final VertexRef targetVertex = getVertex(eachEdge.getTarget());
if (!sourceVertex.getNamespace().equals(targetVertex.getNamespace())) {
List<VertexRef> opposites = oppositeVertices.get(sourceVertex);
if (opposites == null) {
opposites = Lists.newArrayList();
oppositeVertices.put(sourceVertex, opposites);
}
opposites.add(targetVertex);
}
}
}
this.breadcrumbStrategy = getBreadcrumbStrategy(graphML);
}
}
use of org.opennms.features.graphml.model.GraphMLGraph in project opennms by OpenNMS.
the class GraphMLSearchProviderTest method verifyContributesTo.
@Test
public void verifyContributesTo() {
GraphMLGraph graph = new GraphMLGraph();
graph.setProperty(GraphMLProperties.NAMESPACE, "namespace1:graph1");
GraphMLTopologyProvider topologyProvider = new GraphMLTopologyProvider(null, graph, new GraphMLServiceAccessor());
GraphMLSearchProvider searchProvider = new GraphMLSearchProvider(topologyProvider);
Assert.assertEquals(true, searchProvider.contributesTo("namespace1:graph1"));
Assert.assertEquals(true, searchProvider.contributesTo("namespace1:graph2"));
Assert.assertEquals(true, searchProvider.contributesTo("namespace1:graph3"));
Assert.assertEquals(false, searchProvider.contributesTo("namespace1"));
}
Aggregations