def _add_interface_by_subnet(self, context, router, subnet_id, owner):
     LOG.debug("_add_interface_by_subnet(): router=%(router)s, "
               "subnet_id=%(subnet_id)s, owner=%(owner)s",
               {'router': router, 'subnet_id': subnet_id, 'owner': owner})
     subnet = self._core_plugin._get_subnet(context, subnet_id)
     if not subnet['gateway_ip']:
         msg = _('Subnet for router interface must have a gateway IP')
         raise n_exc.BadRequest(resource='router', msg=msg)
     self._check_for_dup_router_subnets(context, router,
                                        subnet['network_id'],
                                        [subnet])
     fixed_ip = {'ip_address': subnet['gateway_ip'],
                 'subnet_id': subnet['id']}
     # TODO(jerryz): move this out of transaction.
     setattr(context, 'GUARD_TRANSACTION', False)
     return (self._core_plugin.create_port(context, {
         'port':
         {'tenant_id': subnet['tenant_id'],
          'network_id': subnet['network_id'],
          'fixed_ips': [fixed_ip],
          'mac_address': utils.get_mac(self, context),
          'admin_state_up': True,
          'device_id': router.id,
          'device_owner': owner,
          'name': ''}}), [subnet], True)
 def _add_interface_by_subnet(self, context, router, subnet_id, owner):
     LOG.debug(
         "_add_interface_by_subnet(): router=%(router)s, "
         "subnet_id=%(subnet_id)s, owner=%(owner)s", {
             'router': router,
             'subnet_id': subnet_id,
             'owner': owner
         })
     subnet = self._core_plugin._get_subnet(context, subnet_id)
     if not subnet['gateway_ip']:
         msg = _('Subnet for router interface must have a gateway IP')
         raise n_exc.BadRequest(resource='router', msg=msg)
     self._check_for_dup_router_subnets(context, router,
                                        subnet['network_id'], [subnet])
     fixed_ip = {
         'ip_address': subnet['gateway_ip'],
         'subnet_id': subnet['id']
     }
     # TODO(jerryz): move this out of transaction.
     setattr(context, 'GUARD_TRANSACTION', False)
     return (self._core_plugin.create_port(
         context, {
             'port': {
                 'tenant_id': subnet['tenant_id'],
                 'network_id': subnet['network_id'],
                 'fixed_ips': [fixed_ip],
                 'mac_address': utils.get_mac(self, context),
                 'admin_state_up': True,
                 'device_id': router.id,
                 'device_owner': owner,
                 'name': ''
             }
         }), [subnet], True)
Beispiel #3
0
def port_migration(context, mech_driver, l3_driver):
    """
    :param mech_driver:
    :param context:
    :return:
    # table ports
    port
    {
        'status': 'DOWN',
        'binding: host_id': '',
        'allowed_address_pairs': [],
        'device_owner': 'network: router_interface',
        'binding: profile': {

        },
        # table ipallocations
        'fixed_ips': [{
            'subnet_id': u'f645b09c-a34a-42fb-9c14-b999e43a54c7',
            'ip_address': u'172.20.21.1'
        }],
        'id': 'fb66def6-bd5e-44a0-a3f7-7c0e8e08d9ff',
        'security_groups': [],
        'device_id': u'e4020c65-7003-468b-a34d-31af297397a0',
        'name': '',
        'admin_state_up': True,
        'network_id': u'f8e34426-ccf7-429c-b726-3809d54cabdc',
        'tenant_id': u'11513667f4ee4a14acb0985659456f24',
        'binding: vif_details': {
        },
        'binding: vnic_type': 'normal',
        'binding: vif_type': 'unbound',
        'mac_address': u'00: 0c: 29: d9: 18: 3f'
    }
    """
    port = {
        'device_owner': 'network: router_interface',
        'fixed_ips': [{
            'subnet_id': u'f645b09c-a34a-42fb-9c14-b999e43a54c7',
            'ip_address': u'172.20.21.1'
        }],
        'id': 'fb66def6-bd5e-44a0-a3f7-7c0e8e08d9ff',
        'device_id': u'e4020c65-7003-468b-a34d-31af297397a0',
        'admin_state_up': True,
        'network_id': u'f8e34426-ccf7-429c-b726-3809d54cabdc',
        'tenant_id': u'11513667f4ee4a14acb0985659456f24',
        'mac_address': u'00: 0c: 29: d9: 18: 3f'
    }
    ipallocation = {
        'subnet_id': u'f645b09c-a34a-42fb-9c14-b999e43a54c7',
        'ip_address': u'172.20.21.1'
    }
    MAC = utils.get_mac(mech_driver, context)
    records = fortinet_db.query_records(context, models_v2.Port)
    with Progress(len(records), 'port_migration') as p:
        for record in records:
            reset(port)
            cls2dict(record, port)
            if port['fixed_ips']:
                fixed_ips = []
                for fixed_ip in port['fixed_ips']:
                    cls2dict(fixed_ip, ipallocation)
                    fixed_ips.append(ipallocation)
                port['fixed_ips'] = fixed_ips
            if port['device_owner'] in [ROUTER_INTF, ROUTER_GW] and \
                            MAC not in port['mac_address']:
                port['mac_address'] = MAC
                if not fortinet_db.query_count(context, models_v2.Port,
                                               mac_address=MAC,
                                               network_id=record.network_id):
                    fortinet_db.update_record(context, record,
                                              mac_address=MAC)
            mech_context = Fake_mech_context(_plugin_context=context,
                                             current=port)
            mech_driver.create_port_precommit(mech_context)
            mech_driver.create_port_postcommit(mech_context)
            db_routerport = fortinet_db.query_record(context,
                                                     l3_db.RouterPort,
                                                     port_id=record.id)
            if getattr(db_routerport, 'port_type', None) in [ROUTER_INTF]:
                l3_driver.add_router_interface(context, port)
            p.update()
