Ejemplo n.º 1
0
    def create_net_assoc(self, context, bgpvpn_id, network_association):
        LOG.debug("create_net_assoc called for bgpvpn %s with network %s" %
                  (bgpvpn_id, network_association['network_id']))

        bgpvpn = self.get_bgpvpn(context, bgpvpn_id)
        oc_client = self._get_opencontrail_api_client(context)

        network_id = network_association['network_id']
        if network_id not in bgpvpn.get('networks', []):
            assoc_uuid = uuidutils.generate_uuid()
            self._set_bgpvpn_association(oc_client, 'ADD', bgpvpn,
                                         [network_id])

            assoc_dict = utils.make_net_assoc_dict(
                assoc_uuid, network_association['tenant_id'], bgpvpn_id,
                network_association['network_id'])
            oc_client.kv_store('STORE',
                               key=assoc_uuid,
                               value={'bgpvpn_net_assoc': assoc_dict})
            return assoc_dict
        else:
            # the tuple (bgpvpn_id, network_id) is necessarily unique
            return self.get_net_assocs(context,
                                       bgpvpn_id,
                                       filters={'network_id': network_id})[0]
Ejemplo n.º 2
0
    def get_net_assoc(self, context, assoc_id, bgpvpn_id, fields=None):
        LOG.debug(
            "get_net_assoc called for %s for BGPVPN %s, with fields = %s" %
            (assoc_id, bgpvpn_id, fields))

        oc_client = self._get_opencontrail_api_client(context)

        try:
            net_assoc = json.loads(oc_client.kv_store('RETRIEVE',
                                                      key=assoc_id))
        except (oc_exc.OpenContrailAPINotFound, ValueError):
            raise bgpvpn_ext.BGPVPNNetAssocNotFound(id=assoc_id,
                                                    bgpvpn_id=bgpvpn_id)

        if (not isinstance(net_assoc, dict)
                or 'bgpvpn_net_assoc' not in net_assoc):
            raise bgpvpn_ext.BGPVPNNetAssocNotFound(id=assoc_id,
                                                    bgpvpn_id=bgpvpn_id)
        net_assoc = net_assoc['bgpvpn_net_assoc']

        if net_assoc['bgpvpn_id'] != bgpvpn_id:
            raise bgpvpn_ext.BGPVPNNetAssocNotFound(id=assoc_id,
                                                    bgpvpn_id=bgpvpn_id)

        # It the bgpvpn was deleted, the 'get_bgpvpn' will clean all related
        # associations and replaces BGPVPNNotFound by a BGPVPNNetAssocNotFound
        try:
            get_fields = [
                'tenant_id', 'route_targets', 'import_targets',
                'export_targets'
            ]
            bgpvpn = self.get_bgpvpn(context,
                                     net_assoc['bgpvpn_id'],
                                     fields=get_fields)
        except bgpvpn_ext.BGPVPNNotFound:
            raise bgpvpn_ext.BGPVPNNetAssocNotFound(id=assoc_id,
                                                    bgpvpn_id=bgpvpn_id)

        # If the network was delete all bgpvpn related association should be
        # deleted also
        try:
            oc_client.id_to_fqname(net_assoc['network_id'])
        except oc_exc.OpenContrailAPINotFound:
            self._set_bgpvpn_association(oc_client, 'DELETE', bgpvpn,
                                         [net_assoc['network_id']])
            oc_client.kv_store('DELETE', key=assoc_id)
            raise bgpvpn_ext.BGPVPNNetAssocNotFound(id=assoc_id,
                                                    bgpvpn_id=bgpvpn_id)

        net_assoc = utils.make_net_assoc_dict(net_assoc['id'],
                                              net_assoc['tenant_id'],
                                              net_assoc['bgpvpn_id'],
                                              net_assoc['network_id'], fields)
        return net_assoc
