Exemple #1
0
    def get_resource(self, key, extras):
        """
        Get single resource kind and call the
        specific kind backend.

        :param key: UUID of the resource
        :param extras: any extras parameters of the call
        :return Resource: OCCI Resource
        """
        # Initialize PoP ID from extras
        if 'pop_id' in extras:
            pop_id = extras['pop_id']
        else:
            pop_id = None

        pop_url = extras['pop_url']
        result = None
        splitted_url = key[1:].split('/')

        # All resources, except PoPs and PoPs links
        # require a PoP ID
        if splitted_url[0] != 'pop' and not pop_id:
            raise HTTPError(400, "Pop-Id missing")

        # Get Resource
        if len(splitted_url) == 2:
            resource_type = splitted_url[0]
            uuid = splitted_url[1]
            uuids = EPARegistry.get_ids(resource_type, pop_url, pop_id)
            if uuid not in uuids:
                raise HTTPError(404, "Resource not found")

            result = self.get_occi_resource(resource_type, uuid)

            # Identify PoP Link
            if resource_type == 'pop':
                for target_link in epa_glue.get_pop_links_target_uuid(pop_url, uuid):
                    self.get_link(resource_type,
                                  result,
                                  target_link[0],
                                  target_link[1],
                                  target_link[2])

            # Identify switch link
            elif resource_type == 'switch':
                for target_uuid in odl_glue.get_switch_interfaces_by_switch_id(pop_url, pop_id, uuid):
                    link_uuid = uuid + '->' + target_uuid
                    self.get_link(resource_type,
                                  result,
                                  target_uuid,
                                  'switch-interface',
                                  link_uuid)

            # Identify switch interface link
            # A switch interface is connected to the switch
            # and can be connected to an osdev
            elif resource_type == 'switch-interface':
                switch_uuid = odl_glue.get_switch_by_interface(pop_url, pop_id, uuid)
                # Link to the switch
                if switch_uuid:
                    switch_link_uuid = uuid + '->' + switch_uuid
                    self.get_link(resource_type,
                                  result,
                                  switch_uuid,
                                  'switch',
                                  switch_link_uuid)

                osdev_uuid = odl_glue.get_os_dev_by_switch_interface(pop_url, pop_id, uuid)
                # Link to the osdev
                if osdev_uuid:
                    osdev_link_uuid = uuid + '->' + osdev_uuid
                    self.get_link(resource_type,
                                  result,
                                  osdev_uuid,
                                  'osdev',
                                  osdev_link_uuid)
            # Identify osdev link
            # An osdev can be connected to a switch interface
            elif resource_type == 'osdev':
                mac = epa_glue.get_mac_by_osdev_uuid(pop_url, pop_id, uuid)
                if mac:
                    switch_interface = odl_glue.get_switch_interface_by_mac(pop_url, pop_id, mac)
                    if switch_interface:
                        link_uuid = uuid + '->' + switch_interface
                        self.get_link(resource_type,
                                      result,
                                      switch_interface,
                                      'switch-interface',
                                      link_uuid)

            # Indentify link for all other resources
            else:
                for target_link in epa_glue.get_links_target_uuid(extras['pop_url'], pop_id, uuid):
                    link_uuid = uuid + '->' + target_link[0]
                    self.get_link(resource_type,
                                  result,
                                  target_link[0],
                                  target_link[1],
                                  link_uuid)

        # Get Link
        elif len(splitted_url) == 3 and splitted_url[1] == 'link':
            link_uuid = splitted_url[2]

            # Not PoP Link
            # Link UUID = source_uuid + '->' + target_uuid
            if '->' in link_uuid:
                source_type = splitted_url[0]
                source_uuid = splitted_url[2].split('->')[0]
                target_uuid = splitted_url[2].split('->')[1]

                # if source_type is switch
                # target type can be only switch interface
                if source_type == 'switch':
                    link_prop = dict()
                    link_prop['target_type'] = 'switch-interface'

                # if source_type is switch
                # target type can be switch and eventually osdev
                elif source_type == 'switch-interface':
                    link_prop = dict()
                    if target_uuid == odl_glue.get_switch_by_interface(pop_url, pop_id, source_uuid):
                        link_prop['target_type'] = 'switch'
                    elif target_uuid == odl_glue.get_os_dev_by_switch_interface(pop_url, pop_id, source_uuid):
                        link_prop['target_type'] = 'osdev'

                # if source_type is switch
                # and target_uuid is of openflow type
                # target_type is a switch
                elif source_type == 'osdev' and 'openflow' in target_uuid:
                    link_prop = dict()
                    link_prop['target_type'] = 'switch-interface'
                else:
                    link_prop = epa_glue.get_link(extras['pop_url'], pop_id, source_uuid, target_uuid)

                # if link_prop is not set
                # the required link does not exist
                if not link_prop:
                    raise HTTPError(404, "Resource Not Found")
                source_entity = self.get_occi_resource(source_type, source_uuid)
                result = self.get_link(source_type,
                                       source_entity,
                                       target_uuid,
                                       link_prop['target_type'],
                                       link_uuid)

            # PoP Link
            else:
                link_result = epa_glue.get_pop_link_source_target(pop_url, link_uuid)
                if link_result and len(link_result) == 2:
                    source = link_result[0]
                    target = link_result[1]
                    source_entity = self.get_occi_resource(splitted_url[0], source)
                    target_entity = self.get_occi_resource(splitted_url[0], target)
                    result = self.get_occi_link(splitted_url[0] + '_link', link_uuid, source_entity, target_entity)

        # If requested resource does not exist
        # raise Resource not found exception
        if result:
            if isinstance(result, Resource) and result.kind.term != 'pop':
                EPARegistry.add_pop_id_to_resource(result, pop_id)
                EPARegistry.add_pop_id_to_links(result.links, pop_id)
            if isinstance(result, Link) and result.kind.term != 'poplink':
                EPARegistry.add_pop_id_to_resource(result, pop_id)
                EPARegistry.add_pop_id_to_link_source(result, pop_id)
                EPARegistry.add_pop_id_to_link_target(result, pop_id)


            return result
        raise HTTPError(404, 'Resource not found: ' + str(key))
