blob: 960e387fc823a8e7699d783b584c555c97396d1d [file] [log] [blame]
stevenvanrossemc5a536a2016-02-16 14:52:39 +01001"""
peusterm79ef6ae2016-07-08 13:53:57 +02002Copyright (c) 2015 SONATA-NFV
3ALL RIGHTS RESERVED.
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
18nor the names of its contributors may be used to endorse or promote
19products derived from this software without specific prior written
20permission.
21
22This work has been performed in the framework of the SONATA project,
23funded by the European Commission under Grant number 671517 through
24the Horizon 2020 and 5G-PPP programmes. The authors would like to
25acknowledge the contributions of their colleagues of the SONATA
26partner consortium (www.sonata-nfv.eu).
stevenvanrossemc5a536a2016-02-16 14:52:39 +010027"""
28
29import argparse
30import pprint
31from tabulate import tabulate
32import zerorpc
33
34
35pp = pprint.PrettyPrinter(indent=4)
36
37class 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:
stevenvanrossem49378152016-05-19 11:33:48 +020051 print("Command not implemented.")
stevenvanrossemc5a536a2016-02-16 14:52:39 +010052
53 def add(self, args):
stevenvanrossemed711fd2016-04-11 16:59:29 +020054 vnf_src_name = self._parse_vnf_name(args.get("source"))
stevenvanrossemed711fd2016-04-11 16:59:29 +020055 vnf_dst_name = self._parse_vnf_name(args.get("destination"))
stevenvanrossema1e59c72016-05-06 14:32:39 +020056
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"),
stevenvanrossem23c48092016-05-06 17:21:12 +020061 match=args.get("match"),
stevenvanrossem898a2af2016-05-06 18:28:57 +020062 bidirectional=args.get("bidirectional"),
63 cookie=args.get("cookie"))
stevenvanrossema1e59c72016-05-06 14:32:39 +020064
stevenvanrossem307aa1f2016-05-06 10:35:15 +020065 # note zerorpc does not support named arguments
stevenvanrossemc5a536a2016-02-16 14:52:39 +010066 r = self.c.network_action_start(
67 #args.get("datacenter"),
stevenvanrossemed711fd2016-04-11 16:59:29 +020068 vnf_src_name,
69 vnf_dst_name,
stevenvanrossema1e59c72016-05-06 14:32:39 +020070 params)
stevenvanrossemc5a536a2016-02-16 14:52:39 +010071 pp.pprint(r)
72
73 def remove(self, args):
stevenvanrossemed711fd2016-04-11 16:59:29 +020074 vnf_src_name = self._parse_vnf_name(args.get("source"))
stevenvanrossemed711fd2016-04-11 16:59:29 +020075 vnf_dst_name = self._parse_vnf_name(args.get("destination"))
stevenvanrossem700ed322016-05-06 14:35:54 +020076
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"),
stevenvanrossem898a2af2016-05-06 18:28:57 +020081 match=args.get("match"),
stevenvanrossem27b6d952016-05-10 16:37:57 +020082 bidirectional=args.get("bidirectional"),
stevenvanrossem898a2af2016-05-06 18:28:57 +020083 cookie=args.get("cookie"))
stevenvanrossem700ed322016-05-06 14:35:54 +020084
stevenvanrossemc5a536a2016-02-16 14:52:39 +010085 r = self.c.network_action_stop(
86 #args.get("datacenter"),
stevenvanrossemed711fd2016-04-11 16:59:29 +020087 vnf_src_name,
88 vnf_dst_name,
stevenvanrossem700ed322016-05-06 14:35:54 +020089 params)
stevenvanrossemc5a536a2016-02-16 14:52:39 +010090 pp.pprint(r)
91
stevenvanrossemed711fd2016-04-11 16:59:29 +020092 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
stevenvanrossema1e59c72016-05-06 14:32:39 +0200104 def _create_dict(self, **kwargs):
105 return kwargs
stevenvanrossemc5a536a2016-02-16 14:52:39 +0100106
107parser = argparse.ArgumentParser(description='son-emu network')
108parser.add_argument(
109 "command",
stevenvanrossem1719bc42016-05-12 11:52:08 +0200110 choices=['add', 'remove'],
111 help="Action to be executed.")
stevenvanrossemc5a536a2016-02-16 14:52:39 +0100112parser.add_argument(
113 "--datacenter", "-d", dest="datacenter",
114 help="Data center to in which the network action should be initiated")
115parser.add_argument(
116 "--source", "-src", dest="source",
117 help="vnf name of the source of the chain")
118parser.add_argument(
119 "--destination", "-dst", dest="destination",
120 help="vnf name of the destination of the chain")
stevenvanrossem6b1d9b92016-05-02 13:10:40 +0200121parser.add_argument(
122 "--weight", "-w", dest="weight",
123 help="weight metric to calculate the path")
stevenvanrossema1e59c72016-05-06 14:32:39 +0200124parser.add_argument(
125 "--match", "-m", dest="match",
126 help="string holding extra matches for the flow entries")
stevenvanrossem23c48092016-05-06 17:21:12 +0200127parser.add_argument(
128 "--bidirectional", "-b", dest="bidirectional",
129 action='store_true',
stevenvanrossem1719bc42016-05-12 11:52:08 +0200130 help="add/remove the flow entries from src to dst and back")
stevenvanrossem898a2af2016-05-06 18:28:57 +0200131parser.add_argument(
132 "--cookie", "-c", dest="cookie",
stevenvanrossem1719bc42016-05-12 11:52:08 +0200133 help="cookie for this flow, as easy to use identifier (eg. per tenant/service)")
stevenvanrossemc5a536a2016-02-16 14:52:39 +0100134
135def main(argv):
stevenvanrossemc5a536a2016-02-16 14:52:39 +0100136 args = vars(parser.parse_args(argv))
137 c = ZeroRpcClient()
138 c.execute_command(args)