class ResourceModelRegistrar(object):
+ """
+ Global registry to keep track of all existing resource models.
+ """
def __init__(self, dc_emulation_max_cpu):
self.e_cpu = dc_emulation_max_cpu
LOG.info("Resource model registrar created with dc_emulation_max_cpu=%r" % dc_emulation_max_cpu)
def register(self, dc, rm):
+ """
+ Register a new resource model.
+ :param dc: Data center to which it is assigned.
+ :param rm: The resource model object.
+ :return: None
+ """
if dc in self._resource_models:
raise Exception("There is already an resource model assigned to this DC.")
self._resource_models[dc] = rm
rm.registrar = self
LOG.info("Registrar: Added resource model: %r" % rm)
- @property
- def num_models(self):
- return len(self._resource_models)
-
@property
def resource_models(self):
return list(self._resource_models.itervalues())
"""
def __init__(self):
- self._flavors=dict()
+ self._flavors = dict()
self._initDefaultFlavors()
self.registrar = None # pointer to registrar
+ self.allocated_compute_instances = dict()
LOG.info("Resource model %r initialized" % self)
def __repr__(self):
return self.__class__.__name__
def _initDefaultFlavors(self):
- # initialize some default flavours (inspired by OpenStack)
+ """
+ initialize some default flavours (naming/sizes inspired by OpenStack)
+ """
self.addFlavour(ResourceFlavor(
"tiny", {"compute": 1, "memory": 32, "disk": 1}))
self.addFlavour(ResourceFlavor(
:return: 3-tuple: (CPU-fraction, Mem-limit, Disk-limit)
"""
LOG.warning("Allocating in BaseResourceModel: %r with flavor: %r" % (name, flavor_name))
+ self.allocated_compute_instances[name] = flavor_name
return -1.0, -1.0, -1.0 # return invalid values to indicate that this RM is a dummy
def free(self, name):
:return: True/False
"""
LOG.warning("Free in BaseResourceModel: %r" % name)
+ del self.allocated_compute_instances[name]
return True
+import time
from emuvim.test.base import SimpleTestTopology
-from emuvim.dcemulator.resourcemodel import BaseResourceModel
+from emuvim.dcemulator.resourcemodel import BaseResourceModel, ResourceFlavor
class testResourceModel(SimpleTestTopology):
def testBaseResourceModelApi(self):
- pass
- # TODO test add flavor etc.
- # TODO test aaloc / free
+ r = BaseResourceModel()
+ # check if default flavors are there
+ assert(len(r._flavors) == 5)
+ # check addFlavor functionality
+ f = ResourceFlavor("test", {"testmetric": 42})
+ r.addFlavour(f)
+ assert("test" in r._flavors)
+ assert(r._flavors.get("test").get("testmetric") == 42)
+ # test if allocate and free runs through
+ assert(len(r.allocate("testc", "tiny")) == 3) # expected: 3tuple
+ assert(r.free("testc"))
def testAddRmToDc(self):
# create network
# check resource model and resource model registrar
assert(self.dc[0]._resource_model is not None)
assert(self.net.rm_registrar.num_models == 1)
- # TODO test if alloc was called on start
- # TODO if free was called on stop
+
+ # check if alloc was called during startCompute
+ assert(len(r.allocated_compute_instances) == 0)
+ self.dc[0].startCompute("tc1")
+ time.sleep(1)
+ assert(len(r.allocated_compute_instances) == 1)
+ # check if free was called during stopCompute
+ self.dc[0].stopCompute("tc1")
+ assert(len(r.allocated_compute_instances) == 0)
# check connectivity by using ping
assert(self.net.ping([self.h[0], self.h[1]]) <= 0.0)
# stop Mininet network