def main():
    # Parse arguments
    args = parse_args()
    if not args.seed:
        args.seed = random.randint(1, 9999)
    logging.basicConfig(level=logging.INFO)
    logging.getLogger("coordsim").setLevel(logging.WARNING)

    # Creating the results directory variable where the simulator result files will be written
    network_stem = os.path.splitext(os.path.basename(args.network))[0]
    service_function_stem = os.path.splitext(
        os.path.basename(args.service_functions))[0]
    simulator_config_stem = os.path.splitext(os.path.basename(args.config))[0]

    results_dir = f"{PROJECT_ROOT}/results/{network_stem}/{service_function_stem}/{simulator_config_stem}" \
                  f"/{DATETIME}_seed{args.seed}"

    # creating the simulator
    simulator = Simulator(os.path.abspath(args.network),
                          os.path.abspath(args.service_functions),
                          os.path.abspath(args.config),
                          test_mode=True,
                          test_dir=results_dir)
    init_state = simulator.init(args.seed)
    log.info("Network Stats after init(): %s", init_state.network_stats)
    nodes_list = [node['id'] for node in init_state.network.get('nodes')]
    nodes_with_capacity = []
    for node in simulator.network.nodes(data=True):
        if node[1]['cap'] > 0:
            nodes_with_capacity.append(node[0])
    sf_list = list(init_state.service_functions.keys())
    sfc_list = list(init_state.sfcs.keys())
    ingress_nodes = get_ingress_nodes_and_cap(simulator.network)
    # we place every sf on each node of the network with some capacity, so placement is calculated only once
    placement = get_placement(nodes_with_capacity, sf_list)
    # Uniformly distributing the schedule for all Nodes with some capacity
    schedule = get_schedule(nodes_list, nodes_with_capacity, sf_list, sfc_list)
    # Since the placement and the schedule are fixed , the action would also be the same throughout
    action = SimulatorAction(placement, schedule)
    # iterations define the number of time we wanna call apply()
    log.info(f"Running for {args.iterations} iterations...")
    for i in tqdm(range(args.iterations)):
        _ = simulator.apply(action)
    # We copy the input files(network, simulator config....) to  the results directory
    copy_input_files(results_dir, os.path.abspath(args.network),
                     os.path.abspath(args.service_functions),
                     os.path.abspath(args.config))
    # Creating the input file in the results directory containing the num_ingress and the Algo used attributes
    create_input_file(results_dir, len(ingress_nodes), "LB")
    log.info(f"Saved results in {results_dir}")
def main():
    # Simulator params
    network = 'triangle.graphml'
    args = {
        'network': f'../../../../params/networks/{network}',
        'service_functions': '../../../../params/services/abc.yaml',
        'resource_functions': '../../../../params/services/resource_functions',
        'config': '../../../../params/config/legacy_config.yaml',
        'seed': 9999,
        'output_path': f'test-out/{network}'
    }

    # Setup logging
    timestamp = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
    os.makedirs(f'{args["output_path"]}/logs', exist_ok=True)
    logging.basicConfig(
        filename=f'{args["output_path"]}/logs/'
        f'{os.path.basename(args["network"])}_{timestamp}_{args["seed"]}.log',
        level=logging.INFO)
    logging.getLogger('coordsim').setLevel(logging.INFO)
    simulator = Simulator(test_mode=True)

    # Setup algorithm
    algo = StaticTriangleAlgo(simulator)
    algo.init(os.path.abspath(args['network']),
              os.path.abspath(args['service_functions']),
              os.path.abspath(args['config']),
              args['seed'],
              args['output_path'],
              resource_functions_path=os.path.abspath(
                  args['resource_functions']))
    # Execute orchestrated simulation
    algo.run()
