#!/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


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)

    client = OFPClient(options.host + ':' + str(options.port))
    commands = {
        'list_nets': lambda a: sys.stdout.write(client.get_networks()),
        'create_net': lambda a: client.create_network(a[1]),
        'update_net': lambda a: client.update_network(a[1]),
        'delete_net': lambda a: client.delete_network(a[1]),
        'list_ports': lambda a: sys.stdout.write(client.get_ports(a[1])),
        'create_port': lambda a: client.create_port(a[1], a[2], a[3]),
        'update_port': lambda a: client.update_port(a[1], a[2], a[3]),
        'delete_port': lambda a: client.delete_port(a[1], a[2], a[3])
    }

    # 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()
