Beispiel #1
0
def config_acl(nf_command, acl_type, router_id, rule_id, acl):
    log.debug('nf_command: %s, acl_id: %r-%r, acl: %r' % (
        nf_command, router_id, rule_id, acl.to_native()))

    args, constrains = get_config_acl_args(nf_command,
                                           acl_type,
                                           router_id,
                                           acl)
    if constrains is not None:
        log.info(constrains)
        return json_response(
            status=INVALID_POST_DATA, description=constrains
        ), HTTP_BAD_REQUEST

    key = (nf_command, 'acl', acl_type, str(router_id), str(acl.rule_id))
    ret_queue = PseudoGeventQueue()
    cmd_waiting_queues_put(router_id,
                           router_hash,
                           key,
                           args,
                           ret_queue)
    output = cmd_response_get(ret_queue)
    if output:
        return json_response(
            status=SERVER_ERROR, description=output
        ), HTTP_INTERNAL_SERVER_ERROR

    return json_response(status=SUCCESS), HTTP_OK
Beispiel #2
0
def create_route(route, router_id):
    log.debug('router_id: %r, route: %r' % (router_id, route.to_native()))

    key = ('create', 'route', str(router_id), route.dst_network.address,
           route.dst_network.netmask)
    ret_queue = PseudoGeventQueue()
    args = [
        ROUTER_SCRIPT,
        'add',
        'route',
        str(router_id),
        route.dst_network.address,
        route.dst_network.netmask,
        route.next_hop,
        str(route.if_type),
        str(route.if_index),
        str(route.isp),
    ]
    cmd_waiting_queues_put(router_id, router_hash, key, args, ret_queue)
    output = cmd_response_get(ret_queue)
    if output:
        return json_response(status=SERVER_ERROR,
                             description=output), HTTP_INTERNAL_SERVER_ERROR

    return json_response(status=SUCCESS), HTTP_OK
Beispiel #3
0
def delete_route(router_id, route_id):
    id_seg = route_id.split('-')
    if id_seg is None or len(id_seg) != 2:
        return json_response(status=RESOURCE_NOT_FOUND), HTTP_NOT_FOUND

    for s in id_seg:
        if not IPv4Type.valid_ip(s):
            return json_response(status=RESOURCE_NOT_FOUND), HTTP_NOT_FOUND
    dst_address, dst_netmask = id_seg

    key = ('delete', 'route', str(router_id), dst_address, dst_netmask)
    ret_queue = PseudoGeventQueue()
    args = [
        ROUTER_SCRIPT,
        'delete',
        'route',
        str(router_id),
        dst_address,
        dst_netmask,
    ]
    cmd_waiting_queues_put(router_id, router_hash, key, args, ret_queue)
    output = cmd_response_get(ret_queue)
    if output:
        return json_response(status=SERVER_ERROR,
                             description=output), HTTP_INTERNAL_SERVER_ERROR

    return json_response(status=SUCCESS), HTTP_OK
Beispiel #4
0
def read_valve(router_id):
    log.debug('valve router_id: %r' % router_id)
    key = ('read', 'valve', str(router_id))
    ret_queue = PseudoGeventQueue()
    args = [
        ROUTER_SCRIPT,
        'get',
        'valve',
        str(router_id),
    ]
    cmd_waiting_queues_put(router_id, router_hash, key, args, ret_queue)
    ret_list = []
    output = cmd_response_get(ret_queue, read_response_handle(ret_list))
    if output:
        return json_response(status=SERVER_ERROR,
                             description=output), HTTP_INTERNAL_SERVER_ERROR

    try:
        valve = Valve(json.loads(ret_list[0]))
        valve.validate()
        return json_response(status=SUCCESS,
                             data=valve.to_primitive(),
                             type="ROUTER"), HTTP_OK
    except Exception, e:
        log.debug(e)
        log.debug(output)
        return json_response(status=SERVER_ERROR,
                             description=str(e)), HTTP_INTERNAL_SERVER_ERROR
