blob: 86ed92b28b23414f0c72d84c1140ec6d24a2eb06 [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 pprint
30import argparse
stevenvanrossem73efd192016-06-29 01:44:07 +020031
32pp = pprint.PrettyPrinter(indent=4)
33
34class RestApiClient():
35
36 def __init__(self):
37 self.cmds = {}
38
39 def execute_command(self, args):
40 if getattr(self, args["command"]) is not None:
41 # call the local method with the same name as the command arg
42 getattr(self, args["command"])(args)
43 else:
44 print("Command not implemented.")
45
46 def add(self, args):
47 vnf_src_name = self._parse_vnf_name(args.get("source"))
48 vnf_dst_name = self._parse_vnf_name(args.get("destination"))
49
50 params = self._create_dict(
51 vnf_src_interface=self._parse_vnf_interface(args.get("source")),
52 vnf_dst_interface=self._parse_vnf_interface(args.get("destination")),
53 weight=args.get("weight"),
54 match=args.get("match"),
55 bidirectional=args.get("bidirectional"),
56 cookie=args.get("cookie"))
57
58 response = put("%s/restapi/network/%s/%s" %
59 (args.get("endpoint"),
60 vnf_src_name,
61 vnf_dst_name),
stevenvanrossemff6b4042016-07-14 20:51:37 +020062 json=params)
stevenvanrossem73efd192016-06-29 01:44:07 +020063 pp.pprint(response.json())
64
65 def remove(self, args):
66 vnf_src_name = self._parse_vnf_name(args.get("source"))
67 vnf_dst_name = self._parse_vnf_name(args.get("destination"))
68
69 params = self._create_dict(
70 vnf_src_interface=self._parse_vnf_interface(args.get("source")),
71 vnf_dst_interface=self._parse_vnf_interface(args.get("destination")),
72 weight=args.get("weight"),
73 match=args.get("match"),
74 bidirectional=args.get("bidirectional"),
75 cookie=args.get("cookie"))
76
77 response = delete("%s/restapi/network/%s/%s" %
78 (args.get("endpoint"),
79 vnf_src_name,
80 vnf_dst_name),
stevenvanrossemff6b4042016-07-14 20:51:37 +020081 json=params)
stevenvanrossem73efd192016-06-29 01:44:07 +020082 pp.pprint(response.json())
83
84 def _parse_vnf_name(self, vnf_name_str):
85 vnf_name = vnf_name_str.split(':')[0]
86 return vnf_name
87
88 def _parse_vnf_interface(self, vnf_name_str):
89 try:
90 vnf_interface = vnf_name_str.split(':')[1]
91 except:
92 vnf_interface = None
93
94 return vnf_interface
95
96 def _create_dict(self, **kwargs):
97 return kwargs
98
99parser = argparse.ArgumentParser(description='son-emu network')
100parser.add_argument(
101 "command",
102 choices=['add', 'remove'],
103 help="Action to be executed.")
104parser.add_argument(
105 "--datacenter", "-d", dest="datacenter",
106 help="Data center to in which the network action should be initiated")
107parser.add_argument(
108 "--source", "-src", dest="source",
109 help="vnf name of the source of the chain")
110parser.add_argument(
111 "--destination", "-dst", dest="destination",
112 help="vnf name of the destination of the chain")
113parser.add_argument(
114 "--weight", "-w", dest="weight",
stevenvanrossem9c8a4122016-07-16 03:23:13 +0200115 help="weight edge attribute to calculate the path")
stevenvanrossem73efd192016-06-29 01:44:07 +0200116parser.add_argument(
117 "--match", "-m", dest="match",
118 help="string holding extra matches for the flow entries")
119parser.add_argument(
120 "--bidirectional", "-b", dest="bidirectional",
stevenvanrossem73efd192016-06-29 01:44:07 +0200121 help="add/remove the flow entries from src to dst and back")
122parser.add_argument(
123 "--cookie", "-c", dest="cookie",
124 help="cookie for this flow, as easy to use identifier (eg. per tenant/service)")
125parser.add_argument(
126 "--endpoint", "-e", dest="endpoint",
peusterm0a336cc2016-07-04 09:15:47 +0200127 default="http://127.0.0.1:5001",
stevenvanrossem73efd192016-06-29 01:44:07 +0200128 help="UUID of the plugin to be manipulated.")
129
130def main(argv):
131 args = vars(parser.parse_args(argv))
132 c = RestApiClient()
133 c.execute_command(args)