| stevenvanrossem | c5a536a | 2016-02-16 14:52:39 +0100 | [diff] [blame] | 1 | """
|
| peusterm | 79ef6ae | 2016-07-08 13:53:57 +0200 | [diff] [blame] | 2 | Copyright (c) 2015 SONATA-NFV
|
| 3 | ALL RIGHTS RESERVED.
|
| 4 |
|
| 5 | Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 | you may not use this file except in compliance with the License.
|
| 7 | You may obtain a copy of the License at
|
| 8 |
|
| 9 | http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
|
| 11 | Unless required by applicable law or agreed to in writing, software
|
| 12 | distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 14 | See the License for the specific language governing permissions and
|
| 15 | limitations under the License.
|
| 16 |
|
| 17 | Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
|
| 18 | nor the names of its contributors may be used to endorse or promote
|
| 19 | products derived from this software without specific prior written
|
| 20 | permission.
|
| 21 |
|
| 22 | This work has been performed in the framework of the SONATA project,
|
| 23 | funded by the European Commission under Grant number 671517 through
|
| 24 | the Horizon 2020 and 5G-PPP programmes. The authors would like to
|
| 25 | acknowledge the contributions of their colleagues of the SONATA
|
| 26 | partner consortium (www.sonata-nfv.eu).
|
| stevenvanrossem | c5a536a | 2016-02-16 14:52:39 +0100 | [diff] [blame] | 27 | """
|
| 28 |
|
| 29 | import argparse
|
| 30 | import pprint
|
| 31 | from tabulate import tabulate
|
| 32 | import zerorpc
|
| 33 |
|
| 34 |
|
| 35 | pp = pprint.PrettyPrinter(indent=4)
|
| 36 |
|
| 37 | class ZeroRpcClient(object):
|
| 38 |
|
| 39 | def __init__(self):
|
| 40 | self.c = zerorpc.Client()
|
| 41 | # TODO connect to DCNetwork API
|
| 42 | #self.c.connect("tcp://127.0.0.1:4242") # TODO hard coded for now. we'll change this later
|
| 43 | self.c.connect("tcp://127.0.0.1:5151")
|
| 44 | self.cmds = {}
|
| 45 |
|
| 46 | def execute_command(self, args):
|
| 47 | if getattr(self, args["command"]) is not None:
|
| 48 | # call the local method with the same name as the command arg
|
| 49 | getattr(self, args["command"])(args)
|
| 50 | else:
|
| stevenvanrossem | 4937815 | 2016-05-19 11:33:48 +0200 | [diff] [blame] | 51 | print("Command not implemented.")
|
| stevenvanrossem | c5a536a | 2016-02-16 14:52:39 +0100 | [diff] [blame] | 52 |
|
| 53 | def add(self, args):
|
| stevenvanrossem | ed711fd | 2016-04-11 16:59:29 +0200 | [diff] [blame] | 54 | vnf_src_name = self._parse_vnf_name(args.get("source"))
|
| stevenvanrossem | ed711fd | 2016-04-11 16:59:29 +0200 | [diff] [blame] | 55 | vnf_dst_name = self._parse_vnf_name(args.get("destination"))
|
| stevenvanrossem | a1e59c7 | 2016-05-06 14:32:39 +0200 | [diff] [blame] | 56 |
|
| 57 | params = self._create_dict(
|
| 58 | vnf_src_interface=self._parse_vnf_interface(args.get("source")),
|
| 59 | vnf_dst_interface=self._parse_vnf_interface(args.get("destination")),
|
| 60 | weight=args.get("weight"),
|
| stevenvanrossem | 23c4809 | 2016-05-06 17:21:12 +0200 | [diff] [blame] | 61 | match=args.get("match"),
|
| stevenvanrossem | 898a2af | 2016-05-06 18:28:57 +0200 | [diff] [blame] | 62 | bidirectional=args.get("bidirectional"),
|
| 63 | cookie=args.get("cookie"))
|
| stevenvanrossem | a1e59c7 | 2016-05-06 14:32:39 +0200 | [diff] [blame] | 64 |
|
| stevenvanrossem | 307aa1f | 2016-05-06 10:35:15 +0200 | [diff] [blame] | 65 | # note zerorpc does not support named arguments
|
| stevenvanrossem | c5a536a | 2016-02-16 14:52:39 +0100 | [diff] [blame] | 66 | r = self.c.network_action_start(
|
| 67 | #args.get("datacenter"),
|
| stevenvanrossem | ed711fd | 2016-04-11 16:59:29 +0200 | [diff] [blame] | 68 | vnf_src_name,
|
| 69 | vnf_dst_name,
|
| stevenvanrossem | a1e59c7 | 2016-05-06 14:32:39 +0200 | [diff] [blame] | 70 | params)
|
| stevenvanrossem | c5a536a | 2016-02-16 14:52:39 +0100 | [diff] [blame] | 71 | pp.pprint(r)
|
| 72 |
|
| 73 | def remove(self, args):
|
| stevenvanrossem | ed711fd | 2016-04-11 16:59:29 +0200 | [diff] [blame] | 74 | vnf_src_name = self._parse_vnf_name(args.get("source"))
|
| stevenvanrossem | ed711fd | 2016-04-11 16:59:29 +0200 | [diff] [blame] | 75 | vnf_dst_name = self._parse_vnf_name(args.get("destination"))
|
| stevenvanrossem | 700ed32 | 2016-05-06 14:35:54 +0200 | [diff] [blame] | 76 |
|
| 77 | params = self._create_dict(
|
| 78 | vnf_src_interface=self._parse_vnf_interface(args.get("source")),
|
| 79 | vnf_dst_interface=self._parse_vnf_interface(args.get("destination")),
|
| 80 | weight=args.get("weight"),
|
| stevenvanrossem | 898a2af | 2016-05-06 18:28:57 +0200 | [diff] [blame] | 81 | match=args.get("match"),
|
| stevenvanrossem | 27b6d95 | 2016-05-10 16:37:57 +0200 | [diff] [blame] | 82 | bidirectional=args.get("bidirectional"),
|
| stevenvanrossem | 898a2af | 2016-05-06 18:28:57 +0200 | [diff] [blame] | 83 | cookie=args.get("cookie"))
|
| stevenvanrossem | 700ed32 | 2016-05-06 14:35:54 +0200 | [diff] [blame] | 84 |
|
| stevenvanrossem | c5a536a | 2016-02-16 14:52:39 +0100 | [diff] [blame] | 85 | r = self.c.network_action_stop(
|
| 86 | #args.get("datacenter"),
|
| stevenvanrossem | ed711fd | 2016-04-11 16:59:29 +0200 | [diff] [blame] | 87 | vnf_src_name,
|
| 88 | vnf_dst_name,
|
| stevenvanrossem | 700ed32 | 2016-05-06 14:35:54 +0200 | [diff] [blame] | 89 | params)
|
| stevenvanrossem | c5a536a | 2016-02-16 14:52:39 +0100 | [diff] [blame] | 90 | pp.pprint(r)
|
| 91 |
|
| stevenvanrossem | ed711fd | 2016-04-11 16:59:29 +0200 | [diff] [blame] | 92 | def _parse_vnf_name(self, vnf_name_str):
|
| 93 | vnf_name = vnf_name_str.split(':')[0]
|
| 94 | return vnf_name
|
| 95 |
|
| 96 | def _parse_vnf_interface(self, vnf_name_str):
|
| 97 | try:
|
| 98 | vnf_interface = vnf_name_str.split(':')[1]
|
| 99 | except:
|
| 100 | vnf_interface = None
|
| 101 |
|
| 102 | return vnf_interface
|
| 103 |
|
| stevenvanrossem | a1e59c7 | 2016-05-06 14:32:39 +0200 | [diff] [blame] | 104 | def _create_dict(self, **kwargs):
|
| 105 | return kwargs
|
| stevenvanrossem | c5a536a | 2016-02-16 14:52:39 +0100 | [diff] [blame] | 106 |
|
| 107 | parser = argparse.ArgumentParser(description='son-emu network')
|
| 108 | parser.add_argument(
|
| 109 | "command",
|
| stevenvanrossem | 1719bc4 | 2016-05-12 11:52:08 +0200 | [diff] [blame] | 110 | choices=['add', 'remove'],
|
| 111 | help="Action to be executed.")
|
| stevenvanrossem | c5a536a | 2016-02-16 14:52:39 +0100 | [diff] [blame] | 112 | parser.add_argument(
|
| 113 | "--datacenter", "-d", dest="datacenter",
|
| 114 | help="Data center to in which the network action should be initiated")
|
| 115 | parser.add_argument(
|
| 116 | "--source", "-src", dest="source",
|
| 117 | help="vnf name of the source of the chain")
|
| 118 | parser.add_argument(
|
| 119 | "--destination", "-dst", dest="destination",
|
| 120 | help="vnf name of the destination of the chain")
|
| stevenvanrossem | 6b1d9b9 | 2016-05-02 13:10:40 +0200 | [diff] [blame] | 121 | parser.add_argument(
|
| 122 | "--weight", "-w", dest="weight",
|
| 123 | help="weight metric to calculate the path")
|
| stevenvanrossem | a1e59c7 | 2016-05-06 14:32:39 +0200 | [diff] [blame] | 124 | parser.add_argument(
|
| 125 | "--match", "-m", dest="match",
|
| 126 | help="string holding extra matches for the flow entries")
|
| stevenvanrossem | 23c4809 | 2016-05-06 17:21:12 +0200 | [diff] [blame] | 127 | parser.add_argument(
|
| 128 | "--bidirectional", "-b", dest="bidirectional",
|
| 129 | action='store_true',
|
| stevenvanrossem | 1719bc4 | 2016-05-12 11:52:08 +0200 | [diff] [blame] | 130 | help="add/remove the flow entries from src to dst and back")
|
| stevenvanrossem | 898a2af | 2016-05-06 18:28:57 +0200 | [diff] [blame] | 131 | parser.add_argument(
|
| 132 | "--cookie", "-c", dest="cookie",
|
| stevenvanrossem | 1719bc4 | 2016-05-12 11:52:08 +0200 | [diff] [blame] | 133 | help="cookie for this flow, as easy to use identifier (eg. per tenant/service)")
|
| stevenvanrossem | c5a536a | 2016-02-16 14:52:39 +0100 | [diff] [blame] | 134 |
|
| 135 | def main(argv):
|
| stevenvanrossem | c5a536a | 2016-02-16 14:52:39 +0100 | [diff] [blame] | 136 | args = vars(parser.parse_args(argv))
|
| 137 | c = ZeroRpcClient()
|
| 138 | c.execute_command(args)
|