Exemple #2
0
    def get_resource_entities(pop_url, pop_id, openstack_types, query):
        """
        Retrieve a list of entities and their links
        for a given list of types
        :param pop_url: Url of PoP DB
        :param pop_id: PoP ID
        :param openstack_types: list of type
        :param query: optional query parameters
        :return dict: keys resources' uuids, values resources' properties
        """
        results = {}
        for resource_type in openstack_types:
            # For switches call Opendaylight
            if resource_type == 'switch':
                for uuid in odl_glue.get_switches_ids(pop_url, pop_id):
                    entity = EPARegistry.get_occi_resource(resource_type, uuid)
                    results[uuid] = entity

            # For switches' interfaces call Opendaylight
            elif resource_type == 'switch-interface':
                for uuid in odl_glue.get_switch_interfaces(pop_url, pop_id):
                    entity = EPARegistry.get_occi_resource(resource_type, uuid)
                    results[uuid] = entity
            # For all others resources query EPA DB
            else:
                for uuid in epa_glue.get_resource_openstack_ids(pop_url, pop_id, resource_type, query):
                    entity = EPARegistry.get_occi_resource(resource_type, uuid)
                    results[uuid] = entity

        # Retrieve links for the resources that should be returned
        links = {}
        for source_uuid in results:
            if results[source_uuid].kind.term == 'switch':
                for target_uuid in odl_glue.get_switch_interfaces_by_switch_id(pop_url, pop_id, source_uuid):
                    target_type = 'switch-interface'
                    link_uuid = source_uuid + '->' + target_uuid
                    source_entity = results[source_uuid]
                    kind = source_entity.kind.term + '_link'
                    target_entity = EPARegistry.get_occi_resource(target_type, target_uuid)
                    links[link_uuid] = EPARegistry.get_occi_link(kind, link_uuid, source_entity, target_entity)
            elif results[source_uuid].kind.term == 'switch-interface':
                switch_uuid = odl_glue.get_switch_by_interface(pop_url, pop_id, source_uuid)
                if switch_uuid:
                    switch_type = 'switch'
                    source_entity = results[source_uuid]
                    kind = source_entity.kind.term + '_link'
                    switch_entity = EPARegistry.get_occi_resource(switch_type, switch_uuid)
                    switch_link_uuid = uuid + '->' + switch_uuid
                    links[switch_link_uuid] = EPARegistry.get_occi_link(kind, switch_link_uuid,
                                                                        results[source_uuid], switch_entity)

                osdev_uuid = odl_glue.get_os_dev_by_switch_interface(pop_url, pop_id, source_uuid)

                if osdev_uuid:
                    osdev_type = 'osdev'
                    osdev_entity = EPARegistry.get_occi_resource(osdev_type, osdev_uuid)
                    osdev_link_uuid = uuid + '->' + osdev_uuid
                    links[osdev_link_uuid] = EPARegistry.get_occi_link(kind, osdev_link_uuid,
                                                                       results[source_uuid], osdev_entity)
            elif results[source_uuid].kind.term == 'osdev':
                mac = epa_glue.get_mac_by_osdev_uuid(pop_url, pop_id, source_uuid)
                if mac:
                    switch_interface = odl_glue.get_switch_interface_by_mac(pop_url, pop_id, mac)
                    if switch_interface:
                        target_uuid = switch_interface
                        target_type = 'switch-interface'
                        link_uuid = source_uuid + '->' + target_uuid
                        source_entity = results[source_uuid]
                        kind = source_entity.kind.term + '_link'
                        target_entity = EPARegistry.get_occi_resource(target_type, target_uuid)
                        links[link_uuid] = EPARegistry.get_occi_link(kind, link_uuid, source_entity, target_entity)
            else:
                for target_link in epa_glue.get_links_target_uuid(pop_url, pop_id, source_uuid):
                    target_uuid = target_link[0]
                    target_type = target_link[1]
                    link_uuid = source_uuid + '->' + target_uuid
                    source_entity = results[source_uuid]
                    kind = source_entity.kind.term + '_link'
                    target_entity = EPARegistry.get_occi_resource(target_type, target_uuid)
                    links[link_uuid] = EPARegistry.get_occi_link(kind, link_uuid, source_entity, target_entity)

        results.update(links)

        EPARegistry.add_pop_id_to_resources(results, pop_id)

        return results.values()