Beispiel #5
0
def read_lan(router_id, if_index):
    key = ('read', 'valve-lan', str(router_id), str(if_index))
    ret_queue = PseudoGeventQueue()
    args = [
        ROUTER_SCRIPT,
        'get',
        'valve-lan',
        str(router_id),
        str(if_index),
    ]
    cmd_waiting_queues_put(router_id, router_hash, key, args, ret_queue)
    ret_list = []
    output = cmd_response_get(ret_queue, read_response_handle(ret_list))
    if output:
        return json_response(status=SERVER_ERROR,
                             description=output), HTTP_INTERNAL_SERVER_ERROR

    try:
        lan = Lan(json.loads(ret_list[0]))
        lan.validate()
        return json_response(status=SUCCESS,
                             data=lan.to_primitive(),
                             type="LAN"), HTTP_OK
    except Exception, e:
        log.debug(e)
        log.debug(output)
        return json_response(status=SERVER_ERROR,
                             description=str(e)), HTTP_INTERNAL_SERVER_ERROR
Beispiel #6
0
def put_cmd(cmd, router_id):
    log.debug('router_id: %r, command: %r' % (router_id, cmd.to_native()))

    key = ('exec-cmd', cmd.command, str(router_id))
    message = []

    args = [
        ROUTER_SCRIPT,
        cmd.command,
        str(router_id),
        cmd.if_type,
        str(cmd.if_index),
        cmd.source,
        cmd.target,
        '%.3f' % (cmd.interval / 1000.0),
    ]
    message.append(args)

    ret_queue = PseudoGeventQueue()
    cmd_waiting_queues_put(router_id, router_hash,
                           key, message, ret_queue)
    ret_list = []
    err = cmd_response_get(ret_queue, read_response_handle(ret_list))
    if err or not ret_list:
        return json_response(
            data={'OUTPUT': ''}, status=SUCCESS), HTTP_OK
    else:
        return json_response(
            data={'OUTPUT': ret_list[0]}, status=SUCCESS), HTTP_OK
Beispiel #7
0
def update_nat(nat_type, router_id, nats):
    key = ('update', 'nat', nat_type, str(router_id))
    message = []
    args = [
        ROUTER_SCRIPT,
        'flush',
        'nat',
        nat_type,
        str(router_id),
    ]
    message.append(args)

    for nat in sorted(nats, cmp=lambda x, y: cmp(x.rule_id, y.rule_id)):
        args, constrains = get_config_nat_args(NF_APPEND, nat_type, router_id,
                                               nat)
        if constrains is not None:
            return json_response(status=INVALID_POST_DATA,
                                 description=constrains), HTTP_BAD_REQUEST
        message.append(args)

    ret_queue = PseudoGeventQueue()
    cmd_waiting_queues_put(router_id, router_hash, key, message, ret_queue)
    output = cmd_response_get(ret_queue)
    if output:
        return json_response(status=SERVER_ERROR,
                             description=output), HTTP_INTERNAL_SERVER_ERROR

    return json_response(status=SUCCESS), HTTP_OK
Beispiel #8
0
def create_lan_worker(lan, router_id):
    log.debug('valve router_id: %d, lan: %r' % (router_id, lan.to_native()))

    key = ('create', 'valve-lan', str(router_id), str(lan.if_index))
    message = []
    message.append(get_delete_lan_args(router_id, lan.if_index))

    args = [
        ROUTER_SCRIPT,
        'add',
        'valve-lan',
        str(router_id),
        str(lan.if_index),
        lan.mac,
        str(lan.vlantag),
        # str(lan.qos.min_bandwidth),
        # str(lan.qos.max_bandwidth),
    ]
    # for ip in lan.ips:
    #    args.append(ip.address)
    #    args.append(ip.netmask)
    message.append(args)

    ret_queue = PseudoGeventQueue()
    cmd_waiting_queues_put(router_id, router_hash, key, message, ret_queue)

    output = cmd_response_get(ret_queue)
    if output:
        return json_response(status=SERVER_ERROR,
                             description=output), HTTP_INTERNAL_SERVER_ERROR

    return json_response(status=SUCCESS), HTTP_OK
