Esempio n. 1
0
def delete_flow(node_id, table_id, flow_id):
    credentials = (odl_user, odl_pass)
    odl = ODLInstance(odl_server, credentials)

    data = json.loads(flask.request.get_data().decode('utf-8'))
    delete_all = data['delete_all']

    try:
        # Get the node object
        node = odl.get_node_by_id(node_id)
        # Get the table object
        table = node.get_table_by_id(table_id)
        # Get the flow
        flow = table.get_all_flows()[flow_id]
    except (NodeNotFound, TableNotFound, FlowNotFound) as e:
        flask.abort(404)

    if delete_all:
        nodes = odl.get_nodes()
        for node in nodes.values():
            node.delete_config_flows_by_name(flow.name)
    else:
        flow.delete()

    return flask.redirect("/")
Esempio n. 2
0
def install_flow(node_id, table_id):
    """ 
    This will install a flow.

    You should pass via POST few args.

    TODO: Better documentation.
    """
    credentials = (odl_user, odl_pass)
    odl = ODLInstance(odl_server, credentials)
    try:
        node = odl.get_node_by_id(node_id)
        table = node.get_table_by_id(table_id)
    except (NodeNotFound, TableNotFound) as e:
        flask.abort(404)

    form = flask.request.form
    table.install_flow(priority = form.get('priority'),
                       name = form.get('name'),
                       eth_type = form.get('eth_type'),
                       eth_source = form.get('eth_source'),
                       eth_destination = form.get('eth_destination'),
                       ipv4_source = form.get('ipv4_source'),
                       ipv4_destination = form.get('ipv4_destination'),
                       connector_id = form.get('output'),
                       template_dir = template_dir)
    return flask.redirect("/")
Esempio n. 3
0
def flow_stats(node_id, table_id, clean_flow_id):
    """ 
    This endpoint returns statistics to plot.
    You should pass a node id, table id and a clean flow id. (removing #, $, -
    and * symbols from id).
    """
    # Main flow
    main = get_rrd_stats(node_id, table_id, clean_flow_id)

    credentials = (odl_user, odl_pass)
    odl = ODLInstance(odl_server, credentials)
    try:
        node = odl.get_node_by_id(node_id)
        table = node.get_table_by_id(table_id)
        flow = table.get_flow_by_clean_id(clean_flow_id)
    except (NodeNotFound, TableNotFound, FlowNotFound) as e:
        return flask.abort(404)

    # Try to get the oposite flow (just in case of where booth are added with
    # same name.
    flows = table.get_config_flows_by_name(flow.name)
    for oposite in flows:
        if oposite.clean_id != clean_flow_id:
            break
    if flows:
        oposite = get_rrd_stats(node_id, table_id, oposite.clean_id)
    else:
        oposite = []

    return flask.jsonify({'main': main,
                          'oposite': oposite})
Esempio n. 4
0
def path_stats(node_id, table_id, clean_flow_id, diff):
    credentials = (odl_user, odl_pass)
    odl = ODLInstance(odl_server, credentials)
    try:
        node = odl.get_node_by_id(node_id)
        table = node.get_table_by_id(table_id)
        flow = table.get_flow_by_clean_id(clean_flow_id)
    except (NodeNotFound, TableNotFound, FlowNotFound) as e:
        return flask.abort(404)

    nodes = odl.get_nodes()
    data = [];
    eth_type = flow.get_ethernet_type()
    eth_src = flow.get_ethernet_source()
    eth_dest = flow.get_ethernet_destination()
    ipv4_src = flow.get_ipv4_source()
    ipv4_dest = flow.get_ipv4_destination()
    if eth_type == 0x0800 and (eth_src != '*' or
                               eth_dest != '*' or
                               ipv4_src != '*' or
                               ipv4_dest != '*'):
        path = explore_path(nodes, flow, eth_type, eth_src, eth_dest, ipv4_src, ipv4_dest)
        for n in path:
            ndata = {
                'id': n['id'],
                'main': get_rrd_stats(n['id'], n['main']['table_id'], n['main']['clean_flow_id'], diff)
            }
            if n.has_key('oposite'):
                ndata['oposite'] = get_rrd_stats(n['id'], n['oposite']['table_id'], n['oposite']['clean_flow_id'], diff)
            data.append(ndata)
    else:
        data.append({'id': node.id, 'main': get_rrd_stats(node.id, table.id, flow.clean_id, diff)})
    return flask.jsonify(data)
