25ac2477598197d101ca7aef9ac8848f140e0967
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).
31 from tabulate
import tabulate
35 pp
= pprint
.PrettyPrinter(indent
=4)
38 class ZeroRpcClient(object):
41 self
.c
= zerorpc
.Client(heartbeat
=None, timeout
=120) #heartbeat=None, timeout=120
42 self
.c
.connect("tcp://127.0.0.1:4242") # TODO hard coded for now. we'll change this later
45 def execute_command(self
, args
):
46 if getattr(self
, args
["command"]) is not None:
47 # call the local method with the same name as the command arg
48 getattr(self
, args
["command"])(args
)
50 print("Command not implemented.")
52 def start(self
, args
):
54 if args
.get("network") is not None:
55 nw_list
= self
._parse
_network
(args
.get("network"))
56 r
= self
.c
.compute_action_start(
57 args
.get("datacenter"),
61 args
.get("docker_command")
66 r
= self
.c
.compute_action_stop(
67 args
.get("datacenter"), args
.get("name"))
71 r
= self
.c
.compute_list(
72 args
.get("datacenter"))
75 # for each container add a line to the output table
81 if len(status
.get("network")) > 0:
82 eth0ip
= status
.get("network")[0].get("ip")
83 eth0status
= "up" if status
.get(
84 "network")[0].get("up") else "down"
85 table
.append([status
.get("datacenter"),
90 status
.get("state").get("Status")])
91 headers
= ["Datacenter",
97 print(tabulate(table
, headers
=headers
, tablefmt
="grid"))
99 def status(self
, args
):
100 r
= self
.c
.compute_status(
101 args
.get("datacenter"), args
.get("name"))
104 def profile(self
, args
):
106 if args
.get("network") is not None:
107 nw_list
= self
._parse
_network
(args
.get("network"))
109 params
= self
._create
_dict
(
111 command
=args
.get("docker_command"),
112 image
=args
.get("image"),
113 input=args
.get("input"),
114 output
=args
.get("output"))
116 for output
in self
.c
.compute_profile(
117 args
.get("datacenter"),
125 def _create_dict(self
, **kwargs
):
128 def _parse_network(self
, network_str
):
130 parse the options for all network interfaces of the vnf
131 :param network_str: (id=x,ip=x.x.x.x/x), ...
132 :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...]
135 networks
= network_str
[1:-1].split('),(')
137 nw_dict
= dict(tuple(e
.split('=')) for e
in nw
.split(','))
138 nw_list
.append(nw_dict
)
144 parser
= argparse
.ArgumentParser(description
='son-emu compute')
147 choices
=['start', 'stop', 'list', 'status', 'profile'],
148 help="Action to be executed.")
150 "--datacenter", "-d", dest
="datacenter",
151 help="Data center to in which the compute instance should be executed")
153 "--name", "-n", dest
="name",
154 help="Name of compute instance e.g. 'vnf1'")
156 "--image","-i", dest
="image",
157 help="Name of container image to be used e.g. 'ubuntu:trusty'")
159 "--dcmd", "-c", dest
="docker_command",
160 help="Startup command of the container e.g. './start.sh'")
162 "--net", dest
="network",
163 help="Network properties of a compute instance e.g. \
164 '(id=input,ip=10.0.10.3/24),(id=output,ip=10.0.10.4/24)' for multiple interfaces.")
166 "--input", "-in", dest
="input",
167 help="input interface of the vnf to profile")
169 "--output", "-out", dest
="output",
170 help="output interface of the vnf to profile")
174 args
= vars(parser
.parse_args(argv
))
176 c
.execute_command(args
)