def vim_network_api_delete_network(connection, msg):
    """
    Handle Delete-Network API request
    """
    global _network_delete_operations

    network_table = tables.tables_get_network_table()

    if msg.by_uuid is not None:
        DLOG.verbose("Delete network, by_uuid=%s." % msg.by_uuid)
        network = network_table.get(msg.by_uuid, None)
    else:
        DLOG.verbose("Delete network, by_name=%s." % msg.by_name)
        network = network_table.get_by_name(msg.by_name)

    if network is not None:
        _network_delete_operations[network.uuid] = connection
        network_director = directors.get_network_director()
        network_director.network_delete(network.uuid, _delete_network_callback)
    else:
        response = rpc.APIResponseDeleteNetwork()
        response.result = rpc.RPC_MSG_RESULT.NOT_FOUND
        connection.send(response.serialize())
        DLOG.verbose("Sent response=%s" % response)
        connection.close()
def vim_network_api_create_network(connection, msg):
    """
    Handle Create-Network API request
    """
    global _network_create_operations

    DLOG.verbose("Create network, name=%s." % msg.name)

    network_table = tables.tables_get_network_table()
    network = network_table.get_by_name(msg.name)
    if network is None:
        _network_create_operations[msg.name] = connection
        network_director = directors.get_network_director()
        network_director.network_create(msg.name, msg.network_type,
                                        msg.segmentation_id,
                                        msg.physical_network, msg.is_shared,
                                        _create_network_callback)
    else:
        response = rpc.APIResponseCreateNetwork()
        response.result = rpc.RPC_MSG_RESULT.CONFLICT
        connection.send(response.serialize())
        DLOG.verbose("Sent response=%s" % response)
        connection.close()
def vim_network_api_create_subnet(connection, msg):
    """
    Handle Create-Subnet API request
    """
    global _subnet_create_operations

    DLOG.verbose("Create subnet, ip=%s, prefix=%s for network %s." %
                 (msg.subnet_ip, msg.subnet_prefix, msg.network_name))
    network_table = tables.tables_get_network_table()
    network = network_table.get_by_name(msg.network_name)
    if network is not None:
        subnet_table = tables.tables_get_subnet_table()
        subnet = subnet_table.get_by_network_and_ip(network.uuid,
                                                    msg.subnet_ip,
                                                    msg.subnet_prefix)
        if subnet is None:
            op_index = "%s.%s.%s" % (network.uuid, msg.subnet_ip,
                                     msg.subnet_prefix)
            _subnet_create_operations[op_index] = connection
            network_director = directors.get_network_director()
            network_director.subnet_create(network.uuid, None, msg.ip_version,
                                           msg.subnet_ip, msg.subnet_prefix,
                                           msg.gateway_ip, msg.is_dhcp_enabled,
                                           _create_subnet_callback)
        else:
            response = rpc.APIResponseCreateNetwork()
            response.result = rpc.RPC_MSG_RESULT.CONFLICT
            connection.send(response.serialize())
            DLOG.verbose("Sent response=%s" % response)
            connection.close()
    else:
        response = rpc.APIResponseCreateSubnet()
        response.result = rpc.RPC_MSG_RESULT.NOT_FOUND
        connection.send(response.serialize())
        DLOG.verbose("Sent response=%s" % response)
        connection.close()