Merge pull request #133 from mpeuster/master
[osm/vim-emu.git] / src / emuvim / cli / network.py
1 """
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).
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:
51 print("Command not implemented.")
52
53 def add(self, args):
54 vnf_src_name = self._parse_vnf_name(args.get("source"))
55 vnf_dst_name = self._parse_vnf_name(args.get("destination"))
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"),
61 match=args.get("match"),
62 bidirectional=args.get("bidirectional"),
63 cookie=args.get("cookie"))
64
65 # note zerorpc does not support named arguments
66 r = self.c.network_action_start(
67 #args.get("datacenter"),
68 vnf_src_name,
69 vnf_dst_name,
70 params)
71 pp.pprint(r)
72
73 def remove(self, args):
74 vnf_src_name = self._parse_vnf_name(args.get("source"))
75 vnf_dst_name = self._parse_vnf_name(args.get("destination"))
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"),
81 match=args.get("match"),
82 bidirectional=args.get("bidirectional"),
83 cookie=args.get("cookie"))
84
85 r = self.c.network_action_stop(
86 #args.get("datacenter"),
87 vnf_src_name,
88 vnf_dst_name,
89 params)
90 pp.pprint(r)
91
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
104 def _create_dict(self, **kwargs):
105 return kwargs
106
107 parser = argparse.ArgumentParser(description='son-emu network')
108 parser.add_argument(
109 "command",
110 choices=['add', 'remove'],
111 help="Action to be executed.")
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")
121 parser.add_argument(
122 "--weight", "-w", dest="weight",
123 help="weight metric to calculate the path")
124 parser.add_argument(
125 "--match", "-m", dest="match",
126 help="string holding extra matches for the flow entries")
127 parser.add_argument(
128 "--bidirectional", "-b", dest="bidirectional",
129 action='store_true',
130 help="add/remove the flow entries from src to dst and back")
131 parser.add_argument(
132 "--cookie", "-c", dest="cookie",
133 help="cookie for this flow, as easy to use identifier (eg. per tenant/service)")
134
135 def main(argv):
136 args = vars(parser.parse_args(argv))
137 c = ZeroRpcClient()
138 c.execute_command(args)