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_multiple_path(self, topo, flow, link_caps, path_count):
     unit_vol = flow.unit_vol
     reversed_unit_vol = flow.unit_reversed_vol
     count = 0
     for i in xrange(0, path_count):
         temp_flow = GenSingleFlow(flow.flow_id, flow.src, flow.dst,
                                   unit_vol, flow.update_type,
                                   reversed_unit_vol)
         path = self.path_generator.generate_path(topo, temp_flow,
                                                  link_caps)
         if path:
             count += 1
         flow.path.append(path)
     if count < path_count and count > 0:
         print(flow)
         print("flow %s->%s vol before: %s" %
               (flow.src, flow.dst, flow.vol))
         print("flow %s->%s reversed_vol before: %s" %
               (flow.dst, flow.src, flow.reversed_vol))
     flow.vol = unit_vol * count
     flow.reversed_vol = reversed_unit_vol * count
     if count < path_count and count > 0:
         print("flow %s->%s vol after: %s" % (flow.src, flow.dst, flow.vol))
         print("flow %s->%s reversed_vol after: %s" %
               (flow.dst, flow.src, flow.reversed_vol))
     return count > 0
    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 create_old_flows(self, topo, tm, flow_cnt, third_sws, flows_by_link):
        src_dst_gen = self.random_src_dst_gen(topo.edge_switches())
        old_link_caps = defaultdict()
        old_flows = defaultdict()
        total_pairs = (len(topo.edge_switches()) *
                       (len(topo.edge_switches()) - 1))

        failed_pairs = defaultdict()
        while len(old_flows) < flow_cnt and len(failed_pairs.keys()) + len(
                old_flows.keys()) < total_pairs:
            src, dst = next(src_dst_gen)
            vol = tm[src][dst] + tm[dst][src]
            possible_pair = self.path_generator.check_eligible_src_dst(
                topo, old_link_caps, src, dst, tm[src][dst], tm[dst][src])
            while (old_flows.has_key((src, dst)) or failed_pairs.has_key((src, dst))
                        or tm[src][dst] == 0 or not possible_pair)\
                    and (len(failed_pairs.keys()) + len(old_flows.keys()) < total_pairs):
                if not possible_pair:
                    failed_pairs[(src, dst)] = True
                src, dst = next(src_dst_gen)
                possible_pair = self.path_generator.check_eligible_src_dst(
                    topo, old_link_caps, src, dst, tm[src][dst], tm[dst][src])

            if len(failed_pairs.keys()) + len(old_flows.keys()) >= total_pairs:
                break

            old_flow = GenSingleFlow(len(old_flows), src, dst, vol)
            self.path_generator.attempts = len(topo.edge_switches())
            has_old_path, third = self.path_generator.generate_path_with_third_switch(
                topo, old_flow, old_link_caps)
            if not has_old_path:
                failed_pairs[(src, dst)] = True
                continue
            else:
                third_sws[(src, dst)] = third
            for i, j in itertools.izip(old_flow.path[0:len(old_flow.path) - 1],
                                       old_flow.path[1:len(old_flow.path)]):
                flows_by_link[(i, j)].add((src, dst))
            self.log.debug(old_flow)
            old_flows[(src, dst)] = old_flow

        if len(failed_pairs.keys()) + len(old_flows.keys()) >= total_pairs:
            self.log.debug("Has %d flows" % len(old_flows.keys()))
        return old_flows, old_link_caps
    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
Example #6
0
    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 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)