Ejemplo n.º 3
0
    def get_net_assoc(self, context, assoc_id, bgpvpn_id, fields=None):
        LOG.debug("get_net_assoc called for %s for BGPVPN %s, with fields = %s"
                  % (assoc_id, bgpvpn_id, fields))

        oc_client = self._get_opencontrail_api_client(context)

        try:
            net_assoc = json.loads(
                oc_client.kv_store('RETRIEVE', key=assoc_id))
        except (oc_exc.OpenContrailAPINotFound, ValueError):
            raise bgpvpn_ext.BGPVPNNetAssocNotFound(id=assoc_id,
                                                    bgpvpn_id=bgpvpn_id)

        if (not isinstance(net_assoc, dict) or
                'bgpvpn_net_assoc' not in net_assoc):
            raise bgpvpn_ext.BGPVPNNetAssocNotFound(id=assoc_id,
                                                    bgpvpn_id=bgpvpn_id)
        net_assoc = net_assoc['bgpvpn_net_assoc']

        if net_assoc['bgpvpn_id'] != bgpvpn_id:
            raise bgpvpn_ext.BGPVPNNetAssocNotFound(id=assoc_id,
                                                    bgpvpn_id=bgpvpn_id)

        # It the bgpvpn was deleted, the 'get_bgpvpn' will clean all related
        # associations and replaces BGPVPNNotFound by a BGPVPNNetAssocNotFound
        try:
            get_fields = ['tenant_id', 'route_targets', 'import_targets',
                          'export_targets']
            bgpvpn = self.get_bgpvpn(context, net_assoc['bgpvpn_id'],
                                     fields=get_fields)
        except bgpvpn.BGPVPNNotFound:
            raise bgpvpn_ext.BGPVPNNetAssocNotFound(id=assoc_id,
                                                    bgpvpn_id=bgpvpn_id)

        # If the network was delete all bgpvpn related association should be
        # deleted also
        try:
            oc_client.id_to_fqname(net_assoc['network_id'])
        except oc_exc.OpenContrailAPINotFound:
            self._set_bgpvpn_association(oc_client, 'DELETE', bgpvpn,
                                         [net_assoc['network_id']])
            oc_client.kv_store('DELETE', key=assoc_id)
            raise bgpvpn_ext.BGPVPNNetAssocNotFound(id=assoc_id,
                                                    bgpvpn_id=bgpvpn_id)

        net_assoc = utils.make_net_assoc_dict(net_assoc['id'],
                                              net_assoc['tenant_id'],
                                              net_assoc['bgpvpn_id'],
                                              net_assoc['network_id'],
                                              fields)
        return net_assoc
Ejemplo n.º 4
0
 def get_net_assoc(self, context, assoc_id, bgpvpn_id, fields=None):
     try:
         bgpvpn_obj = self._vnc_api.bgpvpn_read(
             id=bgpvpn_id, fields=['virtual_network_back_refs'])
     except vnc_exc.NoIdError:
         raise bgpvpn_ext.BGPVPNNotFound(id=bgpvpn_id)
     associated_networks = [
         vn_ref['uuid']
         for vn_ref in bgpvpn_obj.get_virtual_network_back_refs() or []
     ]
     if assoc_id not in associated_networks:
         raise bgpvpn_ext.BGPVPNNetAssocNotFound(id=assoc_id,
                                                 bgpvpn_id=bgpvpn_id)
     return bgpvpn_utils.make_net_assoc_dict(
         assoc_id, bgpvpn_obj.parent_uuid.replace('-', ''), bgpvpn_id,
         assoc_id)
Ejemplo n.º 5
0
 def get_net_assocs(self, context, bgpvpn_id, filters=None, fields=None):
     try:
         bgpvpn_obj = self._vnc_api.bgpvpn_read(
             id=bgpvpn_id, fields=['virtual_network_back_refs'])
     except vnc_exc.NoIdError:
         raise bgpvpn_ext.BGPVPNNotFound(id=bgpvpn_id)
     bgpvpn_net_assocs = []
     for vn_ref in bgpvpn_obj.get_virtual_network_back_refs() or []:
         bgpvpn_net_assoc = bgpvpn_utils.make_net_assoc_dict(
             vn_ref['uuid'],
             bgpvpn_obj.parent_uuid.replace('-', ''),
             bgpvpn_id,
             vn_ref['uuid'],
             fields,
         )
         if bgpvpn_utils.filter_resource(bgpvpn_net_assoc, filters):
             bgpvpn_net_assocs.append(bgpvpn_net_assoc)
     return bgpvpn_net_assocs
Ejemplo n.º 6
0
 def delete_net_assoc(self, context, assoc_id, bgpvpn_id):
     try:
         bgpvpn_obj = self._vnc_api.bgpvpn_read(id=bgpvpn_id)
     except vnc_exc.NoIdError:
         raise bgpvpn_ext.BGPVPNNotFound(id=bgpvpn_id)
     try:
         vn_obj = self._vnc_api.virtual_network_read(id=assoc_id)
     except vnc_exc.NoIdError:
         raise neutron_exc.NetworkNotFound(net_id=assoc_id)
     vn_obj.del_bgpvpn(bgpvpn_obj)
     try:
         self._vnc_api.virtual_network_update(vn_obj)
     except vnc_exc.BadRequest as exc:
         raise neutron_exc.BadRequest(resource='network_association',
                                      msg=str(exc))
     return bgpvpn_utils.make_net_assoc_dict(
         assoc_id,
         bgpvpn_obj.parent_uuid.replace('-', ''),
         bgpvpn_id,
         assoc_id,
     )
Ejemplo n.º 7
0
 def create_net_assoc(self, context, bgpvpn_id, network_association):
     try:
         bgpvpn_obj = self._vnc_api.bgpvpn_read(id=bgpvpn_id)
     except vnc_exc.NoIdError:
         raise bgpvpn_ext.BGPVPNNotFound(id=bgpvpn_id)
     net_id = network_association['network_id']
     try:
         vn_obj = self._vnc_api.virtual_network_read(id=net_id)
     except vnc_exc.NoIdError:
         raise neutron_exc.NetworkNotFound(net_id=net_id)
     vn_obj.add_bgpvpn(bgpvpn_obj)
     try:
         self._vnc_api.virtual_network_update(vn_obj)
     except vnc_exc.BadRequest as exc:
         raise neutron_exc.BadRequest(resource='network_association',
                                      msg=str(exc))
     # Use the network ID as association id
     network_association['id'] = net_id
     network_association['bgpvpn_id'] = bgpvpn_id
     network_association.pop('project_id', None)
     return bgpvpn_utils.make_net_assoc_dict(**network_association)