Beispiel #9
0
def create_valve_worker(valve):
    log.debug('valve: %r' % valve.to_native())

    key = ('create', 'valve', str(valve.router_id))
    message = []
    message.append(get_delete_valve_args(valve.router_id))

    for wan in valve.wans:
        if wan.state == VIF_ATTACH:
            args = [
                ROUTER_SCRIPT,
                'add',
                'valve-wan',
                str(valve.router_id),
                str(wan.if_index),
                str(wan.isp),
                wan.gateway,
                wan.mac,
                str(wan.vlantag),
                str(wan.qos.min_bandwidth),
                str(wan.qos.max_bandwidth),
                str(wan.broadcast_qos.min_bandwidth),
                str(wan.broadcast_qos.max_bandwidth),
            ]
            for ip in wan.ips:
                args.append(ip.address)
                args.append(ip.netmask)
        else:
            continue
        message.append(args)

    for lan in valve.lans:
        if lan.state == VIF_ATTACH:
            args = [
                ROUTER_SCRIPT,
                'add',
                'valve-lan',
                str(valve.router_id),
                str(lan.if_index),
                lan.mac,
                str(lan.vlantag),
                # str(lan.qos.min_bandwidth),
                # str(lan.qos.max_bandwidth),
            ]
            # for ip in lan.ips:
            #    args.append(ip.address)
            #    args.append(ip.netmask)
        else:
            continue
        message.append(args)

    ret_queue = PseudoGeventQueue()
    cmd_waiting_queues_put(valve.router_id, router_hash, key, message,
                           ret_queue)
    output = cmd_response_get(ret_queue)
    if output:
        return json_response(status=SERVER_ERROR,
                             description=output), HTTP_INTERNAL_SERVER_ERROR
    return json_response(status=SUCCESS), HTTP_OK
Beispiel #10
0
def delete_lan(router_id, if_index):
    key = ('delete', 'valve-lan', str(router_id), str(if_index))
    ret_queue = PseudoGeventQueue()
    cmd_waiting_queues_put(router_id, router_hash, key,
                           get_delete_lan_args(router_id, if_index), ret_queue)
    output = cmd_response_get(ret_queue)
    if output:
        return json_response(status=SERVER_ERROR,
                             description=output), HTTP_INTERNAL_SERVER_ERROR

    return json_response(status=SUCCESS), HTTP_OK
Beispiel #11
0
def delete_valve(router_id, remove_vport=False):
    key = ('delete', 'valve', str(router_id))
    ret_queue = PseudoGeventQueue()
    cmd_waiting_queues_put(router_id, router_hash, key,
                           get_delete_valve_args(router_id, remove_vport),
                           ret_queue)
    output = cmd_response_get(ret_queue)
    if output:
        return json_response(status=SERVER_ERROR,
                             description=output), HTTP_INTERNAL_SERVER_ERROR

    return json_response(status=SUCCESS), HTTP_OK
Beispiel #12
0
def config_vpn(router_id, vpn):
    log.debug('router_id: %r, vpn: %r' % (router_id, vpn.to_native()))
    key = ('add', 'vpn', str(router_id), vpn.name)
    args = get_config_vpn_args(router_id, vpn)
    ret_queue = PseudoGeventQueue()
    cmd_waiting_queues_put(router_id, router_hash, key, args, ret_queue)
    output = cmd_response_get(ret_queue)
    if output:
        return json_response(status=SERVER_ERROR,
                             description=output), HTTP_INTERNAL_SERVER_ERROR

    return json_response(status=SUCCESS), HTTP_OK
Beispiel #13
0
def delete_conn_limit(router_id):
    key = ('delete', 'conn', str(router_id))
    args = [ROUTER_SCRIPT, 'delete', 'conntrack', '%d' % router_id]

    ret_queue = PseudoGeventQueue()
    cmd_waiting_queues_put(router_id, router_hash, key, args, ret_queue)
    output = cmd_response_get(ret_queue)
    if output:
        log.error('router %d: %s' % (router_id, output))
        return json_response(status=SERVER_ERROR,
                             description=output), HTTP_INTERNAL_SERVER_ERROR

    return json_response(status=SUCCESS), HTTP_OK
Beispiel #14
0
def read_conn_limit(router_id):
    key = ('read', 'conn', str(router_id))
    args = [ROUTER_SCRIPT, 'get', 'conntrack', '%d' % router_id]

    ret_queue = PseudoGeventQueue()
    cmd_waiting_queues_put(router_id, router_hash, key, args, ret_queue)
    ret_list = []
    output = cmd_response_get(ret_queue, read_response_handle(ret_list))
    if output:
        log.error('router %d: %s' % (router_id, output))
        return json_response(status=SERVER_ERROR,
                             description=output), HTTP_INTERNAL_SERVER_ERROR

    return json_response(status=SUCCESS, data={"RESULT": ret_list[0]}), HTTP_OK
