2a055a3638d0932cb13284de244a7f321a928467
2 Copyright (c) 2015 SONATA-NFV and Paderborn University
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
29 from tabulate
import tabulate
34 pp
= pprint
.PrettyPrinter(indent
=4)
36 class RestApiClient():
41 def execute_command(self
, args
):
42 if getattr(self
, args
["command"]) is not None:
43 # call the local method with the same name as the command arg
44 getattr(self
, args
["command"])(args
)
46 print("Command not implemented.")
48 def start(self
, args
):
51 if args
.get("network") is not None:
52 nw_list
= self
._parse
_network
(args
.get("network"))
53 req
= {'image':args
.get("image"),
54 'command':args
.get("docker_command"),
57 response
= put("%s/restapi/compute/%s/%s/start" %
58 (args
.get("endpoint"),
59 args
.get("datacenter"),
61 json
= json
.dumps(req
))
62 pp
.pprint(response
.json())
66 response
= get("%s/restapi/compute/%s/%s/stop" %
67 (args
.get("endpoint"),
68 args
.get("datacenter"),
70 pp
.pprint(response
.json())
74 list = get('%s/restapi/compute/%s' % (args
.get("endpoint"),args
.get('datacenter'))).json()
78 # for each container add a line to the output table
84 if len(status
.get("network")) > 0:
85 eth0ip
= status
.get("network")[0].get("ip")
86 eth0status
= "up" if status
.get(
87 "network")[0].get("up") else "down"
88 table
.append([status
.get("datacenter"),
93 status
.get("state").get("Status")])
95 headers
= ["Datacenter",
101 print(tabulate(table
, headers
=headers
, tablefmt
="grid"))
103 def status(self
,args
):
105 list = get("%s/restapi/compute/%s/%s" %
106 (args
.get("endpoint"),
107 args
.get("datacenter"),
108 args
.get("name"))).json()
113 def _parse_network(self
, network_str
):
115 parse the options for all network interfaces of the vnf
116 :param network_str: (id=x,ip=x.x.x.x/x), ...
117 :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...]
120 networks
= network_str
[1:-1].split('),(')
122 nw_dict
= dict(tuple(e
.split('=')) for e
in nw
.split(','))
123 nw_list
.append(nw_dict
)
128 parser
= argparse
.ArgumentParser(description
='son-emu datacenter')
131 choices
=['start', 'stop', 'list', 'status'],
132 help="Action to be executed.")
134 "--datacenter", "-d", dest
="datacenter",
135 help="Data center to which the command should be applied.")
137 "--name", "-n", dest
="name",
138 help="Name of compute instance e.g. 'vnf1'.")
140 "--image","-i", dest
="image",
141 help="Name of container image to be used e.g. 'ubuntu:trusty'")
143 "--dcmd", "-c", dest
="docker_command",
144 help="Startup command of the container e.g. './start.sh'")
146 "--net", dest
="network",
147 help="Network properties of a compute instance e.g. \
148 '(id=input,ip=10.0.10.3/24),(id=output,ip=10.0.10.4/24)' for multiple interfaces.")
150 "--endpoint", "-e", dest
="endpoint",
151 default
="http://127.0.0.1:5001",
152 help="UUID of the plugin to be manipulated.")
155 args
= vars(parser
.parse_args(argv
))
157 c
.execute_command(args
)