aeac66577c5620286835e5284348f13ced05f2b0
[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 return None
34
35 def getStatus(self):
36 """
37 Helper method to receive information about this compute instance.
38 """
39 return None
40
41
42 class Datacenter(object):
43 """
44 Represents a logical data center to which compute resources
45 (Docker containers) can be added at runtime.
46
47 Will also implement resource bookkeeping in later versions.
48 """
49
50 def __init__(self, name):
51 self.net = None # DCNetwork to which we belong
52 self.name = name
53 self.switch = None # first prototype assumes one "bigswitch" per DC
54 self.containers = {} # keep track of running containers
55
56 def _get_next_dc_dpid(self):
57 global DCDPID_BASE
58 DCDPID_BASE += 1
59 return DCDPID_BASE
60
61 def create(self):
62 """
63 Each data center is represented by a single switch to which
64 compute resources can be connected at run time.
65
66 TODO: This will be changed in the future to support multiple networks
67 per data center
68 """
69 self.switch = self.net.mnet.addSwitch(
70 "%s.s1" % self.name, dpid=hex(self._get_next_dc_dpid())[2:])
71 logging.debug("created data center switch: %s" % str(self.switch))
72
73 def start(self):
74 pass
75
76 def startCompute(self, name, image=None, network=None):
77 """
78 Create a new container as compute resource and connect it to this
79 data center.
80
81 TODO: This interface will change to support multiple networks to which
82 a single container can be connected.
83 """
84 assert name is not None
85 # set default parameter
86 if image is None:
87 image = "ubuntu"
88 if network is None:
89 network = {} # {"ip": "10.0.0.254/8"}
90 # create the container and connect it to the given network
91 d = self.net.addDocker("%s" % (name), dimage=image)
92 self.net.addLink(d, self.switch, params1=network)
93 self.containers[name] = d
94 return name # we might use UUIDs for naming later on
95
96 def stopCompute(self, name):
97 """
98 Stop and remove a container from this data center.
99 """
100 assert name in self.containers
101 self.net.removeLink(
102 link=None, node1=self.containers[name], node2=self.switch)
103 self.net.removeDocker("%s" % (name))
104 del self.containers[name]
105 return True
106
107 def listCompute(self):
108 """
109 Return a list of all running containers assigned to this
110 data center.
111 """
112 return self.containers.itervalues()