blob: 82fe99ff1ff3523655cf097342d30bc2d42c4ea9 [file] [log] [blame]
peusterm79ef6ae2016-07-08 13:53:57 +02001"""
2Copyright (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).
27"""
stevenvanrossem73efd192016-06-29 01:44:07 +020028from requests import get,put, delete
stevenvanrossem73efd192016-06-29 01:44:07 +020029import argparse
stevenvanrossem73efd192016-06-29 01:44:07 +020030
stevenvanrossem73efd192016-06-29 01:44:07 +020031
32class RestApiClient():
33
34 def __init__(self):
35 self.cmds = {}
36
37 def execute_command(self, args):
38 if getattr(self, args["command"]) is not None:
39 # call the local method with the same name as the command arg
40 getattr(self, args["command"])(args)
41 else:
42 print("Command not implemented.")
43
44 def add(self, args):
stevenvanrossem73efd192016-06-29 01:44:07 +020045 params = self._create_dict(
stevenvanrossemf693a3b2017-06-01 15:15:59 +020046 vnf_src_name=self._parse_vnf_name(args.get("source")),
47 vnf_dst_name = self._parse_vnf_name(args.get("destination")),
stevenvanrossem73efd192016-06-29 01:44:07 +020048 vnf_src_interface=self._parse_vnf_interface(args.get("source")),
49 vnf_dst_interface=self._parse_vnf_interface(args.get("destination")),
50 weight=args.get("weight"),
51 match=args.get("match"),
52 bidirectional=args.get("bidirectional"),
peusterm76e052b2016-09-08 10:53:36 +020053 cookie=args.get("cookie"),
54 priority=args.get("priority"))
stevenvanrossem73efd192016-06-29 01:44:07 +020055
stevenvanrossemf693a3b2017-06-01 15:15:59 +020056 response = put("{0}/restapi/network".format(args.get("endpoint")),
stevenvanrossem1085e7e2017-06-01 16:37:52 +020057 params=params)
stevenvanrossemae588012017-06-06 10:33:19 +020058 print(self._nice_print(response.text))
stevenvanrossem73efd192016-06-29 01:44:07 +020059
60 def remove(self, args):
stevenvanrossem73efd192016-06-29 01:44:07 +020061 params = self._create_dict(
stevenvanrossemf693a3b2017-06-01 15:15:59 +020062 vnf_src_name = self._parse_vnf_name(args.get("source")),
63 vnf_dst_name = self._parse_vnf_name(args.get("destination")),
stevenvanrossem73efd192016-06-29 01:44:07 +020064 vnf_src_interface=self._parse_vnf_interface(args.get("source")),
65 vnf_dst_interface=self._parse_vnf_interface(args.get("destination")),
66 weight=args.get("weight"),
67 match=args.get("match"),
68 bidirectional=args.get("bidirectional"),
peusterm76e052b2016-09-08 10:53:36 +020069 cookie=args.get("cookie"),
70 priority=args.get("priority"))
stevenvanrossem73efd192016-06-29 01:44:07 +020071
stevenvanrossemf693a3b2017-06-01 15:15:59 +020072 response = delete("{0}/restapi/network".format(args.get("endpoint")),
stevenvanrossem1085e7e2017-06-01 16:37:52 +020073 params=params)
stevenvanrossemae588012017-06-06 10:33:19 +020074 print(self._nice_print(response.text))
stevenvanrossem73efd192016-06-29 01:44:07 +020075
76 def _parse_vnf_name(self, vnf_name_str):
77 vnf_name = vnf_name_str.split(':')[0]
78 return vnf_name
79
80 def _parse_vnf_interface(self, vnf_name_str):
81 try:
82 vnf_interface = vnf_name_str.split(':')[1]
83 except:
84 vnf_interface = None
85
86 return vnf_interface
87
88 def _create_dict(self, **kwargs):
89 return kwargs
90
stevenvanrossemae588012017-06-06 10:33:19 +020091 def _nice_print(self, text):
92 # some modules seem to return unicode strings where newlines, other special characters are escaped
93 text = str(text).replace('\\n', '\n')
94 text = str(text).replace('\\"', '"')
95 return text
96
stevenvanrossemf693a3b2017-06-01 15:15:59 +020097parser = argparse.ArgumentParser(description='son-emu-cli network')
stevenvanrossem73efd192016-06-29 01:44:07 +020098parser.add_argument(
99 "command",
100 choices=['add', 'remove'],
101 help="Action to be executed.")
102parser.add_argument(
103 "--datacenter", "-d", dest="datacenter",
104 help="Data center to in which the network action should be initiated")
105parser.add_argument(
106 "--source", "-src", dest="source",
107 help="vnf name of the source of the chain")
108parser.add_argument(
109 "--destination", "-dst", dest="destination",
110 help="vnf name of the destination of the chain")
111parser.add_argument(
112 "--weight", "-w", dest="weight",
stevenvanrossem9c8a4122016-07-16 03:23:13 +0200113 help="weight edge attribute to calculate the path")
stevenvanrossem73efd192016-06-29 01:44:07 +0200114parser.add_argument(
stevenvanrossemae588012017-06-06 10:33:19 +0200115 "--priority", "-p", dest="priority", default="1000",
peusterm76e052b2016-09-08 10:53:36 +0200116 help="priority of flow rule")
117parser.add_argument(
stevenvanrossem73efd192016-06-29 01:44:07 +0200118 "--match", "-m", dest="match",
119 help="string holding extra matches for the flow entries")
120parser.add_argument(
peusterma3070712016-07-18 10:58:31 +0200121 "--bidirectional", "-b", dest="bidirectional", action='store_true',
stevenvanrossem73efd192016-06-29 01:44:07 +0200122 help="add/remove the flow entries from src to dst and back")
123parser.add_argument(
stevenvanrossemae588012017-06-06 10:33:19 +0200124 "--cookie", "-c", dest="cookie", default="10",
stevenvanrossem73efd192016-06-29 01:44:07 +0200125 help="cookie for this flow, as easy to use identifier (eg. per tenant/service)")
126parser.add_argument(
127 "--endpoint", "-e", dest="endpoint",
peusterm0a336cc2016-07-04 09:15:47 +0200128 default="http://127.0.0.1:5001",
stevenvanrossemf693a3b2017-06-01 15:15:59 +0200129 help="REST API endpoint of son-emu (default:http://127.0.0.1:5001)")
stevenvanrossem73efd192016-06-29 01:44:07 +0200130
131def main(argv):
132 args = vars(parser.parse_args(argv))
133 c = RestApiClient()
peusterm76e052b2016-09-08 10:53:36 +0200134 c.execute_command(args)