Search in sources :

Example 6 with GraphML

use of org.opennms.features.graphml.model.GraphML in project opennms by OpenNMS.

the class GraphMLVertexStatusProviderIT method testDefaultStatusProvider.

@Test
public void testDefaultStatusProvider() throws InvalidGraphException {
    GraphML graphML = GraphMLReader.read(getClass().getResourceAsStream("/test-graph.xml"));
    GraphMLTopologyProvider topologyProvider = new GraphMLTopologyProvider(null, graphML.getGraphs().get(0), new GraphMLServiceAccessor());
    GraphMLDefaultVertexStatusProvider statusProvider = new GraphMLDefaultVertexStatusProvider(topologyProvider.getNamespace(), this.alarmSummaryWrapper);
    List<VertexRef> vertices = topologyProvider.getVertices().stream().map(eachVertex -> (VertexRef) eachVertex).collect(Collectors.toList());
    Assert.assertEquals(4, vertices.size());
    Assert.assertEquals(topologyProvider.getNamespace(), statusProvider.getNamespace());
    Assert.assertTrue(statusProvider.contributesTo(topologyProvider.getNamespace()));
    Map<? extends VertexRef, ? extends Status> statusForVertices = statusProvider.getStatusForVertices(topologyProvider, vertices, new Criteria[0]);
    Assert.assertEquals(4, statusForVertices.size());
    Assert.assertEquals(ImmutableMap.of(createVertexRef(topologyProvider.getNamespace(), "north"), createStatus(OnmsSeverity.WARNING, 1), createVertexRef(topologyProvider.getNamespace(), "west"), createStatus(OnmsSeverity.MINOR, 2), createVertexRef(topologyProvider.getNamespace(), "south"), createStatus(OnmsSeverity.MAJOR, 3), createVertexRef(topologyProvider.getNamespace(), "east"), createStatus(OnmsSeverity.NORMAL, 0)), statusForVertices);
}
Also used : Date(java.util.Date) AlarmSummary(org.opennms.netmgt.model.alarm.AlarmSummary) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) GraphMLReader(org.opennms.features.graphml.model.GraphMLReader) QueryResponse(org.opennms.netmgt.measurements.model.QueryResponse) GraphMLPropagateVertexStatusProvider(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLPropagateVertexStatusProvider) SnmpInterfaceDao(org.opennms.netmgt.dao.api.SnmpInterfaceDao) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Files(com.google.common.io.Files) Map(java.util.Map) InvalidGraphException(org.opennms.features.graphml.model.InvalidGraphException) GraphMLServiceAccessor(org.opennms.features.topology.plugins.topo.graphml.internal.GraphMLServiceAccessor) ServiceReference(org.osgi.framework.ServiceReference) Before(org.junit.Before) DatabasePopulator(org.opennms.netmgt.dao.DatabasePopulator) DefaultVertexRef(org.opennms.features.topology.api.topo.DefaultVertexRef) NodeDao(org.opennms.netmgt.dao.api.NodeDao) JUnitConfigurationEnvironment(org.opennms.test.JUnitConfigurationEnvironment) Resources(com.google.common.io.Resources) ImmutableMap(com.google.common.collect.ImmutableMap) ScriptEngineManager(javax.script.ScriptEngineManager) Test(org.junit.Test) GraphML(org.opennms.features.graphml.model.GraphML) EasyMock(org.easymock.EasyMock) Collectors(java.util.stream.Collectors) File(java.io.File) BundleContext(org.osgi.framework.BundleContext) BeanUtils(org.opennms.core.spring.BeanUtils) List(java.util.List) StatusProvider(org.opennms.features.topology.api.topo.StatusProvider) Rule(org.junit.Rule) OpenNMSJUnit4ClassRunner(org.opennms.core.test.OpenNMSJUnit4ClassRunner) GraphMLDefaultVertexStatusProvider(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLDefaultVertexStatusProvider) Paths(java.nio.file.Paths) GraphMLVertexStatus(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLVertexStatus) AlarmSummaryWrapper(org.opennms.features.topology.plugins.topo.graphml.internal.AlarmSummaryWrapper) TransactionOperations(org.springframework.transaction.support.TransactionOperations) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase) Criteria(org.opennms.features.topology.api.topo.Criteria) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Status(org.opennms.features.topology.api.topo.Status) GraphMLScriptVertexStatusProvider(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLScriptVertexStatusProvider) Assert(org.junit.Assert) VertexRef(org.opennms.features.topology.api.topo.VertexRef) TemporaryFolder(org.junit.rules.TemporaryFolder) OnmsSeverity(org.opennms.netmgt.model.OnmsSeverity) GraphML(org.opennms.features.graphml.model.GraphML) GraphMLDefaultVertexStatusProvider(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLDefaultVertexStatusProvider) GraphMLServiceAccessor(org.opennms.features.topology.plugins.topo.graphml.internal.GraphMLServiceAccessor) DefaultVertexRef(org.opennms.features.topology.api.topo.DefaultVertexRef) VertexRef(org.opennms.features.topology.api.topo.VertexRef) Test(org.junit.Test)