Esempio n. 5
0
def delete_flow(node_id, table_id, flow_id):
    credentials = (odl_user, odl_pass)
    odl = ODLInstance(odl_server, credentials)

    data = json.loads(flask.request.get_data().decode('utf-8'))
    delete_all = data['delete_all']

    try:
        # Get the node object
        node = odl.get_node_by_id(node_id)
        # Get the table object
        table = node.get_table_by_id(table_id)
        # Get the flow
        flow = table.get_all_flows()[flow_id]
    except (NodeNotFound, TableNotFound, FlowNotFound) as e:
        flask.abort(404)

    if delete_all:
        nodes = odl.get_nodes()
        for node in nodes.values():
            node.delete_config_flows_by_name(flow.name)
    else:
        flow.delete()

    return flask.redirect("/")
Esempio n. 6
0
def install_flows_for_l3path(path_id):
    """ 
    This will install a l3 full path flows (based on IP Address addresses) in all
    switches in a path.

    You should use first '/api/routes/l3' to see the uid of path. This is a
    necessary argument for this endpoint.
    """
    client_ip = "%s" % flask.request.environ['REMOTE_ADDR']
    try:
        path = session_l3paths[client_ip][path_id]
    except KeyError:
        flask.abort(404)

    ports = get_ports_on_path(path)

    source_host = path[0]
    target_host = path[-1]

    # Install a flow in each switch on the path with correct output
    # port.
    for source_port, target_port in ports:
        source_switch = "%s:%s" % (source_port.split(":")[0], source_port.split(":")[1])
        target_switch = "%s:%s" % (target_port.split(":")[0], target_port.split(":")[1])
        # Just in case check
        if (source_switch != target_switch):
            print "Error 500: Switches are different on path"
            flask.abort(500)

        # Match: source_port, source_host, target_host, eth_type = 0x806 ?
        # Target Action: target_port
        # Lookup for switch in database
        try:
            credentials = (odl_user, odl_pass)
            odl = ODLInstance(odl_server, credentials)
            node = odl.get_node_by_id(target_switch)
            table = node.get_table_by_id(0) # Assuming installing on table 0
        except (NodeNotFound, TableNotFound) as e:
            print "Error: 404 - Switch or table not found in database"
            flask.abort(404)

        print "Inserting flow for %s..." % node.id

        # Install the flow one way
        table.l3output(flow_name = "L3AR%s" % path_id.split("-")[0],
                       connector_id = target_port,
                       source = "%s/32" % source_host,
                       destination = "%s/32" % target_host,
                       template_dir = template_dir)

        # Install the flow another way
        table.l3output(flow_name = "L3AR%s" % path_id.split("-")[0],
                       connector_id = source_port,
                       source = "%s/32" % target_host,
                       destination = "%s/32" % source_host,
                       template_dir = template_dir)

    # Update Json file
    return flask.redirect("/")
Esempio n. 7
0
def e2e_stats(path_id, diff):
    if cache_l2paths.has_key(path_id):
        path = cache_l2paths[path_id]
        flow_name = "L2AR%s" % path_id.split("-")[0]
        source_host = ":".join(path[0].split(":")[1::])
        target_host = ":".join(path[-1].split(":")[1::])
        match = {'eth_type': 0x0800, 'eth_src': source_host, 'eth_dest': target_host, 'ipv4_src': '*', 'ipv4_dest': '*'}
    elif cache_l3paths.has_key(path_id):
        path = cache_l3paths[path_id]
        flow_name = "L3AR%s" % path_id.split("-")[0]
        source_host = path[0] + '/32'
        target_host = path[-1] + '/32'
        match = {'eth_type': 0x0800, 'eth_src': '*', 'eth_dest': '*', 'ipv4_src': source_host, 'ipv4_dest': target_host}
    else:
        flask.abort(404)
    ports = get_ports_on_path(path)
    source_switch = "%s:%s" % (ports[0][1].split(":")[0], ports[0][1].split(":")[1])
    target_switch = "%s:%s" % (ports[-1][0].split(":")[0], ports[-1][0].split(":")[1])
    try:
        credentials = (odl_user, odl_pass)
        odl = ODLInstance(odl_server, credentials)
        main_node = odl.get_node_by_id(source_switch)
        oposite_node = odl.get_node_by_id(target_switch)
        # Assuming installing on table 0
        main_table = main_node.get_table_by_id(0)
        oposite_table = oposite_node.get_table_by_id(0)
    except (NodeNotFound, TableNotFound) as e:
        print "Error: 404 - Switch or table not found in database"
        flask.abort(404)

    main = {}
    for flow in main_table.get_config_flows_by_name(flow_name):
        if check_match_flow(flow, **match):
            main = get_rrd_stats(main_node.id, main_table.id, flow.clean_id, diff)
            break
    oposite = {}
    for flow in oposite_table.get_config_flows_by_name(flow_name):
        if check_reverse_match_flow(flow, **match):
            oposite = get_rrd_stats(oposite_node.id, oposite_table.id, flow.clean_id, diff)
            break
    return flask.jsonify({'main': main,
                          'oposite': oposite,
                          'source': source_host,
                          'target': target_host})
