From 53504946d65c38d54a373e214af1d959b415a510 Mon Sep 17 00:00:00 2001 From: peusterm Date: Thu, 4 Feb 2016 16:09:28 +0100 Subject: [PATCH] added a metadata field to datacenter objects. datacenter CLI now has a list and status functionality --- emuvim/api/zerorpcapi.py | 26 +++++++++++++++++--------- emuvim/dcemulator/net.py | 4 ++-- emuvim/dcemulator/node.py | 15 ++++++++++++--- emuvim/example_topology.py | 8 ++++++-- emuvim/test/test_emulator.py | 5 ++++- 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/emuvim/api/zerorpcapi.py b/emuvim/api/zerorpcapi.py index 1d5a6b1..de43423 100644 --- a/emuvim/api/zerorpcapi.py +++ b/emuvim/api/zerorpcapi.py @@ -56,30 +56,30 @@ class MultiDatacenterApi(object): def __init__(self, dcs): self.dcs = dcs - def compute_action_start(self, dc_name, compute_name, image, network): + def compute_action_start(self, dc_label, compute_name, image, network): # network e.g. {"ip": "10.0.0.254/8"} # TODO what to return UUID / given name / internal name ? logging.debug("RPC CALL: compute start") try: - c = self.dcs.get(dc_name).startCompute( + c = self.dcs.get(dc_label).startCompute( compute_name, image=image, network=network) return str(c.name) except Exception as ex: logging.exception("RPC error.") return ex.message - def compute_action_stop(self, dc_name, compute_name): + def compute_action_stop(self, dc_label, compute_name): logging.debug("RPC CALL: compute stop") try: - return self.dcs.get(dc_name).stopCompute(compute_name) + return self.dcs.get(dc_label).stopCompute(compute_name) except Exception as ex: logging.exception("RPC error.") return ex.message - def compute_list(self, dc_name): + def compute_list(self, dc_label): logging.debug("RPC CALL: compute list") try: - if dc_name is None: + if dc_label is None: # return list with all compute nodes in all DCs all_containers = [] for dc in self.dcs.itervalues(): @@ -89,16 +89,16 @@ class MultiDatacenterApi(object): else: # return list of compute nodes for specified DC return [(c.name, c.getStatus()) - for c in self.dcs.get(dc_name).listCompute()] + for c in self.dcs.get(dc_label).listCompute()] except Exception as ex: logging.exception("RPC error.") return ex.message - def compute_status(self, dc_name, compute_name): + def compute_status(self, dc_label, compute_name): logging.debug("RPC CALL: compute status") try: return self.dcs.get( - dc_name).containers.get(compute_name).getStatus() + dc_label).containers.get(compute_name).getStatus() except Exception as ex: logging.exception("RPC error.") return ex.message @@ -110,3 +110,11 @@ class MultiDatacenterApi(object): except Exception as ex: logging.exception("RPC error.") return ex.message + + def datacenter_status(self, dc_label): + logging.debug("RPC CALL: datacenter status") + try: + return self.dcs.get(dc_label).getStatus() + except Exception as ex: + logging.exception("RPC error.") + return ex.message diff --git a/emuvim/dcemulator/net.py b/emuvim/dcemulator/net.py index 2c7e106..7b238b8 100644 --- a/emuvim/dcemulator/net.py +++ b/emuvim/dcemulator/net.py @@ -29,13 +29,13 @@ class DCNetwork(Dockernet): self, controller=Controller, switch=OVSKernelSwitch, **kwargs) self.addController('c0') - def addDatacenter(self, label): + def addDatacenter(self, label, metadata={}): """ Create and add a logical cloud data center to the network. """ if label in self.dcs: raise Exception("Data center label already exists: %s" % label) - dc = Datacenter(label) + dc = Datacenter(label, metadata=metadata) dc.net = self # set reference to network self.dcs[label] = dc dc.create() # finally create the data center in our Mininet instance diff --git a/emuvim/dcemulator/node.py b/emuvim/dcemulator/node.py index 0a14f20..6c1ecf7 100644 --- a/emuvim/dcemulator/node.py +++ b/emuvim/dcemulator/node.py @@ -66,13 +66,16 @@ class Datacenter(object): DC_COUNTER = 1 - def __init__(self, label): + def __init__(self, label, metadata={}): self.net = None # DCNetwork to which we belong # each node (DC) has a short internal name used by Mininet # this is caused by Mininets naming limitations for swtiches etc. self.name = "dc%d" % Datacenter.DC_COUNTER Datacenter.DC_COUNTER += 1 - self.label = label # use this for user defined names + # use this for user defined names that can be longer than self.name + self.label = label + # dict to store arbitrary metadata (e.g. latitude and longitude) + self.metadata = metadata self.switch = None # first prototype assumes one "bigswitch" per DC self.containers = {} # keep track of running containers @@ -145,4 +148,10 @@ class Datacenter(object): """ Return a dict with status information about this DC. """ - return {"label": self.label} + return { + "label": self.label, + "internalname": self.name, + "switch": self.switch.name, + "n_running_containers": len(self.containers), + "metadata": self.metadata + } diff --git a/emuvim/example_topology.py b/emuvim/example_topology.py index 3c49f99..3c53c58 100644 --- a/emuvim/example_topology.py +++ b/emuvim/example_topology.py @@ -37,8 +37,10 @@ def create_topology1(): """ dc1 = net.addDatacenter("datacenter1") dc2 = net.addDatacenter("datacenter2") - dc3 = net.addDatacenter("a_very_long_data_center_name3") - dc4 = net.addDatacenter("datacenter4") + dc3 = net.addDatacenter("long_data_center_name3") + dc4 = net.addDatacenter( + "datacenter4", + metadata={"mydata": "we can also add arbitrary metadata to each DC"}) """ 3. You can add additional SDN switches for data center @@ -71,6 +73,8 @@ def create_topology1(): # connect data centers to this endpoint zapi1.connectDatacenter(dc1) zapi1.connectDatacenter(dc2) + zapi1.connectDatacenter(dc3) + zapi1.connectDatacenter(dc4) # run API endpoint server (in another thread, don't block) zapi1.start() diff --git a/emuvim/test/test_emulator.py b/emuvim/test/test_emulator.py index 861de1b..8b756b9 100644 --- a/emuvim/test/test_emulator.py +++ b/emuvim/test/test_emulator.py @@ -55,7 +55,10 @@ class simpleTestTopology( unittest.TestCase ): self.net.addLink(self.s[i], self.s[i + 1]) # add some data centers for i in range(0, ndatacenter): - self.dc.append(self.net.addDatacenter('datacenter%d' % i)) + self.dc.append( + self.net.addDatacenter( + 'datacenter%d' % i, + metadata={"unittest_dc": i})) # add some hosts for i in range(0, nhosts): self.h.append(self.net.addHost('h%d' % i)) -- 2.25.1