Ejemplo n.º 8
0
    def get_net_assocs(self, context, bgpvpn_id, filters=None, fields=None):
        LOG.debug("get_net_assocs called for bgpvpn %s, fields = %s, "
                  "filters = %s" % (bgpvpn_id, fields, filters))

        oc_client = self._get_opencontrail_api_client(context)

        get_fields = ['tenant_id', 'route_targets', 'import_targets',
                      'export_targets']
        bgpvpn = self.get_bgpvpn(context, bgpvpn_id, fields=get_fields)

        bgpvpn_net_assocs = []
        for kv_dict in oc_client.kv_store('RETRIEVE'):
            try:
                value = json.loads(kv_dict['value'])
            except ValueError:
                continue
            if (isinstance(value, dict) and
                    'bgpvpn_net_assoc' in value and
                    utils.filter_resource(value['bgpvpn_net_assoc'],
                                          filters) and
                    value['bgpvpn_net_assoc']['bgpvpn_id'] == bgpvpn_id):
                net_assoc = value['bgpvpn_net_assoc']
                # If the network was delete all bgpvpn related association
                # should be deleted also
                try:
                    oc_client.id_to_fqname(net_assoc['network_id'])
                except oc_exc.OpenContrailAPINotFound:
                    self._set_bgpvpn_association(oc_client, 'DELETE', bgpvpn,
                                                 [net_assoc['network_id']])
                    oc_client.kv_store('DELETE', key=net_assoc['id'])
                    continue
                net_assoc = utils.make_net_assoc_dict(net_assoc['id'],
                                                      net_assoc['tenant_id'],
                                                      net_assoc['bgpvpn_id'],
                                                      net_assoc['network_id'],
                                                      fields)
                bgpvpn_net_assocs.append(net_assoc)

        return bgpvpn_net_assocs
Ejemplo n.º 9
0
    def get_net_assocs(self, context, bgpvpn_id, filters=None, fields=None):
        LOG.debug("get_net_assocs called for bgpvpn %s, fields = %s, "
                  "filters = %s" % (bgpvpn_id, fields, filters))

        oc_client = self._get_opencontrail_api_client(context)

        get_fields = [
            'tenant_id', 'route_targets', 'import_targets', 'export_targets'
        ]
        bgpvpn = self.get_bgpvpn(context, bgpvpn_id, fields=get_fields)

        bgpvpn_net_assocs = []
        for kv_dict in oc_client.kv_store('RETRIEVE'):
            try:
                value = json.loads(kv_dict['value'])
            except ValueError:
                continue
            if (isinstance(value, dict)
                    and 'bgpvpn_net_assoc' in value and utils.filter_resource(
                        value['bgpvpn_net_assoc'], filters)
                    and value['bgpvpn_net_assoc']['bgpvpn_id'] == bgpvpn_id):
                net_assoc = value['bgpvpn_net_assoc']
                # If the network was delete all bgpvpn related association
                # should be deleted also
                try:
                    oc_client.id_to_fqname(net_assoc['network_id'])
                except oc_exc.OpenContrailAPINotFound:
                    self._set_bgpvpn_association(oc_client, 'DELETE', bgpvpn,
                                                 [net_assoc['network_id']])
                    oc_client.kv_store('DELETE', key=net_assoc['id'])
                    continue
                net_assoc = utils.make_net_assoc_dict(net_assoc['id'],
                                                      net_assoc['tenant_id'],
                                                      net_assoc['bgpvpn_id'],
                                                      net_assoc['network_id'],
                                                      fields)
                bgpvpn_net_assocs.append(net_assoc)

        return bgpvpn_net_assocs
Ejemplo n.º 10
0
    def create_net_assoc(self, context, bgpvpn_id, network_association):
        LOG.debug("create_net_assoc called for bgpvpn %s with network %s"
                  % (bgpvpn_id, network_association['network_id']))

        bgpvpn = self.get_bgpvpn(context, bgpvpn_id)
        oc_client = self._get_opencontrail_api_client(context)

        network_id = network_association['network_id']
        if network_id not in bgpvpn.get('networks', []):
            assoc_uuid = uuidutils.generate_uuid()
            self._set_bgpvpn_association(oc_client, 'ADD', bgpvpn,
                                         [network_id])

            assoc_dict = utils.make_net_assoc_dict(
                assoc_uuid, network_association['tenant_id'],
                bgpvpn_id, network_association['network_id'])
            oc_client.kv_store('STORE', key=assoc_uuid,
                               value={'bgpvpn_net_assoc': assoc_dict})
            return assoc_dict
        else:
            # the tuple (bgpvpn_id, network_id) is necessarily unique
            return self.get_net_assocs(context, bgpvpn_id,
                                       filters={'network_id': network_id})[0]