Esempio n. 8
0
def delete_low_priority_flows(node_id, table_id):
    credentials = (odl_user, odl_pass)
    odl = ODLInstance(odl_server, credentials)

    try:
        node = odl.get_node_by_id(node_id)
        table = node.get_table_by_id(table_id)
        table.delete_low_priority_flows()
        return flask.redirect("/")
    except (NodeNotFloud, TableNotFound) as e:
        flask.abort(404)
Esempio n. 9
0
def path_stats(node_id, table_id, clean_flow_id, diff):
    credentials = (odl_user, odl_pass)
    odl = ODLInstance(odl_server, credentials)
    try:
        node = odl.get_node_by_id(node_id)
        table = node.get_table_by_id(table_id)
        flow = table.get_flow_by_clean_id(clean_flow_id)
    except (NodeNotFound, TableNotFound, FlowNotFound) as e:
        return flask.abort(404)

    nodes = odl.get_nodes()
    data = []
    eth_type = flow.get_ethernet_type()
    eth_src = flow.get_ethernet_source()
    eth_dest = flow.get_ethernet_destination()
    ipv4_src = flow.get_ipv4_source()
    ipv4_dest = flow.get_ipv4_destination()
    if eth_type == 0x0800 and (eth_src != '*' or eth_dest != '*'
                               or ipv4_src != '*' or ipv4_dest != '*'):
        path = explore_path(nodes, flow, eth_type, eth_src, eth_dest, ipv4_src,
                            ipv4_dest)
        for n in path:
            ndata = {
                'id':
                n['id'],
                'main':
                get_rrd_stats(n['id'], n['main']['table_id'],
                              n['main']['clean_flow_id'], diff)
            }
            if n.has_key('oposite'):
                ndata['oposite'] = get_rrd_stats(n['id'],
                                                 n['oposite']['table_id'],
                                                 n['oposite']['clean_flow_id'],
                                                 diff)
            data.append(ndata)
    else:
        data.append({
            'id':
            node.id,
            'main':
            get_rrd_stats(node.id, table.id, flow.clean_id, diff)
        })
    return flask.jsonify(data)
Esempio n. 10
0
def delete_flow(node_id, table_id, flow_id):
    credentials = (odl_user, odl_pass)
    odl = ODLInstance(odl_server, credentials)

    delete_all = flask.request.form.get('delete_all')

    try:
        # Get the node object
        node = odl.get_node_by_id(node_id)
        # Get the table object
        table = node.get_table_by_id(table_id)
        # Get the flow
        flow = table.get_flow_by_id(flow_id)
    except (NodeNotFound, TableNotFound, FlowNotFound) as e:
        flask.abort(404)

    if delete_all:
        nodes = odl.get_nodes()
        for node in nodes.values():
            node.delete_config_flows_by_name(flow.name)
    else:
        flow.delete()

    return flask.redirect("/")