Example #3
0
def create_simulator(agent_helper):
    """Create a simulator object"""
    from siminterface.simulator import Simulator

    return Simulator(agent_helper.network_path,
                     agent_helper.service_path,
                     agent_helper.sim_config_path,
                     test_mode=agent_helper.test_mode,
                     test_dir=agent_helper.config_dir)
 def test_gym_registration(self):
     config = get_config(AGENT_CONFIG)
     simulator = Simulator(NETWORK, SERVICE, SIM_CONFIG)
     env = GymEnv(agent_config=config,
                  simulator=simulator,
                  network_file=NETWORK,
                  service_file=SERVICE)
     np.random.seed(123)
     env.seed(123)
 def test_min_max_delay(self):
     config = get_config(AGENT_CONFIG)
     simulator = Simulator(NETWORK, SERVICE, SIM_CONFIG)
     env = gym.make('rlsp-env-v1',
                    agent_config=config,
                    simulator=simulator,
                    network_file=NETWORK,
                    service_file=SERVICE)
     self.assertEqual(env.min_delay, 15)
     self.assertEqual(env.max_delay, 45)
 def test_step(self):
     config = get_config(AGENT_CONFIG)
     simulator = Simulator(NETWORK, SERVICE, SIM_CONFIG)
     env = gym.make('rlsp-env-v1',
                    agent_config=config,
                    simulator=simulator,
                    network_file=NETWORK,
                    service_file=SERVICE)
     np.random.seed(123)
     env.seed(123)
     env.reset()
     action = env.action_space.sample()
     env.step(action)
    def test_step(self):
        config = get_config(agent_config)
        simulator = Simulator(network, service, sim_config)
        env = gym.make('rlsp-env-v1',
                       agent_config=config,
                       simulator=simulator,
                       service_requirement_file=service_requirement,
                       network_file=network,
                       service_file=service)
        np.random.seed(123)
        env.seed(123)
        obs = env.reset()
        logger.info(f"obs {obs}")
        # action = env.action_space.sample()
        #         action = [0.1528252,  0.19189513, 0.13835746, 0.5151016,  0.8268603,  0.11794249,
        #  0.07534754, 0.8041661,  0.6766331,  0.83818173, 0.74750483, 0.2683317,
        #  0.1885326,  0.17871094, 0.48484454, 0.80052173, 0.5470235,  0.939061,
        #  0.7532571,  0.95109504, 0.7161905,  0.437634,   0.8504298,  0.56406283,
        #  0.44520622, 0.9043397,  0.7369696  ,0.06333494, 0.85315746 ,0.35849094,
        #  0.7802861,  0.42221898, 0.00383798 ,0.5678534 , 0.24563274 ,0.7632712 ,
        #  0.8775916,  0.5324118 , 0.40174866 ,0.5244014 , 0.53869325 ,0.62608093,
        #  0.55334944, 0.65927726 ,0.3466312  ,0.81105894, 0.5835827  ,0.4352289,
        #  0.01409177, 0.68956935, 0.2725983  ,0.83922625, 0.12063584 ,0.85230696,
        #  0.24010704, 0.7814054 , 0.6302035  ,0.7396873  ,0.3951772  ,0.42432162,
        #  0.8822647 , 0.5477109 , 0.6531822  ,0.8758906  ,0.12640443, 0.31692567,
        #  0.54074305, 0.7312232 , 0.40281135 ,0.46530938 ,0.29194754 ,0.1864658,
        #  0.906782  , 0.73796546, 0.9524628  ,0.9118524  ,0.8770285  ,0.57136434,
        #  0.6312382 , 0.5789188 , 0.14968178 ,0.41891995 ,0.09121216 ,0.01003964,
        #  0.6886566 , 0.13875785, 0.99779034 ,0.36943817 ,0.5710171  ,0.41438,
        #  0.81586087, 0.14191775, 0.2810072  ,0.8571065  ,0.83860654 ,0.2865804,
        #  0.17616676, 0.11686828, 0.66831094 ,0.27806306 ,0.24364603 ,0.3952267,
        #  0.1256759 , 0.5283989 , 0.7713088  ,0.01707859 ,0.86026347 ,0.96787435,
        #  0.76898026, 0.24870834, 0.02020283 ,0.26120338 ,0.11047094 ,0.0451458,
        #  0.7435941 , 0.14921127, 0.29213443 ,0.22803156 ,0.34240273 ,0.7448049,
        #  0.34957966, 0.6426194 , 0.8075939  ,0.00796008 ,0.961782   ,0.8310099,
        #  0.9726601 , 0.3459133 ]

        action = [1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1]

        logger.info(f"action is : {action}")
        env.step(action)
