summaryrefslogtreecommitdiffhomepage
path: root/bin/ryu-client
blob: 292ee261f5603a8784b7f6eefd7849b9c33845fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
#
# Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
# Copyright (C) 2011 Isaku Yamahata <yamahata at valinux co jp>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
from optparse import OptionParser

from ryu.app.client import OFPClient
from ryu.app.client import QuantumIfaceClient
from ryu.app.client import SwitchConfClient
from ryu.app.client import TunnelClient


def client_test():
    parser = OptionParser(usage="Usage: %prog [OPTIONS] <command> [args]")
    parser.add_option("-H", "--host", dest="host", type="string",
                      default="127.0.0.1", help="ip address rest api service")
    parser.add_option("-p", "--port", dest="port", type="int", default="8080")

    options, args = parser.parse_args()
    if len(args) == 0:
        parser.print_help()
        sys.exit(1)

    address = options.host + ':' + str(options.port)
    ofp_client = OFPClient(address)
    tun_client = TunnelClient(address)
    sc_client = SwitchConfClient(address)
    qi_client = QuantumIfaceClient(address)

    commands = {
        'list_nets': lambda a: sys.stdout.write(ofp_client.get_networks()),
        'create_net': lambda a: ofp_client.create_network(a[1]),
        'update_net': lambda a: ofp_client.update_network(a[1]),
        'delete_net': lambda a: ofp_client.delete_network(a[1]),
        'list_ports': lambda a: sys.stdout.write(ofp_client.get_ports(a[1])),
        'create_port': lambda a: ofp_client.create_port(a[1], a[2], a[3]),
        'update_port': lambda a: ofp_client.update_port(a[1], a[2], a[3]),
        'delete_port': lambda a: ofp_client.delete_port(a[1], a[2], a[3]),

        'get_tun_key': lambda a: sys.stdout.write(
            tun_client.get_tunnel_key(a[1])),
        'delete_tun_key': lambda a: tun_client.delete_tunnel_key(a[1]),
        'create_tun_key': lambda a: tun_client.create_tunnel_key(a[1], a[2]),
        'update_tun_key': lambda a: tun_client.update_tunnel_key(a[1], a[2]),
        'list_tun_ports': lambda a: sys.stdout.write(
            tun_client.list_ports(a[1])),
        'delete_tun_port': lambda a: tun_client.delete_port(a[1], a[2]),
        'get_remote_dpid': lambda a: sys.stdout.write(
            tun_client.get_remote_dpid(a[1], a[2])),
        'create_remote_dpid': lambda a: tun_client.create_remote_dpid(
            a[1], a[2], a[3]),
        'update_remote_dpid': lambda a: tun_client.update_remote_dpid(
            a[1], a[2], a[3]),

        'sc_list_sw': lambda a: sys.stdout.write(sc_client.list_switches()),
        'sc_delete_sw': lambda a: sc_client.delete_switch(a[1]),
        'sc_list_keys': lambda a: sys.stdout.write(sc_client.list_keys(a[1])),
        'sc_set_key': lambda a: sc_client.set_key(a[1], a[2], a[3]),
        'sc_get_key': lambda a: sys.stdout.write(
            sc_client.get_key(a[1], a[2])),
        'sc_delete_key': lambda a: sc_client.delete_key(a[1], a[2]),

        'qi_list_iface': lambda a: sys.stdout.write(qi_client.list_ifaces()),
        'qi_delete_iface': lambda a: qi_client.delete_iface(a[1]),
        'qi_list_keys': lambda a: sys.stdout.write(
            qi_client.list_keys(a[1])),
        'qi_create_key': lambda a: qi_client.create_network_id(
            a[1], a[2], a[3]),
        'qi_update_key': lambda a: qi_client.update_network_id(
            a[1], a[2], a[3]),
        'qi_get_net_id': lambda a: sys.stdout.write(
            qi_client.get_network_id(a[1])),
        'qi_create_net_id': lambda a: qi_client.create_network_id(a[1], a[2]),
        'qi_update_net_id': lambda a: qi_client.update_network_id(a[1], a[2]),
    }

    # allow '-', instead of '_'
    commands.update(dict([(k.replace('_', '-'), v)
                          for (k, v) in commands.items()]))

    cmd = args[0]
    commands[cmd](args)

if __name__ == "__main__":
    client_test()