blob: 6e4a0839c64308ae3bb7e2afc32ecec574e921d2 [file] [log] [blame]
peusterm9c252b62016-01-06 16:59:53 +01001"""
2Distributed Cloud Emulator (dcemulator)
3(c) 2015 by Manuel Peuster <manuel.peuster@upb.de>
4"""
5
6import logging
7import threading
8import zerorpc
9
peustermbd44f4a2016-01-13 14:53:30 +010010logging.basicConfig(level=logging.INFO)
peusterm9c252b62016-01-06 16:59:53 +010011
12
13class ZeroRpcApiEndpoint(object):
peusterme4e89d32016-01-07 09:14:54 +010014 """
15 Simple API endpoint that offers a zerorpc-based
16 interface. This interface will be used by the
17 default command line client.
18 It can be used as a reference to implement
19 REST interfaces providing the same semantics,
20 like e.g. OpenStack compute API.
21 """
peusterm9c252b62016-01-06 16:59:53 +010022
23 def __init__(self, listenip, port):
24 self.dcs = {}
25 self.ip = listenip
26 self.port = port
27 logging.debug("Created API endpoint %s(%s:%d)" % (
28 self.__class__.__name__, self.ip, self.port))
29
30 def connectDatacenter(self, dc):
peusterma47db032016-02-04 14:55:29 +010031 self.dcs[dc.label] = dc
peusterm9c252b62016-01-06 16:59:53 +010032 logging.info("Connected DC(%s) to API endpoint %s(%s:%d)" % (
peusterma47db032016-02-04 14:55:29 +010033 dc.label, self.__class__.__name__, self.ip, self.port))
peusterm9c252b62016-01-06 16:59:53 +010034
35 def start(self):
36 thread = threading.Thread(target=self._api_server_thread, args=())
37 thread.daemon = True
38 thread.start()
39 logging.debug("Started API endpoint %s(%s:%d)" % (
40 self.__class__.__name__, self.ip, self.port))
41
42 def _api_server_thread(self):
43 s = zerorpc.Server(MultiDatacenterApi(self.dcs))
44 s.bind("tcp://%s:%d" % (self.ip, self.port))
45 s.run()
46
47
48class MultiDatacenterApi(object):
peusterme4e89d32016-01-07 09:14:54 +010049 """
50 Just pass through the corresponding request to the
51 selected data center. Do not implement provisioning
52 logic here because will will have multiple API
53 endpoint implementations at the end.
54 """
peusterm9c252b62016-01-06 16:59:53 +010055
56 def __init__(self, dcs):
57 self.dcs = dcs
58
stevenvanrossem8fbf9782016-02-17 11:40:23 +010059 def compute_action_start(self, dc_label, compute_name, image, command, network):
peusterm7f8e8402016-02-28 18:38:10 +010060 """
61 Start a new compute instance: A docker container
62 :param dc_label: name of the DC
63 :param compute_name: compute container name
64 :param image: image name
65 :param command: command to execute
stevenvanrossem14c89052016-04-10 23:49:59 +020066 :param network: list of all interface of the vnf, with their parameters (id=id1,ip=x.x.x.x/x),...
67 :return: networks list({"id":"input","ip": "10.0.0.254/8"}, {"id":"output","ip": "11.0.0.254/24"})
peusterm7f8e8402016-02-28 18:38:10 +010068 """
peusterm4e98b632016-01-12 14:08:07 +010069 # TODO what to return UUID / given name / internal name ?
peusterm70baaf82016-01-06 17:14:40 +010070 logging.debug("RPC CALL: compute start")
peusterm7aae6852016-01-12 14:53:18 +010071 try:
peusterm53504942016-02-04 16:09:28 +010072 c = self.dcs.get(dc_label).startCompute(
stevenvanrossem8fbf9782016-02-17 11:40:23 +010073 compute_name, image=image, command=command, network=network)
peustermfa4bcc72016-01-15 11:08:09 +010074 return str(c.name)
peusterm7aae6852016-01-12 14:53:18 +010075 except Exception as ex:
76 logging.exception("RPC error.")
77 return ex.message
peusterm9c252b62016-01-06 16:59:53 +010078
peusterm53504942016-02-04 16:09:28 +010079 def compute_action_stop(self, dc_label, compute_name):
peustermbd44f4a2016-01-13 14:53:30 +010080 logging.debug("RPC CALL: compute stop")
peusterm7aae6852016-01-12 14:53:18 +010081 try:
peusterm53504942016-02-04 16:09:28 +010082 return self.dcs.get(dc_label).stopCompute(compute_name)
peusterm7aae6852016-01-12 14:53:18 +010083 except Exception as ex:
84 logging.exception("RPC error.")
85 return ex.message
peusterm9c252b62016-01-06 16:59:53 +010086
peusterm53504942016-02-04 16:09:28 +010087 def compute_list(self, dc_label):
peustermbd44f4a2016-01-13 14:53:30 +010088 logging.debug("RPC CALL: compute list")
peusterm7aae6852016-01-12 14:53:18 +010089 try:
peusterm53504942016-02-04 16:09:28 +010090 if dc_label is None:
peusterm2ec74e12016-01-13 11:17:53 +010091 # return list with all compute nodes in all DCs
92 all_containers = []
93 for dc in self.dcs.itervalues():
94 all_containers += dc.listCompute()
95 return [(c.name, c.getStatus())
96 for c in all_containers]
97 else:
98 # return list of compute nodes for specified DC
99 return [(c.name, c.getStatus())
peusterm53504942016-02-04 16:09:28 +0100100 for c in self.dcs.get(dc_label).listCompute()]
peusterm7aae6852016-01-12 14:53:18 +0100101 except Exception as ex:
102 logging.exception("RPC error.")
103 return ex.message
peusterm4e98b632016-01-12 14:08:07 +0100104
peusterm53504942016-02-04 16:09:28 +0100105 def compute_status(self, dc_label, compute_name):
peustermbd44f4a2016-01-13 14:53:30 +0100106 logging.debug("RPC CALL: compute status")
peusterm7aae6852016-01-12 14:53:18 +0100107 try:
108 return self.dcs.get(
peusterm53504942016-02-04 16:09:28 +0100109 dc_label).containers.get(compute_name).getStatus()
peusterm7aae6852016-01-12 14:53:18 +0100110 except Exception as ex:
111 logging.exception("RPC error.")
112 return ex.message
peustermd313dc12016-02-04 15:36:02 +0100113
114 def datacenter_list(self):
115 logging.debug("RPC CALL: datacenter list")
116 try:
117 return [d.getStatus() for d in self.dcs.itervalues()]
118 except Exception as ex:
119 logging.exception("RPC error.")
120 return ex.message
peusterm53504942016-02-04 16:09:28 +0100121
122 def datacenter_status(self, dc_label):
123 logging.debug("RPC CALL: datacenter status")
124 try:
125 return self.dcs.get(dc_label).getStatus()
126 except Exception as ex:
127 logging.exception("RPC error.")
128 return ex.message