dcs = {}
-class ComputeStart(Resource):
+
+class Compute(Resource):
"""
Start a new compute instance: A docker container (note: zerorpc does not support keyword arguments)
:param dc_label: name of the DC
global dcs
def put(self, dc_label, compute_name):
- logging.debug("API CALL: compute start")
- try:
- #check if json data is a dict
- data = request.json
- if data is None:
- data = {}
- elif type(data) is not dict:
- data = json.loads(request.json)
-
- network = data.get("network")
- nw_list = self._parse_network(network)
- image = data.get("image")
- command = data.get("docker_command")
+ # check if json data is a dict
+ data = request.json
+ if data is None:
+ data = {}
+ elif type(data) is not dict:
+ data = json.loads(request.json)
+
+ network = data.get("network")
+ nw_list = self._parse_network(network)
+ image = data.get("image")
+ command = data.get("docker_command")
+
+ try:
+ logging.debug("API CALL: compute start")
c = dcs.get(dc_label).startCompute(
- compute_name, image= image, command= command, network= nw_list)
+ compute_name, image=image, command=command, network=nw_list)
# return docker inspect dict
return c.getStatus(), 200
except Exception as ex:
logging.exception("API error.")
return ex.message, 500
+ def get(self, dc_label, compute_name):
+
+ logging.debug("API CALL: compute status")
+
+ try:
+ return dcs.get(dc_label).containers.get(compute_name).getStatus(), 200
+ except Exception as ex:
+ logging.exception("API error.")
+ return ex.message, 500
+
+ def delete(self, dc_label, compute_name):
+ logging.debug("API CALL: compute stop")
+ try:
+ return dcs.get(dc_label).stopCompute(compute_name), 200
+ except Exception as ex:
+ logging.exception("API error.")
+ return ex.message, 500
+
def _parse_network(self, network_str):
'''
parse the options for all network interfaces of the vnf
nw_list = list()
# TODO make this more robust with regex check
- if network_str is None :
+ if network_str is None:
return nw_list
networks = network_str[1:-1].split('),(')
return nw_list
-class ComputeStop(Resource):
-
- global dcs
-
- def get(self, dc_label, compute_name):
- logging.debug("API CALL: compute stop")
- try:
- return dcs.get(dc_label).stopCompute(compute_name), 200
- except Exception as ex:
- logging.exception("API error.")
- return ex.message,500
-
class ComputeList(Resource):
-
global dcs
def get(self, dc_label):
else:
# return list of compute nodes for specified DC
return [(c.name, c.getStatus())
- for c in dcs.get(dc_label).listCompute()], 200
+ for c in dcs.get(dc_label).listCompute()], 200
except Exception as ex:
logging.exception("API error.")
return ex.message, 500
-class ComputeStatus(Resource):
-
- global dcs
-
- def get(self, dc_label, compute_name):
-
- logging.debug("API CALL: compute list")
-
- try:
- return dcs.get(dc_label).containers.get(compute_name).getStatus(), 200
- except Exception as ex:
- logging.exception("API error.")
- return ex.message, 500
-
class DatacenterList(Resource):
-
global dcs
def get(self):
logging.exception("API error.")
return ex.message, 500
-class DatacenterStatus(Resource):
+class DatacenterStatus(Resource):
global dcs
def get(self, dc_label):
except Exception as ex:
logging.exception("API error.")
return ex.message, 500
-
-
# need to import total module to set its global variable dcs
import compute
-from compute import dcs, ComputeList, ComputeStart, ComputeStatus, ComputeStop, DatacenterList, DatacenterStatus
+from compute import dcs, ComputeList, Compute, DatacenterList, DatacenterStatus
# need to import total module to set its global variable net
import network
logging.basicConfig(level=logging.INFO)
-
class RestApiEndpoint(object):
-
"""
Simple API endpoint that offers a REST
interface. This interface will be used by the
self.api = Api(self.app)
# setup endpoints
+ self.api.add_resource(Compute, "/restapi/compute/<dc_label>/<compute_name>")
self.api.add_resource(ComputeList, "/restapi/compute/<dc_label>")
- self.api.add_resource(ComputeStart, "/restapi/compute/<dc_label>/<compute_name>/start")
- self.api.add_resource(ComputeStop, "/restapi/compute/<dc_label>/<compute_name>/stop")
- self.api.add_resource(ComputeStatus, "/restapi/compute/<dc_label>/<compute_name>")
- self.api.add_resource(DatacenterList, "/restapi/datacenter")
+
self.api.add_resource(DatacenterStatus, "/restapi/datacenter/<dc_label>")
+ self.api.add_resource(DatacenterList, "/restapi/datacenter")
- self.api.add_resource(NetworkAction, "/restapi/network/<vnf_src_name>/<vnf_dst_name>",)
+ self.api.add_resource(NetworkAction, "/restapi/network/<vnf_src_name>/<vnf_dst_name>", )
self.api.add_resource(MonitorInterfaceAction,
"/restapi/monitor/<vnf_name>/<metric>",
logging.debug("Created API endpoint %s(%s:%d)" % (self.__class__.__name__, self.ip, self.port))
-
def connectDatacenter(self, dc):
compute.dcs[dc.label] = dc
- logging.info("Connected DC(%s) to API endpoint %s(%s:%d)" % (dc.label, self.__class__.__name__, self.ip, self.port))
+ logging.info(
+ "Connected DC(%s) to API endpoint %s(%s:%d)" % (dc.label, self.__class__.__name__, self.ip, self.port))
def connectDCNetwork(self, DCnetwork):
-
network.net = DCnetwork
monitor.net = DCnetwork
self.__class__.__name__, self.ip, self.port))
def start(self):
- thread = threading.Thread(target= self._start_flask, args=())
+ thread = threading.Thread(target=self._start_flask, args=())
thread.daemon = True
thread.start()
logging.info("Started API endpoint @ http://%s:%d" % (self.ip, self.port))
-
def _start_flask(self):
- self.app.run(self.ip, self.port, debug=True, use_reloader=False)
\ No newline at end of file
+ self.app.run(self.ip, self.port, debug=True, use_reloader=False)
acknowledge the contributions of their colleagues of the SONATA
partner consortium (www.sonata-nfv.eu).
"""
-from requests import get,put
+from requests import get, put, delete
from tabulate import tabulate
import pprint
import argparse
pp = pprint.PrettyPrinter(indent=4)
-class RestApiClient():
+class RestApiClient():
def __init__(self):
self.cmds = {}
def start(self, args):
- req = {'image':args.get("image"),
- 'command':args.get("docker_command"),
- 'network':args.get("network")}
+ req = {'image': args.get("image"),
+ 'command': args.get("docker_command"),
+ 'network': args.get("network")}
- response = put("%s/restapi/compute/%s/%s/start" %
+ response = put("%s/restapi/compute/%s/%s" %
(args.get("endpoint"),
args.get("datacenter"),
args.get("name")),
- json = req)
+ json=req)
pp.pprint(response.json())
def stop(self, args):
- response = get("%s/restapi/compute/%s/%s/stop" %
- (args.get("endpoint"),
- args.get("datacenter"),
- args.get("name")))
+ response = delete("%s/restapi/compute/%s/%s" %
+ (args.get("endpoint"),
+ args.get("datacenter"),
+ args.get("name")))
pp.pprint(response.json())
- def list(self,args):
+ def list(self, args):
- list = get('%s/restapi/compute/%s' % (args.get("endpoint"),args.get('datacenter'))).json()
+ list = get('%s/restapi/compute/%s' % (args.get("endpoint"), args.get('datacenter'))).json()
table = []
for c in list:
"Status"]
print(tabulate(table, headers=headers, tablefmt="grid"))
- def status(self,args):
+ def status(self, args):
list = get("%s/restapi/compute/%s/%s" %
(args.get("endpoint"),
args.get("datacenter"),
args.get("name"))).json()
+
pp.pprint(list)
-parser = argparse.ArgumentParser(description='son-emu datacenter')
+parser = argparse.ArgumentParser(description='son-emu compute')
parser.add_argument(
"command",
choices=['start', 'stop', 'list', 'status'],
"--name", "-n", dest="name",
help="Name of compute instance e.g. 'vnf1'.")
parser.add_argument(
- "--image","-i", dest="image",
+ "--image", "-i", dest="image",
help="Name of container image to be used e.g. 'ubuntu:trusty'")
parser.add_argument(
"--dcmd", "-c", dest="docker_command",
default="http://127.0.0.1:5001",
help="UUID of the plugin to be manipulated.")
+
def main(argv):
args = vars(parser.parse_args(argv))
c = RestApiClient()
- c.execute_command(args)
\ No newline at end of file
+ c.execute_command(args)
from emuvim.dcemulator.node import EmulatorCompute
import ast
-class testRestApi( SimpleTestTopology ):
+
+class testRestApi(SimpleTestTopology):
"""
Tests to check the REST API endpoints of the emulator.
"""
def testRestApi(self):
-
# create network
self.createNet(nswitches=0, ndatacenter=2, nhosts=2, ndockers=0)
print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
subprocess.call("son-emu-cli compute status -d datacenter0 -n vnf1", shell=True)
output = subprocess.check_output("son-emu-cli compute status -d datacenter0 -n vnf1", shell=True)
- output= ast.literal_eval(output)
+ output = ast.literal_eval(output)
# check compute status result
self.assertTrue(output["name"] == "vnf1")