Example 7 with GraphML

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(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;
}
Also used : LayerDefinitionRepository(org.opennms.features.topology.plugins.topo.asset.layers.LayerDefinitionRepository) Logger(org.slf4j.Logger) FilterParser(org.opennms.features.topology.plugins.topo.asset.filter.FilterParser) GraphMLNode(org.opennms.features.graphml.model.GraphMLNode) IdGenerator(org.opennms.features.topology.plugins.topo.asset.layers.IdGenerator) LoggerFactory(org.slf4j.LoggerFactory) LayerDefinition(org.opennms.features.topology.plugins.topo.asset.layers.LayerDefinition) GraphML(org.opennms.features.graphml.model.GraphML) LayerDefinitionRepository(org.opennms.features.topology.plugins.topo.asset.layers.LayerDefinitionRepository) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) GraphMLProperties(org.opennms.features.topology.plugins.topo.graphml.GraphMLProperties) ItemProvider(org.opennms.features.topology.plugins.topo.asset.layers.ItemProvider) Map(java.util.Map) Filter(org.opennms.features.topology.plugins.topo.asset.filter.Filter) LayerBuilder(org.opennms.features.topology.plugins.topo.asset.layers.LayerBuilder) Layer(org.opennms.features.topology.plugins.topo.asset.layers.Layer) GraphMLEdge(org.opennms.features.graphml.model.GraphMLEdge) GraphMLGraph(org.opennms.features.graphml.model.GraphMLGraph) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsNode(org.opennms.netmgt.model.OnmsNode) GraphML(org.opennms.features.graphml.model.GraphML) LayerDefinition(org.opennms.features.topology.plugins.topo.asset.layers.LayerDefinition) GraphMLGraph(org.opennms.features.graphml.model.GraphMLGraph) Layer(org.opennms.features.topology.plugins.topo.asset.layers.Layer) Filter(org.opennms.features.topology.plugins.topo.asset.filter.Filter) LayerBuilder(org.opennms.features.topology.plugins.topo.asset.layers.LayerBuilder) GraphMLEdge(org.opennms.features.graphml.model.GraphMLEdge) GraphMLNode(org.opennms.features.graphml.model.GraphMLNode) FilterParser(org.opennms.features.topology.plugins.topo.asset.filter.FilterParser)

Example 8 with 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);
    }
}
Also used : GraphmlType(org.graphdrawing.graphml.GraphmlType) GraphML(org.opennms.features.graphml.model.GraphML)

Example 9 with GraphML

use of org.opennms.features.graphml.model.GraphML 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);
    }
}
Also used : GraphML(org.opennms.features.graphml.model.GraphML) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) GraphMLGraph(org.opennms.features.graphml.model.GraphMLGraph) VertexHopGraphProvider(org.opennms.features.topology.api.support.VertexHopGraphProvider) VertexRef(org.opennms.features.topology.api.topo.VertexRef) GraphMLEdge(org.opennms.features.graphml.model.GraphMLEdge) FileInputStream(java.io.FileInputStream)

Example 10 with GraphML

use of org.opennms.features.graphml.model.GraphML in project opennms by OpenNMS.

the class AssetGraphMLProvider method regenerateAssetTopology.

/**
 * Regenerates the AssetTopology defined by the providerId
 * @param providerId The providerId to regenerate.
 */
public synchronized void regenerateAssetTopology(String providerId) {
    Objects.requireNonNull(providerId);
    try {
        LOG.debug("Regenerating Asset Topology providerId: {}", providerId);
        if (!assetGraphDefinitionRepository.exists(providerId))
            throw new IllegalStateException(String.format("Asset Graph Definition with id '%s' cannot be regenerated, because it does not exist", providerId));
        GeneratorConfig config = assetGraphDefinitionRepository.getConfigDefinition(providerId);
        final GraphML graphML = transactionOperations.execute(status -> new AssetGraphGenerator(nodeProvider).generateGraphs(config));
        final GraphmlType graphmlType = GraphMLWriter.convert(graphML);
        if (graphmlRepository.exists(providerId))
            graphmlRepository.delete(providerId);
        graphmlRepository.save(config.getProviderId(), config.getLabel(), graphmlType);
    } catch (Exception ex) {
        LOG.error("problem regenerating asset topology ", ex);
        throw new RuntimeException(ex);
    }
}
Also used : GraphmlType(org.graphdrawing.graphml.GraphmlType) GraphML(org.opennms.features.graphml.model.GraphML)

Aggregations

GraphML (org.opennms.features.graphml.model.GraphML)13 Test (org.junit.Test)8 List (java.util.List)7 File (java.io.File)5 TestNodeProvider (org.opennms.features.topology.plugins.topo.asset.util.TestNodeProvider)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 InvalidGraphException (org.opennms.features.graphml.model.InvalidGraphException)4 VertexRef (org.opennms.features.topology.api.topo.VertexRef)4 OnmsNode (org.opennms.netmgt.model.OnmsNode)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Lists (com.google.common.collect.Lists)3 Date (java.util.Date)3 Assert (org.junit.Assert)3 GraphMLReader (org.opennms.features.graphml.model.GraphMLReader)3 Criteria (org.opennms.features.topology.api.topo.Criteria)3 DefaultVertexRef (org.opennms.features.topology.api.topo.DefaultVertexRef)3 Status (org.opennms.features.topology.api.topo.Status)3 NodeBuilder (org.opennms.features.topology.plugins.topo.asset.util.NodeBuilder)3