Esempio n. 11
0
def install_flows_for_l2path(path_id):
    """
    This will install a l2 full path flows (based on MAC addresses) in all
    switches in a path.

    You should use first '/api/routes/l2' to see the uid of path. This is a
    necessary argument for this endpoint.
    """
    client_ip = "%s" % flask.request.environ['REMOTE_ADDR']
    try:
        path = session_l2paths[client_ip][path_id]
        path, src_vlan, dst_vlan = path['path'], path['src-vlan'], path['dst-vlan']
        src_vlan = src_vlan if src_vlan != '' else None
        dst_vlan = dst_vlan if dst_vlan != '' else None
    except KeyError:
        flask.abort(404)

    ports = get_ports_on_path(path)

    # [(u'openflow:562958149829575:2', u'openflow:562958149829575:21'),
    #  (u'openflow:440565346114459:257', u'openflow:440565346114459:217')]

    data = json.loads(flask.request.get_data().decode('utf-8'))
    source_host = data['source']
    target_host = data['destination']
    # source_host = ":".join(path[0].split(":")[1::])
    # target_host = ":".join(path[-1].split(":")[1::])

    # Install a flow in each switch on the path with correct output
    # port.
    if dst_vlan:
        source_host, target_host = target_host, source_host
        src_vlan, dst_vlan = dst_vlan, src_vlan
        ports.reverse()

    first = src_vlan
    for source_port, target_port in ports:
        source_switch = "%s:%s" % (source_port.split(":")[0], source_port.split(":")[1])
        target_switch = "%s:%s" % (target_port.split(":")[0], target_port.split(":")[1])
        # Just in case check
        if (source_switch != target_switch):
            print "Error 500: Switches are different on path"
            flask.abort(500)

        # Match: source_port, source_host, target_host, eth_type = 0x806 ?
        # Target Action: target_port
        # Lookup for switch in database
        try:
            credentials = (odl_user, odl_pass)
            odl = ODLInstance(odl_server, credentials)
            node = odl.get_node_by_id(target_switch)
            table = node.get_table_by_id(0) # Assuming installing on table 0
        except (NodeNotFound, TableNotFound) as e:
            print "Error: 404 - Switch or table not found in database"
            flask.abort(404)

        print "Inserting flow for %s..." % node.id

        flow_name = "L2AR%s" % path_id.split('-')[0]
        if first:
            table.l2output(flow_name = flow_name,
                           in_port = source_port,
                           connector_id = target_port,
                           source = source_host,
                           destination = target_host,
                           template_dir = template_dir,
                           ingress_vlan = src_vlan)

            table.l2output(flow_name = flow_name,
                           in_port = target_port,
                           connector_id = source_port,
                           source = target_host,
                           destination = source_host,
                           template_dir = template_dir,
                           egress_vlan = src_vlan)

            first = False
            continue

        # Install the flow one way
        table.l2output(flow_name = flow_name,
                       in_port = source_port,
                       connector_id = target_port,
                       source = source_host,
                       destination = target_host,
                       template_dir = template_dir)

        # Install the flow another way
        table.l2output(flow_name = flow_name,
                       in_port = target_port,
                       connector_id = source_port,
                       source = target_host,
                       destination = source_host,
                       template_dir = template_dir)

    cache_l2paths[path_id] = path
    # Update Json file
    return flask.redirect("/")
