Example #1
0
    def get_gateway_vport(self, context, tenant_id, netpart_id,
                          nuage_vport_id):
        nuage_vport = gw_helper.get_nuage_vport(self.restproxy,
                                                nuage_vport_id)
        if not nuage_vport:
            # Just return empty list. Plugin will throw 404
            return []

        nuage_subnet_id = nuage_vport['parentID']
        if nuage_vport['VLANID']:
            nuage_gw_vport = self._get_gateway_vport(context, tenant_id,
                                                     netpart_id,
                                                     nuage_vport['VLANID'])
            nuage_gw_vport['vlanid'] = nuage_vport['VLANID']
            return nuage_gw_vport

        ret = dict()
        ret['nuage_subnet_id'] = nuage_subnet_id

        nuage_vport_type = nuage_vport['type']
        if nuage_vport_type == constants.BRIDGE_VPORT_TYPE:
            # Get the bridge interface on the vport
            nuage_br_intf = gw_helper.get_interface_by_vport(
                self.restproxy,
                nuage_vport_id,
                nuage_vport_type)
            if nuage_br_intf:
                ret['interface'] = nuage_br_intf['ID']

        elif nuage_vport_type == constants.HOST_VPORT_TYPE:
            # Get the host interface on the vport
            nuage_host_intf = gw_helper.get_interface_by_vport(
                self.restproxy,
                nuage_vport_id,
                nuage_vport_type)
            if nuage_host_intf:
                ret['interface'] = nuage_host_intf['ID']
                ret['port_id'] = strip_cms_id(
                    nuage_host_intf['externalID'])

        ret['vport_type'] = nuage_vport_type
        ret['vport_name'] = nuage_vport['name']
        ret['vlanid'] = None
        return ret
    def get_gateway_vport(self, context, tenant_id, netpart_id,
                          nuage_vport_id):
        nuage_vport = gw_helper.get_nuage_vport(self.restproxy, nuage_vport_id)
        if not nuage_vport:
            # Just return empty list. Plugin will throw 404
            return []

        subnet_mapping = nuagedb.get_subnet_l2dom_by_nuage_id(
            context.session, nuage_vport['parentID'])

        if nuage_vport['VLANID']:
            nuage_gw_vport = self._get_gateway_vport(context, tenant_id,
                                                     netpart_id,
                                                     nuage_vport['VLANID'])
            nuage_gw_vport['vlanid'] = nuage_vport['VLANID']
            return nuage_gw_vport

        ret = dict()
        ret['subnet_id'] = strip_cms_id(subnet_mapping["subnet_id"])
        # gridinv - for VSD managed subnets external ID is empty,
        # se we have to compute subnet_id in plugin from nuage_subnet_id
        ret['nuage_subnet_id'] = subnet_mapping["nuage_subnet_id"]

        nuage_vport_type = nuage_vport['type']
        if nuage_vport_type == constants.BRIDGE_VPORT_TYPE:
            # Get the bridge interface on the vport
            nuage_br_intf = gw_helper.get_interface_by_vport(
                self.restproxy, nuage_vport_id, nuage_vport_type)

            if nuage_br_intf:
                ret['interface'] = nuage_br_intf['ID']
        elif nuage_vport_type == constants.HOST_VPORT_TYPE:
            # Get the host interface on the vport
            nuage_host_intf = gw_helper.get_interface_by_vport(
                self.restproxy, nuage_vport_id, nuage_vport_type)
            if nuage_host_intf:
                ret['interface'] = nuage_host_intf['ID']
                ret['port_id'] = strip_cms_id(nuage_host_intf['externalID'])

        ret['vport_type'] = nuage_vport_type
        ret['vport_name'] = nuage_vport['name']
        ret['vlanid'] = None
        return ret
    def get_gateway_vports(self, context, tenant_id, netpart_id, filters):
        subnet_id = filters['subnet'][0]
        nuage_subnet_id = filters['nuage_subnet_id'][0]
        # Get the nuage l2domain/subnet corresponding to neutron subnet
        # nuage_subnet_id passed in filters by plugin

        subnet_mapping = nuagedb.get_subnet_l2dom_by_nuage_id(
            context.session, nuage_subnet_id)

        if not subnet_mapping:
            msg = _("Nuage subnet for neutron subnet %(subn)s not found " %
                    {'subn': subnet_id})  # noqa H702
            raise restproxy.RESTProxyError(msg)

        if subnet_mapping['nuage_l2dom_tmplt_id']:
            subnet_type = constants.L2DOMAIN
        else:
            subnet_type = constants.SUBNET

        if 'id' in filters:
            # This is to get vport by id
            vport = gw_helper.get_nuage_vport(self.restproxy, filters['id'][0])
            # Return an empty list for neutronclient get_res_by_id_or_name()
            if not vport:
                return []

            if vport['parentID'] != subnet_mapping['nuage_subnet_id']:
                return []

            vport_list = [vport]
        elif 'name' in filters:
            # This is to get vport by name
            vport = gw_helper.get_nuage_vport_by_name(
                self.restproxy, subnet_mapping['nuage_subnet_id'],
                filters['name'][0], subnet_type)
            # Return an empty list for neutronclient get_res_by_id_or_name()
            if not vport:
                return []

            if vport['parentID'] != subnet_mapping['nuage_subnet_id']:
                return []

            vport_list = [vport]
        else:
            if subnet_type == constants.SUBNET:
                # Get all host/bridge vports
                vport_list = gw_helper.get_vports_for_subnet(
                    self.restproxy, subnet_mapping['nuage_subnet_id'])
            else:
                # Get all host/bridge vports
                vport_list = gw_helper.get_vports_for_l2domain(
                    self.restproxy, subnet_mapping['nuage_subnet_id'])

        resp_list = []
        for vport in vport_list:
            vport_type = vport['type']
            resp = dict()
            if vport_type in [
                    constants.HOST_VPORT_TYPE, constants.BRIDGE_VPORT_TYPE
            ]:
                resp['vport_id'] = vport['ID']
                resp['vport_type'] = vport_type
                resp['vport_name'] = vport['name']
                # Get the host/bridge interface
                nuage_interface = gw_helper.get_interface_by_vport(
                    self.restproxy, vport['ID'], vport_type)

                resp['port_id'] = None
                if nuage_interface:
                    resp['interface'] = nuage_interface['ID']
                    resp['port_id'] = strip_cms_id(
                        nuage_interface['externalID'])

                if vport_type == constants.HOST_VPORT_TYPE:
                    resp['subnet_id'] = subnet_id
                else:
                    resp['subnet_id'] = subnet_id

                if not vport['VLANID']:
                    # Skip this vport as it does not have any vlan.
                    # This should never happen as if vport is created a vlan
                    # is always associated with it
                    continue

                # Get the gw interface
                nuage_vlan = gw_helper.get_gateway_port_vlan(
                    self.restproxy, vport['VLANID'])

                resp['nuage_vlan_id'] = nuage_vlan['ID']
                resp['gateway'] = nuage_vlan['gatewayID']
                resp['gatewayport'] = nuage_vlan['parentID']
                resp['value'] = nuage_vlan['value']

                resp_list.append(resp)

        if tenant_id:
            updated_vport_list = []
            for vport in resp_list:
                ent_perm = gw_helper.get_ent_permission_on_vlan(
                    self.restproxy, vport['nuage_vlan_id'])
                if ent_perm:
                    vlan_perm = self._check_tenant_perm(
                        vport['nuage_vlan_id'], tenant_id,
                        ent_perm['permittedEntityID'])
                    if vlan_perm:
                        updated_vport_list.append(vport)
            return updated_vport_list

        return resp_list
    def _get_gateway_vport(self, context, tenant_id, netpart_id,
                           nuage_vlan_id):
        # subnet is required to keep the o/p format consistent with
        # create_gateway_vport o/p
        ret = {
            'subnet_id': None,
            'interface': None,
            'vport_id': None,
            'vport_type': None,
            'vport_name': None,
            'port_id': None
        }

        # Get the gw interface
        try:
            nuage_vlan = gw_helper.get_gateway_port_vlan(
                self.restproxy, nuage_vlan_id)
        except Exception as e:
            if e.code == constants.RES_NOT_FOUND:
                return
            raise

        # Check for tenant permission
        # tenant_id is None in case of admin. We don't check permissions
        # for admin.netpart_id will be None when called from
        # delete_gateway_vport. We don't have to check permissions for
        # delete as client always calls get() before delete and we check
        # permissions during get()
        if tenant_id and netpart_id:
            ent_perm = gw_helper.get_ent_permission_on_vlan(
                self.restproxy, nuage_vlan['ID'])
            has_perm = self._check_tenant_perm(nuage_vlan_id, tenant_id,
                                               ent_perm['permittedEntityID'])
            if not has_perm:
                msg = _("Tenant %(ten)s does not have permission for vlan %("
                        "vlan)s" % {
                            'ten': tenant_id,  # noqa H702
                            'vlan': nuage_vlan_id
                        })
                LOG.warn(msg)
                raise restproxy.RESTProxyError(msg)

        ret['gateway'] = nuage_vlan['gatewayID']
        ret['gatewayport'] = nuage_vlan['parentID']
        ret['value'] = nuage_vlan['value']

        # Check if it is associated with a vport
        nuage_vport_id = nuage_vlan['vportID']
        if not nuage_vport_id:
            msg = _("Nuage gateway interface %s is not associated with any "
                    "vport" % nuage_vlan_id)  # noqa H702
            raise restproxy.RESTProxyError(msg)

        # Get the vport
        nuage_vport = gw_helper.get_nuage_vport(self.restproxy, nuage_vport_id)
        if nuage_vport:
            subnet_mapping = nuagedb.get_subnet_l2dom_by_nuage_id(
                context.session, nuage_vport['parentID'])
            ret['subnet_id'] = subnet_mapping["subnet_id"]
            ret['nuage_subnet_id'] = nuage_vport['parentID']
            nuage_vport_type = nuage_vport['type']
            if nuage_vport_type == constants.BRIDGE_VPORT_TYPE:
                # Get the bridge interface on the vport
                nuage_br_intf = gw_helper.get_interface_by_vport(
                    self.restproxy, nuage_vport_id, nuage_vport_type)

                if nuage_br_intf:
                    ret['interface'] = nuage_br_intf['ID']
            elif nuage_vport_type == constants.HOST_VPORT_TYPE:
                # Get the host interface on the vport
                nuage_host_intf = gw_helper.get_interface_by_vport(
                    self.restproxy, nuage_vport_id, nuage_vport_type)
                if nuage_host_intf:
                    ret['interface'] = nuage_host_intf['ID']
                    ret['port_id'] = strip_cms_id(
                        nuage_host_intf['externalID'])
            else:
                msg = _("Nuage vport associated with gateway interface %s is"
                        " not connected to a bridge/host interface" %
                        nuage_vlan_id)  # noqa H702
                raise restproxy.RESTProxyError(msg)

            ret['vport_id'] = nuage_vport_id
            ret['vport_type'] = nuage_vport_type
            ret['vport_name'] = nuage_vport['name']

        return ret