blob: 2a055a3638d0932cb13284de244a7f321a928467 [file] [log] [blame]
peusterm79ef6ae2016-07-08 13:53:57 +02001"""
2Copyright (c) 2015 SONATA-NFV and Paderborn University
3ALL RIGHTS RESERVED.
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
18nor the names of its contributors may be used to endorse or promote
19products derived from this software without specific prior written
20permission.
21
22This work has been performed in the framework of the SONATA project,
23funded by the European Commission under Grant number 671517 through
24the Horizon 2020 and 5G-PPP programmes. The authors would like to
25acknowledge the contributions of their colleagues of the SONATA
26partner consortium (www.sonata-nfv.eu).
27"""
hadik3r237d3f52016-06-27 17:57:49 +020028from requests import get,put
29from tabulate import tabulate
30import pprint
31import argparse
32import json
33
34pp = pprint.PrettyPrinter(indent=4)
35
36class RestApiClient():
37
38 def __init__(self):
39 self.cmds = {}
40
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)
45 else:
46 print("Command not implemented.")
47
48 def start(self, args):
49
50 nw_list = list()
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"),
55 'network':nw_list}
56
stevenvanrossem73efd192016-06-29 01:44:07 +020057 response = put("%s/restapi/compute/%s/%s/start" %
hadik3r237d3f52016-06-27 17:57:49 +020058 (args.get("endpoint"),
59 args.get("datacenter"),
60 args.get("name")),
61 json = json.dumps(req))
stevenvanrossem73efd192016-06-29 01:44:07 +020062 pp.pprint(response.json())
63
hadik3r237d3f52016-06-27 17:57:49 +020064 def stop(self, args):
65
stevenvanrossem73efd192016-06-29 01:44:07 +020066 response = get("%s/restapi/compute/%s/%s/stop" %
hadik3r237d3f52016-06-27 17:57:49 +020067 (args.get("endpoint"),
68 args.get("datacenter"),
69 args.get("name")))
stevenvanrossem73efd192016-06-29 01:44:07 +020070 pp.pprint(response.json())
hadik3r237d3f52016-06-27 17:57:49 +020071
72 def list(self,args):
73
74 list = get('%s/restapi/compute/%s' % (args.get("endpoint"),args.get('datacenter'))).json()
75
76 table = []
77 for c in list:
78 # for each container add a line to the output table
79 if len(c) > 1:
80 name = c[0]
81 status = c[1]
82 eth0ip = None
83 eth0status = "down"
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"),
89 name,
90 status.get("image"),
91 eth0ip,
92 eth0status,
93 status.get("state").get("Status")])
94
95 headers = ["Datacenter",
96 "Container",
97 "Image",
98 "eth0 IP",
99 "eth0 status",
100 "Status"]
101 print(tabulate(table, headers=headers, tablefmt="grid"))
102
103 def status(self,args):
104
105 list = get("%s/restapi/compute/%s/%s" %
106 (args.get("endpoint"),
107 args.get("datacenter"),
108 args.get("name"))).json()
109 pp.pprint(list)
110
111
112
113 def _parse_network(self, network_str):
114 '''
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"}, ...]
118 '''
119 nw_list = list()
120 networks = network_str[1:-1].split('),(')
121 for nw in networks:
122 nw_dict = dict(tuple(e.split('=')) for e in nw.split(','))
123 nw_list.append(nw_dict)
124
125 return nw_list
126
127
128parser = argparse.ArgumentParser(description='son-emu datacenter')
129parser.add_argument(
130 "command",
hadik3rbe81adb2016-06-27 19:03:36 +0200131 choices=['start', 'stop', 'list', 'status'],
hadik3r237d3f52016-06-27 17:57:49 +0200132 help="Action to be executed.")
133parser.add_argument(
134 "--datacenter", "-d", dest="datacenter",
135 help="Data center to which the command should be applied.")
136parser.add_argument(
137 "--name", "-n", dest="name",
138 help="Name of compute instance e.g. 'vnf1'.")
139parser.add_argument(
140 "--image","-i", dest="image",
141 help="Name of container image to be used e.g. 'ubuntu:trusty'")
142parser.add_argument(
143 "--dcmd", "-c", dest="docker_command",
144 help="Startup command of the container e.g. './start.sh'")
145parser.add_argument(
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.")
149parser.add_argument(
hadik3r237d3f52016-06-27 17:57:49 +0200150 "--endpoint", "-e", dest="endpoint",
peusterm0a336cc2016-07-04 09:15:47 +0200151 default="http://127.0.0.1:5001",
hadik3r237d3f52016-06-27 17:57:49 +0200152 help="UUID of the plugin to be manipulated.")
153
154def main(argv):
155 args = vars(parser.parse_args(argv))
156 c = RestApiClient()
157 c.execute_command(args)