Beispiel #15
0
def delete_vpn(router_id, vpn_name):
    key = ('delete', 'vpn', str(router_id), vpn_name)
    args = [
        ROUTER_SCRIPT,
        'delete',
        'vpn',
        str(router_id),
        vpn_name,
    ]
    ret_queue = PseudoGeventQueue()
    cmd_waiting_queues_put(router_id, router_hash, key, args, ret_queue)
    output = cmd_response_get(ret_queue)
    if output:
        return json_response(status=SERVER_ERROR,
                             description=output), HTTP_INTERNAL_SERVER_ERROR

    return json_response(status=SUCCESS), HTTP_OK
Beispiel #16
0
def delete_acl(acl_type, router_id, rule_id):
    key = ('delete', 'acl', acl_type, str(router_id), str(rule_id))
    ret_queue = PseudoGeventQueue()
    args = [
        ROUTER_SCRIPT,
        'delete', 'acl',
        acl_type,
        str(router_id),
        str(rule_id),
    ]
    cmd_waiting_queues_put(router_id,
                           router_hash,
                           key,
                           args,
                           ret_queue)
    output = cmd_response_get(ret_queue)
    if output:
        return json_response(
            status=SERVER_ERROR, description=output
        ), HTTP_INTERNAL_SERVER_ERROR

    return json_response(status=SUCCESS), HTTP_OK
Beispiel #17
0
def update_vpn(vpns, router_id):
    key = ('update', 'vpn', str(router_id))
    message = []
    args = [
        ROUTER_SCRIPT,
        'flush',
        'vpn',
        str(router_id),
    ]
    message.append(args)

    for vpn in vpns:
        args = get_config_vpn_args(router_id, vpn)
        message.append(args)

    ret_queue = PseudoGeventQueue()
    cmd_waiting_queues_put(router_id, router_hash, key, message, ret_queue)
    output = cmd_response_get(ret_queue)
    if output:
        return json_response(status=SERVER_ERROR,
                             description=output), HTTP_INTERNAL_SERVER_ERROR
    return json_response(status=SUCCESS), HTTP_OK
Beispiel #18
0
def create_router_worker(router):
    log.debug('router: %r' % router.to_native())

    key = ('create', 'router', str(router.router_id))
    message_del = get_delete_router_args(router.router_id)
    message_create = []

    for wan in router.wans:
        if wan.state == VIF_ATTACH:
            args = [
                ROUTER_SCRIPT,
                'add',
                'wan',
                str(router.router_id),
                str(wan.if_index),
                str(wan.isp),
                wan.gateway,
                wan.mac,
                str(wan.vlantag),
                str(wan.qos.min_bandwidth),
                str(wan.qos.max_bandwidth),
                str(wan.broadcast_qos.min_bandwidth),
                str(wan.broadcast_qos.max_bandwidth),
            ]
            for ip in wan.ips:
                args.append(ip.address)
                args.append(ip.netmask)
        else:
            continue
        message_create.append(args)

    for lan in router.lans:
        if lan.state == VIF_ATTACH:
            args = [
                ROUTER_SCRIPT,
                'add',
                'lan',
                str(router.router_id),
                str(lan.if_index),
                lan.mac,
                str(lan.vlantag),
                str(lan.qos.min_bandwidth),
                str(lan.qos.max_bandwidth),
            ]
            for ip in lan.ips:
                args.append(ip.address)
                args.append(ip.netmask)
        else:
            continue
        message_create.append(args)

    args = [
        ROUTER_SCRIPT,
        'add',
        'conntrack',
        str(router.router_id),
        str(router.conn_limit.conn_max),
        str(router.conn_limit.new_conn_per_sec),
    ]
    message_create.append(args)

    message = message_del + message_create

    ret_queue = PseudoGeventQueue()
    cmd_waiting_queues_put(router.router_id, router_hash, key, message,
                           ret_queue)
    output = cmd_response_get(ret_queue)
    if output:
        return json_response(status=SERVER_ERROR,
                             description=output), HTTP_INTERNAL_SERVER_ERROR
    return json_response(status=SUCCESS), HTTP_OK