def ordering_src_dst(self, switch_list, tm):
     list_src_dst = []
     for src in xrange(len(switch_list)):
         for dst in xrange(src + 1, len(switch_list)):
             list_src_dst.append(FlowSrcDst(src, dst, tm[src][dst], tm[dst][src]))
     list_src_dst.sort()
     return list_src_dst
    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_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 == [])

            path_count = flow.non_empty_path_count
            flow.path = []
            self.path_generator.attempts = len(topo.edge_switches())

            has_path = self.generate_multiple_path(topo, flow, link_caps,
                                                   path_count)
            if has_path or (not has_path and not is_old_no_path):
                if not has_path and not is_old_no_path:
                    update_type = constants.REMOVING_FLOW
                flows[(src, dst)] = flow
                current_path_count = len(flow.path)
                current_non_empty_path_count = flow.non_empty_path_count
                if current_path_count < path_count:
                    flow.path = [p for p in flow.path if p]
                    for i in range(
                            0,
                            current_path_count - current_non_empty_path_count):
                        flow.path.append([])
            elif not has_path and is_old_no_path:
                old_flows.pop((src, dst))
            if not has_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 = GenMulFlow(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())
            has_path = self.generate_multiple_path(topo, flow, link_caps,
                                                   constants.NO_MULT_PATH)
            if has_path:
                old_flow = GenMulFlow(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