def port_migration(context, mech_driver, l3_driver):
    """
    :param mech_driver:
    :param context:
    :return:
    # table ports
    port
    {
        'status': 'DOWN',
        'binding: host_id': '',
        'allowed_address_pairs': [],
        'device_owner': 'network: router_interface',
        'binding: profile': {

        },
        # table ipallocations
        'fixed_ips': [{
            'subnet_id': u'f645b09c-a34a-42fb-9c14-b999e43a54c7',
            'ip_address': u'172.20.21.1'
        }],
        'id': 'fb66def6-bd5e-44a0-a3f7-7c0e8e08d9ff',
        'security_groups': [],
        'device_id': u'e4020c65-7003-468b-a34d-31af297397a0',
        'name': '',
        'admin_state_up': True,
        'network_id': u'f8e34426-ccf7-429c-b726-3809d54cabdc',
        'tenant_id': u'11513667f4ee4a14acb0985659456f24',
        'binding: vif_details': {
        },
        'binding: vnic_type': 'normal',
        'binding: vif_type': 'unbound',
        'mac_address': u'00: 0c: 29: d9: 18: 3f'
    }
    """
    port = {
        'device_owner':
        'network: router_interface',
        'fixed_ips': [{
            'subnet_id': u'f645b09c-a34a-42fb-9c14-b999e43a54c7',
            'ip_address': u'172.20.21.1'
        }],
        'id':
        'fb66def6-bd5e-44a0-a3f7-7c0e8e08d9ff',
        'device_id':
        u'e4020c65-7003-468b-a34d-31af297397a0',
        'admin_state_up':
        True,
        'network_id':
        u'f8e34426-ccf7-429c-b726-3809d54cabdc',
        'tenant_id':
        u'11513667f4ee4a14acb0985659456f24',
        'mac_address':
        u'00: 0c: 29: d9: 18: 3f'
    }
    ipallocation = {
        'subnet_id': u'f645b09c-a34a-42fb-9c14-b999e43a54c7',
        'ip_address': u'172.20.21.1'
    }
    MAC = utils.get_mac(mech_driver, context)
    records = fortinet_db.query_records(context, models_v2.Port)
    with Progress(len(records), 'port_migration') as p:
        for record in records:
            reset(port)
            cls2dict(record, port)
            if port['fixed_ips']:
                fixed_ips = []
                for fixed_ip in port['fixed_ips']:
                    cls2dict(fixed_ip, ipallocation)
                    fixed_ips.append(ipallocation)
                port['fixed_ips'] = fixed_ips
            if port['device_owner'] in [ROUTER_INTF, ROUTER_GW] and \
               MAC not in port['mac_address']:
                port['mac_address'] = MAC
                if not fortinet_db.query_count(context,
                                               models_v2.Port,
                                               mac_address=MAC,
                                               network_id=record.network_id):
                    fortinet_db.update_record(context, record, mac_address=MAC)
            mech_context = Fake_mech_context(_plugin_context=context,
                                             current=port)
            mech_driver.create_port_precommit(mech_context)
            mech_driver.create_port_postcommit(mech_context)
            db_routerport = fortinet_db.query_record(context,
                                                     l3_db.RouterPort,
                                                     port_id=record.id)
            if getattr(db_routerport, 'port_type', None) in [ROUTER_INTF]:
                l3_driver.add_router_interface(context, port)
            p.update()