Ejemplo n.º 1
0
def _graph_contains_subgraph_edges(graph, subgraph, subgraph_edges):
    """Check if graph contains all the expected edges

    For each (sub-graph) expected edge, check if a corresponding edge exists
    in the graph with relevant properties check

    :type graph: driver.Graph
    :type subgraph: driver.Graph
    :type subgraph_edges: set of driver.Edge
    :rtype: bool
    """
    for e in subgraph_edges:
        graph_v_id_source = subgraph.get_vertex(e.source_id).get(MAPPED_V_ID)
        graph_v_id_target = subgraph.get_vertex(e.target_id).get(MAPPED_V_ID)
        if not graph_v_id_source or not graph_v_id_target:
            raise VitrageAlgorithmError('Cant get vertex for edge %s' % e)
        found_graph_edge = graph.get_edge(graph_v_id_source,
                                          graph_v_id_target,
                                          e.label)

        if not found_graph_edge and e.get(NEG_CONDITION):
            continue

        if not found_graph_edge or not check_filter(found_graph_edge, e,
                                                    NEG_CONDITION):
            return False
    return True
Ejemplo n.º 2
0
    def create_graph_from_matching_vertices(self,
                                            vertex_attr_filter=None,
                                            query_dict=None,
                                            edge_attr_filter=None):
        if query_dict:
            vertices = self.graph.get_vertices(query_dict=query_dict)
        elif vertex_attr_filter:
            vertices = self.graph.get_vertices(
                vertex_attr_filter=vertex_attr_filter)
        else:
            vertices = self.graph.get_vertices()

        vertices_ids = [vertex.vertex_id for vertex in vertices]

        graph = NXGraph('graph')
        graph._g = self.graph._g.subgraph(vertices_ids)

        # delete non matching edges
        if edge_attr_filter:
            for source, target, edge_data in graph._g.edges_iter(data=True):
                if not check_filter(edge_data, edge_attr_filter):
                    graph.remove_edge(u=source, v=target)

        LOG.debug('match query, find graph: nodes %s, edges %s',
                  str(graph._g.nodes(data=True)),
                  str(graph._g.edges(data=True)))
        LOG.debug('match query, real graph: nodes %s, edges %s',
                  str(self.graph._g.nodes(data=True)),
                  str(self.graph._g.edges(data=True)))
        return graph
Ejemplo n.º 3
0
    def create_graph_from_matching_vertices(self,
                                            vertex_attr_filter=None,
                                            query_dict=None,
                                            edge_attr_filter=None):
        if query_dict:
            vertices = self.graph.get_vertices(query_dict=query_dict)
        elif vertex_attr_filter:
            vertices = self.graph.get_vertices(
                vertex_attr_filter=vertex_attr_filter)
        else:
            vertices = self.graph.get_vertices()

        vertices_ids = [vertex.vertex_id for vertex in vertices]

        graph = NXGraph('graph')
        graph._g = self.graph._g.subgraph(vertices_ids)

        # delete non matching edges
        if edge_attr_filter:
            for source, target, edge_data in graph._g.edges_iter(data=True):
                if not check_filter(edge_data, edge_attr_filter):
                    graph.remove_edge(u=source, v=target)

        LOG.debug('match query, find graph: nodes %s, edges %s',
                  str(graph._g.nodes(data=True)),
                  str(graph._g.edges(data=True)))
        LOG.debug('match query, real graph: nodes %s, edges %s',
                  str(self.graph._g.nodes(data=True)),
                  str(self.graph._g.edges(data=True)))
        return graph
Ejemplo n.º 4
0
def _update_mapping(subgraph, graph, subgraph_id, graph_id, validate):
    subgraph_vertex = subgraph.get_vertex(subgraph_id)
    if validate:
        graph_vertex = graph.get_vertex(graph_id)
        if not check_filter(graph_vertex, subgraph_vertex, MAPPED_V_ID):
            return False
    subgraph_vertex[MAPPED_V_ID] = graph_id
    subgraph.update_vertex(subgraph_vertex)
    return True
Ejemplo n.º 5
0
    def apply_edge_attr_filter(graph, edge_attr_filter):
        edges = graph._g.edges(data=True, keys=True)
        edges_to_remove = [(u, v, k) for (u, v, k, d) in edges
                           if not check_filter(d, edge_attr_filter)]

        if edges_to_remove and is_frozen(graph._g):
            graph._g = graph._g.copy()

        for source, target, key in edges_to_remove:
            graph._g.remove_edge(u=source, v=target, key=key)
Ejemplo n.º 6
0
    def test_basic_regex(self):
        event_properties = {
            "time": 121354,
            "vitrage_type": "zabbix",
            "vitrage_category": "ALARM",
            "rawtext": "Interface kukoo down on {HOST.NAME}",
            "host": "some_host_kukoo"
        }

        attr_filter = {
            "vitrage_category": "ALARM",
            "rawtext.regex": "Interface ([_a-zA-Z0-9'\-]+) down on {"
            "HOST.NAME}",
            "host": "some_host_kukoo"
        }
        self.assertEqual(
            True, check_filter(data=event_properties, attr_filter=attr_filter))
Ejemplo n.º 7
0
    def test_basic_regex_with_no_match(self):
        event_properties = {
            "time": 121354,
            "vitrage_type": "zabbix",
            "vitrage_category": "ALARM",
            "rawtext": "Text With No Match",
            "host": "some_host_kukoo"
        }

        attr_filter = {
            "vitrage_category": "ALARM",
            "rawtext.RegEx": "Interface ([_a-zA-Z0-9'\-]+) down on {"
            "HOST.NAME}",
            "host": "some_host_kukoo"
        }
        self.assertFalse(
            check_filter(data=event_properties, attr_filter=attr_filter))
Ejemplo n.º 8
0
def _graph_contains_subgraph_edges(graph, subgraph, subgraph_edges):
    """Check if graph contains all the expected edges

    For each (sub-graph) expected edge, check if a corresponding edge exists
    in the graph with relevant properties check

    :type graph: driver.Graph
    :type subgraph: driver.Graph
    :type subgraph_edges: set of driver.Edge
    :rtype: bool
    """
    for e in subgraph_edges:
        graph_v_id_source = subgraph.get_vertex(e.source_id).get(MAPPED_V_ID)
        graph_v_id_target = subgraph.get_vertex(e.target_id).get(MAPPED_V_ID)
        if not graph_v_id_source or not graph_v_id_target:
            raise VitrageAlgorithmError('Cant get vertex for edge' + str(e))
        found_graph_edge = graph.get_edge(graph_v_id_source,
                                          graph_v_id_target,
                                          e.label)
        if not found_graph_edge or not check_filter(found_graph_edge, e):
            return False
    return True
Ejemplo n.º 9
0
 def check_vertex(vertex_data):
     return check_filter(vertex_data, vertex_attr_filter)
Ejemplo n.º 10
0
 def check_edge(edge_data):
     return check_filter(edge_data, edge_attr_filter)
Ejemplo n.º 11
0
 def check_vertex(vertex_data):
     return check_filter(vertex_data, vertex_attr_filter)
Ejemplo n.º 12
0
 def check_edge(edge_data):
     return check_filter(edge_data, edge_attr_filter)
Ejemplo n.º 13
0
 def _apply_edge_attr_filter(graph, edge_attr_filter):
     for source, target, edge_data in graph._g.edges_iter(data=True):
         if not check_filter(edge_data, edge_attr_filter):
             graph._g.remove_edge(u=source, v=target)