def delete_port(self, host_id, ports, segments):
        with self.sync_lock:
            network_id = ports['network_id']
            device_id = ports['device_id']
            port_id = ports['id']
            tenant_id = ports['tenant_id']
            if not db.is_vm_created(device_id, host_id,
                                    port_id, network_id, tenant_id):
                LOG.info(_("No such vm in database, ignore it"))
                return

            # Delete configuration in device
            # only if it is the last vm of host in this network
            vm_count = db.get_vm_count(network_id, host_id)
            if vm_count == 1:
                LOG.info(_("Delete physical port configuration: "
                           "All VMs of host %s in network %s is deleted. "),
                         host_id, network_id)
                segment_type = segments[0]['network_type']
                segment_id = segments[0]['segmentation_id']
                if segment_type == 'vlan':
                    vlan_id = int(segment_id)
                    self.delete_vlan_config(network_id, host_id, vlan_id)
                else:
                    LOG.info(_("Not supported network type %s."),
                             str(segment_type))
            else:
                LOG.info(_("The network %s still have %d vms, "
                           "ignore this operation."),
                         network_id, vm_count)
            db.delete_vm(device_id, host_id, port_id, network_id, tenant_id)
    def create_port_postcommit(self, context):
        """Create network and port on physical device."""
        LOG.info(_("Create port begin."))

        # Here we only process virtual machine and DHCP server's port.
        port = context.current
        device_owner = port['device_owner']
        if not device_owner.startswith('compute') and \
                device_owner != n_const.DEVICE_OWNER_DHCP:
            LOG.info(_("Ignore port owner %s when creating port."),
                     device_owner)
            return

        device_id = port['device_id']
        host_id = context.host
        port_id = port['id']
        tenant_id = port['tenant_id']
        network_id = port['network_id']

        with self.sync_lock:
            if db.is_vm_created(device_id, host_id,
                                port_id, network_id, tenant_id):
                LOG.info(_("The port %s of virtual machine %s has "
                           "already inserted into the network %s."),
                         str(port_id), str(device_id), str(network_id))
                return

            LOG.info(_("Insert port %s's information into database."),
                     str(port_id))

            db.create_vm(device_id, host_id, port_id, network_id, tenant_id)
            # Get the count of port that created in the same network and host.
            port_count = db.get_vm_count(network_id, host_id)
            if port_count == 1:
                segments = context.network.network_segments
                segment_type = segments[0]['network_type']
                if segment_type == 'vlan':
                    vlan_id = int(segments[0]['segmentation_id'])
                    self._create_vlan_network(network_id, host_id, vlan_id)
                else:
                    LOG.info(_("Not supported network type %s"), segment_type)
            else:
                LOG.info(_("Physical switch has already configured. "
                           "There are %d VMs in network %s."),
                         port_count, network_id)
        LOG.info(_("Create port end."))