def add_port(self, vm_uuid_str, vif_uuid_str, interface_name, mac_address,
                 **kwargs):
        """
        Add a port to the agent. The information is stored in the _ports
        dictionary since the vrouter agent may not be running at the
        moment or the RPC may fail.
        """

        vif_uuid = self._uuid_from_string(vif_uuid_str)
        # ip_address and network_uuid are optional to this API but must
        # be present in the message. For instance, when running in
        # CloudStack/XenServer/XAPI these arguments are not known.
        if 'ip_address' in kwargs:
            ip_address = kwargs['ip_address']
        else:
            ip_address = '0.0.0.0'

        if 'network_uuid' in kwargs:
            network_uuid = self._uuid_string_to_hex(kwargs['network_uuid'])
        else:
            network_uuid = [0] * 16

        # create port with mandatory arguments
        data = ttypes.Port(
            self._uuid_to_hex(vif_uuid),
            self._uuid_string_to_hex(vm_uuid_str),
            interface_name,
            ip_address,
            network_uuid,
            mac_address)

        if 'display_name' in kwargs:
            data.display_name = kwargs['display_name']
        if 'hostname' in kwargs:
            data.hostname = kwargs['hostname']
        if 'vm_project_uuid' in kwargs:
            data.vm_project_uuid = self._uuid_string_to_hex(
                kwargs['vm_project_uuid'])

        data.validate()

        if self._client is None:
            self._client = self._rpc_client_instance()

        if self._client is None:
            self._ports[vif_uuid] = data
            return False

        self._resynchronize()

        self._ports[vif_uuid] = data

        try:
            result = self._client.AddPort([data])
        except:
            self._client = None
            raise
        return result
    def plug(self, instance, vif):
        iface_id = vif['id']
        dev = self.get_vif_devname(vif)
        linux_net.create_tap_dev(dev)

        # port_id(tuuid), instance_id(tuuid), tap_name(string), 
        # ip_address(string), vn_id(tuuid)
        import socket
        from gen_py.instance_service import ttypes
        port = ttypes.Port(self._convert_to_bl(iface_id), 
                           self._convert_to_bl(instance['uuid']), 
                           dev, 
                           vif['network']['subnets'][0]['ips'][0]['address'],
                           self._convert_to_bl(vif['network']['id']),
                           vif['address'],
	                   instance['display_name'],
	                   instance['hostname'],
	                   instance['host'],
	                   self._convert_to_bl(instance['project_id']))

        self._agent_inform(port, iface_id, True)
    def unplug(self, instance, vif):
        """Unplug the VIF from the network by deleting the port from
        the bridge."""
        LOG.debug(_('Unplug'))
        iface_id = vif['id']
        dev = self.get_vif_devname(vif)

        import socket
        from gen_py.instance_service import ttypes
        port = ttypes.Port(self._convert_to_bl(iface_id), 
                           self._convert_to_bl(instance['uuid']), 
                           dev, 
                           vif['network']['subnets'][0]['ips'][0]['address'],
                           self._convert_to_bl(vif['network']['id']),
                           vif['address'],
	                   instance['display_name'],
	                   instance['hostname'],
	                   instance['host'],
	                   self._convert_to_bl(instance['project_id']))

        self._agent_inform(port, iface_id, False)
        linux_net.delete_net_dev(dev)