def retrieve(self, link, extras):
        """
        Retrive the link relying on the convention that a link has ID
        source_uuid->target_uuid
        :param link: link to be retrieved
        :param extras: Any extra parameters that can be specified by the user
        """
        resource_type = link.identifier[1:].split("/")[0]
        source_uuid = link.identifier[1:].split("/")[4].split("->")[0]
        target_uuid = link.identifier[1:].split("/")[4].split("->")[1]
        pop_id = extras["pop_id"]
        result = epa_glue.get_link(extras["pop_url"], pop_id, source_uuid, target_uuid)

        if resource_type == "osdev" and "openflow" in target_uuid:
            link.attributes["occi.epa.label"] = "wired"
        else:
            if "label" in result:
                link.attributes["occi.epa.label"] = result["label"]
    def retrieve(self, link, extras):
        """
        Retrive the link relying on the convention that a link has ID
        source_uuid->target_uuid
        :param link: link to be retrieved
        :param extras: Any extra parameters that can be specified by the user
        """
        resource_type = link.identifier[1:].split('/')[0]
        source_uuid = link.identifier[1:].split('/')[4].split('->')[0]
        target_uuid = link.identifier[1:].split('/')[4].split('->')[1]
        pop_id = extras['pop_id']
        result = epa_glue.get_link(extras['pop_url'], pop_id, source_uuid,
                                   target_uuid)

        if resource_type == 'osdev' and 'openflow' in target_uuid:
            link.attributes['occi.epa.label'] = 'wired'
        else:
            if 'label' in result:
                link.attributes['occi.epa.label'] = result['label']
Example #3
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))