def topology_to_graph(topology):
    graph = NXGraph()
    nodes = topology['nodes']
    for n in nodes:
        graph.add_vertex(Vertex(n['vitrage_id'], n))

    edges = topology['links']
    for i in range(len(edges)):
        s_id = nodes[edges[i]['source']]['vitrage_id']
        t_id = nodes[edges[i]['target']]['vitrage_id']
        graph.add_edge(Edge(s_id, t_id, edges[i]['relationship_type']))
    return graph
Пример #2
0
    def _create_graph_from_graph_dictionary(self, api_graph):
        self.assertIsNotNone(api_graph)
        graph = NXGraph()

        nodes = api_graph['nodes']
        for i in range(len(nodes)):
            graph.add_vertex(Vertex(str(i), nodes[i]))

        edges = api_graph['links']
        for i in range(len(edges)):
            graph.add_edge(Edge(str(edges[i]['source']),
                                str(edges[i]['target']),
                                edges[i][EdgeProperties.RELATIONSHIP_TYPE]))

        return graph
Пример #3
0
    def _create_graph_from_tree_dictionary(self,
                                           api_graph,
                                           graph=None,
                                           ancestor=None):
        children = []
        graph = NXGraph() if not graph else graph

        if 'children' in api_graph:
            children = api_graph.copy()['children']
            del api_graph['children']

        vertex = Vertex(api_graph[VProps.VITRAGE_ID], api_graph)
        graph.add_vertex(vertex)
        if ancestor:
            graph.add_edge(Edge(ancestor[VProps.VITRAGE_ID],
                                vertex[VProps.VITRAGE_ID],
                                'label'))

        for entity in children:
            self._create_graph_from_tree_dictionary(entity, graph, vertex)

        return graph
Пример #4
0
    def _create_entity_graph(cls, name, num_of_alarms_per_host,
                             num_of_alarms_per_vm,
                             num_of_hosts_per_node,
                             num_of_vms_per_host,
                             num_of_tests_per_host):

        start = time.time()
        g = NXGraph(name)
        g.add_vertex(v_node)
        g.add_vertex(v_switch)
        g.add_edge(e_node_to_switch)

        # Add Hosts
        for host_id in range(num_of_hosts_per_node):
            host_to_add = add_connected_vertex(g,
                                               RESOURCE,
                                               NOVA_HOST_DATASOURCE,
                                               host_id,
                                               ELabel.CONTAINS,
                                               v_node,
                                               True)

            g.add_edge(graph_utils.create_edge(host_to_add.vertex_id,
                                               v_switch.vertex_id, 'USES'))

            # Add Host Alarms
            for j in range(num_of_alarms_per_host):
                add_connected_vertex(g, ALARM, ALARM_ON_HOST,
                                     cls.host_alarm_id, ELabel.ON,
                                     host_to_add, False,
                                     {VProps.RESOURCE_ID: host_id,
                                      VProps.NAME: host_id})
                cls.host_alarm_id += 1

            # Add Host Tests
            for j in range(num_of_tests_per_host):
                add_connected_vertex(g, TEST, TEST_ON_HOST, cls.host_test_id,
                                     ELabel.ON, host_to_add)
                cls.host_test_id += 1

            # Add Host Vms
            for j in range(num_of_vms_per_host):
                vm_to_add = add_connected_vertex(g,
                                                 RESOURCE,
                                                 NOVA_INSTANCE_DATASOURCE,
                                                 cls.vm_id,
                                                 ELabel.CONTAINS,
                                                 host_to_add,
                                                 True)
                cls.vm_id += 1
                cls.vms.append(vm_to_add)

                # Add Instance Alarms
                for k in range(num_of_alarms_per_vm):
                    add_connected_vertex(g, ALARM, ALARM_ON_VM,
                                         cls.vm_alarm_id, ELabel.ON,
                                         vm_to_add, False,
                                         {VProps.RESOURCE_ID: cls.vm_id - 1,
                                          VProps.NAME: cls.vm_id - 1})
                    cls.vm_alarm_id += 1

        end = time.time()
        LOG.debug('Graph creation took ' + str(end - start) +
                  ' seconds, size is: ' + str(len(g)))
        expected_graph_size = \
            2 + num_of_hosts_per_node + num_of_hosts_per_node * \
            num_of_alarms_per_host + num_of_hosts_per_node * \
            num_of_vms_per_host + num_of_hosts_per_node * \
            num_of_vms_per_host * num_of_alarms_per_vm + \
            num_of_tests_per_host * num_of_hosts_per_node
        if not expected_graph_size == len(g):
            raise VitrageError('Init failed, graph size unexpected {0} != {1}'
                               .format(expected_graph_size, len(g)))
        return g
