def CallSingle(types, modules, *args): params = MergeDicts(modules) result = ModelLinkList() for type in GetCollection(types): factory = GetLinkFactory(params, type) link = CallFactoryMethod(factory, "OneToOne", args) result.add(link) return result
def Call(name, types, modules, *args): params = MergeDicts(modules) result = ModelLinkList() for type in GetCollection(types): factory = GetLinkFactory(params, type) links = CallFactoryMethod(factory, name, args) result = result.append(links) return result
def DiscoverConnect(sources, target_attributes, allowed_links, chain_attribute, chain_filters, link_type, max_length, connect_function = None, debug = None): """ sources - ModelList of elements to start from target_attributes - list of attributes (pairs) to detect target attributes allowed_links - list of attributes (pairs) to filter allowed links during traversing chain_attribute - attribute that will be used to generate path string for chain_filters the path string will be generated using values of the required attributes, separated by '-' e.g. "ib_node-ib_port-ib_port-ib_switch_element-ib_switch" chain_filters - list of regular expressions, all of them must succeed on target path string , or it will be discarded link_type - type for a new link example: DiscoverConnect(list_of_switches, [("type", "ib_switch"), ("type", "node")], [("type", "ib")], "type", ["^.*-ib_port-ib_port-[^p]*$"], "logical_ib") """ result = ModelLinkList() for source in sources: result = result.append(DiscoverConnectOne(source, target_attributes, allowed_links, chain_attribute, chain_filters, link_type, max_length, connect_function, debug)) return result
def DiscoverConnectOne(source, target_attributes, allowed_links, chain_attribute, chain_filters, link_type, max_length, connect_function = None, debug = None): debug = debug if debug is not None else False if connect_function is None: connect_function = OneWithOne compiled_chain_filters = [] for chain_filter in chain_filters: compiled_chain_filters.append(re.compile(chain_filter)) #--------------- def Step(paths): new_paths = [] if debug: print "paths: ", map(lambda y: map(lambda x: x.GetConstOrNull("_id"), y), paths) for path in paths: neighbor_list = ModelObjectList() for allowed_link in allowed_links: for neighbor in path[-1].GetAllNeighbors(allowed_link[0], allowed_link[1]).Uniq(): neighbor_list.add(neighbor) neighbor_list = neighbor_list.Uniq() for neighbor in neighbor_list: if neighbor in path: continue ext_path = path[:] ext_path.append(neighbor) new_paths.append(ext_path) if debug: print "added neighbor: ", neighbor.GetConstOrNull("_id") if debug: print "new paths: ", map(lambda y: map(lambda x: x.GetConstOrNull("_id"), y), new_paths) return new_paths def CheckPath(path): chain = "" prefix = "" for object in path: if not object.TestAttribute(chain_attribute): return False chain += prefix + object.GetAttribute(chain_attribute).GetValue().GetRaw() prefix = "-" for chain_filter in compiled_chain_filters: if not chain_filter.match(chain): if debug: print "chain %s failed regexp" % chain return False if debug: print "chain %s passed regexp" % chain return True #--------------- result = ModelLinkList() paths = [[source]] for i in xrange(1, max_length): # 1 elememnt already presents if debug: print "step ", i new_paths = Step(paths) unfinished_paths = [] finished_paths = [] for path in new_paths: added = False for target_attribute in target_attributes: name = target_attribute[0] value = target_attribute[1] if path[-1].TestAttribute(name) and path[-1].GetAttribute(name).eq(value): finished_paths.append(path) added = True if not added: unfinished_paths.append(path) for path in finished_paths: if CheckPath(path): result.add(connect_function(path[0], path[-1], link_type)) if debug: print "added for: ", map(lambda x: x.GetConstOrNull("_id"), path) paths = unfinished_paths if debug: print "finished" return result