def update_pop(pop_url, uuid, timestamp, properties=None):
    """
    Update a node in PoP DB representing a Point Of Presence
    :param pop_url: Url of PoP DB
    :param uuid: Uuid of the node to be updated
    :param timestamp: timestamp of the update
    :param properties: optionally dict containing key values representing new
    PoP Properties
    """
    if not properties:
        properties = dict()
    properties['type'] = 'pop'
    index = ('pop', 'uuid', uuid)
    graph_db = neo4j.Graph(pop_url)
    neo_resource.update_node(graph_db, index, timestamp, properties=properties)
def update_pop(pop_url, uuid, timestamp, properties=None):
    """
    Update a node in PoP DB representing a Point Of Presence
    :param pop_url: Url of PoP DB
    :param uuid: Uuid of the node to be updated
    :param timestamp: timestamp of the update
    :param properties: optionally dict containing key values representing new
    PoP Properties
    """
    if not properties:
        properties = dict()
    properties['type'] = 'pop'
    index = ('pop', 'uuid', uuid)
    graph_db = neo4j.Graph(pop_url)
    neo_resource.update_node(graph_db, index, timestamp, properties=properties)
 def update_resource(self, graph_db, timestamp, properties=None):
     """
     Update the resource in the DB
     :param graph_db: Graph DB instance
     :param timestamp: timestamp in epoch
     :param properties: optional properties of the node
     :return node: Resource node
     """
     with mutex:
         node = neo_resource.update_node(graph_db, self.index, timestamp, properties=properties)
         return node
 def update_resource(self, graph_db, timestamp, properties=None):
     """
     Update the resource in the DB
     :param graph_db: Graph DB instance
     :param timestamp: timestamp in epoch
     :param properties: optional properties of the node
     :return node: Resource node
     """
     with mutex:
         node = neo_resource.update_node(graph_db,
                                         self.index,
                                         timestamp,
                                         properties=properties)
         return node
    def store(self, graph_db, properties, pop, timestamp,
              in_edges=None, out_edges=None, host_nodes=None,
              controller_services=None, hypervisors=None, update=False):
        """
        Store the resource to the Graph DB
        :param graph_db: Graph DB instance
        :param properties: properties of the node
        :param pop: PoP ID
        :param timestamp: timestamp in epoch
        :param in_edges: inward relations
        :param out_edges: outward relations
        :param host_nodes: Machine node where the resource is running on
        :param controller_services: Controller services that manage the resource
        :param hypervisors: hypervisor where the resource is running on, if any
        :param update: if true, it updates an existing node
        """
        with mutex:
            properties['pop'] = pop

            if update:
                openstack_node = neo_resource.update_node(graph_db, self.index, timestamp, properties=properties)
            else:
                openstack_node = neo_resource.add_node(graph_db, self.index, timestamp, properties=properties)

            if openstack_node:
                if in_edges:
                    for in_edge in in_edges:
                        if in_edges[in_edge]['mandatory']:
                            in_node = OpenstackResource(in_edge).get_or_add_resource(graph_db, timestamp)
                        else:
                            in_node = OpenstackResource(in_edge).get_resource(graph_db)
                        if in_node:
                            neo_resource.add_edge(graph_db, in_node, openstack_node, timestamp,
                                                  in_edges[in_edge]['label'])

                if out_edges:
                    for out_edge in out_edges:
                        if out_edges[out_edge]['mandatory']:
                            out_node = OpenstackResource(out_edge).get_or_add_resource(graph_db, timestamp)
                        else:
                            out_node = OpenstackResource(out_edge).get_resource(graph_db)
                        if out_node:
                            neo_resource.add_edge(graph_db, openstack_node, out_node, timestamp,
                                                  out_edges[out_edge]['label'])

                if host_nodes:
                    for host in host_nodes:
                        host_node = HostNode(host).get_resource(graph_db, timestamp)
                        if host_node:
                            neo_resource.add_edge(graph_db, openstack_node, host_node, timestamp, 'runs_on')

                if hypervisors:
                    for host in hypervisors:
                        hypervisor = Hypervisor(host).get_hypervisor(graph_db)
                        if hypervisor:
                            neo_resource.add_edge(graph_db, openstack_node, hypervisor, timestamp, 'deployed_on')

                if controller_services:
                    for service in controller_services:
                        service_node = ControllerService().get_resource_by_type(graph_db, service)
                        if service_node:
                            neo_resource.add_edge(graph_db, service_node, openstack_node,
                                                  timestamp, 'manages')

                if openstack_node.properties['type'] == 'port':
                    if 'profile' in openstack_node.properties['attributes']:
                        port_attributes = json.loads(openstack_node.properties['attributes'])
                        if 'profile' in port_attributes and 'pci_slot' in port_attributes['profile']:
                            pci_dev = neo_resource.get_nodes_by_attribute(graph_db,
                                                                          openstack_node.properties['hostname'],
                                                                          port_attributes['profile']['pci_slot'])

                        if pci_dev and len(pci_dev) == 1:
                            neo_resource.add_edge(graph_db, openstack_node, pci_dev[0],
                                                  timestamp, 'runs_on')
                return openstack_node
    def store(self,
              graph_db,
              properties,
              pop,
              timestamp,
              in_edges=None,
              out_edges=None,
              host_nodes=None,
              controller_services=None,
              hypervisors=None,
              update=False):
        """
        Store the resource to the Graph DB
        :param graph_db: Graph DB instance
        :param properties: properties of the node
        :param pop: PoP ID
        :param timestamp: timestamp in epoch
        :param in_edges: inward relations
        :param out_edges: outward relations
        :param host_nodes: Machine node where the resource is running on
        :param controller_services: Controller services that manage the resource
        :param hypervisors: hypervisor where the resource is running on, if any
        :param update: if true, it updates an existing node
        """
        with mutex:
            properties['pop'] = pop

            if update:
                openstack_node = neo_resource.update_node(
                    graph_db, self.index, timestamp, properties=properties)
            else:
                openstack_node = neo_resource.add_node(graph_db,
                                                       self.index,
                                                       timestamp,
                                                       properties=properties)

            if openstack_node:
                if in_edges:
                    for in_edge in in_edges:
                        if in_edges[in_edge]['mandatory']:
                            in_node = OpenstackResource(
                                in_edge).get_or_add_resource(
                                    graph_db, timestamp)
                        else:
                            in_node = OpenstackResource(in_edge).get_resource(
                                graph_db)
                        if in_node:
                            neo_resource.add_edge(graph_db, in_node,
                                                  openstack_node, timestamp,
                                                  in_edges[in_edge]['label'])

                if out_edges:
                    for out_edge in out_edges:
                        if out_edges[out_edge]['mandatory']:
                            out_node = OpenstackResource(
                                out_edge).get_or_add_resource(
                                    graph_db, timestamp)
                        else:
                            out_node = OpenstackResource(
                                out_edge).get_resource(graph_db)
                        if out_node:
                            neo_resource.add_edge(graph_db, openstack_node,
                                                  out_node, timestamp,
                                                  out_edges[out_edge]['label'])

                if host_nodes:
                    for host in host_nodes:
                        host_node = HostNode(host).get_resource(
                            graph_db, timestamp)
                        if host_node:
                            neo_resource.add_edge(graph_db, openstack_node,
                                                  host_node, timestamp,
                                                  'runs_on')

                if hypervisors:
                    for host in hypervisors:
                        hypervisor = Hypervisor(host).get_hypervisor(graph_db)
                        if hypervisor:
                            neo_resource.add_edge(graph_db, openstack_node,
                                                  hypervisor, timestamp,
                                                  'deployed_on')

                if controller_services:
                    for service in controller_services:
                        service_node = ControllerService(
                        ).get_resource_by_type(graph_db, service)
                        if service_node:
                            neo_resource.add_edge(graph_db, service_node,
                                                  openstack_node, timestamp,
                                                  'manages')

                if openstack_node.properties['type'] == 'port':
                    if 'profile' in openstack_node.properties['attributes']:
                        port_attributes = json.loads(
                            openstack_node.properties['attributes'])
                        if 'profile' in port_attributes and 'pci_slot' in port_attributes[
                                'profile']:
                            pci_dev = neo_resource.get_nodes_by_attribute(
                                graph_db,
                                openstack_node.properties['hostname'],
                                port_attributes['profile']['pci_slot'])

                        if pci_dev and len(pci_dev) == 1:
                            neo_resource.add_edge(graph_db, openstack_node,
                                                  pci_dev[0], timestamp,
                                                  'runs_on')
                return openstack_node