Пример #5
0
    def _create_graph(self):
        graph = NXGraph('Multi tenancy graph')
        self._add_alarm_persistency_subscription(graph)

        # create vertices
        cluster_vertex = create_cluster_placeholder_vertex()
        zone_vertex = self._create_resource('zone_1', NOVA_ZONE_DATASOURCE)
        host_vertex = self._create_resource('host_1', NOVA_HOST_DATASOURCE)
        instance_1_vertex = self._create_resource('instance_1',
                                                  NOVA_INSTANCE_DATASOURCE,
                                                  project_id='project_1')
        instance_2_vertex = self._create_resource('instance_2',
                                                  NOVA_INSTANCE_DATASOURCE,
                                                  project_id='project_1')
        instance_3_vertex = self._create_resource('instance_3',
                                                  NOVA_INSTANCE_DATASOURCE,
                                                  project_id='project_2')
        instance_4_vertex = self._create_resource('instance_4',
                                                  NOVA_INSTANCE_DATASOURCE,
                                                  project_id='project_2')
        alarm_on_host_vertex = self._create_alarm(
            'alarm_on_host',
            'alarm_on_host',
            metadata={
                VProps.VITRAGE_TYPE: NOVA_HOST_DATASOURCE,
                VProps.NAME: 'host_1',
                VProps.RESOURCE_ID: 'host_1',
                VProps.VITRAGE_OPERATIONAL_SEVERITY:
                OperationalAlarmSeverity.SEVERE,
                VProps.VITRAGE_AGGREGATED_SEVERITY:
                OperationalAlarmSeverity.SEVERE
            })
        alarm_on_instance_1_vertex = self._create_alarm(
            'alarm_on_instance_1',
            'deduced_alarm',
            project_id='project_1',
            vitrage_resource_project_id='project_1',
            metadata={
                VProps.VITRAGE_TYPE: NOVA_INSTANCE_DATASOURCE,
                VProps.NAME: 'instance_1',
                VProps.RESOURCE_ID: 'sdg7849ythksjdg',
                VProps.VITRAGE_OPERATIONAL_SEVERITY:
                OperationalAlarmSeverity.SEVERE,
                VProps.VITRAGE_AGGREGATED_SEVERITY:
                OperationalAlarmSeverity.SEVERE
            })
        alarm_on_instance_2_vertex = self._create_alarm(
            'alarm_on_instance_2',
            'deduced_alarm',
            vitrage_resource_project_id='project_1',
            metadata={
                VProps.VITRAGE_TYPE:
                NOVA_INSTANCE_DATASOURCE,
                VProps.NAME:
                'instance_2',
                VProps.RESOURCE_ID:
                'nbfhsdugf',
                VProps.VITRAGE_OPERATIONAL_SEVERITY:
                OperationalAlarmSeverity.WARNING,
                VProps.VITRAGE_AGGREGATED_SEVERITY:
                OperationalAlarmSeverity.WARNING
            })
        alarm_on_instance_3_vertex = self._create_alarm(
            'alarm_on_instance_3',
            'deduced_alarm',
            project_id='project_2',
            vitrage_resource_project_id='project_2',
            metadata={
                VProps.VITRAGE_TYPE:
                NOVA_INSTANCE_DATASOURCE,
                VProps.NAME:
                'instance_3',
                VProps.RESOURCE_ID:
                'nbffhsdasdugf',
                VProps.VITRAGE_OPERATIONAL_SEVERITY:
                OperationalAlarmSeverity.CRITICAL,
                VProps.VITRAGE_AGGREGATED_SEVERITY:
                OperationalAlarmSeverity.CRITICAL
            })
        alarm_on_instance_4_vertex = self._create_alarm(
            'alarm_on_instance_4',
            'deduced_alarm',
            vitrage_resource_project_id='project_2',
            metadata={
                VProps.VITRAGE_TYPE:
                NOVA_INSTANCE_DATASOURCE,
                VProps.NAME:
                'instance_4',
                VProps.RESOURCE_ID:
                'ngsuy76hgd87f',
                VProps.VITRAGE_OPERATIONAL_SEVERITY:
                OperationalAlarmSeverity.WARNING,
                VProps.VITRAGE_AGGREGATED_SEVERITY:
                OperationalAlarmSeverity.WARNING
            })

        # create links
        edges = list()
        edges.append(
            graph_utils.create_edge(cluster_vertex.vertex_id,
                                    zone_vertex.vertex_id,
                                    EdgeLabel.CONTAINS,
                                    update_timestamp=str(utcnow())))
        edges.append(
            graph_utils.create_edge(zone_vertex.vertex_id,
                                    host_vertex.vertex_id,
                                    EdgeLabel.CONTAINS,
                                    update_timestamp=str(utcnow())))
        edges.append(
            graph_utils.create_edge(host_vertex.vertex_id,
                                    instance_1_vertex.vertex_id,
                                    EdgeLabel.CONTAINS,
                                    update_timestamp=str(utcnow())))
        edges.append(
            graph_utils.create_edge(host_vertex.vertex_id,
                                    instance_2_vertex.vertex_id,
                                    EdgeLabel.CONTAINS,
                                    update_timestamp=str(utcnow())))
        edges.append(
            graph_utils.create_edge(host_vertex.vertex_id,
                                    instance_3_vertex.vertex_id,
                                    EdgeLabel.CONTAINS,
                                    update_timestamp=str(utcnow())))
        edges.append(
            graph_utils.create_edge(host_vertex.vertex_id,
                                    instance_4_vertex.vertex_id,
                                    EdgeLabel.CONTAINS,
                                    update_timestamp=str(utcnow())))
        edges.append(
            graph_utils.create_edge(alarm_on_host_vertex.vertex_id,
                                    host_vertex.vertex_id,
                                    EdgeLabel.ON,
                                    update_timestamp=str(utcnow())))
        edges.append(
            graph_utils.create_edge(alarm_on_instance_1_vertex.vertex_id,
                                    instance_1_vertex.vertex_id,
                                    EdgeLabel.ON,
                                    update_timestamp=str(utcnow())))
        edges.append(
            graph_utils.create_edge(alarm_on_instance_2_vertex.vertex_id,
                                    instance_2_vertex.vertex_id,
                                    EdgeLabel.ON,
                                    update_timestamp=str(utcnow())))
        edges.append(
            graph_utils.create_edge(alarm_on_instance_3_vertex.vertex_id,
                                    instance_3_vertex.vertex_id,
                                    EdgeLabel.ON,
                                    update_timestamp=str(utcnow())))
        edges.append(
            graph_utils.create_edge(alarm_on_instance_4_vertex.vertex_id,
                                    instance_4_vertex.vertex_id,
                                    EdgeLabel.ON,
                                    update_timestamp=str(utcnow())))
        edges.append(
            graph_utils.create_edge(alarm_on_host_vertex.vertex_id,
                                    alarm_on_instance_1_vertex.vertex_id,
                                    EdgeLabel.CAUSES,
                                    update_timestamp=str(utcnow())))
        edges.append(
            graph_utils.create_edge(alarm_on_host_vertex.vertex_id,
                                    alarm_on_instance_2_vertex.vertex_id,
                                    EdgeLabel.CAUSES,
                                    update_timestamp=str(utcnow())))
        edges.append(
            graph_utils.create_edge(alarm_on_host_vertex.vertex_id,
                                    alarm_on_instance_3_vertex.vertex_id,
                                    EdgeLabel.CAUSES,
                                    update_timestamp=str(utcnow())))
        edges.append(
            graph_utils.create_edge(alarm_on_host_vertex.vertex_id,
                                    alarm_on_instance_4_vertex.vertex_id,
                                    EdgeLabel.CAUSES,
                                    update_timestamp=str(utcnow())))

        # add vertices to graph
        graph.add_vertex(cluster_vertex)
        graph.add_vertex(zone_vertex)
        graph.add_vertex(host_vertex)
        graph.add_vertex(instance_1_vertex)
        graph.add_vertex(instance_2_vertex)
        graph.add_vertex(instance_3_vertex)
        graph.add_vertex(instance_4_vertex)
        graph.add_vertex(alarm_on_host_vertex)
        graph.add_vertex(alarm_on_instance_1_vertex)
        graph.add_vertex(alarm_on_instance_2_vertex)
        graph.add_vertex(alarm_on_instance_3_vertex)
        graph.add_vertex(alarm_on_instance_4_vertex)

        # add links to graph
        for edge in edges:
            graph.add_edge(edge)

        return graph
