def setUp(self): # Initialize music API music = db_backend.get_client() cfg.CONF.set_override('keyspace', 'conductor') music.keyspace_create(keyspace=cfg.CONF.keyspace) self.sp = SolverRequestParser() c1 = access_dist.AccessDistance(_name = 'c1', _type = 't1', _demand_list = ['d1', 'd2']) c2 = access_dist.AccessDistance(_name = 'c2', _type = 't1', _demand_list = ['d1']) self.sp.constraints = {'c1': c1, 'c2': c2} self.sp.demands = {'d1': demand.Demand('d1'), 'd2': demand.Demand('d2')}
def test_drop_no_latency_rule_candidates(self): # test case: # one demand 'demand_1' contains two candidates (d1_candidate1 and d1_candidate2) # candidate1 locates in USA and candidate2 locates in ITA # the parameter 'diff_bw_candidates_and_countries' contains a list with one element 'ITA' # this function should get rid of candidate2 (locates in ITA) from the demand1 candidate list d1_candidate1 = dict() d1_candidate1['candidate_id'] = 'd1_candidate1' d1_candidate1['country'] = 'USA' d1_candidate2 = dict() d1_candidate2['candidate_id'] = 'd1_candidate2' d1_candidate2['country'] = 'ITA' test_demand_1 = demand.Demand('demand_1') test_demand_1.resources['d1_candidate1'] = d1_candidate1 test_demand_1.resources['d1_candidate2'] = d1_candidate2 self.sp.demands = {'demand_1': test_demand_1} self.sp.obj_func_param = ['demand_1'] diff_bw_candidates_and_countries = ['ITA'] self.sp.latencyTriage = TriageLatency() self.sp.latencyTriage.latencyDroppedCandiate = mock.MagicMock(return_value=None) self.sp.drop_no_latency_rule_candidates(diff_bw_candidates_and_countries) self.assertEqual(self.sp.demands['demand_1'].resources, {'d1_candidate1': {'candidate_id': 'd1_candidate1', 'country': 'USA'}})
def parse_template(self, json_template=None, country_groups=None, regions_maps=None): if json_template is None: LOG.error("No template specified") return "Error" # get request type self.request_type = json_template['conductor_solver']['request_type'] # get demands demand_list = json_template["conductor_solver"]["demands"] for demand_id, candidate_list in demand_list.items(): current_demand = demand.Demand(demand_id) # candidate should only have minimal information like location_id for candidate in candidate_list["candidates"]: candidate_id = candidate["candidate_id"] current_demand.resources[candidate_id] = candidate current_demand.sort_base = 0 # this is only for testing self.demands[demand_id] = current_demand # get locations location_list = json_template["conductor_solver"]["locations"] for location_id, location_info in location_list.items(): loc = demand.Location(location_id) loc.loc_type = "coordinates" loc.value = (float(location_info["latitude"]), float(location_info["longitude"])) loc.country = location_info[ 'country'] if 'country' in location_info else None self.locations[location_id] = loc # get constraints input_constraints = json_template["conductor_solver"]["constraints"] for constraint_id, constraint_info in input_constraints.items(): constraint_type = constraint_info["type"] constraint_demands = list() parsed_demands = constraint_info["demands"] if isinstance(parsed_demands, list): for d in parsed_demands: constraint_demands.append(d) else: constraint_demands.append(parsed_demands) if constraint_type == "distance_to_location": c_property = constraint_info.get("properties") location_id = c_property.get("location") op = operator.le # default operator c_op = c_property.get("distance").get("operator") if c_op == ">": op = operator.gt elif c_op == ">=": op = operator.ge elif c_op == "<": op = operator.lt elif c_op == "<=": op = operator.le elif c_op == "=": op = operator.eq dist_value = c_property.get("distance").get("value") my_access_distance_constraint = access_dist.AccessDistance( constraint_id, constraint_type, constraint_demands, _comparison_operator=op, _threshold=dist_value, _location=self.locations[location_id]) self.constraints[my_access_distance_constraint.name] = \ my_access_distance_constraint elif constraint_type == "distance_between_demands": c_property = constraint_info.get("properties") op = operator.le # default operator c_op = c_property.get("distance").get("operator") if c_op == ">": op = operator.gt elif c_op == ">=": op = operator.ge elif c_op == "<": op = operator.lt elif c_op == "<=": op = operator.le elif c_op == "=": op = operator.eq dist_value = c_property.get("distance").get("value") my_aic_distance_constraint = aic_dist.AICDistance( constraint_id, constraint_type, constraint_demands, _comparison_operator=op, _threshold=dist_value) self.constraints[my_aic_distance_constraint.name] = \ my_aic_distance_constraint elif constraint_type == "inventory_group": my_inventory_group_constraint = \ inventory_group.InventoryGroup( constraint_id, constraint_type, constraint_demands) self.constraints[my_inventory_group_constraint.name] = \ my_inventory_group_constraint elif constraint_type == "region_fit": c_property = constraint_info.get("properties") controller = c_property.get("controller") request = c_property.get("request") # inventory type is cloud for region_fit inventory_type = "cloud" my_service_constraint = service_constraint.Service( constraint_id, constraint_type, constraint_demands, _controller=controller, _request=request, _cost=None, _inventory_type=inventory_type) self.constraints[my_service_constraint.name] = \ my_service_constraint elif constraint_type == "instance_fit": c_property = constraint_info.get("properties") controller = c_property.get("controller") request = c_property.get("request") # inventory type is service for instance_fit inventory_type = "service" my_service_constraint = service_constraint.Service( constraint_id, constraint_type, constraint_demands, _controller=controller, _request=request, _cost=None, _inventory_type=inventory_type) self.constraints[my_service_constraint.name] = \ my_service_constraint elif constraint_type == "zone": c_property = constraint_info.get("properties") location_id = c_property.get("location") qualifier = c_property.get("qualifier") category = c_property.get("category") location = self.locations[location_id] if location_id else None my_zone_constraint = zone.Zone(constraint_id, constraint_type, constraint_demands, _qualifier=qualifier, _category=category, _location=location) self.constraints[my_zone_constraint.name] = my_zone_constraint elif constraint_type == "attribute": c_property = constraint_info.get("properties") my_attribute_constraint = \ attribute_constraint.Attribute(constraint_id, constraint_type, constraint_demands, _properties=c_property) self.constraints[my_attribute_constraint.name] = \ my_attribute_constraint elif constraint_type == "threshold": c_property = constraint_info.get("properties") my_threshold_constraint = \ threshold.Threshold(constraint_id, constraint_type, constraint_demands, _properties=c_property) self.constraints[ my_threshold_constraint.name] = my_threshold_constraint elif constraint_type == "hpa": LOG.debug("Creating constraint - {}".format(constraint_type)) c_property = constraint_info.get("properties") my_hpa_constraint = hpa.HPA(constraint_id, constraint_type, constraint_demands, _properties=c_property) self.constraints[my_hpa_constraint.name] = my_hpa_constraint elif constraint_type == "vim_fit": LOG.debug("Creating constraint - {}".format(constraint_type)) c_property = constraint_info.get("properties") my_vim_constraint = vim_fit.VimFit(constraint_id, constraint_type, constraint_demands, _properties=c_property) self.constraints[my_vim_constraint.name] = my_vim_constraint else: LOG.error("unknown constraint type {}".format(constraint_type)) return # get objective function if "objective" not in json_template["conductor_solver"] \ or not json_template["conductor_solver"]["objective"]: self.objective = objective.Objective() elif json_template["conductor_solver"]["version"] in V2_IDS: objective_function = json_template["conductor_solver"]["objective"] self.objective = generic_objective.GenericObjective( objective_function) else: input_objective = json_template["conductor_solver"]["objective"] self.objective = objective.Objective() self.objective.goal = input_objective["goal"] self.objective.operation = input_objective["operation"] self.latencyTriage = TriageLatency() LOG.info("objective function params") for operand_data in input_objective["operands"]: if operand_data["function"] == "latency_between": self.obj_func_param.append( operand_data["function_param"][1]) LOG.info("done - objective function params") for operand_data in input_objective["operands"]: operand = objective.Operand() operand.operation = operand_data["operation"] operand.weight = float(operand_data["weight"]) if operand_data["function"] == "latency_between": LOG.debug("Processing objective function latency_between") self.latencyTriage.takeOpimaztionType( operand_data["function"]) func = latency_between.LatencyBetween("latency_between") func.region_group = self.assign_region_group_weight( country_groups, regions_maps) param = operand_data["function_param"][0] if param in self.locations: func.loc_a = self.locations[param] elif param in self.demands: func.loc_a = self.demands[param] param = operand_data["function_param"][1] if param in self.locations: func.loc_z = self.locations[param] elif param in self.demands: func.loc_z = self.demands[param] operand.function = func elif operand_data["function"] == "distance_between": LOG.debug("Processing objective function distance_between") self.latencyTriage.takeOpimaztionType( operand_data["function"]) func = distance_between.DistanceBetween("distance_between") param = operand_data["function_param"][0] if param in self.locations: func.loc_a = self.locations[param] elif param in self.demands: func.loc_a = self.demands[param] param = operand_data["function_param"][1] if param in self.locations: func.loc_z = self.locations[param] elif param in self.demands: func.loc_z = self.demands[param] operand.function = func elif operand_data["function"] == "aic_version": self.objective.goal = "min_aic" func = aic_version.AICVersion("aic_version") func.loc = operand_data["function_param"] operand.function = func elif operand_data["function"] == "cost": func = cost.Cost("cost") func.loc = operand_data["function_param"] operand.function = func elif operand_data["function"] == "hpa_score": func = hpa_score.HPAScore("hpa_score") operand.function = func self.objective.operand_list.append(operand) self.latencyTriage.updateTriageLatencyDB(self.plan_id, self.request_id)