def _link_default_netpartition(self, netpart_name, l2template, l3template,
                                   l3isolated, l3shared):
        params = {
            'name': netpart_name,
            'l3template': l3template,
            'l2template': l2template
        }
        (np_id, l3dom_tid,
         l2dom_tid) = self.vsdclient.link_default_netpartition(params)
        # verify that the provided zones have been created already
        shared_match, isolated_match = self.vsdclient.validate_zone_create(
            l3dom_tid, l3isolated, l3shared)
        if not shared_match or not isolated_match:
            msg = ('Default zone names must be provided for '
                   'default net-partiton')
            raise n_exc.BadRequest(resource='net_partition', msg=msg)

        # basic verifications passed. add default netpartition to the DB
        session = lib_db_api.get_writer_session()
        netpartition = nuagedb.get_net_partition_by_name(session, netpart_name)

        with session.begin():
            if netpartition:
                nuagedb.delete_net_partition(session, netpartition)
            nuagedb.add_net_partition(session, np_id, l3dom_tid, l2dom_tid,
                                      netpart_name, l3isolated, l3shared)
        self._default_np_id = np_id
    def _validate_create_net_partition(self, context, net_part_name):
        """Sync net partition configuration between plugin and VSD"""
        # About parallelism: This method could be executed in parallel
        # by different neutron instances. The decorator
        # retry_if_session_inactive makes sure that that the method will be
        # retried when database transaction errors occur like deadlock and
        # duplicate inserts.
        self._validate_net_partition_name(net_part_name)
        session = context.session
        with session.begin(subtransactions=True):
            netpart_db = nuagedb.get_net_partition_by_name(
                session, net_part_name)
            nuage_netpart = self.vsdclient.get_netpartition_data(net_part_name)
            if nuage_netpart:
                if netpart_db:
                    # Net-partition exists in neutron and vsd
                    def_netpart = (
                        cfg.CONF.RESTPROXY.default_net_partition_name)
                    share_netpart = constants.SHARED_INFRASTRUCTURE
                    if (def_netpart == net_part_name
                            or share_netpart == net_part_name):
                        if nuage_netpart['np_id'] != netpart_db['id']:
                            msg = ("Net-partition %s exists in "
                                   "Neutron and VSD, but the id is different" %
                                   net_part_name)
                            raise n_exc.BadRequest(resource='net_partition',
                                                   msg=msg)
                        self._update_net_partition(session, net_part_name,
                                                   netpart_db, nuage_netpart)
                        LOG.info(
                            "Net-partition %s already exists,"
                            " so will just use it", net_part_name)
                        return self._make_net_partition_dict(netpart_db)
                    else:
                        if nuage_netpart['np_id'] != netpart_db['id']:
                            msg = (('Net-partition %s already exists in '
                                    'Neutron and VSD, but the id is '
                                    'different') % net_part_name)
                        else:
                            msg = (('Net-partition %s already exists in '
                                    'Neutron and VSD with same id') %
                                   net_part_name)

                        raise n_exc.BadRequest(resource='net_partition',
                                               msg=msg)

                # Net-partition exists in vsd and not in neutron
                netpart_db = NuageApi._add_net_partition(
                    session, nuage_netpart, net_part_name)
                return self._make_net_partition_dict(netpart_db)
            else:
                if netpart_db:
                    # Net-partition exists in neutron and not VSD
                    LOG.info(
                        "Existing net-partition %s will be deleted and "
                        "re-created in db", net_part_name)
                    nuagedb.delete_net_partition(session, netpart_db)

                # Net-partition does not exist in neutron and VSD
                return self._create_net_partition(session, net_part_name)
 def delete_net_partition(self, context, id):
     net_partition = nuagedb.get_net_partition_by_id(context.session, id)
     if not net_partition:
         raise nuage_exc.NuageNotFound(resource='net_partition',
                                       resource_id=id)
     self._validate_delete_net_partition(context, id, net_partition['name'])
     self.vsdclient.delete_net_partition(net_partition['id'])
     with context.session.begin(subtransactions=True):
         nuagedb.delete_net_partition(context.session, net_partition)
    def _validate_create_net_partition(self, net_part_name, session):
        nuage_netpart = self.vsdclient.get_netpartition_data(net_part_name)
        netpart_db = nuagedb.get_net_partition_by_name(session, net_part_name)

        if nuage_netpart:
            with session.begin(subtransactions=True):
                if netpart_db:
                    # Net-partition exists in neutron and vsd
                    def_netpart = (
                        cfg.CONF.RESTPROXY.default_net_partition_name)
                    if def_netpart == net_part_name:
                        if nuage_netpart['np_id'] != netpart_db['id']:
                            msg = ("Default net-partition %s exists in "
                                   "Neutron and VSD, but the id is different" %
                                   net_part_name)
                            raise n_exc.BadRequest(resource='net_partition',
                                                   msg=msg)
                        self._update_net_partition(session, netpart_db,
                                                   nuage_netpart)
                        LOG.info("Default net-partition %s already exists,"
                                 " so will just use it" % net_part_name)
                        return self._make_net_partition_dict(netpart_db)
                    else:
                        if nuage_netpart['np_id'] != netpart_db['id']:
                            msg = (('Net-partition %s already exists in '
                                    'Neutron and VSD, but the id is '
                                    'different') % net_part_name)
                        else:
                            msg = (('Net-partition %s already exists in '
                                    'Neutron and VSD with same id') %
                                   net_part_name)

                        raise n_exc.BadRequest(resource='net_partition',
                                               msg=msg)

                # Net-partition exists in vsd and not in neutron
                netpart_db = NuageApi._add_net_partition(
                    session, nuage_netpart, net_part_name)
                return self._make_net_partition_dict(netpart_db)
        else:

            if netpart_db:
                # Net-partition exists in neutron and not VSD
                LOG.info(
                    "Existing net-partition %s will be deleted and "
                    "re-created in db", net_part_name)
                nuagedb.delete_net_partition(session, netpart_db)

            # Net-partition does not exist in neutron and VSD
            return self._create_net_partition(session, net_part_name)
 def _validate_net_partition(self, subnet, db_context):
     netpartition_db = nuagedb.get_net_partition_by_name(
         db_context.session, subnet['net_partition'])
     netpartition = self.nuageclient.get_netpartition_by_name(
         subnet['net_partition'])
     require(netpartition, "netpartition", subnet['net_partition'])
     if netpartition_db:
         if netpartition_db['id'] != netpartition['id']:
             net_partdb = nuagedb.get_net_partition_with_lock(
                 db_context.session, netpartition_db['id'])
             nuagedb.delete_net_partition(db_context.session, net_partdb)
             self._add_net_partition(db_context.session, netpartition)
     else:
         self._add_net_partition(db_context.session, netpartition)
     return netpartition['id']
Esempio n. 6
0
    def _validate_net_partition(self, np_id_or_name, context):
        # check db first
        netpartition_db = self._get_netpartition_from_db(
            context.session, np_id_or_name)

        # check vsd by the net-partition in db if found, else try np_id_or_name
        # - note that np_id_or_name only makes sense when a name is given -
        netpartition = self.vsdclient.get_netpartition_by_name(
            netpartition_db['name'] if netpartition_db else np_id_or_name)
        require(netpartition, "netpartition", np_id_or_name)
        if netpartition_db:
            if netpartition_db['id'] == netpartition['id']:
                return netpartition_db
            else:
                # fix neutron with enterprise id (it seems changed?)
                # ... not sure how this would ever happen (legacy code) ...
                net_part_db = nuagedb.get_net_partition_with_lock(
                    context.session, netpartition_db['id'])
                nuagedb.delete_net_partition(context.session, net_part_db)
                return self._add_net_partition(context.session, netpartition)
        else:
            # enterprise exists on VSD but not yet in neutron; add it
            return self._add_net_partition(context.session, netpartition)