Пример #6
0
    def _create_graph(self):
        graph = NXGraph('Multi tenancy graph', uuid=True)

        # create vertices
        cluster_vertex = create_cluster_placeholder_vertex()
        zone_vertex = self._create_resource('zone_1',
                                            NOVA_ZONE_DATASOURCE)
        host_vertex = self._create_resource('host_1',
                                            NOVA_HOST_DATASOURCE)
        instance_1_vertex = self._create_resource('instance_1',
                                                  NOVA_INSTANCE_DATASOURCE,
                                                  project_id='project_1')
        instance_2_vertex = self._create_resource('instance_2',
                                                  NOVA_INSTANCE_DATASOURCE,
                                                  project_id='project_1')
        instance_3_vertex = self._create_resource('instance_3',
                                                  NOVA_INSTANCE_DATASOURCE,
                                                  project_id='project_2')
        instance_4_vertex = self._create_resource('instance_4',
                                                  NOVA_INSTANCE_DATASOURCE,
                                                  project_id='project_2')
        alarm_on_host_vertex = self._create_alarm(
            'alarm_on_host',
            'alarm_on_host',
            metadata={'type': 'nova.host',
                      'name': 'host_1',
                      'resource_id': 'host_1'})
        alarm_on_instance_1_vertex = self._create_alarm(
            'alarm_on_instance_1',
            'deduced_alarm',
            project_id='project_1',
            metadata={'type': 'nova.instance',
                      'name': 'instance_1',
                      'resource_id': 'sdg7849ythksjdg'})
        alarm_on_instance_2_vertex = self._create_alarm(
            'alarm_on_instance_2',
            'deduced_alarm',
            metadata={'type': 'nova.instance',
                      'name': 'instance_2',
                      'resource_id': 'nbfhsdugf'})
        alarm_on_instance_3_vertex = self._create_alarm(
            'alarm_on_instance_3',
            'deduced_alarm',
            project_id='project_2',
            metadata={'type': 'nova.instance',
                      'name': 'instance_3',
                      'resource_id': 'nbffhsdasdugf'})
        alarm_on_instance_4_vertex = self._create_alarm(
            'alarm_on_instance_4',
            'deduced_alarm',
            metadata={'type': 'nova.instance',
                      'name': 'instance_4',
                      'resource_id': 'ngsuy76hgd87f'})

        # create links
        edges = list()
        edges.append(graph_utils.create_edge(
            cluster_vertex.vertex_id,
            zone_vertex.vertex_id,
            'contains'))
        edges.append(graph_utils.create_edge(
            zone_vertex.vertex_id,
            host_vertex.vertex_id,
            'contains'))
        edges.append(graph_utils.create_edge(
            host_vertex.vertex_id,
            instance_1_vertex.vertex_id,
            'contains'))
        edges.append(graph_utils.create_edge(
            host_vertex.vertex_id,
            instance_2_vertex.vertex_id,
            'contains'))
        edges.append(graph_utils.create_edge(
            host_vertex.vertex_id,
            instance_3_vertex.vertex_id,
            'contains'))
        edges.append(graph_utils.create_edge(
            host_vertex.vertex_id,
            instance_4_vertex.vertex_id,
            'contains'))
        edges.append(graph_utils.create_edge(
            alarm_on_host_vertex.vertex_id,
            host_vertex.vertex_id,
            'on'))
        edges.append(graph_utils.create_edge(
            alarm_on_instance_1_vertex.vertex_id,
            instance_1_vertex.vertex_id,
            'on'))
        edges.append(graph_utils.create_edge(
            alarm_on_instance_2_vertex.vertex_id,
            instance_2_vertex.vertex_id,
            'on'))
        edges.append(graph_utils.create_edge(
            alarm_on_instance_3_vertex.vertex_id,
            instance_3_vertex.vertex_id,
            'on'))
        edges.append(graph_utils.create_edge(
            alarm_on_instance_4_vertex.vertex_id,
            instance_4_vertex.vertex_id,
            'on'))
        edges.append(graph_utils.create_edge(
            alarm_on_host_vertex.vertex_id,
            alarm_on_instance_1_vertex.vertex_id,
            'causes'))
        edges.append(graph_utils.create_edge(
            alarm_on_host_vertex.vertex_id,
            alarm_on_instance_2_vertex.vertex_id,
            'causes'))
        edges.append(graph_utils.create_edge(
            alarm_on_host_vertex.vertex_id,
            alarm_on_instance_3_vertex.vertex_id,
            'causes'))
        edges.append(graph_utils.create_edge(
            alarm_on_host_vertex.vertex_id,
            alarm_on_instance_4_vertex.vertex_id,
            'causes'))

        # add vertices to graph
        graph.add_vertex(cluster_vertex)
        graph.add_vertex(zone_vertex)
        graph.add_vertex(host_vertex)
        graph.add_vertex(instance_1_vertex)
        graph.add_vertex(instance_2_vertex)
        graph.add_vertex(instance_3_vertex)
        graph.add_vertex(instance_4_vertex)
        graph.add_vertex(alarm_on_host_vertex)
        graph.add_vertex(alarm_on_instance_1_vertex)
        graph.add_vertex(alarm_on_instance_2_vertex)
        graph.add_vertex(alarm_on_instance_3_vertex)
        graph.add_vertex(alarm_on_instance_4_vertex)

        # add links to graph
        for edge in edges:
            graph.add_edge(edge)

        return graph