Example #8
0
def main():
    args = parse_args()
    if not args.seed:
        args.seed = random.randint(1, 9999)
    # os.makedirs("logs", exist_ok=True)
    # logging.basicConfig(filename="logs/{}_{}_{}.log".format(os.path.basename(args.network),
    #                                                        DATETIME, args.seed), level=logging.INFO)
    # logging.getLogger("coordsim").setLevel(logging.WARNING)
    # logging.getLogger("bjointsp").setLevel(logging.INFO)

    # Creating the results directory variable where the simulator result files will be written
    network_stem = os.path.splitext(os.path.basename(args.network))[0]
    service_function_stem = os.path.splitext(
        os.path.basename(args.service_functions))[0]
    simulator_config_stem = os.path.splitext(os.path.basename(args.config))[0]

    results_dir = f"{PROJECT_ROOT}/results/{network_stem}/{service_function_stem}/{simulator_config_stem}" \
                  f"/{DATETIME}_seed{args.seed}"
    # creating the simulator
    # initializing the simulator with absolute paths to the network, service_functions and the config. files.
    simulator = Simulator(os.path.abspath(args.network),
                          os.path.abspath(args.service_functions),
                          os.path.abspath(args.config),
                          test_mode=True,
                          test_dir=results_dir)
    init_state = simulator.init(args.seed)
    # log.info("Network Stats after init(): %s", init_state.network_stats)
    # assuming for now that there is only one SFC.
    sfc_name = list(init_state.sfcs.keys())[0]
    sf_list = list(init_state.sfcs.get(sfc_name))
    sf_delays_dict = init_state.service_functions
    nodes_list = [node['id'] for node in init_state.network.get('nodes')]
    ingress_nodes, node_cap = get_ingress_nodes_and_cap(simulator.network)
    # Getting the Mean Flow Data rate and flow size from the simulator_config.yaml file
    with open(args.config, "r") as f:
        conf = yaml.load(f, yaml.SafeLoader)
        flow_dr_mean = conf.get('flow_dr_mean')
        flow_size = conf.get('flow_size_shape')
        run_duration = conf.get('run_duration')
        inter_arrival_mean = conf.get('inter_arrival_mean')
    flow_duration = (float(flow_size) / float(flow_dr_mean)
                     ) * 1000  # Converted flow duration to ms

    # Getting Processing delay from Service_functions file
    with open(args.service_functions, "r") as f:
        service_func = yaml.load(f, yaml.SafeLoader)
        processing_delay = service_func['sf_list'][
            sf_list[0]]['processing_delay_mean']

    # The template object is fixed, so it is created just once and used throughout for the 'place' fx of BJointSP
    template = create_template(sfc_name, sf_list, sf_delays_dict)

    # Since after the init() call to Simulator we don't get any traffic info back
    # we can't create the source needed for 'place' function of BJointSP,
    # So we for just the first 'place' call to BJointSP create a source  with just ingress nodes having *
    # *vnf_source as the vnf in it and date_rate of flow_dr_mean from the config file

    num_flows = run_duration / inter_arrival_mean
    overlapping_flows = math.ceil(
        (num_flows * (processing_delay + flow_duration)) / run_duration)
    source_list = []
    j = 1
    for i in range(len(ingress_nodes)):
        flows = []
        for _ in range(int(overlapping_flows / flow_dr_mean)):
            flows.append({"id": "f" + str(j), "data_rate": flow_dr_mean})
            j += 1
        source_list.append({
            'node': ingress_nodes[i],
            'vnf': "vnf_source",
            'flows': flows
        })
    # Since the simulator right now does not have any link_dr , we are using a high value = 1000 for now.
    first_result = bjointsp_place(os.path.abspath(args.network),
                                  template,
                                  source_list,
                                  source_template_object=True,
                                  cpu=node_cap,
                                  mem=node_cap,
                                  networkx=simulator.network,
                                  write_result=False,
                                  logging_level=None)
    # creating the schedule and placement for the simulator from the first result file that BJointSP returns.
    placement, schedule = get_placement_and_schedule(first_result, nodes_list,
                                                     sfc_name, sf_list)

    # We run the simulator iterations number of times to get the Traffic info from the SimulatorAction object
    # We generate new source file for the BJointSP from the traffic info we get from the simulator for each iteration
    # Using this source file and the already generated Template file we call the 'place' fx of BJointSP
    # In-case no source exists, we use the previous placement and schedule
    for _ in tqdm(range(args.iterations)):
        action = SimulatorAction(placement, schedule)
        apply_state = simulator.apply(action)
        source, source_exists = create_source_object(
            apply_state.traffic, sf_list, sfc_name, ingress_nodes,
            flow_dr_mean, processing_delay, flow_duration, run_duration)
        if source_exists:
            result = bjointsp_place(os.path.abspath(args.network),
                                    template,
                                    source,
                                    source_template_object=True,
                                    cpu=node_cap,
                                    mem=node_cap,
                                    networkx=simulator.network,
                                    write_result=False,
                                    logging_level=None)
            placement, schedule = get_placement_and_schedule(
                result, nodes_list, sfc_name, sf_list)

    copy_input_files(results_dir, os.path.abspath(args.network),
                     os.path.abspath(args.service_functions),
                     os.path.abspath(args.config))
    create_input_file(results_dir, len(ingress_nodes), "BJointSP-Fixed")
    print(f"Saved results in {results_dir}")
