def check_update_before_writing(self, update, tm): old_flows = [] new_flows = [] for old_flow, new_flow in itertools.izip(update.old_flows, update.new_flows): vol = tm[old_flow.src][old_flow.dst] reversed_vol = tm[old_flow.dst][old_flow.src] old_flow_1 = GenSingleFlow(len(old_flows), old_flow.src, old_flow.dst, vol, old_flow.update_type) old_flow_1.path = old_flow.path old_flows.append(old_flow_1) old_flow_2 = GenSingleFlow(len(old_flows), old_flow.dst, old_flow.src, reversed_vol, old_flow.update_type) old_flow_2.path = list(reversed(old_flow.path)) old_flows.append(old_flow_2) new_flow_1 = GenSingleFlow(len(new_flows), new_flow.src, new_flow.dst, vol, new_flow.update_type) new_flow_1.path = new_flow.path new_flows.append(new_flow_1) new_flow_2 = GenSingleFlow(len(new_flows), new_flow.dst, new_flow.src, reversed_vol, new_flow.update_type) new_flow_2.path = list(reversed(new_flow.path)) new_flows.append(new_flow_2) return not ez_flow_tool.has_deadlock(old_flows, new_flows)
def read_flows(self, flow_file, checking_deadlock=False): flow_reader = open(flow_file, 'r') old_caps = {} new_caps = {} old_flows = [] new_flows = [] line = flow_reader.readline() statistic_line = None has_statistic_line = self.has_statistic_info(line) if has_statistic_line: statistic_line = copy(line) line = flow_reader.readline() while line: strs = line.strip('\n').split("\t") if len(strs) <= 1: continue end_points = strs[0].strip('(').strip(')').split(',') src, dst = (int(end_points[0]), int(end_points[1])) old_vol = float(strs[1]) old_flow = GenSingleFlow(len(old_flows), src, dst, old_vol) path_items = strs[2].strip('[').strip(']').split(',') if path_items[0] == '': old_flow.path = [] else: for item in path_items: old_flow.path.append(int(item)) old_flows.append(old_flow) new_vol = float(strs[3]) new_flow = GenSingleFlow(len(new_flows), src, dst, new_vol) path_items = strs[4].strip('[').strip(']').split(',') if path_items[0] == '': new_flow.path = [] else: for item in path_items: new_flow.path.append(int(item)) new_flows.append(new_flow) line = flow_reader.readline() flow_reader.close() if checking_deadlock and ez_flow_tool.has_deadlock( old_flows, new_flows): return NetworkUpdate([], []) update = NetworkUpdate(old_flows, new_flows) if statistic_line: update.set_statistic_info_from_string(statistic_line) return update
def generate_one_state_from_old(self, topo, tm, flow_cnt, old_flows): src_dst_queue = deque(deepcopy(self.pairs)) link_caps = defaultdict() flows = defaultdict() empty_path_count = 0 for (src, dst) in old_flows.keys(): flow = deepcopy(old_flows[(src, dst)]) flow.vol = self.compute_new_vol(flow.vol) flow.reversed_vol = self.compute_new_vol(flow.reversed_vol) src_dst_queue.remove( FlowSrcDst(src, dst, tm[src][dst], tm[dst][src])) is_old_no_path = (flow.path == []) flow.path = [] self.path_generator.attempts = len(topo.edge_switches()) flow.path = self.path_generator.generate_path( topo, flow, link_caps) if flow.path or (not flow.path and not is_old_no_path): if not flow.path and not is_old_no_path: flow.update_type = constants.REMOVING_FLOW flows[(src, dst)] = flow elif not flow.path and is_old_no_path: old_flows.pop((src, dst)) if not flow.path: empty_path_count += 1 while len(src_dst_queue) > 0 and empty_path_count > 0: flow_src_dst = src_dst_queue.popleft() src = flow_src_dst.lt_id dst = flow_src_dst.gt_id vol = flow_src_dst.vol flow = GenSingleFlow(len(flows), src, dst, vol, update_type=constants.ADDING_FLOW, reversed_vol=flow_src_dst.reversed_vol) self.generate_middleboxes(topo, flow) self.path_generator.attempts = len(topo.edge_switches()) flow.path = self.path_generator.generate_path( topo, flow, link_caps) if flow.path: old_flow = GenSingleFlow( len(flows), src, dst, vol, reversed_vol=flow_src_dst.reversed_vol) flows[(src, dst)] = flow old_flows[(src, dst)] = old_flow empty_path_count -= 1 return flows, link_caps
def generate_one_state(self, topo, tm, flow_cnt): src_dst_queue = deque(deepcopy(self.pairs)) # src_dst_gen = self.random_src_dst_gen(topo.edge_switches()) link_caps = defaultdict() flows = defaultdict() while len(src_dst_queue) > 0 and len(flows) < flow_cnt: flow_src_dst = src_dst_queue.pop() src = flow_src_dst.lt_id dst = flow_src_dst.gt_id vol = flow_src_dst.vol while len(src_dst_queue) > 0 and ( flows.has_key((src, dst)) or tm[src][dst] == 0 or not self.path_generator.check_eligible_src_dst( topo, link_caps, src, dst, tm[src][dst], tm[dst][src])): flow_src_dst = src_dst_queue.popleft() src = flow_src_dst.lt_id dst = flow_src_dst.gt_id vol = flow_src_dst.vol flow = GenSingleFlow(len(flows), src, dst, vol, update_type=constants.ADDING_FLOW, reversed_vol=flow_src_dst.reversed_vol) # self.generate_middleboxes(topo, flow) self.path_generator.attempts = len(topo.edge_switches()) flow.path = self.path_generator.generate_path( topo, flow, link_caps) if not flow.path: continue # flow.vol = tm[src][dst] flows[(src, dst)] = flow # reversed_flow = Flow(len(flows), dst, src, tm[dst][src]) # reversed_flow.path = list(reversed(flow.path)) # flows[(dst, src)] = reversed_flow return flows, link_caps
def generate_flows(self, topo, old_tm, flow_cnt): debug = self.log.debug third_sws = defaultdict() flows_by_link = defaultdict(set) old_flows, old_link_caps = self.create_old_flows( topo, old_tm, flow_cnt, third_sws, flows_by_link) if len(flows_by_link.keys()) == 0: return [], [] failed_ids = self.generate_failed_edges_by_percentage( flows_by_link, self.failure_rate, flow_cnt) new_topo = deepcopy(topo) for i, j in failed_ids: new_topo.graph.remove_edge(i, j) new_link_caps = defaultdict() new_flows = defaultdict() changed_paths = defaultdict() bad_pairs = defaultdict() for (src, dst) in old_flows.keys(): old_flow = old_flows[(src, dst)] new_flow = GenSingleFlow(old_flow.flow_id, src, dst, self.compute_new_vol(old_flow.vol)) if not self.is_affected_link(failed_ids, flows_by_link, (src, dst)): new_flow.path = old_flow.path if self.path_generator.check_capacity(new_topo, new_flow.path, new_link_caps, new_flow.vol): self.path_generator.allocate_link_cap( new_flow.path, new_link_caps, new_flow.vol, new_flow.reversed_vol) else: self.set_back_to_old_flow(new_flow, old_flow, new_link_caps) else: changed = self.try_generate_new_path(new_topo, new_flow, old_flow, new_link_caps, third_sws) if changed: changed_paths[(src, dst)] = True else: bad_pairs[(src, dst)] = True new_flows[(src, dst)] = new_flow if len(changed_paths) < self.failure_rate * flow_cnt: for (src, dst) in old_flows.keys(): if (src, dst) not in changed_paths.keys() and ( src, dst) not in bad_pairs.keys(): new_flow = new_flows[(src, dst)] old_flow = old_flows[(src, dst)] changed = self.try_generate_new_path(new_topo, new_flow, old_flow, new_link_caps,\ third_sws) if changed: changed_paths[(src, dst)] = True if len(changed_paths) == self.failure_rate * flow_cnt: break return self.return_flows(old_flows, new_flows, old_link_caps, new_link_caps)