Пример #7
0
class GraphCloneWorkerBase(coord.Service):
    def __init__(self,
                 worker_id,
                 conf,
                 task_queues):
        super(GraphCloneWorkerBase, self).__init__(worker_id, conf)
        self._conf = conf
        self._task_queue = task_queues[worker_id]
        self._entity_graph = NXGraph()

    name = 'GraphCloneWorkerBase'

    @abc.abstractmethod
    def _init_instance(self):
        """This method is executed in the newly created process"""
        raise NotImplementedError

    def run(self):
        super(GraphCloneWorkerBase, self).run()
        self._entity_graph.notifier._subscriptions = []  # Quick n dirty
        self._init_instance()
        if self._entity_graph.num_vertices():
            LOG.info("%s - Started %s (%s vertices)", self.__class__.__name__,
                     self.worker_id, self._entity_graph.num_vertices())
        else:
            LOG.info("%s - Started empty %s", self.__class__.__name__,
                     self.worker_id)
        self._read_queue()

    def _read_queue(self):
        LOG.debug("%s - reading queue %s",
                  self.__class__.__name__, self.worker_id)
        while True:
            try:
                next_task = self._task_queue.get()
                self.do_task(next_task)
            except Exception:
                LOG.exception("Graph may not be in sync.")
            if isinstance(self._task_queue,
                          multiprocessing.queues.JoinableQueue):
                self._task_queue.task_done()

    def do_task(self, task):
        action = task[0]
        if action == GRAPH_UPDATE:
            (action, before, current, is_vertex) = task
            self._graph_update(before, current, is_vertex)
        elif action == READ_DB_GRAPH:
            self._read_db_graph()
        elif action == WAIT_FOR_WORKER_START:
            # Nothing to do, manager is just verifying this worker is alive
            pass

    def _graph_update(self, before, current, is_vertex):
        if current:
            if is_vertex:
                self._entity_graph.add_vertex(current)
            else:
                self._entity_graph.add_edge(current)
        else:
            if is_vertex:
                self._entity_graph.remove_vertex(before)
            else:
                self._entity_graph.remove_edge(before)

    def _read_db_graph(self):
        db = storage.get_connection_from_config(self._conf)
        graph_snapshot = db.graph_snapshots.query()
        NXGraph.read_gpickle(graph_snapshot.graph_snapshot, self._entity_graph)
        GraphPersistency.do_replay_events(db, self._entity_graph,
                                          graph_snapshot.event_id)
        self._entity_graph.ready = True