Search in sources :

Example 1 with LayerDefinitionRepository

use of org.opennms.features.topology.plugins.topo.asset.layers.LayerDefinitionRepository in project opennms by OpenNMS.

the class AssetGraphGenerator method applyFilters.

public static void applyFilters(List<OnmsNode> nodes, Map<String, Filter> filterMap, LayerDefinitionRepository layerDefinitionRepository) {
    final List<LayerDefinition> layersToFilter = layerDefinitionRepository.getDefinitions(filterMap.keySet());
    layersToFilter.stream().filter(layerToFilter -> filterMap.get(layerToFilter.getKey()) != null).forEach(layerToFilter -> {
        final List<OnmsNode> filteredNodes = nodes.stream().filter(n -> {
            ItemProvider itemProvider = layerToFilter.getLayer().getItemProvider();
            Filter filter = filterMap.get(layerToFilter.getKey());
            return filter.apply(itemProvider.getItem(n));
        }).collect(Collectors.toList());
        if (!filteredNodes.isEmpty()) {
            final Layer layer = layerToFilter.getLayer();
            LOG.debug("Found nodes to remove due to filter settings. Removing nodes {}", filteredNodes.stream().map(n -> String.format("(id: %s, label: %s)", n.getId(), n.getLabel())).collect(Collectors.toList()));
            nodes.removeAll(filteredNodes);
        }
    });
}
Also used : 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) ItemProvider(org.opennms.features.topology.plugins.topo.asset.layers.ItemProvider) OnmsNode(org.opennms.netmgt.model.OnmsNode) Filter(org.opennms.features.topology.plugins.topo.asset.filter.Filter) LayerDefinition(org.opennms.features.topology.plugins.topo.asset.layers.LayerDefinition) Layer(org.opennms.features.topology.plugins.topo.asset.layers.Layer)

Example 2 with LayerDefinitionRepository

use of org.opennms.features.topology.plugins.topo.asset.layers.LayerDefinitionRepository 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;
}
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 3 with LayerDefinitionRepository

use of org.opennms.features.topology.plugins.topo.asset.layers.LayerDefinitionRepository in project opennms by OpenNMS.

the class PersistenceNodeProviderIT method verifyLayerRestrictions.

/**
     * In order to build a valid hierarchy, each{@link Layer}
     * must ensure that the value for each node IS NOT null.
     * To let the database do the filtering, a {@link Restriction}
     * annotation must be present.
     * This tests ensures that each defined {@link Layer} actually provides a restriction and that the {@link Restriction} works as expected.
     */
@Test
public void verifyLayerRestrictions() throws Exception {
    List<LayerDefinition> mapping = new LayerDefinitionRepository().getDefinitions(NodeParamLabels.ALL_KEYS);
    Assert.assertEquals(NodeParamLabels.ALL_KEYS.size(), Layers.values().length);
    Assert.assertEquals(Layers.values().length, mapping.size());
    final List<OnmsNode> nodes = new PersistenceNodeProvider(genericPersistenceAccessor).getNodes(mapping);
    Assert.assertEquals(1, nodes.size());
    OnmsNode actualNode = nodes.get(0);
    OnmsNode expectedNode = nodeDao.findByLabel("Node 2").get(0);
    Assert.assertEquals(expectedNode.getId(), actualNode.getId());
}
Also used : LayerDefinitionRepository(org.opennms.features.topology.plugins.topo.asset.layers.LayerDefinitionRepository) OnmsNode(org.opennms.netmgt.model.OnmsNode) LayerDefinition(org.opennms.features.topology.plugins.topo.asset.layers.LayerDefinition) PersistenceNodeProvider(org.opennms.features.topology.plugins.topo.asset.layers.PersistenceNodeProvider) Test(org.junit.Test)

Example 4 with LayerDefinitionRepository

use of org.opennms.features.topology.plugins.topo.asset.layers.LayerDefinitionRepository in project opennms by OpenNMS.

the class FilterParserTest method testFilterCode.

/**
	 * Excerpt from CreateAssetTopology - code which runs filter
	 * @param nodeList
	 * @param filterMap
	 * @return
	 */
public List<OnmsNode> testFilterCode(List<OnmsNode> nodeList, Map<String, Filter> filterMap) {
    final LayerDefinitionRepository layerDefinitionRepository = new LayerDefinitionRepository();
    final List<OnmsNode> nodes = new ArrayList<OnmsNode>(nodeList);
    AssetGraphGenerator.applyFilters(nodes, filterMap, layerDefinitionRepository);
    return nodes;
}
Also used : LayerDefinitionRepository(org.opennms.features.topology.plugins.topo.asset.layers.LayerDefinitionRepository) OnmsNode(org.opennms.netmgt.model.OnmsNode) ArrayList(java.util.ArrayList)

Aggregations

LayerDefinitionRepository (org.opennms.features.topology.plugins.topo.asset.layers.LayerDefinitionRepository)4 OnmsNode (org.opennms.netmgt.model.OnmsNode)4 ArrayList (java.util.ArrayList)3 LayerDefinition (org.opennms.features.topology.plugins.topo.asset.layers.LayerDefinition)3 List (java.util.List)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Collectors (java.util.stream.Collectors)2 GraphML (org.opennms.features.graphml.model.GraphML)2 GraphMLEdge (org.opennms.features.graphml.model.GraphMLEdge)2 GraphMLGraph (org.opennms.features.graphml.model.GraphMLGraph)2 GraphMLNode (org.opennms.features.graphml.model.GraphMLNode)2 Filter (org.opennms.features.topology.plugins.topo.asset.filter.Filter)2 FilterParser (org.opennms.features.topology.plugins.topo.asset.filter.FilterParser)2 IdGenerator (org.opennms.features.topology.plugins.topo.asset.layers.IdGenerator)2 ItemProvider (org.opennms.features.topology.plugins.topo.asset.layers.ItemProvider)2 Layer (org.opennms.features.topology.plugins.topo.asset.layers.Layer)2 LayerBuilder (org.opennms.features.topology.plugins.topo.asset.layers.LayerBuilder)2 GraphMLProperties (org.opennms.features.topology.plugins.topo.graphml.GraphMLProperties)2 Logger (org.slf4j.Logger)2