1 # Copyright (c) 2015 SONATA-NFV and Paderborn University
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
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).
26 from requests
import put
, delete
30 class RestApiClient():
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
)
40 print("Command not implemented.")
43 params
= self
._create
_dict
(
44 vnf_src_name
=self
._parse
_vnf
_name
(args
.get("source")),
45 vnf_dst_name
=self
._parse
_vnf
_name
(args
.get("destination")),
46 vnf_src_interface
=self
._parse
_vnf
_interface
(args
.get("source")),
47 vnf_dst_interface
=self
._parse
_vnf
_interface
(
48 args
.get("destination")),
49 weight
=args
.get("weight"),
50 match
=args
.get("match"),
51 bidirectional
=args
.get("bidirectional"),
52 cookie
=args
.get("cookie"),
53 priority
=args
.get("priority"))
55 response
= put("{0}/restapi/network".format(args
.get("endpoint")),
57 print(self
._nice
_print
(response
.text
))
59 def remove(self
, args
):
60 params
= self
._create
_dict
(
61 vnf_src_name
=self
._parse
_vnf
_name
(args
.get("source")),
62 vnf_dst_name
=self
._parse
_vnf
_name
(args
.get("destination")),
63 vnf_src_interface
=self
._parse
_vnf
_interface
(args
.get("source")),
64 vnf_dst_interface
=self
._parse
_vnf
_interface
(
65 args
.get("destination")),
66 weight
=args
.get("weight"),
67 match
=args
.get("match"),
68 bidirectional
=args
.get("bidirectional"),
69 cookie
=args
.get("cookie"),
70 priority
=args
.get("priority"))
72 response
= delete("{0}/restapi/network".format(args
.get("endpoint")),
74 print(self
._nice
_print
(response
.text
))
76 def _parse_vnf_name(self
, vnf_name_str
):
77 vnf_name
= vnf_name_str
.split(':')[0]
80 def _parse_vnf_interface(self
, vnf_name_str
):
82 vnf_interface
= vnf_name_str
.split(':')[1]
88 def _create_dict(self
, **kwargs
):
91 def _nice_print(self
, text
):
92 # some modules seem to return unicode strings where newlines, other
93 # special characters are escaped
94 text
= str(text
).replace('\\n', '\n')
95 text
= str(text
).replace('\\"', '"')
99 parser
= argparse
.ArgumentParser(description
='son-emu-cli network')
102 choices
=['add', 'remove'],
103 help="Action to be executed.")
105 "--datacenter", "-d", dest
="datacenter",
106 help="Data center to in which the network action should be initiated")
108 "--source", "-src", dest
="source",
109 help="vnf name of the source of the chain")
111 "--destination", "-dst", dest
="destination",
112 help="vnf name of the destination of the chain")
114 "--weight", "-w", dest
="weight",
115 help="weight edge attribute to calculate the path")
117 "--priority", "-p", dest
="priority", default
="1000",
118 help="priority of flow rule")
120 "--match", "-m", dest
="match",
121 help="string holding extra matches for the flow entries")
123 "--bidirectional", "-b", dest
="bidirectional", action
='store_true',
124 help="add/remove the flow entries from src to dst and back")
126 "--cookie", "-c", dest
="cookie", default
="10",
127 help="cookie for this flow, as easy to use identifier (eg. per tenant/service)")
129 "--endpoint", "-e", dest
="endpoint",
130 default
="http://127.0.0.1:5001",
131 help="REST API endpoint of son-emu (default:http://127.0.0.1:5001)")
135 args
= vars(parser
.parse_args(argv
))
137 c
.execute_command(args
)