Exemplo n.º 1
0
def place(network_file, template_file, source_file, source_template_object=False, fixed_vnfs=None,
          prev_embedding_file=None, cpu=None, mem=None, dr=None, networkx=None, networkx_cap='cap', write_result=True,
          print_best=True, logging_level=logging.INFO):
    seed = random.randint(0, 9999)
    seed_subfolder = False
    random.seed(seed)

    # set up logging into file Data/logs/heuristic/scenario_timestamp_seed.log
    timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
    if logging_level is None:
        logging.disable(logging.CRITICAL)
    else:
        os.makedirs("logs/heuristic/obj{}".format(obj), exist_ok=True)
        logging.basicConfig(filename="logs/heuristic/obj{}/{}_{}_{}.log"
                            .format(obj, os.path.basename(network_file)[:-4], timestamp, seed),
                            level=logging_level, format="%(asctime)s(%(levelname)s):\t%(message)s", datefmt="%H:%M:%S")

    # if a NetworkX object is passed, use that - including all of its capacities, delays, etc
    if networkx is not None:
        nodes, links = reader.read_networkx(networkx, cap=networkx_cap)
    else:
        nodes, links = reader.read_network(network_file, cpu, mem, dr)

    # When 'source_template_object' is True, we would need to read from objects instead of files
    template, source_components = reader.read_template(template_file, template_object=source_template_object,
                                                       return_src_components=True)
    sources = reader.read_sources(source_file, source_components, source_object=source_template_object)

    templates = [template]
    # print(template)
    # exit()
    components = {j for t in templates for j in t.components}
    fixed = []
    if fixed_vnfs is not None:
        fixed = reader.read_fixed_instances(fixed_vnfs, components)
    prev_embedding = {}
    if networkx is not None:
        prev_embedding = reader.read_prev_placement(networkx, templates)
    elif prev_embedding_file is not None:
        prev_embedding = reader.read_prev_embedding(prev_embedding_file, templates, nodes, links)

    input_files = [network_file, template_file, source_file, fixed_vnfs, prev_embedding_file]
    # TODO: support >1 template

    # print("Using seed {}".format(seed))

    logger.info("Starting initial embedding at {}".format(timestamp))
    # print("Initial embedding\n")
    init_time, runtime, obj_value, changed, overlays = control.solve(nodes, links, templates, prev_embedding, sources,
                                                                     fixed, obj, print_best=print_best)
    if overlays is None:
        logger.error("Could not find placement. Returning None.")
        return None
    # If the write_result variable is True we receive the path to a result file
    # If the write_result variable is False we a result dict.
    result = writer.write_heuristic_result(runtime, obj_value, changed, overlays.values(), input_files, obj, nodes,
                                           links, seed, seed_subfolder, write_result, source_template_object)

    return result
Exemplo n.º 2
0
def place(network_file,
          template_file,
          source_file,
          fixed_file=None,
          prev_embedding_file=None,
          cpu=None,
          mem=None,
          dr=None):
    seed = random.randint(0, 9999)
    seed_subfolder = False
    random.seed(seed)

    # set up logging into file Data/logs/heuristic/scenario_timestamp_seed.log
    # logging.disable(logging.CRITICAL)     # disable logging
    timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
    os.makedirs("logs/heuristic/obj{}".format(obj), exist_ok=True)
    logging.basicConfig(filename="logs/heuristic/obj{}/{}_{}_{}.log".format(
        obj,
        os.path.basename(network_file)[:-4], timestamp, seed),
                        level=logging.DEBUG,
                        format="%(asctime)s(%(levelname)s):\t%(message)s",
                        datefmt="%H:%M:%S")

    nodes, links = reader.read_network(network_file, cpu, mem, dr)
    template, source_components = reader.read_template(
        template_file, return_src_components=True)
    templates = [template]
    # print(template)
    # exit()
    sources = reader.read_sources(source_file, source_components)
    components = {j for t in templates for j in t.components}
    fixed = []
    if fixed_file is not None:
        fixed = reader.read_fixed_instances(fixed_file, components)
    prev_embedding = {}
    if prev_embedding_file is not None:
        prev_embedding = reader.read_prev_embedding(prev_embedding_file,
                                                    templates, nodes, links)

    input_files = [
        network_file, template_file, source_file, fixed_file,
        prev_embedding_file
    ]
    # TODO: support >1 template

    # print("Using seed {}".format(seed))

    logger.info("Starting initial embedding at {}".format(timestamp))
    # print("Initial embedding\n")
    # TODO: make less verbose or only as verbose when asked for (eg, with -v argument)
    init_time, runtime, obj_value, changed, overlays = control.solve(
        nodes, links, templates, prev_embedding, sources, fixed, obj)
    result = writer.write_heuristic_result(runtime, obj_value, changed,
                                           overlays.values(), input_files, obj,
                                           nodes, links, seed, seed_subfolder)

    return result