Extended network API to support multiple interfaces in a single Docker container...
[osm/vim-emu.git] / emuvim / dcemulator / node.py
old mode 100644 (file)
new mode 100755 (executable)
index 0e6eae8..336126c
@@ -52,7 +52,7 @@ class EmulatorCompute(Docker):
         status["state"] = self.dcli.inspect_container(self.dc)["State"]
         status["id"] = self.dcli.inspect_container(self.dc)["Id"]
         status["datacenter"] = (None if self.datacenter is None
-                                else self.datacenter.name)
+                                else self.datacenter.label)
         return status
 
 
@@ -64,9 +64,18 @@ class Datacenter(object):
     Will also implement resource bookkeeping in later versions.
     """
 
-    def __init__(self, name):
+    DC_COUNTER = 1
+
+    def __init__(self, label, metadata={}):
         self.net = None  # DCNetwork to which we belong
-        self.name = name
+        # 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
+        # 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
 
@@ -90,13 +99,15 @@ class Datacenter(object):
     def start(self):
         pass
 
-    def startCompute(self, name, image=None, network=None):
+    def startCompute(self, name, image=None, command=None, network=None):
         """
         Create a new container as compute resource and connect it to this
         data center.
-
-        TODO: This interface will change to support multiple networks to which
-        a single container can be connected.
+        :param name: name (string)
+        :param image: image name (string)
+        :param command: command (string)
+        :param network: networks list({"ip": "10.0.0.254/8"}, {"ip": "11.0.0.254/24"})
+        :return:
         """
         assert name is not None
         # no duplications
@@ -107,9 +118,17 @@ class Datacenter(object):
             image = "ubuntu"
         if network is None:
             network = {}  # {"ip": "10.0.0.254/8"}
-        # create the container and connect it to the given network
-        d = self.net.addDocker("%s" % (name), dimage=image)
-        self.net.addLink(d, self.switch, params1=network)
+        if isinstance(network, dict):
+            network = [network]  # if we have only one network, put it in a list
+        if isinstance(network, list):
+            if len(network) < 1:
+                network.append({})
+
+        # create the container
+        d = self.net.addDocker("%s" % (name), dimage=image, dcmd=command)
+        # connect all given networks
+        for nw in network:
+            self.net.addLink(d, self.switch, params1=nw)
         # do bookkeeping
         self.containers[name] = d
         d.datacenter = self
@@ -134,3 +153,15 @@ class Datacenter(object):
         data center.
         """
         return list(self.containers.itervalues())
+
+    def getStatus(self):
+        """
+        Return a dict with status information about this DC.
+        """
+        return {
+            "label": self.label,
+            "internalname": self.name,
+            "switch": self.switch.name,
+            "n_running_containers": len(self.containers),
+            "metadata": self.metadata
+        }