aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/benchmark/contexts/model.py
diff options
context:
space:
mode:
authorQiLiang <liangqi1@huawei.com>2015-10-15 14:24:27 +0800
committerQiLiang <liangqi1@huawei.com>2015-10-20 11:35:26 +0800
commit1e3e9b5d7ff40eb6921e60dc1a3254e8c984a08c (patch)
tree92b1e8e81ddb0b2a0cf7e7f38688ebaf1d409f8f /yardstick/benchmark/contexts/model.py
parent8069fee968b73833d314b41f004c8f1cb1ab6c28 (diff)
Heat context code refactor
Heat context code refactor to cater for the evolution of the Yardstick framework. At test_case.yaml context segment add "type" to indicate the context type, see samples/ping-heat-context.yaml for an example. And the default context type is Heat, so the existing yaml file do not need to change. JIRA: YARDSTICK-168 Change-Id: Ida0ce12c17cd9b88d7acfb4c9eb1ac6986394b38 Signed-off-by: QiLiang <liangqi1@huawei.com>
Diffstat (limited to 'yardstick/benchmark/contexts/model.py')
-rw-r--r--yardstick/benchmark/contexts/model.py238
1 files changed, 238 insertions, 0 deletions
diff --git a/yardstick/benchmark/contexts/model.py b/yardstick/benchmark/contexts/model.py
new file mode 100644
index 000000000..91020b92f
--- /dev/null
+++ b/yardstick/benchmark/contexts/model.py
@@ -0,0 +1,238 @@
+##############################################################################
+# Copyright (c) 2015 Ericsson AB and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+""" Logical model
+
+"""
+
+
+class Object(object):
+ '''Base class for classes in the logical model
+ Contains common attributes and methods
+ '''
+ def __init__(self, name, context):
+ # model identities and reference
+ self.name = name
+ self._context = context
+
+ # stack identities
+ self.stack_name = None
+ self.stack_id = None
+
+ @property
+ def dn(self):
+ '''returns distinguished name for object'''
+ return self.name + "." + self._context.name
+
+
+class PlacementGroup(Object):
+ '''Class that represents a placement group in the logical model
+ Concept comes from the OVF specification. Policy should be one of
+ "availability" or "affinity (there are more but they are not supported)"
+ '''
+ map = {}
+
+ def __init__(self, name, context, policy):
+ if policy not in ["affinity", "availability"]:
+ raise ValueError("placement group '%s', policy '%s' is not valid" %
+ (name, policy))
+ self.name = name
+ self.members = set()
+ self.stack_name = context.name + "-" + name
+ self.policy = policy
+ PlacementGroup.map[name] = self
+
+ def add_member(self, name):
+ self.members.add(name)
+
+ @staticmethod
+ def get(name):
+ if name in PlacementGroup.map:
+ return PlacementGroup.map[name]
+ else:
+ return None
+
+
+class Router(Object):
+ '''Class that represents a router in the logical model'''
+ def __init__(self, name, network_name, context, external_gateway_info):
+ super(self.__class__, self).__init__(name, context)
+
+ self.stack_name = context.name + "-" + network_name + "-" + self.name
+ self.stack_if_name = self.stack_name + "-if0"
+ self.external_gateway_info = external_gateway_info
+
+
+class Network(Object):
+ '''Class that represents a network in the logical model'''
+ list = []
+
+ def __init__(self, name, context, attrs):
+ super(self.__class__, self).__init__(name, context)
+ self.stack_name = context.name + "-" + self.name
+ self.subnet_stack_name = self.stack_name + "-subnet"
+ self.subnet_cidr = attrs.get('cidr', '10.0.1.0/24')
+ self.router = None
+
+ if "external_network" in attrs:
+ self.router = Router("router", self.name,
+ context, attrs["external_network"])
+
+ Network.list.append(self)
+
+ def has_route_to(self, network_name):
+ '''determines if this network has a route to the named network'''
+ if self.router and self.router.external_gateway_info == network_name:
+ return True
+ return False
+
+ @staticmethod
+ def find_by_route_to(external_network):
+ '''finds a network that has a route to the specified network'''
+ for network in Network.list:
+ if network.has_route_to(external_network):
+ return network
+
+ @staticmethod
+ def find_external_network():
+ '''return the name of an external network some network in this
+ context has a route to'''
+ for network in Network.list:
+ if network.router:
+ return network.router.external_gateway_info
+ return None
+
+
+class Server(Object):
+ '''Class that represents a server in the logical model'''
+ list = []
+
+ def __init__(self, name, context, attrs):
+ super(self.__class__, self).__init__(name, context)
+ self.stack_name = self.name + "." + context.name
+ self.keypair_name = context.keypair_name
+ self.secgroup_name = context.secgroup_name
+ self.context = context
+ self.public_ip = None
+ self.private_ip = None
+
+ if attrs is None:
+ attrs = {}
+
+ self.placement_groups = []
+ placement = attrs.get("placement", [])
+ placement = placement if type(placement) is list else [placement]
+ for p in placement:
+ pg = PlacementGroup.get(p)
+ if not pg:
+ raise ValueError("server '%s', placement '%s' is invalid" %
+ (name, p))
+ self.placement_groups.append(pg)
+ pg.add_member(self.stack_name)
+
+ self.instances = 1
+ if "instances" in attrs:
+ self.instances = attrs["instances"]
+
+ # dict with key network name, each item is a dict with port name and ip
+ self.ports = {}
+
+ self.floating_ip = None
+ if "floating_ip" in attrs:
+ self.floating_ip = {}
+
+ if self.floating_ip is not None:
+ ext_net = Network.find_external_network()
+ assert ext_net is not None
+ self.floating_ip["external_network"] = ext_net
+
+ self._image = None
+ if "image" in attrs:
+ self._image = attrs["image"]
+
+ self._flavor = None
+ if "flavor" in attrs:
+ self._flavor = attrs["flavor"]
+
+ Server.list.append(self)
+
+ @property
+ def image(self):
+ '''returns a server's image name'''
+ if self._image:
+ return self._image
+ else:
+ return self._context.image
+
+ @property
+ def flavor(self):
+ '''returns a server's flavor name'''
+ if self._flavor:
+ return self._flavor
+ else:
+ return self._context.flavor
+
+ def _add_instance(self, template, server_name, networks, scheduler_hints):
+ '''adds to the template one server and corresponding resources'''
+ port_name_list = []
+ for network in networks:
+ port_name = server_name + "-" + network.name + "-port"
+ self.ports[network.name] = {"stack_name": port_name}
+ template.add_port(port_name, network.stack_name,
+ network.subnet_stack_name,
+ sec_group_id=self.secgroup_name)
+ port_name_list.append(port_name)
+
+ if self.floating_ip:
+ external_network = self.floating_ip["external_network"]
+ if network.has_route_to(external_network):
+ self.floating_ip["stack_name"] = server_name + "-fip"
+ template.add_floating_ip(self.floating_ip["stack_name"],
+ external_network,
+ port_name,
+ network.router.stack_if_name,
+ self.secgroup_name)
+
+ template.add_server(server_name, self.image, self.flavor,
+ ports=port_name_list,
+ key_name=self.keypair_name,
+ scheduler_hints=scheduler_hints)
+
+ def add_to_template(self, template, networks, scheduler_hints=None):
+ '''adds to the template one or more servers (instances)'''
+ if self.instances == 1:
+ server_name = self.stack_name
+ self._add_instance(template, server_name, networks,
+ scheduler_hints=scheduler_hints)
+ else:
+ # TODO(hafe) fix or remove, no test/sample for this
+ for i in range(self.instances):
+ server_name = "%s-%d" % (self.stack_name, i)
+ self._add_instance(template, server_name, networks,
+ scheduler_hints=scheduler_hints)
+
+
+def update_scheduler_hints(scheduler_hints, added_servers, placement_group):
+ ''' update scheduler hints from server's placement configuration
+ TODO: this code is openstack specific and should move somewhere else
+ '''
+ if placement_group.policy == "affinity":
+ if "same_host" in scheduler_hints:
+ host_list = scheduler_hints["same_host"]
+ else:
+ host_list = scheduler_hints["same_host"] = []
+ else:
+ if "different_host" in scheduler_hints:
+ host_list = scheduler_hints["different_host"]
+ else:
+ host_list = scheduler_hints["different_host"] = []
+
+ for name in added_servers:
+ if name in placement_group.members:
+ host_list.append({'get_resource': name})