Esempio n. 12
0
def install_flows_for_l2path(path_id):
    """
    This will install a l2 full path flows (based on MAC addresses) in all
    switches in a path.

    You should use first '/api/routes/l2' to see the uid of path. This is a
    necessary argument for this endpoint.
    """
    client_ip = "%s" % flask.request.environ['REMOTE_ADDR']
    try:
        path = session_l2paths[client_ip][path_id]
        path, src_vlan, dst_vlan = path['path'], path['src-vlan'], path[
            'dst-vlan']
        src_vlan = src_vlan if src_vlan != '' else None
        dst_vlan = dst_vlan if dst_vlan != '' else None
    except KeyError:
        flask.abort(404)

    ports = get_ports_on_path(path)

    # [(u'openflow:562958149829575:2', u'openflow:562958149829575:21'),
    #  (u'openflow:440565346114459:257', u'openflow:440565346114459:217')]

    data = json.loads(flask.request.get_data().decode('utf-8'))
    source_host = data['source']
    target_host = data['destination']
    # source_host = ":".join(path[0].split(":")[1::])
    # target_host = ":".join(path[-1].split(":")[1::])

    # Install a flow in each switch on the path with correct output
    # port.
    if dst_vlan:
        source_host, target_host = target_host, source_host
        src_vlan, dst_vlan = dst_vlan, src_vlan
        ports.reverse()

    first = src_vlan
    for source_port, target_port in ports:
        source_switch = "%s:%s" % (source_port.split(":")[0],
                                   source_port.split(":")[1])
        target_switch = "%s:%s" % (target_port.split(":")[0],
                                   target_port.split(":")[1])
        # Just in case check
        if (source_switch != target_switch):
            print "Error 500: Switches are different on path"
            flask.abort(500)

        # Match: source_port, source_host, target_host, eth_type = 0x806 ?
        # Target Action: target_port
        # Lookup for switch in database
        try:
            credentials = (odl_user, odl_pass)
            odl = ODLInstance(odl_server, credentials)
            node = odl.get_node_by_id(target_switch)
            table = node.get_table_by_id(0)  # Assuming installing on table 0
        except (NodeNotFound, TableNotFound) as e:
            print "Error: 404 - Switch or table not found in database"
            flask.abort(404)

        print "Inserting flow for %s..." % node.id

        flow_name = "L2AR%s" % path_id.split('-')[0]
        if first:
            table.l2output(flow_name=flow_name,
                           in_port=source_port,
                           connector_id=target_port,
                           source=source_host,
                           destination=target_host,
                           template_dir=template_dir,
                           ingress_vlan=src_vlan)

            table.l2output(flow_name=flow_name,
                           in_port=target_port,
                           connector_id=source_port,
                           source=target_host,
                           destination=source_host,
                           template_dir=template_dir,
                           egress_vlan=src_vlan)

            first = False
            continue

        # Install the flow one way
        table.l2output(flow_name=flow_name,
                       in_port=source_port,
                       connector_id=target_port,
                       source=source_host,
                       destination=target_host,
                       template_dir=template_dir)

        # Install the flow another way
        table.l2output(flow_name=flow_name,
                       in_port=target_port,
                       connector_id=source_port,
                       source=target_host,
                       destination=source_host,
                       template_dir=template_dir)

    cache_l2paths[path_id] = path
    # Update Json file
    return flask.redirect("/")
Esempio n. 13
0
def e2e_stats(path_id, diff):
    if cache_l2paths.has_key(path_id):
        path = cache_l2paths[path_id]
        flow_name = "L2AR%s" % path_id.split("-")[0]
        source_host = ":".join(path[0].split(":")[1::])
        target_host = ":".join(path[-1].split(":")[1::])
        match = {
            'eth_type': 0x0800,
            'eth_src': source_host,
            'eth_dest': target_host,
            'ipv4_src': '*',
            'ipv4_dest': '*'
        }
    elif cache_l3paths.has_key(path_id):
        path = cache_l3paths[path_id]
        flow_name = "L3AR%s" % path_id.split("-")[0]
        source_host = path[0] + '/32'
        target_host = path[-1] + '/32'
        match = {
            'eth_type': 0x0800,
            'eth_src': '*',
            'eth_dest': '*',
            'ipv4_src': source_host,
            'ipv4_dest': target_host
        }
    else:
        flask.abort(404)
    ports = get_ports_on_path(path)
    source_switch = "%s:%s" % (ports[0][1].split(":")[0],
                               ports[0][1].split(":")[1])
    target_switch = "%s:%s" % (ports[-1][0].split(":")[0],
                               ports[-1][0].split(":")[1])
    try:
        credentials = (odl_user, odl_pass)
        odl = ODLInstance(odl_server, credentials)
        main_node = odl.get_node_by_id(source_switch)
        oposite_node = odl.get_node_by_id(target_switch)
        # Assuming installing on table 0
        main_table = main_node.get_table_by_id(0)
        oposite_table = oposite_node.get_table_by_id(0)
    except (NodeNotFound, TableNotFound) as e:
        print "Error: 404 - Switch or table not found in database"
        flask.abort(404)

    main = {}
    for flow in main_table.get_config_flows_by_name(flow_name):
        if check_match_flow(flow, **match):
            main = get_rrd_stats(main_node.id, main_table.id, flow.clean_id,
                                 diff)
            break
    oposite = {}
    for flow in oposite_table.get_config_flows_by_name(flow_name):
        if check_reverse_match_flow(flow, **match):
            oposite = get_rrd_stats(oposite_node.id, oposite_table.id,
                                    flow.clean_id, diff)
            break
    return flask.jsonify({
        'main': main,
        'oposite': oposite,
        'source': source_host,
        'target': target_host
    })