示例#1
0
    def _process_portbindings_create_and_update(self, context, port_data,
                                                port):
        binding_profile = port.get(portbindings.PROFILE)
        binding_profile_set = validators.is_attr_set(binding_profile)
        if not binding_profile_set and binding_profile is not None:
            del port[portbindings.PROFILE]

        binding_vnic = port.get(portbindings.VNIC_TYPE)
        binding_vnic_set = validators.is_attr_set(binding_vnic)
        if not binding_vnic_set and binding_vnic is not None:
            del port[portbindings.VNIC_TYPE]
        # REVISIT(irenab) Add support for vnic_type for plugins that
        # can handle more than one type.
        # Currently implemented for ML2 plugin that does not use
        # PortBindingMixin.

        host = port_data.get(portbindings.HOST_ID)
        host_set = validators.is_attr_set(host)
        with db_api.context_manager.writer.using(context):
            bind_port = context.session.query(
                pmodels.PortBindingPort).filter_by(port_id=port['id']).first()
            if host_set:
                if not bind_port:
                    context.session.add(
                        pmodels.PortBindingPort(port_id=port['id'], host=host))
                else:
                    bind_port.host = host
            else:
                host = bind_port.host if bind_port else None
        self._extend_port_dict_binding_host(port, host)
def put_port_hostid(context, port_id, host):
    # REVISIT(kevinbenton): this is a workaround to avoid portbindings_db
    # relational table generation until one of the functions is called.
    from neutron.db.models import portbinding
    if not validators.is_attr_set(host):
        LOG.warning(_LW("No host_id in port request to track port location."))
        return
    if port_id == '':
        LOG.warning(_LW("Received an empty port ID for host_id '%s'"), host)
        return
    if host == '':
        LOG.debug("Received an empty host_id for port '%s'", port_id)
        return
    LOG.debug("Logging port %(port)s on host_id %(host)s",
              {'port': port_id, 'host': host})
    with context.session.begin(subtransactions=True):
        location = portbinding.PortBindingPort(port_id=port_id, host=host)
        context.session.merge(location)