2 Copyright (c) 2015 SONATA-NFV
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
9 http://www.apache.org/licenses/LICENSE-2.0
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.
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
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).
28 from requests
import get
,put
, delete
32 pp
= pprint
.PrettyPrinter(indent
=4)
34 class RestApiClient():
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
)
44 print("Command not implemented.")
47 vnf_src_name
= self
._parse
_vnf
_name
(args
.get("source"))
48 vnf_dst_name
= self
._parse
_vnf
_name
(args
.get("destination"))
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 priority
=args
.get("priority"))
59 response
= put("%s/restapi/network/%s/%s" %
60 (args
.get("endpoint"),
64 pp
.pprint(response
.json())
66 def remove(self
, args
):
67 vnf_src_name
= self
._parse
_vnf
_name
(args
.get("source"))
68 vnf_dst_name
= self
._parse
_vnf
_name
(args
.get("destination"))
70 params
= self
._create
_dict
(
71 vnf_src_interface
=self
._parse
_vnf
_interface
(args
.get("source")),
72 vnf_dst_interface
=self
._parse
_vnf
_interface
(args
.get("destination")),
73 weight
=args
.get("weight"),
74 match
=args
.get("match"),
75 bidirectional
=args
.get("bidirectional"),
76 cookie
=args
.get("cookie"),
77 priority
=args
.get("priority"))
79 response
= delete("%s/restapi/network/%s/%s" %
80 (args
.get("endpoint"),
84 pp
.pprint(response
.json())
86 def _parse_vnf_name(self
, vnf_name_str
):
87 vnf_name
= vnf_name_str
.split(':')[0]
90 def _parse_vnf_interface(self
, vnf_name_str
):
92 vnf_interface
= vnf_name_str
.split(':')[1]
98 def _create_dict(self
, **kwargs
):
101 parser
= argparse
.ArgumentParser(description
='son-emu network')
104 choices
=['add', 'remove'],
105 help="Action to be executed.")
107 "--datacenter", "-d", dest
="datacenter",
108 help="Data center to in which the network action should be initiated")
110 "--source", "-src", dest
="source",
111 help="vnf name of the source of the chain")
113 "--destination", "-dst", dest
="destination",
114 help="vnf name of the destination of the chain")
116 "--weight", "-w", dest
="weight",
117 help="weight edge attribute to calculate the path")
119 "--priority", "-p", dest
="priority", default
="0",
120 help="priority of flow rule")
122 "--match", "-m", dest
="match",
123 help="string holding extra matches for the flow entries")
125 "--bidirectional", "-b", dest
="bidirectional", action
='store_true',
126 help="add/remove the flow entries from src to dst and back")
128 "--cookie", "-c", dest
="cookie",
129 help="cookie for this flow, as easy to use identifier (eg. per tenant/service)")
131 "--endpoint", "-e", dest
="endpoint",
132 default
="http://127.0.0.1:5001",
133 help="UUID of the plugin to be manipulated.")
136 args
= vars(parser
.parse_args(argv
))
138 c
.execute_command(args
)