def remove_resource(self, graph_db):
     """
     Remove the resource from the node
     :param graph_db: Graph DB instance
     """
     with mutex:
         neo_resource.delete_node(graph_db, self.index)
 def remove_resource(self, graph_db):
     """
     Remove the resource from the node
     :param graph_db: Graph DB instance
     """
     with mutex:
         neo_resource.delete_node(graph_db, self.index)
def delete_pop(pop_url, uuid):
    """
    Delete PoP with given uuid
    :param pop_url: Url of PoP DB
    :param uuid: PoP uuid
    """
    graph_db = neo4j.Graph(pop_url)
    index = ('pop', 'uuid', uuid)
    neo_resource.delete_node(graph_db, index=index)
def delete_pop(pop_url, uuid):
    """
    Delete PoP with given uuid
    :param pop_url: Url of PoP DB
    :param uuid: PoP uuid
    """
    graph_db = neo4j.Graph(pop_url)
    index = ('pop', 'uuid', uuid)
    neo_resource.delete_node(graph_db, index=index)
    def store(self, path, hwloc_file, cpu_file=None, sriov_file=None, dpdk_file=None, timestamp=None):
        """
        Store information contained in files created by the EPA agents into Neo4j.
        using a networkx graph
        :param path: Path of the files
        :param hwloc_file: Hardware locality file
        :param cpu_file: Optional cpu information file
        :param sriov_file: Optional SR-IOV information file
        :param dpdk_file: Optional DPDK information file
        :param timestamp: Optional timestamp in epoch
        """
        graph = nx.DiGraph()
        xml_root = Et.parse(path + hwloc_file).getroot()
        deleted_edges = {}
        for child in xml_root:
            _parse_object_hwloc(graph, child, self.hostname, deleted_edges, self.pop_id)

        if cpu_file is not None:
            processors_dict = _parse_cpu_info(path + cpu_file)
            _enrich_graph_cpuinfo(graph, processors_dict)

        if dpdk_file is not None:
            dpdk_dict = _parse_dpdk_info(path + dpdk_file)
            _enrich_graph_dpdkinfo(graph, dpdk_dict)

        if sriov_file is not None:
            sriov_dict = _parse_sriov_info(path + sriov_file)
            _enrich_graph_sriovinfo(graph, sriov_dict)

        if timestamp is not None:
            now = timestamp
        else:
            now = time.time()

        neo_id_nodes = {}

        nodes_to_add = []
        nodes_stored = []

        query_string = (
            "Match n Where n.hostname = {hostname} " "And n.resource_type = {resource_type} Return n.physical_name"
        )

        res = self.graph_db.cypher.execute(query_string, hostname=self.hostname, resource_type="physical")

        for item in res:
            print str(item)
            nodes_stored.append(item["n.physical_name"])

        for nx_node in graph.nodes():
            nodes_to_add.append(str(nx_node))
            neo_id_nodes[nx_node] = neo_resource.add_node(
                self.graph_db, (self.label, self.index, nx_node), now, get_node_properties(graph, nx_node)
            )

        nodes_to_remove = [item for item in nodes_stored if item not in nodes_to_add]

        for node in nodes_to_remove:
            neo_resource.delete_node(self.graph_db, (self.label, self.index, node))

        for edge in graph.edges():
            source = edge[0]
            target = edge[1]
            edge_label = ""
            if "label" in graph.edge[source][target]:
                edge_label = graph.edge[source][target]["label"]
            db_src = neo_id_nodes[source]
            db_target = neo_id_nodes[target]
            rel_stored = neo_resource.get_edge(self.graph_db, db_src, db_target)
            if rel_stored is None:
                neo_resource.add_edge(self.graph_db, db_src, db_target, timestamp, edge_label)
            else:
                neo_resource.update_edge(self.graph_db, timestamp, edge_label, db_src=db_src, db_target=db_target)
    def store(self,
              path,
              hwloc_file,
              cpu_file=None,
              sriov_file=None,
              dpdk_file=None,
              timestamp=None):
        """
        Store information contained in files created by the EPA agents into Neo4j.
        using a networkx graph
        :param path: Path of the files
        :param hwloc_file: Hardware locality file
        :param cpu_file: Optional cpu information file
        :param sriov_file: Optional SR-IOV information file
        :param dpdk_file: Optional DPDK information file
        :param timestamp: Optional timestamp in epoch
        """
        graph = nx.DiGraph()
        xml_root = Et.parse(path + hwloc_file).getroot()
        deleted_edges = {}
        for child in xml_root:
            _parse_object_hwloc(graph, child, self.hostname, deleted_edges,
                                self.pop_id)

        if cpu_file is not None:
            processors_dict = _parse_cpu_info(path + cpu_file)
            _enrich_graph_cpuinfo(graph, processors_dict)

        if dpdk_file is not None:
            dpdk_dict = _parse_dpdk_info(path + dpdk_file)
            _enrich_graph_dpdkinfo(graph, dpdk_dict)

        if sriov_file is not None:
            sriov_dict = _parse_sriov_info(path + sriov_file)
            _enrich_graph_sriovinfo(graph, sriov_dict)

        if timestamp is not None:
            now = timestamp
        else:
            now = time.time()

        neo_id_nodes = {}

        nodes_to_add = []
        nodes_stored = []

        query_string = 'Match n Where n.hostname = {hostname} ' \
                       'And n.resource_type = {resource_type} Return n.physical_name'

        res = self.graph_db.cypher.execute(query_string,
                                           hostname=self.hostname,
                                           resource_type='physical')

        for item in res:
            print str(item)
            nodes_stored.append(item['n.physical_name'])

        for nx_node in graph.nodes():
            nodes_to_add.append(str(nx_node))
            neo_id_nodes[nx_node] = neo_resource.add_node(
                self.graph_db, (self.label, self.index, nx_node), now,
                get_node_properties(graph, nx_node))

        nodes_to_remove = [
            item for item in nodes_stored if item not in nodes_to_add
        ]

        for node in nodes_to_remove:
            neo_resource.delete_node(self.graph_db,
                                     (self.label, self.index, node))

        for edge in graph.edges():
            source = edge[0]
            target = edge[1]
            edge_label = ''
            if 'label' in graph.edge[source][target]:
                edge_label = graph.edge[source][target]['label']
            db_src = neo_id_nodes[source]
            db_target = neo_id_nodes[target]
            rel_stored = neo_resource.get_edge(self.graph_db, db_src,
                                               db_target)
            if rel_stored is None:
                neo_resource.add_edge(self.graph_db, db_src, db_target,
                                      timestamp, edge_label)
            else:
                neo_resource.update_edge(self.graph_db,
                                         timestamp,
                                         edge_label,
                                         db_src=db_src,
                                         db_target=db_target)