blob: 7566f44c6472eb8693b402141aad8cfd7b34cbf7 [file] [log] [blame]
peusterm72f09882018-05-15 17:10:27 +02001# Copyright (c) 2015 SONATA-NFV and Paderborn University
2# ALL RIGHTS RESERVED.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16# Neither the name of the SONATA-NFV, Paderborn University
17# nor the names of its contributors may be used to endorse or promote
18# products derived from this software without specific prior written
19# permission.
20#
21# This work has been performed in the framework of the SONATA project,
22# funded by the European Commission under Grant number 671517 through
23# the Horizon 2020 and 5G-PPP programmes. The authors would like to
24# acknowledge the contributions of their colleagues of the SONATA
25# partner consortium (www.sonata-nfv.eu).
26from requests import put, delete
stevenvanrossem73efd192016-06-29 01:44:07 +020027import argparse
stevenvanrossem73efd192016-06-29 01:44:07 +020028
stevenvanrossem73efd192016-06-29 01:44:07 +020029
30class RestApiClient():
31
32 def __init__(self):
33 self.cmds = {}
34
35 def execute_command(self, args):
36 if getattr(self, args["command"]) is not None:
37 # call the local method with the same name as the command arg
38 getattr(self, args["command"])(args)
39 else:
40 print("Command not implemented.")
41
42 def add(self, args):
stevenvanrossem73efd192016-06-29 01:44:07 +020043 params = self._create_dict(
stevenvanrossemf693a3b2017-06-01 15:15:59 +020044 vnf_src_name=self._parse_vnf_name(args.get("source")),
peusterm72f09882018-05-15 17:10:27 +020045 vnf_dst_name=self._parse_vnf_name(args.get("destination")),
stevenvanrossem73efd192016-06-29 01:44:07 +020046 vnf_src_interface=self._parse_vnf_interface(args.get("source")),
peusterm72f09882018-05-15 17:10:27 +020047 vnf_dst_interface=self._parse_vnf_interface(
48 args.get("destination")),
stevenvanrossem73efd192016-06-29 01:44:07 +020049 weight=args.get("weight"),
50 match=args.get("match"),
51 bidirectional=args.get("bidirectional"),
peusterm76e052b2016-09-08 10:53:36 +020052 cookie=args.get("cookie"),
53 priority=args.get("priority"))
stevenvanrossem73efd192016-06-29 01:44:07 +020054
stevenvanrossemf693a3b2017-06-01 15:15:59 +020055 response = put("{0}/restapi/network".format(args.get("endpoint")),
stevenvanrossem1085e7e2017-06-01 16:37:52 +020056 params=params)
stevenvanrossemae588012017-06-06 10:33:19 +020057 print(self._nice_print(response.text))
stevenvanrossem73efd192016-06-29 01:44:07 +020058
59 def remove(self, args):
stevenvanrossem73efd192016-06-29 01:44:07 +020060 params = self._create_dict(
peusterm72f09882018-05-15 17:10:27 +020061 vnf_src_name=self._parse_vnf_name(args.get("source")),
62 vnf_dst_name=self._parse_vnf_name(args.get("destination")),
stevenvanrossem73efd192016-06-29 01:44:07 +020063 vnf_src_interface=self._parse_vnf_interface(args.get("source")),
peusterm72f09882018-05-15 17:10:27 +020064 vnf_dst_interface=self._parse_vnf_interface(
65 args.get("destination")),
stevenvanrossem73efd192016-06-29 01:44:07 +020066 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]
peusterm72f09882018-05-15 17:10:27 +020083 except BaseException:
stevenvanrossem73efd192016-06-29 01:44:07 +020084 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):
peusterm72f09882018-05-15 17:10:27 +020092 # some modules seem to return unicode strings where newlines, other
93 # special characters are escaped
stevenvanrossemae588012017-06-06 10:33:19 +020094 text = str(text).replace('\\n', '\n')
95 text = str(text).replace('\\"', '"')
96 return text
97
peusterm72f09882018-05-15 17:10:27 +020098
stevenvanrossemf693a3b2017-06-01 15:15:59 +020099parser = argparse.ArgumentParser(description='son-emu-cli network')
stevenvanrossem73efd192016-06-29 01:44:07 +0200100parser.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(
stevenvanrossemae588012017-06-06 10:33:19 +0200117 "--priority", "-p", dest="priority", default="1000",
peusterm76e052b2016-09-08 10:53:36 +0200118 help="priority of flow rule")
119parser.add_argument(
stevenvanrossem73efd192016-06-29 01:44:07 +0200120 "--match", "-m", dest="match",
121 help="string holding extra matches for the flow entries")
122parser.add_argument(
peusterma3070712016-07-18 10:58:31 +0200123 "--bidirectional", "-b", dest="bidirectional", action='store_true',
stevenvanrossem73efd192016-06-29 01:44:07 +0200124 help="add/remove the flow entries from src to dst and back")
125parser.add_argument(
stevenvanrossemae588012017-06-06 10:33:19 +0200126 "--cookie", "-c", dest="cookie", default="10",
stevenvanrossem73efd192016-06-29 01:44:07 +0200127 help="cookie for this flow, as easy to use identifier (eg. per tenant/service)")
128parser.add_argument(
129 "--endpoint", "-e", dest="endpoint",
peusterm0a336cc2016-07-04 09:15:47 +0200130 default="http://127.0.0.1:5001",
stevenvanrossemf693a3b2017-06-01 15:15:59 +0200131 help="REST API endpoint of son-emu (default:http://127.0.0.1:5001)")
stevenvanrossem73efd192016-06-29 01:44:07 +0200132
peusterm72f09882018-05-15 17:10:27 +0200133
stevenvanrossem73efd192016-06-29 01:44:07 +0200134def main(argv):
135 args = vars(parser.parse_args(argv))
136 c = RestApiClient()
peusterm76e052b2016-09-08 10:53:36 +0200137 c.execute_command(args)