87a5a6e38317c8e20627bb386f0804244d1b496d
[osm/vim-emu.git] / emuvim / dcemulator / node.py
1 """
2 Distributed Cloud Emulator (dcemulator)
3 (c) 2015 by Manuel Peuster <manuel.peuster@upb.de>
4 """
5 from mininet.node import Docker
6 import logging
7
8
9 DCDPID_BASE = 1000 # start of switch dpid's used for data center switches
10
11
12 class EmulatorCompute(Docker):
13 """
14 Emulator specific compute node class.
15 Inherits from Dockernet's Docker host class.
16 Represents a single container connected to a (logical)
17 data center.
18 We can add emulator specific helper functions to it.
19 """
20
21 def __init__(
22 self, name, dimage, **kwargs):
23 logging.debug("Create EmulatorCompute instance: %s" % name)
24
25 # call original Docker.__init__
26 Docker.__init__(self, name, dimage, **kwargs)
27
28 def getNetworkStatus(self):
29 """
30 Helper method to receive information about the virtual networks
31 this compute instance is connected to.
32 """
33 # format list of tuples (name, Ip, MAC, isUp, status)
34 return [(str(i), i.IP(), i.MAC(), i.isUp(), i.status())
35 for i in self.intfList()]
36
37 def getStatus(self):
38 """
39 Helper method to receive information about this compute instance.
40 """
41 status = {}
42 status["name"] = self.name
43 status["network"] = self.getNetworkStatus()
44 status["image"] = self.dimage
45 status["cpu_quota"] = self.cpu_quota
46 status["cpu_period"] = self.cpu_period
47 status["cpu_shares"] = self.cpu_shares
48 status["cpuset"] = self.cpuset
49 status["mem_limit"] = self.mem_limit
50 status["memswap_limit"] = self.memswap_limit
51 status["state"] = self.dcli.inspect_container(self.dc)["State"]
52 status["id"] = self.dcli.inspect_container(self.dc)["Id"]
53 return status
54
55
56 class Datacenter(object):
57 """
58 Represents a logical data center to which compute resources
59 (Docker containers) can be added at runtime.
60
61 Will also implement resource bookkeeping in later versions.
62 """
63
64 def __init__(self, name):
65 self.net = None # DCNetwork to which we belong
66 self.name = name
67 self.switch = None # first prototype assumes one "bigswitch" per DC
68 self.containers = {} # keep track of running containers
69
70 def _get_next_dc_dpid(self):
71 global DCDPID_BASE
72 DCDPID_BASE += 1
73 return DCDPID_BASE
74
75 def create(self):
76 """
77 Each data center is represented by a single switch to which
78 compute resources can be connected at run time.
79
80 TODO: This will be changed in the future to support multiple networks
81 per data center
82 """
83 self.switch = self.net.mnet.addSwitch(
84 "%s.s1" % self.name, dpid=hex(self._get_next_dc_dpid())[2:])
85 logging.debug("created data center switch: %s" % str(self.switch))
86
87 def start(self):
88 pass
89
90 def startCompute(self, name, image=None, network=None):
91 """
92 Create a new container as compute resource and connect it to this
93 data center.
94
95 TODO: This interface will change to support multiple networks to which
96 a single container can be connected.
97 """
98 assert name is not None
99 # set default parameter
100 if image is None:
101 image = "ubuntu"
102 if network is None:
103 network = {} # {"ip": "10.0.0.254/8"}
104 # create the container and connect it to the given network
105 d = self.net.addDocker("%s" % (name), dimage=image)
106 self.net.addLink(d, self.switch, params1=network)
107 self.containers[name] = d
108 return name # we might use UUIDs for naming later on
109
110 def stopCompute(self, name):
111 """
112 Stop and remove a container from this data center.
113 """
114 assert name in self.containers
115 self.net.removeLink(
116 link=None, node1=self.containers[name], node2=self.switch)
117 self.net.removeDocker("%s" % (name))
118 del self.containers[name]
119 return True
120
121 def listCompute(self):
122 """
123 Return a list of all running containers assigned to this
124 data center.
125 """
126 return self.containers.itervalues()