Example #9
0
def main():
    args = parse_args()
    timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
    os.makedirs("logs", exist_ok=True)
    logging.basicConfig(filename="logs/{}_{}_{}.log".format(os.path.basename(args.network),
                                                            timestamp, args.seed), level=logging.INFO)
    logging.getLogger("coordsim").setLevel(logging.WARNING)
    logging.getLogger("bjointsp").setLevel(logging.INFO)
    # creating the simulator
    # initializing the simulator with absolute paths to the network, service_functions and the config. files.
    simulator = Simulator(os.path.abspath(args.network),
                          os.path.abspath(args.service_functions),
                          os.path.abspath(args.config), test_mode=True)

    init_state = simulator.init(args.seed)
    log.info("Network Stats after init(): %s", init_state.network_stats)
    # assuming for now that there is only one SFC.
    sfc_name = list(init_state.sfcs.keys())[0]
    sf_list = list(init_state.sfcs.get(sfc_name))
    sf_delays_dict = init_state.service_functions
    nodes_list = [node['id'] for node in init_state.network.get('nodes')]
    ingress_nodes, node_cap = get_ingress_nodes_and_cap(simulator.network)
    # Getting the Mean Flow Data rate from the sim_config.yaml file
    with open(args.config, "r") as f:
        conf = yaml.load(f, yaml.SafeLoader)
        flow_dr_mean = conf.get('flow_dr_mean')
    # The template file is fixed, so it is created just once and used throughout for the 'place' fx of BJointSP
    template = create_template(sfc_name, sf_list, sf_delays_dict)

    # Since after the init() call to Simulator we don't get any traffic info back
    # we can't create the source file needed for 'place' function of BJointSP,
    # So we for just the first 'place' call to BJointSP create a source file with just ingress nodes having *
    # *vnf_source as the vnf in it and date_rate of flow_dr_mean from the config file
    with open(BJOINTSP_FIRST_SRC_LOCATION, "w") as f:
        source_list = []
        for i in range(len(ingress_nodes)):
            source_list.append({'node': ingress_nodes[i], 'vnf': "vnf_source", 'flows': [{"id": "f" + str(i + 1),
                                                                                          "data_rate": flow_dr_mean}
                                                                                         ]})
        yaml.safe_dump(source_list, f, default_flow_style=False)

    # Since the simulator right now does not have any link_dr , we are using a high value = 1000 for now.
    first_result = bjointsp_place(os.path.abspath(args.network),
                                  os.path.abspath(template),
                                  os.path.abspath(BJOINTSP_FIRST_SRC_LOCATION), cpu=node_cap, mem=node_cap, dr=1000,
                                  networkx=simulator.network)
    # creating the schedule and placement for the simulator from the first result file that BJointSP returns.
    placement, schedule = get_placement_and_schedule(os.path.abspath(first_result), nodes_list, sfc_name, sf_list)

    # We run the simulator iterations number of times to get the Traffic info from the SimulatorAction object
    # We generate new source file for the BJointSP from the traffic info we get from the simulator for each iteration
    # Using this source file and the already generated Template file we call the 'place' fx of BJointSP
    # Incase no source exists, we use the previous placement and schedule
    for i in range(args.iterations):
        action = SimulatorAction(placement, schedule)
        apply_state = simulator.apply(action)
        log.info("Network Stats after apply() # %s: %s", i + 1, apply_state.network_stats)
        source, source_exists = create_source_file(apply_state.traffic, sf_list, sfc_name, flow_dr_mean)
        if source_exists:
            result = bjointsp_place(os.path.abspath(args.network), os.path.abspath(template), os.path.abspath(source),
                                    cpu=node_cap, mem=node_cap, dr=1000, networkx=simulator.network)
            placement, schedule = get_placement_and_schedule(os.path.abspath(result), nodes_list, sfc_name, sf_list)
    simulator.params.adapter.writer.write_results(simulator.params.adapter.metrics.metrics)
    #     'output_path': f'out/{network}'
    # }

    # Evaluation params
    network = 'dfn_58.graphml'
    args = {
        'network': f'../../../params/networks/{network}',
        'service_functions': '../../../params/services/3sfcs.yaml',
        'config': '../../../params/config/hc_0.5.yaml',
        'seed': 155,
        'output_path': f'out/{network}'
    }

    # Setup logging to screen
    logging.basicConfig(level=logging.INFO)
    logging.getLogger('bjointsp').setLevel(logging.WARNING)
    logging.getLogger('coordsim').setLevel(logging.INFO)
    logging.getLogger('coordsim.reader').setLevel(logging.WARNING)
    simulator = Simulator(test_mode=True)

    # Setup algorithm
    algo = BJointSPAlgo(simulator,
                        recalc_before_drop=True,
                        logging_level=logging.INFO)
    algo.init(os.path.abspath(args['network']),
              os.path.abspath(args['service_functions']),
              os.path.abspath(args['config']), args['seed'],
              args['output_path'])
    # Execute orchestrated simulation
    algo.run()