Ejemplo n.º 1
0
class StreamingSimulation(unittest.TestCase):
    """Simulate a Streaming Scenario."""
    def ageing(self):
        return

    def setUp(self):
        self.simulation_bus = SimulationBus(packetencoder=NdnTlvEncoder())

        self.fetch_tool1 = Fetch(
            "nfn0",
            None,
            255,
            NdnTlvEncoder(),
            interfaces=[self.simulation_bus.add_interface("fetchtoo11")])

        self.nfn0 = NFNForwarder(
            port=0,
            encoder=NdnTlvEncoder(),
            interfaces=[self.simulation_bus.add_interface("nfn0")],
            log_level=255,
            executors={"PYTHONSTREAM": NFNPythonExecutorStreaming()},
            ageing_interval=1)

        self.nfn0.executors["PYTHONSTREAM"].initialize_executor(
            self.nfn0.nfnlayer.queue_to_lower,
            self.nfn0.nfnlayer.queue_from_lower, self.nfn0.nfnlayer.cs, False)
        self.nfn0.icnlayer.pit.ageing = self.ageing
        self.nfn0.timeoutpreventionlayer.ageing = self.ageing

        self.nfn1 = NFNForwarder(
            port=0,
            encoder=NdnTlvEncoder(),
            interfaces=[self.simulation_bus.add_interface("nfn1")],
            log_level=255,
            executors={"PYTHONSTREAM": NFNPythonExecutorStreaming()},
            ageing_interval=1)

        self.nfn1.executors["PYTHONSTREAM"].initialize_executor(
            self.nfn1.nfnlayer.queue_to_lower,
            self.nfn1.nfnlayer.queue_from_lower, self.nfn1.nfnlayer.cs, False)
        self.nfn1.icnlayer.pit.ageing = self.ageing
        self.nfn1.timeoutpreventionlayer.ageing = self.ageing

        self.nfn12 = NFNForwarder(
            port=0,
            encoder=NdnTlvEncoder(),
            interfaces=[self.simulation_bus.add_interface("nfn12")],
            log_level=255,
            executors={"PYTHONSTREAM": NFNPythonExecutorStreaming()},
            ageing_interval=1)

        self.nfn12.executors["PYTHONSTREAM"].initialize_executor(
            self.nfn12.nfnlayer.queue_to_lower,
            self.nfn12.nfnlayer.queue_from_lower, self.nfn12.nfnlayer.cs,
            False)
        self.nfn12.icnlayer.pit.ageing = self.ageing
        self.nfn12.timeoutpreventionlayer.ageing = self.ageing

        self.repo1 = ICNDataRepository(
            "/tmp/repo1",
            Name("/repo/r1"),
            0,
            255,
            NdnTlvEncoder(),
            False,
            False,
            interfaces=[self.simulation_bus.add_interface("repo1")])

        self.mgmt_client0 = MgmtClient(
            self.nfn0.mgmt.mgmt_sock.getsockname()[1])
        self.mgmt_client1 = MgmtClient(
            self.nfn1.mgmt.mgmt_sock.getsockname()[1])
        self.mgmt_client01 = MgmtClient(
            self.nfn12.mgmt.mgmt_sock.getsockname()[1])

    def tearDown(self):
        self.nfn0.stop_forwarder()
        self.nfn1.stop_forwarder()
        self.nfn12.stop_forwarder()
        self.repo1.stop_repo()
        self.fetch_tool1.stop_fetch()
        self.simulation_bus.stop_process()
        self.tearDown_repo()

    def setup_faces_and_connections(self):
        self.nfn0.start_forwarder()
        self.nfn1.start_forwarder()
        self.nfn12.start_forwarder()

        self.repo1.start_repo()

        self.simulation_bus.start_process()

        time.sleep(3)

        #setup forwarding rules
        self.mgmt_client0.add_face("nfn1", None, 0)
        self.mgmt_client0.add_forwarding_rule(Name("/lib0"), [0])

        self.mgmt_client0.add_face("nfn12", None, 0)
        self.mgmt_client0.add_forwarding_rule(Name("/lib1"), [1])

        self.mgmt_client01.add_face("nfn1", None, 0)
        self.mgmt_client01.add_forwarding_rule(Name("/lib1"), [0])

        self.mgmt_client1.add_face("repo1", None, 0)
        self.mgmt_client1.add_forwarding_rule(Name("/repo/r1"), [0])

        self.mgmt_client1.add_face("nfn0", None, 0)
        self.mgmt_client1.add_forwarding_rule(Name("/lib0"), [1])

        #setup function code

        #multi name
        self.mgmt_client1.add_new_content(
            Name("/lib0/multiname"),
            "PYTHONSTREAM\nmultiname\ndef multiname(arg):\n    a = get_next(arg)\n    a = a.upper()\n    b = get_next(arg)\n    b = b.upper()\n    c = get_next(arg)\n    c = c.upper()\n    d = get_next(arg)\n    d = d.upper()\n    e = get_next(arg)\n    e = e.upper()\n    f = get_next(arg)\n    f = f.upper()\n    g = get_next(arg)\n    g = g.upper()\n    h = get_next(arg)\n    h = h.upper()\n    i = get_next(arg)\n    i = i.upper()\n    j = get_next(arg)\n    j = j.upper()\n    return a + b + c + d + e + f + g + h + i + j"
        )

        #single name - two layer
        self.mgmt_client0.add_new_content(
            Name("/lib0/node0"),
            "PYTHONSTREAM\ngetnext_on_writeout\ndef getnext_on_writeout(arg):\n    print('Start äussere')\n    a = get_next(arg)\n    a = a.upper()\n    b = get_next(arg)\n    b = b.upper()\n    c = get_next(arg)\n    c = c.upper()\n    d = get_next(arg)\n    d = d.upper()\n    e = get_next(arg)\n    e = e.upper()\n    f = get_next(arg)\n    f = f.upper()\n    g = get_next(arg)\n    g = g.upper()\n    h = get_next(arg)\n    h = h.upper()\n    i = get_next(arg)\n    i = i.upper()\n    j = get_next(arg)\n    j = j.upper()\n    print('Ende äussere')\n    return a + b + c + d + e + f + g + h + i + j"
        )

        self.mgmt_client1.add_new_content(
            Name("/lib0/node1"),
            "PYTHONSTREAM\nwriteout_on_getnext\ndef writeout_on_getnext(arg):\n    print('Start innere')\n    write_out_on_get_next(arg)\n    return print('Ende innere')\n"
        )

        #single name - three layer
        self.mgmt_client0.add_new_content(
            Name("/lib1/node0"),
            "PYTHONSTREAM\ngetnext_on_writeout\ndef getnext_on_writeout(arg):\n    print('Start äussere')\n    a = get_next(arg)\n    a = a.upper()\n    b = get_next(arg)\n    b = b.upper()\n    c = get_next(arg)\n    c = c.upper()\n    d = get_next(arg)\n    d = d.upper()\n    e = get_next(arg)\n    e = e.upper()\n    f = get_next(arg)\n    f = f.upper()\n    g = get_next(arg)\n    g = g.upper()\n    h = get_next(arg)\n    h = h.upper()\n    i = get_next(arg)\n    i = i.upper()\n    j = get_next(arg)\n    j = j.upper()\n    print('Ende äussere')\n    return a + b + c + d + e + f + g + h + i + j"
        )

        self.mgmt_client01.add_new_content(
            Name("/lib1/node1"),
            "PYTHONSTREAM\nwriteout_on_getnext\ndef writeout_on_getnext(arg):\n    print('Start mittlere')\n    write_out_on_get_next(arg)\n    return print('Ende mittlere')\n"
        )

        self.mgmt_client1.add_new_content(
            Name("/lib1/node2"),
            "PYTHONSTREAM\nwriteout_on_getnext\ndef writeout_on_getnext(arg):\n    print('Start innere')\n    write_out_on_get_next(arg)\n    return print('Ende innere')\n"
        )

    def generate_name_files(self, path: str, number: int):
        with open(path + "/name" + str(number), "w") as f:
            f.write("Content of name" + str(number) + ". ")
        f.close()

    def setup_repo(self):
        self.path = "/tmp/repo1"
        try:
            os.stat(self.path)
        except:
            os.mkdir(self.path)
        with open(self.path + "/data1", 'w+') as content_file:
            content_file.write("sdo:\n")
            for i in range(1, 11):
                content_file.write("/repo/r1/name" + str(i) + "\n")
                self.generate_name_files(self.path, i)
            content_file.write("sdo:endstreaming\n")

    def tearDown_repo(self):
        try:
            shutil.rmtree(self.path)
            os.remove("/tmp/repo")
        except:
            pass

    def test_multiname(self):
        """Multiname test with input data from repo"""
        self.setup_repo()
        self.setup_faces_and_connections()

        name = Name("/lib0/multiname")
        name += '_(/repo/r1/data1)'
        name += "NFN"

        res = self.fetch_tool1.fetch_data(name, timeout=40)
        print(res)
        self.assertEqual(
            "CONTENT OF NAME1. CONTENT OF NAME2. CONTENT OF NAME3. CONTENT OF NAME4. CONTENT OF NAME5. CONTENT OF NAME6. CONTENT OF NAME7. CONTENT OF NAME8. CONTENT OF NAME9. CONTENT OF NAME10. ",
            res)

    def test_twolayer(self):
        """Singlename test over two layers with input data from repo"""
        self.setup_repo()
        self.setup_faces_and_connections()

        scenario_node_1 = Name("/lib0/node1")
        scenario_node_1 += "#(=/repo/r1/data1=)"
        scenario_node_1 += "NFN"

        scenario_node_0 = Name("/lib0/node0")
        scenario_node_0 += '_("' + str(scenario_node_1) + '")'
        scenario_node_0 += "NFN"

        res = self.fetch_tool1.fetch_data(scenario_node_0, timeout=40)
        print(res)
        self.assertEqual(
            "CONTENT OF NAME1. CONTENT OF NAME2. CONTENT OF NAME3. CONTENT OF NAME4. CONTENT OF NAME5. CONTENT OF NAME6. CONTENT OF NAME7. CONTENT OF NAME8. CONTENT OF NAME9. CONTENT OF NAME10. ",
            res)

    def test_threelayer(self):
        """Singlename test over three layers with input data from repo"""
        self.setup_repo()
        self.setup_faces_and_connections()

        scenario_node_2 = Name("/lib1/node2")
        scenario_node_2 += "#(=/repo/r1/data1=)"
        scenario_node_2 += "NFN"

        scenario_node_1 = Name("/lib1/node1")
        scenario_node_1 += "#(=" + str(scenario_node_2) + "=)"
        scenario_node_1 += "NFN"

        scenario_node_0 = Name("/lib1/node0")
        scenario_node_0 += '_("' + str(scenario_node_1) + '")'
        scenario_node_0 += "NFN"

        res = self.fetch_tool1.fetch_data(scenario_node_0, timeout=40)
        print(res)
        self.assertEqual(
            "CONTENT OF NAME1. CONTENT OF NAME2. CONTENT OF NAME3. CONTENT OF NAME4. CONTENT OF NAME5. CONTENT OF NAME6. CONTENT OF NAME7. CONTENT OF NAME8. CONTENT OF NAME9. CONTENT OF NAME10. ",
            res)
Ejemplo n.º 2
0
class Initiation(unittest.TestCase):
    """Simulate a Map Reduce Scenario where timeout prevention is required."""
    @abc.abstractmethod
    def get_encoder(self) -> BasicEncoder:
        return SimpleStringEncoder

    def setUp(self):
        self.encoder_type = self.get_encoder()
        self.simulation_bus = SimulationBus(packetencoder=self.encoder_type())

        self.fetch_tool1 = Fetch(
            "distributor",
            None,
            255,
            self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("fetchtool1")])

        self.distributor = NFNForwarder(
            port=0,
            encoder=self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("distributor")],
            log_level=255,
            ageing_interval=3)
        self.nfn1 = NFNForwarder(
            port=0,
            encoder=self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("nfn1")],
            log_level=255,
            ageing_interval=3)
        self.nfn2 = NFNForwarder(
            port=0,
            encoder=self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("nfn2")],
            log_level=255,
            ageing_interval=3)
        self.nfn3 = NFNForwarder(
            port=0,
            encoder=self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("nfn3")],
            log_level=255,
            ageing_interval=3)
        self.nfn4 = NFNForwarder(
            port=0,
            encoder=self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("nfn4")],
            log_level=255,
            ageing_interval=3)

        self.repo1 = ICNDataRepository(
            "/tmp/repo1", Name("/repo/r1"), 0, 255, self.encoder_type(), False,
            False, [self.simulation_bus.add_interface("repo1")])
        self.repo2 = ICNDataRepository(
            "/tmp/repo2", Name("/repo/r2"), 0, 255, self.encoder_type(), False,
            False, [self.simulation_bus.add_interface("repo2")])
        self.repo3 = ICNDataRepository(
            "/tmp/repo3", Name("/repo/r3"), 0, 255, self.encoder_type(), False,
            False, [self.simulation_bus.add_interface("repo3")])
        self.repo4 = ICNDataRepository(
            "/tmp/repo4", Name("/repo/r4"), 0, 255, self.encoder_type(), False,
            False, [self.simulation_bus.add_interface("repo4")])

        self.nfn1.icnlayer.pit.set_pit_timeout(50)
        self.nfn1.icnlayer.cs.set_cs_timeout(0)
        self.nfn2.icnlayer.pit.set_pit_timeout(50)
        self.nfn2.icnlayer.cs.set_cs_timeout(0)
        self.nfn3.icnlayer.pit.set_pit_timeout(50)
        self.nfn3.icnlayer.cs.set_cs_timeout(0)
        self.nfn4.icnlayer.pit.set_pit_timeout(50)
        self.nfn4.icnlayer.cs.set_cs_timeout(0)

        self.mgmt_client0 = MgmtClient(
            self.distributor.mgmt.mgmt_sock.getsockname()[1])
        self.mgmt_client1 = MgmtClient(
            self.nfn1.mgmt.mgmt_sock.getsockname()[1])
        self.mgmt_client2 = MgmtClient(
            self.nfn2.mgmt.mgmt_sock.getsockname()[1])
        self.mgmt_client3 = MgmtClient(
            self.nfn3.mgmt.mgmt_sock.getsockname()[1])
        self.mgmt_client4 = MgmtClient(
            self.nfn4.mgmt.mgmt_sock.getsockname()[1])

    def tearDown(self):
        self.distributor.stop_forwarder()
        self.nfn1.stop_forwarder()
        self.nfn2.stop_forwarder()
        self.nfn3.stop_forwarder()
        self.nfn4.stop_forwarder()
        self.repo1.stop_repo()
        self.repo2.stop_repo()
        self.repo3.stop_repo()
        self.repo4.stop_repo()
        self.fetch_tool1.stop_fetch()
        self.simulation_bus.stop_process()
        self.tearDown_repo()

    def setup_faces_and_connections(self):
        self.distributor.start_forwarder()
        self.nfn1.start_forwarder()
        self.nfn2.start_forwarder()
        self.nfn3.start_forwarder()
        self.nfn4.start_forwarder()

        self.repo1.start_repo()
        self.repo2.start_repo()
        self.repo3.start_repo()
        self.repo4.start_repo()

        self.simulation_bus.start_process()

        time.sleep(3)

        # setup forwarding rules
        self.mgmt_client0.add_face("nfn1", None, 0)
        self.mgmt_client0.add_forwarding_rule(Name("/lib"), [0])
        self.mgmt_client0.add_face("nfn2", None, 0)
        self.mgmt_client0.add_forwarding_rule(Name("/lib"), [1])
        self.mgmt_client0.add_face("nfn3", None, 0)
        self.mgmt_client0.add_forwarding_rule(Name("/lib"), [2])
        self.mgmt_client0.add_face("nfn4", None, 0)
        self.mgmt_client0.add_forwarding_rule(Name("/lib4/func4"), [3])

        self.mgmt_client1.add_face("repo1", None, 0)
        self.mgmt_client1.add_forwarding_rule(Name("/repo/r1"), [0])
        self.mgmt_client2.add_face("repo2", None, 0)
        self.mgmt_client2.add_forwarding_rule(Name("/repo/r2"), [0])
        self.mgmt_client3.add_face("repo3", None, 0)
        self.mgmt_client3.add_forwarding_rule(Name("/repo/r3"), [0])
        self.mgmt_client4.add_face("repo4", None, 0)
        self.mgmt_client4.add_forwarding_rule(Name("/repo/r4"), [0])

        self.mgmt_client1.add_face("nfn0", None, 0)
        self.mgmt_client1.add_forwarding_rule(Name("/lib"), [1])
        self.mgmt_client2.add_face("nfn0", None, 0)
        self.mgmt_client2.add_forwarding_rule(Name("/lib"), [1])
        self.mgmt_client3.add_face("nfn0", None, 0)
        self.mgmt_client3.add_forwarding_rule(Name("/lib"), [1])
        self.mgmt_client4.add_face("nfn0", None, 0)
        self.mgmt_client4.add_forwarding_rule(Name("/lib4/func4"), [1])

        #setup function code
        #self.mgmt_client1.add_new_content(Name("/lib/func1"),"PYTHON\nf\ndef f(n):\n return n")
        self.mgmt_client1.add_new_content(
            Name("/lib/func1"),
            "PYTHON\nf\ndef f():\n  result =[]\n  x,y =0,1\n  while x<n:\n    result.append(x)\n    x,y = y, y+x\n  return result"
        )
        self.mgmt_client2.add_new_content(
            Name("/lib/func1"),
            "PYTHON\nf\ndef f(n):\n  result =[]\n  x,y =0,1\n  while x<n:\n    result.append(x)\n    x,y = y, y+x\n  return result"
        )
        # self.mgmt_client2.add_new_content(Name("/lib/func2"),"func2")
        self.mgmt_client3.add_new_content(
            Name("/lib/func1"),
            "PYTHON\nf\ndef f(n):\n  result =[]\n  x,y =0,1\n  while x<n:\n    result.append(x)\n    x,y = y, y+x\n  return result"
        )
        self.mgmt_client4.add_new_content(Name("/lib4/func4"), "func4")

        # self.mgmt_client1.add_new_content(Name("/lib/func1"),
        #                                   "PYTHON\nf\ndef f():\n    for i in range(0,100000000):\n        a.upper()\n    return a.upper()")
        # self.mgmt_client2.add_new_content(Name("/lib/func2"),
        #                                   "PYTHON\nf\ndef f(a):\n    for i in range(0,100000000):\n        a.upper()\n    return a.upper()")
        # self.mgmt_client3.add_new_content(Name("/lib/func3"),
        #                                   "PYTHON\nf\ndef f(a):\n    for i in range(0,100000000):\n        a.upper()\n    return a.upper()")
        # self.mgmt_client4.add_new_content(Name("/lib/func4"),
        #                                   "PYTHON\nf\ndef f(a):\n    for i in range(0,100000000):\n        a.upper()\n    return a.upper()")
        #

    def setup_repo(self):
        for i in range(1, 5):
            self.path = "/tmp/repo" + str(i)
            try:
                os.stat(self.path)
            except:
                os.mkdir(self.path)
            with open(self.path + "/data" + str(i), 'w+') as content_file:
                content_file.write("data" + str(i))

    def tearDown_repo(self):
        try:
            shutil.rmtree(self.path)
            os.remove("/tmp/repo")
        except:
            pass

    def test_simple_Fs(self):
        self.setup_repo()
        self.setup_faces_and_connections()
        name1 = Name("/lib/func1")
        name1 += '_()'
        name1 += "NFN"

        name2 = Name("/lib/func1")
        name2 += '_(500000)'
        name2 += "NFN"

        name3 = Name("/lib/func1")
        name3 += '_(5000)'
        name3 += "NFN"

        name4 = Name("/lib/func1")
        name4 += '_(900000000000000000)'
        name4 += "NFN"

        name5 = Name("/lib/func1")
        name5 += '_(68899455874)'
        name5 += "NFN"

        t1 = Fs_thread(name1, fetch_tool=self.fetch_tool1)
        t2 = Fs_thread(name2, fetch_tool=self.fetch_tool1)
        t3 = Fs_thread(name3, fetch_tool=self.fetch_tool1)
        t4 = Fs_thread(name4, fetch_tool=self.fetch_tool1)
        t5 = Fs_thread(name5, fetch_tool=self.fetch_tool1)

        t1.start()
        t2.start()
        t3.start()
        t4.start()
        t5.start()
        t1.join()
        t2.join()
        t3.join()
        t4.join()
        t5.join()
Ejemplo n.º 3
0
def simulation(classic, amount_of_parts):
    generateExampleFiles("../InputFiles/exampleInputFile", amount_of_parts)

    simulation_bus = SimulationBus(packetencoder=NdnTlvEncoder())

    nfn_fwd0 = NFNForwarder(
        port=0,
        encoder=NdnTlvEncoder(),
        interfaces=[simulation_bus.add_interface("nfn0")],
        log_level=255,
        executors={"PYTHONSTREAM": NFNPythonExecutorStreaming()},
        ageing_interval=1)
    nfn_fwd0.executors["PYTHONSTREAM"].initialize_executor(
        nfn_fwd0.nfnlayer.queue_to_lower, nfn_fwd0.nfnlayer.queue_from_lower,
        nfn_fwd0.nfnlayer.cs, classic)
    nfn_fwd0.icnlayer.pit.ageing = ageing
    nfn_fwd0.timeoutpreventionlayer.ageing = ageing

    nfn_fwd1 = NFNForwarder(
        port=0,
        encoder=NdnTlvEncoder(),
        interfaces=[simulation_bus.add_interface("nfn1")],
        log_level=255,
        executors={"PYTHONSTREAM": NFNPythonExecutorStreaming()},
        ageing_interval=1)
    nfn_fwd1.executors["PYTHONSTREAM"].initialize_executor(
        nfn_fwd1.nfnlayer.queue_to_lower, nfn_fwd1.nfnlayer.queue_from_lower,
        nfn_fwd1.nfnlayer.cs, classic)
    nfn_fwd1.icnlayer.pit.ageing = ageing
    nfn_fwd1.timeoutpreventionlayer.ageing = ageing

    nfn_fwd2 = NFNForwarder(
        port=0,
        encoder=NdnTlvEncoder(),
        interfaces=[simulation_bus.add_interface("nfn2")],
        log_level=255,
        executors={"PYTHONSTREAM": NFNPythonExecutorStreaming()},
        ageing_interval=1)
    nfn_fwd2.executors["PYTHONSTREAM"].initialize_executor(
        nfn_fwd2.nfnlayer.queue_to_lower, nfn_fwd2.nfnlayer.queue_from_lower,
        nfn_fwd2.nfnlayer.cs, classic)
    nfn_fwd2.icnlayer.pit.ageing = ageing
    nfn_fwd2.timeoutpreventionlayer.ageing = ageing

    repo = ICNDataRepository("../InputFiles",
                             Name("/repo/r1"),
                             0,
                             255,
                             NdnTlvEncoder(),
                             False,
                             False,
                             interfaces=[simulation_bus.add_interface("repo")])
    repo.start_repo()

    mgmt_client0 = MgmtClient(nfn_fwd0.mgmt.mgmt_sock.getsockname()[1])
    mgmt_client1 = MgmtClient(nfn_fwd1.mgmt.mgmt_sock.getsockname()[1])
    mgmt_client2 = MgmtClient(nfn_fwd2.mgmt.mgmt_sock.getsockname()[1])

    fetch_tool = Fetch("nfn0",
                       None,
                       255,
                       NdnTlvEncoder(),
                       interfaces=[simulation_bus.add_interface("fetchtoo1")])

    nfn_fwd0.start_forwarder()
    nfn_fwd1.start_forwarder()
    nfn_fwd2.start_forwarder()

    simulation_bus.start_process()

    mgmt_client0.add_face("nfn1", None, 0)
    mgmt_client0.add_forwarding_rule(Name("/lib"), [0])
    mgmt_client1.add_face("nfn2", None, 0)
    mgmt_client1.add_forwarding_rule(Name("/lib"), [0])
    mgmt_client2.add_face("repo", None, 0)
    mgmt_client2.add_forwarding_rule(Name("/repo/r1"), [0])

    mgmt_client0.add_new_content(
        Name("/lib/node0"),
        "PYTHONSTREAM\ngetnext_on_writeout\ndef getnext_on_writeout(arg):\n    print('Start dritte')\n    res = ''\n    a = get_next(arg)\n    while a != None:\n        res = res + a\n        a = get_next(arg)\n    print('Ende dritte')\n    return res"
    )

    mgmt_client1.add_new_content(
        Name("/lib/node1"),
        "PYTHONSTREAM\nwriteout_on_getnext\ndef writeout_on_getnext(arg):\n    print('Start zweite')\n    a = get_next(arg)\n    while a and check_end_streaming(a) is False:\n        a = a.upper()\n        sleep(1)\n        write_out(a)\n        a = get_next(arg)\n    last_write_out()\n    return print('Ende zweite')\n"
    )

    mgmt_client2.add_new_content(
        Name("/lib/node2"),
        "PYTHONSTREAM\nwriteout_on_getnext\ndef writeout_on_getnext(arg):\n    print('Start erste')\n    write_out_on_get_next(arg)\n    return print('Ende erste')\n"
    )

    scenario_node_2 = Name("/lib/node2")
    scenario_node_2 += "#(=/repo/r1/exampleInputFile=)"
    scenario_node_2 += "NFN"

    scenario_node_1 = Name("/lib/node1")
    scenario_node_1 += "#(=" + str(scenario_node_2) + "=)"
    scenario_node_1 += "NFN"

    scenario_node_0 = Name("/lib/node0")
    scenario_node_0 += '_("' + str(scenario_node_1) + '")'
    scenario_node_0 += "NFN"

    start_time = time.perf_counter()
    res = fetch_tool.fetch_data(scenario_node_0, timeout=60)
    end_time = time.perf_counter()
    time_needed = end_time - start_time

    if res.startswith("CONTENT OF NAME"):
        print("Time:", time_needed, res)
        return time_needed
    else:
        print("Something went wrong")
        None
Ejemplo n.º 4
0
class test_NFNForwarderThunks(unittest.TestCase):
    """Test NFN Forwarder with Thunks"""

    #TODO: Add optimizer to execute the plans
    #TODO: Add REPO TO TEST

    def get_encoder(self):
        return NdnTlvEncoder()

    def setUp(self):
        self.encoder = self.get_encoder()
        self.forwarder1 = NFNForwarder(0,
                                       encoder=self.get_encoder(),
                                       log_level=255,
                                       use_thunks=True)

        self.forwarder2 = NFNForwarder(0,
                                       encoder=self.get_encoder(),
                                       log_level=255,
                                       use_thunks=True)
        self.forwarder3 = NFNForwarder(0,
                                       encoder=self.get_encoder(),
                                       log_level=255,
                                       use_thunks=True)
        self.forwarder1_port = self.forwarder1.linklayer.interfaces[
            0].get_port()
        self.forwarder2_port = self.forwarder2.linklayer.interfaces[
            0].get_port()
        self.forwarder3_port = self.forwarder3.linklayer.interfaces[
            0].get_port()

        self.forwarder1.icnlayer.pit.set_pit_timeout(10)
        self.forwarder1.icnlayer.pit.set_pit_retransmits(10)

        self.client = Fetch("127.0.0.1", self.forwarder1_port, 255,
                            self.get_encoder())
        self.mgmt1 = MgmtClient(self.forwarder1_port)
        self.mgmt2 = MgmtClient(self.forwarder2_port)
        self.mgmt3 = MgmtClient(self.forwarder3_port)

        self.forwarder1.start_forwarder()
        self.forwarder2.start_forwarder()
        self.forwarder3.start_forwarder()

        self.mgmt1.add_face("127.0.0.1", self.forwarder2_port, 0)
        self.mgmt1.add_face("127.0.0.1", self.forwarder3_port, 0)

        self.mgmt1.add_forwarding_rule(Name("/dat"), [0])
        self.mgmt1.add_forwarding_rule(Name("/fct"), [1])

        self.content1 = "This is our test content object" * 20
        self.function1 = "PYTHON\nf\ndef f(a):\n    return a.upper()"

        self.mgmt2.add_new_content(Name("/dat/data/d1"), self.content1)
        self.mgmt3.add_new_content(Name("/fct/f1"), self.function1)

    def tearDown(self):
        self.forwarder1.stop_forwarder()
        self.forwarder2.stop_forwarder()
        self.forwarder3.stop_forwarder()
        self.client.stop_fetch()

    def test_simple_thunk_query(self):
        """Test a simple thunk query"""
        name = Name("/fct/f1")
        name += "_(/dat/data/d1)"
        name += "THUNK"
        name += "NFN"
        res = self.client.fetch_data(name, timeout=4)
        self.assertEqual(res, "659")
        #print(self.forwarder1.thunk_layer.planTable.get_plan(self.forwarder1.thunk_layer.removeThunkMarker(name)))
        name2 = Name("/fct/f1")
        name2 += "_(/dat/data/d1)"
        name2 += "NFN"
        res2 = self.client.fetch_data(name2)
        self.assertEqual(res2, self.content1.upper())

    def test_simple_thunk_query_additional_fwd_rule_to_fct(self):
        """Test a simple thunk query. Add additional rule to have cheap computation at data location"""
        self.mgmt2.add_face("127.0.0.1", self.forwarder3_port, 0)
        self.mgmt2.add_forwarding_rule(Name("/fct"), [0])
        name = Name("/fct/f1")
        name += "_(/dat/data/d1)"
        name += "THUNK"
        name += "NFN"
        res = self.client.fetch_data(name, timeout=4)
        self.assertEqual(res, "39")
        #print(self.forwarder1.thunk_layer.planTable.get_plan(self.forwarder1.thunk_layer.removeThunkMarker(name)))
        name2 = Name("/fct/f1")
        name2 += "_(/dat/data/d1)"
        name2 += "NFN"
        res2 = self.client.fetch_data(name2)
        self.assertEqual(res2, self.content1.upper())

    def test_simple_thunk_query_additional_fwd_rule_to_data(self):
        """Test a simple thunk query. Add additional rule to have cheap computation at fct location"""
        self.mgmt3.add_face("127.0.0.1", self.forwarder2_port, 0)
        self.mgmt3.add_forwarding_rule(Name("/dat"), [0])
        name = Name("/fct/f1")
        name += "_(/dat/data/d1)"
        name += "THUNK"
        name += "NFN"
        res = self.client.fetch_data(name, timeout=4)
        self.assertEqual(res, "620")
        #print(self.forwarder1.thunk_layer.planTable.get_plan(self.forwarder1.thunk_layer.removeThunkMarker(name)))
        name2 = Name("/fct/f1")
        name2 += "_(/dat/data/d1)"
        name2 += "NFN"
        res2 = self.client.fetch_data(name2)
        self.assertEqual(res2, self.content1.upper())

    def test_simple_thunk_query_additional_fwd_rule_to_fct_and_data(self):
        """Test a simple thunk query. Add additional rule to have cheap computation at fct and data location"""
        self.mgmt2.add_face("127.0.0.1", self.forwarder3_port, 0)
        self.mgmt2.add_forwarding_rule(Name("/fct"), [0])
        self.mgmt3.add_face("127.0.0.1", self.forwarder2_port, 0)
        self.mgmt3.add_forwarding_rule(Name("/dat"), [0])
        name = Name("/fct/f1")
        name += "_(/dat/data/d1)"
        name += "THUNK"
        name += "NFN"
        res = self.client.fetch_data(name, timeout=50)
        self.assertEqual(res, "39")
        #print(self.forwarder1.thunk_layer.planTable.get_plan(self.forwarder1.thunk_layer.removeThunkMarker(name)))
        name2 = Name("/fct/f1")
        name2 += "_(/dat/data/d1)"
        name2 += "NFN"
        res2 = self.client.fetch_data(name2)
        self.assertEqual(res2, self.content1.upper())
Ejemplo n.º 5
0
class MapReduceSimulation(unittest.TestCase):
    """Simulate a Map Reduce Scenario where timeout prevention is required."""
    @abc.abstractmethod
    def get_encoder(self) -> BasicEncoder:
        return SimpleStringEncoder

    def setUp(self):
        self.encoder_type = self.get_encoder()
        self.simulation_bus = SimulationBus(packetencoder=self.encoder_type())

        self.fetch_tool1 = Fetch(
            "nfn0",
            None,
            255,
            self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("fetchtool1")])

        self.nfn0 = NFNForwarder(
            port=0,
            encoder=self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("nfn0")],
            log_level=255,
            ageing_interval=1)
        self.nfn1 = NFNForwarder(
            port=0,
            encoder=self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("nfn1")],
            log_level=255,
            ageing_interval=1)
        self.nfn2 = NFNForwarder(
            port=0,
            encoder=self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("nfn2")],
            log_level=255,
            ageing_interval=1)
        self.nfn3 = NFNForwarder(
            port=0,
            encoder=self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("nfn3")],
            log_level=255,
            ageing_interval=1)
        self.nfn4 = NFNForwarder(
            port=0,
            encoder=self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("nfn4")],
            log_level=255,
            ageing_interval=1)

        self.repo1 = ICNDataRepository(
            "/tmp/repo1", Name("/repo/r1"), 0, 255, self.encoder_type(), False,
            False, [self.simulation_bus.add_interface("repo1")])
        self.repo2 = ICNDataRepository(
            "/tmp/repo2", Name("/repo/r2"), 0, 255, self.encoder_type(), False,
            False, [self.simulation_bus.add_interface("repo2")])
        self.repo3 = ICNDataRepository(
            "/tmp/repo3", Name("/repo/r3"), 0, 255, self.encoder_type(), False,
            False, [self.simulation_bus.add_interface("repo3")])
        self.repo4 = ICNDataRepository(
            "/tmp/repo4", Name("/repo/r4"), 0, 255, self.encoder_type(), False,
            False, [self.simulation_bus.add_interface("repo4")])

        self.nfn1.icnlayer.pit.set_pit_timeout(0)
        self.nfn1.icnlayer.cs.set_cs_timeout(30)
        self.nfn2.icnlayer.pit.set_pit_timeout(0)
        self.nfn2.icnlayer.cs.set_cs_timeout(30)
        self.nfn3.icnlayer.pit.set_pit_timeout(0)
        self.nfn3.icnlayer.cs.set_cs_timeout(30)
        self.nfn4.icnlayer.pit.set_pit_timeout(0)
        self.nfn4.icnlayer.cs.set_cs_timeout(30)

        self.nfn0.nfnlayer.optimizer = MapReduceOptimizer(
            self.nfn0.icnlayer.cs, self.nfn0.icnlayer.fib,
            self.nfn0.icnlayer.pit, self.nfn0.linklayer.faceidtable)
        self.nfn1.nfnlayer.optimizer = MapReduceOptimizer(
            self.nfn1.icnlayer.cs, self.nfn1.icnlayer.fib,
            self.nfn1.icnlayer.pit, self.nfn1.linklayer.faceidtable)
        self.nfn2.nfnlayer.optimizer = MapReduceOptimizer(
            self.nfn2.icnlayer.cs, self.nfn2.icnlayer.fib,
            self.nfn2.icnlayer.pit, self.nfn2.linklayer.faceidtable)
        self.nfn3.nfnlayer.optimizer = MapReduceOptimizer(
            self.nfn3.icnlayer.cs, self.nfn3.icnlayer.fib,
            self.nfn3.icnlayer.pit, self.nfn3.linklayer.faceidtable)
        self.nfn4.nfnlayer.optimizer = MapReduceOptimizer(
            self.nfn4.icnlayer.cs, self.nfn4.icnlayer.fib,
            self.nfn4.icnlayer.pit, self.nfn4.linklayer.faceidtable)

        self.mgmt_client0 = MgmtClient(
            self.nfn0.mgmt.mgmt_sock.getsockname()[1])
        self.mgmt_client1 = MgmtClient(
            self.nfn1.mgmt.mgmt_sock.getsockname()[1])
        self.mgmt_client2 = MgmtClient(
            self.nfn2.mgmt.mgmt_sock.getsockname()[1])
        self.mgmt_client3 = MgmtClient(
            self.nfn3.mgmt.mgmt_sock.getsockname()[1])
        self.mgmt_client4 = MgmtClient(
            self.nfn4.mgmt.mgmt_sock.getsockname()[1])

    def tearDown(self):
        self.nfn0.stop_forwarder()
        self.nfn1.stop_forwarder()
        self.nfn2.stop_forwarder()
        self.nfn3.stop_forwarder()
        self.nfn4.stop_forwarder()
        self.repo1.stop_repo()
        self.repo2.stop_repo()
        self.repo3.stop_repo()
        self.repo4.stop_repo()
        self.fetch_tool1.stop_fetch()
        self.simulation_bus.stop_process()
        self.tearDown_repo()

    def setup_faces_and_connections(self):
        self.nfn0.start_forwarder()
        self.nfn1.start_forwarder()
        self.nfn2.start_forwarder()
        self.nfn3.start_forwarder()
        self.nfn4.start_forwarder()

        self.repo1.start_repo()
        self.repo2.start_repo()
        self.repo3.start_repo()
        self.repo4.start_repo()

        self.simulation_bus.start_process()

        time.sleep(3)

        # setup forwarding rules
        self.mgmt_client0.add_face("nfn1", None, 0)
        self.mgmt_client0.add_forwarding_rule(Name("/lib/func1"), [0])
        self.mgmt_client0.add_face("nfn2", None, 0)
        self.mgmt_client0.add_forwarding_rule(Name("/lib/func2"), [1])
        self.mgmt_client0.add_face("nfn3", None, 0)
        self.mgmt_client0.add_forwarding_rule(Name("/lib/func3"), [2])
        self.mgmt_client0.add_face("nfn4", None, 0)
        self.mgmt_client0.add_forwarding_rule(Name("/lib/func4"), [3])

        self.mgmt_client1.add_face("repo1", None, 0)
        self.mgmt_client1.add_forwarding_rule(Name("/repo/r1"), [0])
        self.mgmt_client2.add_face("repo2", None, 0)
        self.mgmt_client2.add_forwarding_rule(Name("/repo/r2"), [0])
        self.mgmt_client3.add_face("repo3", None, 0)
        self.mgmt_client3.add_forwarding_rule(Name("/repo/r3"), [0])
        self.mgmt_client4.add_face("repo4", None, 0)
        self.mgmt_client4.add_forwarding_rule(Name("/repo/r4"), [0])

        self.mgmt_client1.add_face("nfn0", None, 0)
        self.mgmt_client1.add_forwarding_rule(Name("/lib"), [1])

        #setup function code
        self.mgmt_client0.add_new_content(
            Name("/lib/reduce4"),
            "PYTHON\nreduce4\ndef reduce4(a,b,c,d):\n     return a+b+c+d")
        self.mgmt_client1.add_new_content(
            Name("/lib/func1"),
            "PYTHON\nf\ndef f(a):\n    for i in range(0,100000000):\n        a.upper()\n    return a.upper()"
        )
        self.mgmt_client2.add_new_content(
            Name("/lib/func2"),
            "PYTHON\nf\ndef f(a):\n    for i in range(0,100000000):\n        a.upper()\n    return a.upper()"
        )
        self.mgmt_client3.add_new_content(
            Name("/lib/func3"),
            "PYTHON\nf\ndef f(a):\n    for i in range(0,100000000):\n        a.upper()\n    return a.upper()"
        )
        self.mgmt_client4.add_new_content(
            Name("/lib/func4"),
            "PYTHON\nf\ndef f(a):\n    for i in range(0,100000000):\n        a.upper()\n    return a.upper()"
        )

    def setup_repo(self):
        for i in range(1, 5):
            self.path = "/tmp/repo" + str(i)
            try:
                os.stat(self.path)
            except:
                os.mkdir(self.path)
            with open(self.path + "/data" + str(i), 'w+') as content_file:
                content_file.write("data" + str(i))

    def tearDown_repo(self):
        try:
            shutil.rmtree(self.path)
            os.remove("/tmp/repo")
        except:
            pass

    def test_simple_map_reduce(self):
        """Simple map reduce test with input data as string parameter"""
        self.setup_faces_and_connections()

        name = Name("/lib/reduce4")
        name += '_(/lib/func1("helloworld1"),/lib/func2("helloworld2"),/lib/func3("helloworld3"),/lib/func4("helloworld4"))'
        name += "NFN"

        res = self.fetch_tool1.fetch_data(name, timeout=0)
        time.sleep(3)
        print(res)
        self.assertEqual("HELLOWORLD1HELLOWORLD2HELLOWORLD3HELLOWORLD4", res)

    def test_simple_map_reduce_data_from_repo(self):
        """Simple map reduce test with input data from repo"""
        self.setup_repo()
        self.setup_faces_and_connections()

        name = Name("/lib/reduce4")
        name += '_(/lib/func1(/repo/r1/data1),/lib/func2(/repo/r2/data2),/lib/func3(/repo/r3/data3),/lib/func4(/repo/r4/data4))'
        name += "NFN"

        res = self.fetch_tool1.fetch_data(name, timeout=0)
        time.sleep(3)
        print(res)
        self.assertEqual("DATA1DATA2DATA3DATA4", res)

    def test_simple_map_reduce_data_from_repo_to_data(self):
        """Simple map reduce test with input data from repo forwarding to data"""
        self.setup_repo()
        self.setup_faces_and_connections()

        name = Name("/repo/r1/data1")
        name += '/lib/reduce4(/lib/func1(_),/lib/func2(/repo/r2/data2),/lib/func3(/repo/r3/data3),/lib/func4(/repo/r4/data4))'
        name += "NFN"

        res = self.fetch_tool1.fetch_data(name, timeout=0)
        time.sleep(3)
        print(res)
        self.assertEqual("DATA1DATA2DATA3DATA4", res)
Ejemplo n.º 6
0
class cases_FetchNFN(object):

    @abc.abstractmethod
    def get_encoder(self):
        """get the packet encoder to be used"""

    def setUp(self):
        self.data1 = "data1"
        self.data2 = 'a' * 5000
        self.data3 = 'b' * 20000

        self.path = "/tmp/repo_unit_test"
        try:
            os.stat(self.path)
        except:
            os.mkdir(self.path)
        with open(self.path + "/d1", 'w+') as content_file:
            content_file.write(self.data1)
        with open(self.path + "/d2", 'w+') as content_file:
            content_file.write(self.data2)
        with open(self.path + "/d3", 'w+') as content_file:
            content_file.write('b' * 20000)

        self.ICNRepo: ICNDataRepository = ICNDataRepository("/tmp/repo_unit_test", Name("/test/data"), 0,
                                                            encoder=self.get_encoder(), log_level=255)
        self.forwarder1: NFNForwarder = NFNForwarder(0, log_level=255, encoder=self.get_encoder())
        self.forwarder2: NFNForwarder = NFNForwarder(0, log_level=255, encoder=self.get_encoder())

        self.repo_port = self.ICNRepo.linklayer.interfaces[0].get_port()
        self.fwd_port1 = self.forwarder1.linklayer.interfaces[0].get_port()
        self.fwd_port2 = self.forwarder2.linklayer.interfaces[0].get_port()

        self.fetch = Fetch("127.0.0.1", self.fwd_port1, encoder=self.get_encoder())

    def add_face_and_forwadingrule(self):
        #create new face
        self.mgmtClient1 = MgmtClient(self.fwd_port1)
        self.mgmtClient1.add_face("127.0.0.1", self.fwd_port2, 0)
        self.mgmtClient1.add_forwarding_rule(Name("/lib"), [0])
        self.mgmtClient1.add_face("127.0.0.1", self.repo_port, 0)
        self.mgmtClient1.add_forwarding_rule(Name("/test"), [0])

        self.mgmtClient2 = MgmtClient(self.fwd_port2)
        self.mgmtClient2.add_face("127.0.0.1", self.repo_port, 0)
        self.mgmtClient2.add_forwarding_rule(Name("/test"), [0])

    def tearDown(self):
        try:
            shutil.rmtree(self.path)
            os.remove(self.path)
        except:
            pass
        try:
            self.mgmtClient1.shutdown()
            self.mgmtClient2.shutdown()
            self.ICNRepo.stop_repo()
            self.forwarder1.stop_forwarder()
            self.forwarder2.stop_forwarder()
            self.fetch.stop_fetch()
        except:
            pass

    def test_fetch_single_data_from_repo_over_forwarder(self):
        """Test fetch data from repo over forwarder"""
        self.ICNRepo.start_repo()
        self.forwarder1.start_forwarder()
        self.forwarder2.start_forwarder()
        time.sleep(2)
        self.add_face_and_forwadingrule()
        time.sleep(5)
        fetch_name = "/test/data/d1"
        try:
            content = None
            while content is None or content == 'Received Nack: no forwarding rule':
                content = self.fetch.fetch_data(fetch_name, timeout=20)
        except:
            self.fail
        self.assertEqual(self.data1, content)

    def test_fetch_chunked_data_from_repo_over_forwarder(self):
        """Test fetch chunked data from repo over forwarder"""
        self.ICNRepo.start_repo()
        self.forwarder1.start_forwarder()
        self.forwarder2.start_forwarder()
        time.sleep(2)
        self.add_face_and_forwadingrule()
        time.sleep(5)
        fetch_name = "/test/data/d3"
        content = None
        while content is None or content == 'Received Nack: no forwarding rule':
            content = self.fetch.fetch_data(fetch_name, timeout=20)
        self.assertEqual(self.data3, content)

    def test_compute_on_single_data_over_forwarder(self):
        """Test fetch result with single input data"""
        self.ICNRepo.start_repo()
        self.forwarder1.start_forwarder()
        self.forwarder2.start_forwarder()
        time.sleep(2)
        self.add_face_and_forwadingrule()
        time.sleep(5)
        self.mgmtClient1.add_new_content(Name("/test/data/d1"), self.data1)
        self.mgmtClient2.add_new_content(Name("/lib/func/f1"), "PYTHON\nf\ndef f(a):\n    return a.upper()")
        fetch_name = Name("/lib/func/f1")
        fetch_name += "_(/test/data/d1)"
        fetch_name += "NFN"
        try:
            content = None
            while content is None or content == 'Received Nack: no forwarding rule':
                content = self.fetch.fetch_data(fetch_name, timeout=20)
        except:
            self.fail()
        self.assertEqual(self.data1.upper(), content)

    def test_compute_on_single_data_over_forwarder_data_from_repo(self):
        """Test fetch result with single input data from repo"""
        self.ICNRepo.start_repo()
        self.forwarder1.start_forwarder()
        self.forwarder2.start_forwarder()
        time.sleep(2)
        self.add_face_and_forwadingrule()
        time.sleep(5)
        self.mgmtClient2.add_new_content(Name("/lib/func/f1"), "PYTHON\nf\ndef f(a):\n    return a.upper()")
        fetch_name = Name("/lib/func/f1")
        fetch_name += "_(/test/data/d1)"
        fetch_name += "NFN"
        try:
            content = self.fetch.fetch_data(fetch_name, timeout=20)
        except:
            self.fail()
        self.assertEqual(self.data1.upper(), content)

    def test_compute_on_large_data_over_forwarder_data_from_repo(self):
        """Test fetch result with large input data from repo"""
        self.ICNRepo.start_repo()
        self.forwarder1.start_forwarder()
        self.forwarder2.start_forwarder()
        time.sleep(2)
        self.add_face_and_forwadingrule()
        time.sleep(5)
        self.mgmtClient2.add_new_content(Name("/lib/func/f1"), "PYTHON\nf\ndef f(a):\n    return a.upper()")
        fetch_name = Name("/lib/func/f1")
        fetch_name += "_(/test/data/d3)"
        fetch_name += "NFN"
        time.sleep(1)
        content = None
        try:
            while content is None or content == 'Received Nack: no forwarding rule':
                content = self.fetch.fetch_data(fetch_name, timeout=20)
        except:
            self.fail()
        self.assertEqual(self.data3.upper(), content)

    def test_compute_on_large_data_over_forwarder_data_from_repo_to_data_prefix(self):
        """Test fetch result with large input data from repo with a to data prefix"""
        self.ICNRepo.start_repo()
        self.forwarder1.start_forwarder()
        self.forwarder2.start_forwarder()
        time.sleep(2)
        self.add_face_and_forwadingrule()
        time.sleep(5)
        self.mgmtClient2.add_new_content(Name("/lib/func/f1"), "PYTHON\nf\ndef f(a):\n    return a.upper()")
        fetch_name = Name("/test/data/d3")
        fetch_name += "/lib/func/f1(_)"
        fetch_name += "NFN"
        time.sleep(1)
        content = None
        try:
            while content is None or content == 'Received Nack: no forwarding rule':
                content = self.fetch.fetch_data(fetch_name, timeout=20)
        except:
            self.fail()
        self.assertEqual(self.data3.upper(), content)


    def test_fetch_single_data_from_repo_over_forwarder_native_code(self):
        """Test fetch data from repo over forwarder using native code"""
        if platform.system() != 'Darwin':
            self.skipTest("Test only for OSX available")
        execs = {"PYTHON": NFNPythonExecutor(), "x86": x86Executor()}
        self.forwarder1 = NFNForwarder(0, log_level=255, encoder=self.get_encoder(), executors=execs)
        self.fwd_port1 = self.forwarder1.linklayer.interfaces[0].get_port()
        self.fetch = Fetch("127.0.0.1", self.fwd_port1, encoder=self.get_encoder())
        self.ICNRepo.start_repo()
        self.forwarder1.start_forwarder()
        self.forwarder2.start_forwarder()
        time.sleep(2)
        self.add_face_and_forwadingrule()
        nfnfile = open('NFN-x86-file-osx', 'r')
        content_obj = nfnfile.read()
        self.forwarder1.icnlayer.cs.add_content_object(Content("/func/native/test", content_obj))
        #self.mgmtClient1.add_new_content(Name("/func/native/test"), content_obj)
        time.sleep(5)
        fetch_name = Name("/func/native/test")
        fetch_name += '_("hello")'
        fetch_name += 'NFN'
        try:
            content = None
            while content is None or content == 'Received Nack: no forwarding rule':
                content = self.fetch.fetch_data(fetch_name, timeout=20)
        except:
            self.fail
        self.assertEqual("5", content)
Ejemplo n.º 7
0
class cases_NFNForwarder(object):
    """Test the ICN Forwarder"""
    @abc.abstractmethod
    def get_encoder(self):
        """returns the encoder to be used """

    def setUp(self):
        self.encoder = self.get_encoder()
        self.forwarder1 = NFNForwarder(0,
                                       encoder=self.get_encoder(),
                                       log_level=255)
        self.forwarder2 = NFNForwarder(0,
                                       encoder=self.get_encoder(),
                                       log_level=255)
        self.forwarder1_port = self.forwarder1.linklayer.interfaces[
            0].get_port()
        self.forwarder2_port = self.forwarder2.linklayer.interfaces[
            0].get_port()

        self.testSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.testSock.bind(("0.0.0.0", 0))

    def tearDown(self):
        self.testSock.close()
        self.forwarder1.stop_forwarder()
        self.forwarder2.stop_forwarder()
        pass

    def test_NFNForwarder_simple_find_content_one_node(self):
        """Test a simple forwarding scenario, getting content from a Node"""
        self.forwarder1.start_forwarder()

        # new content
        testMgmtSock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        testMgmtSock1.connect(("127.0.0.1", self.forwarder1_port))
        testMgmtSock1.send(
            "GET /icnlayer/newcontent/%2Ftest%2Fdata%2Fobject:HelloWorld HTTP/1.1\r\n\r\n"
            .encode())
        data = testMgmtSock1.recv(1024)
        testMgmtSock1.close()
        time.sleep(3)

        self.assertEqual(
            data.decode(),
            "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newcontent OK\r\n"
        )

        #create test content
        name = Name("/test/data/object")
        test_content = Content(name, content="HelloWorld")
        cs_fwd1 = self.forwarder1.icnlayer.cs
        self.assertEqual(
            cs_fwd1.find_content_object(name).content, test_content)

        #create interest
        interest = Interest("/test/data/object")
        encoded_interest = self.encoder.encode(interest)
        #send interest
        self.testSock.sendto(encoded_interest,
                             ("127.0.0.1", self.forwarder1_port))
        #receive content
        encoded_content, addr = self.testSock.recvfrom(8192)
        content = self.encoder.decode(encoded_content)
        self.assertEqual(content, test_content)

    def test_NFNForwarder_simple_find_content_two_nodes(self):
        """Test a simple forwarding scenario with one additional node forwarding the data"""
        self.forwarder1.start_forwarder()
        self.forwarder2.start_forwarder()
        #client <---> node1 <---> node2

        #create a face
        testMgmtSock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        testMgmtSock1.connect(("127.0.0.1", self.forwarder1_port))
        port_to = self.forwarder2_port
        testMgmtSock1.send(("GET /linklayer/newface/127.0.0.1:" +
                            str(port_to) + ":0 HTTP/1.1\r\n\r\n").encode())
        data = testMgmtSock1.recv(1024)
        testMgmtSock1.close()
        self.assertEqual(
            data.decode(),
            "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newface OK:0\r\n"
        )
        self.assertEqual(
            self.forwarder1.linklayer.faceidtable.get_face_id(
                AddressInfo(("127.0.0.1", self.forwarder2_port), 0)), 0)

        #register a prefix
        testMgmtSock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        testMgmtSock2.connect(("127.0.0.1", self.forwarder1_port))
        testMgmtSock2.send(
            "GET /icnlayer/newforwardingrule/%2Ftest%2Fdata:0 HTTP/1.1\r\n\r\n"
            .encode())
        data = testMgmtSock2.recv(1024)
        testMgmtSock2.close()
        self.assertEqual(
            data.decode(),
            "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newforwardingrule OK:0\r\n"
        )
        self.assertEqual(
            self.forwarder1.icnlayer.fib.find_fib_entry(
                Name("/test/data")).faceid, [0])

        # new content
        testMgmtSock3 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        testMgmtSock3.connect(("127.0.0.1", self.forwarder2_port))
        testMgmtSock3.send(
            "GET /icnlayer/newcontent/%2Ftest%2Fdata%2Fobject:HelloWorld HTTP/1.1\r\n\r\n"
            .encode())
        data = testMgmtSock3.recv(1024)
        testMgmtSock3.close()
        self.assertEqual(
            data.decode(),
            "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newcontent OK\r\n"
        )

        #create test content
        name = Name("/test/data/object")
        test_content = Content(name, content="HelloWorld")
        cs_fwd2 = self.forwarder2.icnlayer.cs
        self.assertEqual(
            cs_fwd2.find_content_object(name).content, test_content)

        #create interest
        interest = Interest("/test/data/object")
        encoded_interest = self.encoder.encode(interest)
        #send interest
        self.testSock.sendto(encoded_interest,
                             ("127.0.0.1", self.forwarder1_port))

        #receive content
        encoded_content, addr = self.testSock.recvfrom(8192)
        content = self.encoder.decode(encoded_content)
        self.assertEqual(content, test_content)
        time.sleep(4)
        self.assertEqual(self.forwarder1.icnlayer.pit.get_container_size(), 0)

    def test_NFNForwarder_simple_compute_two_nodes(self):
        """Test a simple forwarding scenario with one additional node forwarding the data"""
        self.forwarder1.start_forwarder()
        self.forwarder2.start_forwarder()
        # client <---> node1 <---> node2

        # create a face
        testMgmtSock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        testMgmtSock1.connect(("127.0.0.1", self.forwarder1_port))
        port_to = self.forwarder2_port
        testMgmtSock1.send(("GET /linklayer/newface/127.0.0.1:" +
                            str(port_to) + ":0 HTTP/1.1\r\n\r\n").encode())
        data = testMgmtSock1.recv(1024)
        testMgmtSock1.close()
        self.assertEqual(
            data.decode(),
            "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newface OK:0\r\n"
        )
        self.assertEqual(
            self.forwarder1.linklayer.faceidtable.get_face_id(
                AddressInfo(("127.0.0.1", self.forwarder2_port), 0)), 0)

        # register a prefix
        testMgmtSock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        testMgmtSock2.connect(("127.0.0.1", self.forwarder1_port))
        testMgmtSock2.send(
            "GET /icnlayer/newforwardingrule/%2Flib%2Ffunc:0 HTTP/1.1\r\n\r\n".
            encode())
        data = testMgmtSock2.recv(1024)
        testMgmtSock2.close()
        self.assertEqual(
            data.decode(),
            "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newforwardingrule OK:0\r\n"
        )
        self.assertEqual([0],
                         self.forwarder1.icnlayer.fib.find_fib_entry(
                             Name("/lib/func")).faceid)

        #add function
        testMgmtSock3 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        testMgmtSock3.connect(("127.0.0.1", self.forwarder2_port))
        testMgmtSock3.send(
            "GET /icnlayer/newcontent/%2Flib%2Ffunc%2Ff1:PYTHON\nf\ndef f():\n    return 'Hello World' HTTP/1.1\r\n\r\n"
            .encode())
        data = testMgmtSock3.recv(1024)
        testMgmtSock3.close()
        self.assertEqual(
            data.decode(),
            "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newcontent OK\r\n"
        )

        # create interest
        name = Name("/lib/func/f1")
        name += "_()"
        name += "NFN"
        encoded_interest = self.encoder.encode(Interest(name))
        # send interest
        self.testSock.sendto(encoded_interest,
                             ("127.0.0.1", self.forwarder1_port))
        # receive content
        encoded_content, addr = self.testSock.recvfrom(8192)
        content: Content = self.encoder.decode(encoded_content)
        self.assertEqual("Hello World", content.content)
        self.assertEqual(name, content.name)
        time.sleep(15)
        self.assertEqual(self.forwarder1.icnlayer.pit.get_container_size(), 0)

    def test_NFNForwarder_compute_param_two_nodes(self):
        """Test a simple forwarding scenario with one additional node forwarding the data"""
        self.forwarder1.start_forwarder()
        self.forwarder2.start_forwarder()
        # client <---> node1 <---> node2

        # create faces
        fid1 = self.forwarder1.linklayer.faceidtable.get_or_create_faceid(
            AddressInfo(("127.0.0.1", self.forwarder2_port), 0))
        fid2 = self.forwarder2.linklayer.faceidtable.get_or_create_faceid(
            AddressInfo(("127.0.0.1", self.forwarder1_port), 0))

        # register prefixes
        self.forwarder1.icnlayer.fib.add_fib_entry(Name("/lib/func"), [fid1],
                                                   True)
        self.forwarder2.icnlayer.fib.add_fib_entry(Name("/test"), [fid2], True)

        # add function
        testMgmtSock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        testMgmtSock1.connect(("127.0.0.1", self.forwarder2_port))
        testMgmtSock1.send(
            "GET /icnlayer/newcontent/%2Flib%2Ffunc%2Ff1:PYTHON\nf\ndef f(a):\n    return a.upper() HTTP/1.1\r\n\r\n"
            .encode())
        data = testMgmtSock1.recv(1024)
        testMgmtSock1.close()
        self.assertEqual(
            data.decode(),
            "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newcontent OK\r\n"
        )

        # add content
        testMgmtSock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        testMgmtSock2.connect(("127.0.0.1", self.forwarder1_port))
        testMgmtSock2.send(
            "GET /icnlayer/newcontent/%2Ftest%2Fdata%2Fobject:HelloWorld HTTP/1.1\r\n\r\n"
            .encode())
        data = testMgmtSock2.recv(1024)
        testMgmtSock2.close()
        self.assertEqual(
            data.decode(),
            "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newcontent OK\r\n"
        )

        # create interest
        name = Name("/lib/func/f1")
        name += "_(/test/data/object)"
        name += "NFN"
        encoded_interest = self.encoder.encode(Interest(name))
        # send interest
        self.testSock.sendto(encoded_interest,
                             ("127.0.0.1", self.forwarder1_port))
        # receive content
        self.testSock.settimeout(3)
        encoded_content, addr = self.testSock.recvfrom(8192)
        time.sleep(0.1)
        content: Content = self.encoder.decode(encoded_content)
        self.assertEqual("HELLOWORLD", content.content)
        self.assertEqual(name, content.name)
        time.sleep(15)
        self.assertEqual(self.forwarder1.icnlayer.pit.get_container_size(), 0)

    def test_NFNForwarder_compute_subcomp_two_nodes(self):
        """Test a simple forwarding scenario with one additional node forwarding the data"""
        self.forwarder1.start_forwarder()
        self.forwarder2.start_forwarder()
        # client <---> node1 <---> node2

        # create faces
        fid1 = self.forwarder1.linklayer.faceidtable.get_or_create_faceid(
            AddressInfo(("127.0.0.1", self.forwarder2_port), 0))
        fid2 = self.forwarder2.linklayer.faceidtable.get_or_create_faceid(
            AddressInfo(("127.0.0.1", self.forwarder1_port), 0))

        # register prefixes
        self.forwarder1.icnlayer.fib.add_fib_entry(Name("/lib/func"), [fid1],
                                                   True)
        self.forwarder2.icnlayer.fib.add_fib_entry(Name("/test"), [fid2], True)

        # add function
        testMgmtSock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        testMgmtSock1.connect(("127.0.0.1", self.forwarder2_port))
        testMgmtSock1.send(
            "GET /icnlayer/newcontent/%2Flib%2Ffunc%2Ff1:PYTHON\nf\ndef f(a):\n    return a.upper() HTTP/1.1\r\n\r\n"
            .encode())
        data = testMgmtSock1.recv(1024)
        testMgmtSock1.close()
        self.assertEqual(
            data.decode(),
            "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newcontent OK\r\n"
        )

        # add content
        testMgmtSock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        testMgmtSock2.connect(("127.0.0.1", self.forwarder1_port))
        testMgmtSock2.send(
            "GET /icnlayer/newcontent/%2Ftest%2Fdata%2Fobject:tluser HTTP/1.1\r\n\r\n"
            .encode())
        data = testMgmtSock2.recv(1024)
        testMgmtSock2.close()
        self.assertEqual(
            data.decode(),
            "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newcontent OK\r\n"
        )

        # add function 2
        testMgmtSock3 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        testMgmtSock3.connect(("127.0.0.1", self.forwarder1_port))
        testMgmtSock3.send(
            "GET /icnlayer/newcontent/%2Flib%2Ffunc%2Ff2:PYTHON\nf\ndef f(a):\n    return a[::-1] HTTP/1.1\r\n\r\n"
            .encode())
        data = testMgmtSock3.recv(1024)
        testMgmtSock3.close()
        self.assertEqual(
            data.decode(),
            "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newcontent OK\r\n"
        )

        # create interest
        name = Name("/lib/func/f1")
        name += "_(/lib/func/f2(/test/data/object))"
        name += "NFN"
        encoded_interest = self.encoder.encode(Interest(name))
        # send interest
        self.testSock.sendto(encoded_interest,
                             ("127.0.0.1", self.forwarder1_port))
        # receive content
        encoded_content, addr = self.testSock.recvfrom(8192)
        content: Content = self.encoder.decode(encoded_content)
        self.assertEqual("RESULT", content.content)
        self.assertEqual(name, content.name)
        time.sleep(15)
        self.assertEqual(self.forwarder1.icnlayer.pit.get_container_size(), 0)

    def test_NFNForwarder_compute_subcomp_two_nodes_chunking_result(self):
        """Test a simple forwarding scenario with one additional node forwarding the data"""
        self.forwarder1.start_forwarder()
        self.forwarder2.start_forwarder()
        # client <---> node1 <---> node2

        # create faces
        fid1 = self.forwarder1.linklayer.faceidtable.get_or_create_faceid(
            AddressInfo(("127.0.0.1", self.forwarder2_port), 0))
        fid2 = self.forwarder2.linklayer.faceidtable.get_or_create_faceid(
            AddressInfo(("127.0.0.1", self.forwarder1_port), 0))
        # register prefixes
        self.forwarder1.icnlayer.fib.add_fib_entry(Name("/lib/func"), [fid1],
                                                   True)
        self.forwarder2.icnlayer.fib.add_fib_entry(Name("/test"), [fid2], True)

        # add function
        testMgmtSock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        testMgmtSock1.connect(("127.0.0.1", self.forwarder2_port))
        testMgmtSock1.send(
            "GET /icnlayer/newcontent/%2Flib%2Ffunc%2Ff1:PYTHON\nf\ndef f(a):\n    return a.upper() + str(20000*'a') HTTP/1.1\r\n\r\n"
            .encode())
        data = testMgmtSock1.recv(1024)
        testMgmtSock1.close()
        self.assertEqual(
            data.decode(),
            "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newcontent OK\r\n"
        )

        # add content
        testMgmtSock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        testMgmtSock2.connect(("127.0.0.1", self.forwarder1_port))
        testMgmtSock2.send(
            "GET /icnlayer/newcontent/%2Ftest%2Fdata%2Fobject:tluser HTTP/1.1\r\n\r\n"
            .encode())
        data = testMgmtSock2.recv(1024)
        testMgmtSock2.close()
        self.assertEqual(
            data.decode(),
            "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newcontent OK\r\n"
        )

        # add function 2
        testMgmtSock3 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        testMgmtSock3.connect(("127.0.0.1", self.forwarder1_port))
        testMgmtSock3.send(
            "GET /icnlayer/newcontent/%2Flib%2Ffunc%2Ff2:PYTHON\nf\ndef f(a):\n    return a[::-1] HTTP/1.1\r\n\r\n"
            .encode())
        data = testMgmtSock3.recv(1024)
        testMgmtSock3.close()
        self.assertEqual(
            data.decode(),
            "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newcontent OK\r\n"
        )

        # create interest
        name = Name("/lib/func/f1")
        name += "_(/lib/func/f2(/test/data/object))"
        name += "NFN"
        encoded_interest = self.encoder.encode(Interest(name))
        # send interest
        self.testSock.sendto(encoded_interest,
                             ("127.0.0.1", self.forwarder1_port))
        # receive content
        encoded_content, addr = self.testSock.recvfrom(8192)
        content: Content = self.encoder.decode(encoded_content)
        self.assertEqual(
            'mdo:20006:/lib/func/f1/_(/lib/func/f2(/test/data/object))/NFN/c0;/lib/func/f1/_(/lib/func/f2(/test/data/object))/NFN/c1;/lib/func/f1/_(/lib/func/f2(/test/data/object))/NFN/c2;/lib/func/f1/_(/lib/func/f2(/test/data/object))/NFN/c3:/lib/func/f1/_(/lib/func/f2(/test/data/object))/NFN/m1',
            content.content)
        self.assertEqual(name, content.name)
Ejemplo n.º 8
0
class EdgeComputingSimpleSimulation1(unittest.TestCase):
    """run the simple Edge Computing Scenario Simulation"""
    @abc.abstractmethod
    def get_encoder(self) -> BasicEncoder:
        return SimpleStringEncoder

    def setUp(self):
        self.encoder_type = self.get_encoder()
        self.simulation_bus = SimulationBus(packetencoder=self.encoder_type())

        self.fetch_tool1 = Fetch(
            "rsu1",
            None,
            255,
            self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("fetchtool1")])
        self.fetch_tool2 = Fetch(
            "rsu2",
            None,
            255,
            self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("fetchtool2")])
        self.fetch_tool3 = Fetch(
            "rsu3",
            None,
            255,
            self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("fetchtool3")])

        self.rsu1 = NFNForwarder(
            port=0,
            encoder=self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("rsu1")],
            log_level=255,
            ageing_interval=1)

        self.rsu2 = NFNForwarder(
            port=0,
            encoder=self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("rsu2")],
            log_level=255,
            ageing_interval=1)
        self.rsu3 = NFNForwarder(
            port=0,
            encoder=self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("rsu3")],
            log_level=255,
            ageing_interval=1)

        self.rsu1.icnlayer.pit.set_pit_timeout(0)
        self.rsu1.icnlayer.cs.set_cs_timeout(30)
        self.rsu2.icnlayer.pit.set_pit_timeout(0)
        self.rsu2.icnlayer.cs.set_cs_timeout(30)
        self.rsu3.icnlayer.pit.set_pit_timeout(0)
        self.rsu3.icnlayer.cs.set_cs_timeout(30)

        self.rsu1.nfnlayer.optimizer = EdgeComputingOptimizer(
            self.rsu1.icnlayer.cs, self.rsu1.icnlayer.fib,
            self.rsu1.icnlayer.pit, self.rsu1.linklayer.faceidtable)
        self.rsu2.nfnlayer.optimizer = EdgeComputingOptimizer(
            self.rsu2.icnlayer.cs, self.rsu2.icnlayer.fib,
            self.rsu2.icnlayer.pit, self.rsu2.linklayer.faceidtable)
        self.rsu3.nfnlayer.optimizer = EdgeComputingOptimizer(
            self.rsu3.icnlayer.cs, self.rsu3.icnlayer.fib,
            self.rsu3.icnlayer.pit, self.rsu3.linklayer.faceidtable)

        self.mgmt_client1 = MgmtClient(
            self.rsu1.mgmt.mgmt_sock.getsockname()[1])
        self.mgmt_client2 = MgmtClient(
            self.rsu2.mgmt.mgmt_sock.getsockname()[1])
        self.mgmt_client3 = MgmtClient(
            self.rsu3.mgmt.mgmt_sock.getsockname()[1])

    def tearDown(self):
        self.rsu1.stop_forwarder()
        self.rsu2.stop_forwarder()
        self.rsu3.stop_forwarder()
        self.fetch_tool1.stop_fetch()
        self.fetch_tool2.stop_fetch()
        self.fetch_tool3.stop_fetch()
        self.simulation_bus.stop_process()

    def setup_faces_and_connections(self):
        self.rsu1.start_forwarder()
        self.rsu2.start_forwarder()
        self.rsu3.start_forwarder()

        self.simulation_bus.start_process()

        #setup rsu1

        self.mgmt_client1.add_face("rsu2", None, 0)
        self.mgmt_client1.add_forwarding_rule(Name("/rsu"), [0])
        self.mgmt_client1.add_new_content(
            Name("/rsu/func/f1"),
            "PYTHON\nf\ndef f(a):\n    for i in range(0,20000000):\n        a.upper()\n    return a.upper() + ' RSU1'"
        )

        #setup rsu2
        self.mgmt_client2.add_face("rsu1", None, 0)
        self.mgmt_client2.add_face("rsu3", None, 0)
        self.mgmt_client2.add_forwarding_rule(Name("/rsu"), [0, 1])
        self.mgmt_client2.add_new_content(
            Name("/rsu/func/f1"),
            "PYTHON\nf\ndef f(a):\n    for i in range(0,30000000):\n        a.upper()\n    return a.upper() + ' RSU2'"
        )

        #setup rsu3
        self.mgmt_client3.add_face("rsu2", None, 0)
        self.mgmt_client3.add_forwarding_rule(Name("/rsu"), [0])
        self.mgmt_client3.add_new_content(
            Name("/rsu/func/f1"),
            "PYTHON\nf\ndef f(a):\n    for i in range(0,40000000):\n        a.upper()\n    return a.upper() + ' RSU3'"
        )

    def test_without_data_from_client(self):
        """execute a simple function on the rsus"""
        self.setup_faces_and_connections()

        name = Name("/rsu/func/f1")
        name += '_("helloworld")'
        name += "NFN"

        res = self.fetch_tool1.fetch_data(name, timeout=5)
        self.assertEqual(res, "HELLOWORLD RSU1")
        print("Result at RSU1:", res)
        time.sleep(5)
        res = self.fetch_tool2.fetch_data(name, timeout=5)
        print("Result as fetched via RSU2:", res)
        self.assertEqual(res, "HELLOWORLD RSU1")

    def test_inner_call_without_data_from_client(self):
        """execute one function on the first node and another function on the second node"""
        self.setup_faces_and_connections()
        self.mgmt_client2.add_new_content(
            Name("/rsu/func/f2"), "PYTHON\nf\ndef f(a):\n    return a[::-1]")

        name1 = Name("/rsu/func/f1")
        name1 += '_("helloworld")'
        name1 += "NFN"

        name2 = Name("/rsu/func/f2")
        name2 += '_(/rsu/func/f1("helloworld"))'
        name2 += "NFN"

        res1 = self.fetch_tool1.fetch_data(name1, timeout=20)
        print(res1)

        time.sleep(6)
        res2 = self.fetch_tool2.fetch_data(name2, timeout=20)
        print(res2)

    def test_result_placement_using_names(self):
        """execute one function on a node where the client will be, when the computation is finished"""
        self.setup_faces_and_connections()

        self.mgmt_client3.add_new_content(
            Name("/rsu/id3/func/f2"),
            "PYTHON\nf\ndef f(a):\n    for i in range(0,20000000):\n        a.upper()\n    return a.upper() + ' PLACED ON RSU3'"
        )

        name1 = Name("/rsu/id3/func/f2")
        name1 += '_("helloworld")'
        name1 += "NFN"

        res1 = self.fetch_tool1.fetch_data(name1, timeout=10)
        print(res1)

        res2 = self.fetch_tool3.fetch_data(name1, timeout=10)
        print(res2)
Ejemplo n.º 9
0
class TimeoutPreventionSimulation(unittest.TestCase):
    """Simulate a Scenario where timeout prevention is required"""
    @abc.abstractmethod
    def get_encoder(self) -> BasicEncoder:
        return SimpleStringEncoder

    def setUp(self):
        self.encoder_type = self.get_encoder()
        self.simulation_bus = SimulationBus(packetencoder=self.encoder_type())

        self.fetch_tool1 = Fetch(
            "nfn1",
            None,
            255,
            self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("fetchtool1")])

        self.nfn1 = NFNForwarder(
            port=0,
            encoder=self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("nfn1")],
            log_level=255,
            ageing_interval=1)
        self.nfn2 = NFNForwarder(
            port=0,
            encoder=self.encoder_type(),
            interfaces=[self.simulation_bus.add_interface("nfn2")],
            log_level=255,
            ageing_interval=1)

        self.simulation_bus.add_interface("deadend")

        self.nfn1.icnlayer.pit.set_pit_timeout(0)
        self.nfn1.icnlayer.cs.set_cs_timeout(30)
        self.nfn2.icnlayer.pit.set_pit_timeout(0)
        self.nfn2.icnlayer.cs.set_cs_timeout(30)

        self.nfn1.nfnlayer.optimizer = ToDataFirstOptimizer(
            self.nfn1.icnlayer.cs, self.nfn1.icnlayer.fib,
            self.nfn1.icnlayer.pit, self.nfn1.linklayer.faceidtable)
        self.nfn2.nfnlayer.optimizer = ToDataFirstOptimizer(
            self.nfn2.icnlayer.cs, self.nfn2.icnlayer.fib,
            self.nfn2.icnlayer.pit, self.nfn2.linklayer.faceidtable)

        self.mgmt_client1 = MgmtClient(
            self.nfn1.mgmt.mgmt_sock.getsockname()[1])
        self.mgmt_client2 = MgmtClient(
            self.nfn2.mgmt.mgmt_sock.getsockname()[1])

    def tearDown(self):
        self.nfn1.stop_forwarder()
        self.nfn2.stop_forwarder()
        self.fetch_tool1.stop_fetch()
        self.simulation_bus.stop_process()

    def setup_faces_and_connections(self):
        self.nfn1.start_forwarder()
        self.nfn2.start_forwarder()

        self.simulation_bus.start_process()

        # setup nfn1
        self.mgmt_client1.add_face("nfn2", None, 0)
        self.mgmt_client1.add_forwarding_rule(Name("/lib"), [0])
        self.mgmt_client1.add_new_content(Name("/test/data/string"),
                                          "This is a String")

        # setup nfn2
        self.mgmt_client1.add_face("nfn1", None, 0)
        self.mgmt_client1.add_forwarding_rule(Name("/test"), [0])
        self.mgmt_client2.add_new_content(
            Name("/lib/func/f1"),
            "PYTHON\nf\ndef f(a):\n    for i in range(0,100000000):\n        a.upper()\n    return a.upper() + ' WITHOUT TIMEOUT'"
        )

    def test_simple_timeout_prevention(self):
        """Simple test to see if timeout prevention works"""
        self.setup_faces_and_connections()

        name = Name("/lib/func/f1")
        name += '_("helloworld")'
        name += "NFN"

        res = self.fetch_tool1.fetch_data(name, timeout=0)
        time.sleep(3)
        self.assertEqual("HELLOWORLD WITHOUT TIMEOUT", res)
        print(res)

    def test_timeout_prevention_if_no_comp(self):
        """Simple test to see if timeout prevention works if no computation is available and no fwd rule"""
        self.setup_faces_and_connections()

        name = Name("/lib/func/f2")
        name += '_("helloworld")'
        name += "NFN"

        res = self.fetch_tool1.fetch_data(name, timeout=0)
        self.assertEqual(
            "Received Nack: one or many input data (function or data) is unavailable",
            res)
        time.sleep(3)
        print(res)

    def test_timeout_prevention_if_no_comp_no_data_fwd(self):
        """Simple test to see if timeout prevention works if no computation is available and no data are replied"""
        self.setup_faces_and_connections()

        name = Name("/lib/func/f2")
        name += '_("helloworld")'
        name += "NFN"

        self.mgmt_client2.add_face("deadend", None, 0)
        self.mgmt_client2.add_forwarding_rule(Name("/lib"), [0])

        res = self.fetch_tool1.fetch_data(name, timeout=0)
        self.assertEqual(
            "Received Nack: one or many input data (function or data) is unavailable",
            res)
        time.sleep(3)
        print(res)
Ejemplo n.º 10
0
nfn_fwd1 = NFNForwarder(port=0,
                        encoder=NdnTlvEncoder(),
                        interfaces=[simulation_bus.add_interface("nfn1")],
                        log_level=255,
                        ageing_interval=1)

mgmt_client0 = MgmtClient(nfn_fwd0.mgmt.mgmt_sock.getsockname()[1])
mgmt_client1 = MgmtClient(nfn_fwd1.mgmt.mgmt_sock.getsockname()[1])

fetch_tool = Fetch("nfn0",
                   None,
                   255,
                   NdnTlvEncoder(),
                   interfaces=[simulation_bus.add_interface("fetchtool1")])

nfn_fwd0.start_forwarder()
nfn_fwd1.start_forwarder()
simulation_bus.start_process()

mgmt_client0.add_face("nfn1", None, 0)
mgmt_client0.add_forwarding_rule(Name("/data"), [0])

mgmt_client0.add_new_content(
    Name("/func/combine"), "PYTHON\nfunc\ndef func(a, b):\n    return a + b")
mgmt_client1.add_new_content(Name("/data/obj1"), "World")

name = Name("/func/combine")
name += '_("Hello",/data/obj1)'
name += "NFN"

res = fetch_tool.fetch_data(name, timeout=20)
Ejemplo n.º 11
0
class cases_Simulation():
    """Test the PiCN Simulation System"""

    @abc.abstractmethod
    def get_encoder(self) -> BasicEncoder:
        pass

    def setUp(self):
        self.encoder_type = self.get_encoder()

        self.simulation_bus = SimulationBus(packetencoder=self.encoder_type())

        self.fetchiface = self.simulation_bus.add_interface("fetch")
        self.encoder = self.encoder_type()
        self.icn_forwarder1 = ICNForwarder(port=0, encoder=self.encoder_type(),
                                           interfaces=[self.simulation_bus.add_interface("icnfwd1")], log_level=255)
        self.icn_forwarder2 = ICNForwarder(port=0, encoder=self.encoder_type(),
                                           interfaces=[self.simulation_bus.add_interface("icnfwd2")], log_level=255)

        #self.simulation_bus.start_process()

    def tearDown(self):
        try:
            self.simulation_bus.stop_process()
            self.icn_forwarder1.stop_forwarder()
            self.icn_forwarder2.stop_forwarder()
        except:
            self.icn_forwarder2.stop_repo()
            pass

    def test_send_single_packet(self):
        """Test fetching a single content object over the simulation bus"""
        self.icn_forwarder1.start_forwarder()
        self.icn_forwarder2.start_forwarder()
        self.simulation_bus.start_process()

        fid1 = self.icn_forwarder1.linklayer.faceidtable.get_or_create_faceid(AddressInfo("icnfwd2", 0))
        self.icn_forwarder1.icnlayer.fib.add_fib_entry(Name("/test"), [fid1])

        self.icn_forwarder2.icnlayer.cs.add_content_object(Content("/test/data", "HelloWorld"), static=True)

        interest = Interest("/test/data")
        wire_data = self.encoder.encode(interest)
        self.fetchiface.send(wire_data, "icnfwd1")

        res, src = self.fetchiface.receive()
        self.assertEqual(src, "icnfwd1")
        c = self.encoder.decode(res)
        self.assertEqual(c, Content("/test/data", "HelloWorld"))

    def test_send_single_packet_with_delay(self):
        """Test fetching a single content object over the simulation bus"""

        delay_func = lambda packet: 0.5

        self.fetchiface = self.simulation_bus.add_interface("fetch", delay_func=delay_func)
        self.icn_forwarder1 = ICNForwarder(port=0, encoder=self.encoder_type(),
                                           interfaces=[self.simulation_bus.add_interface("icnfwd1", delay_func=delay_func)])
        self.icn_forwarder2 = ICNForwarder(port=0, encoder=self.encoder_type(),
                                           interfaces=[self.simulation_bus.add_interface("icnfwd2", delay_func=delay_func)])
        self.simulation_bus.start_process()

        self.icn_forwarder1.start_forwarder()
        self.icn_forwarder2.start_forwarder()

        fid1 = self.icn_forwarder1.linklayer.faceidtable.get_or_create_faceid(AddressInfo("icnfwd2", 0))
        self.icn_forwarder1.icnlayer.fib.add_fib_entry(Name("/test"), [fid1])

        self.icn_forwarder2.icnlayer.cs.add_content_object(Content("/test/data", "HelloWorld"), static=True)

        interest = Interest("/test/data")
        wire_data = self.encoder.encode(interest)
        self.fetchiface.send(wire_data, "icnfwd1")

        res, src = self.fetchiface.receive()
        self.assertEqual(src, "icnfwd1")
        c = self.encoder.decode(res)
        self.assertEqual(c, Content("/test/data", "HelloWorld"))

    def test_send_single_packet_with_packet_loss(self):
        """Test fetching a single content object over the simulation bus"""

        packet_loss_func = lambda packet: True

        self.fetchiface = self.simulation_bus.add_interface("fetch", packet_loss_func=packet_loss_func)
        self.icn_forwarder1 = ICNForwarder(port=0, encoder=self.encoder_type(),
                                           interfaces=[self.simulation_bus.add_interface("icnfwd1", packet_loss_func=packet_loss_func)])
        self.icn_forwarder2 = ICNForwarder(port=0, encoder=self.encoder_type(),
                                           interfaces=[self.simulation_bus.add_interface("icnfwd2", packet_loss_func=packet_loss_func)])
        self.simulation_bus.start_process()

        self.icn_forwarder1.start_forwarder()
        self.icn_forwarder2.start_forwarder()

        fid1 = self.icn_forwarder1.linklayer.faceidtable.get_or_create_faceid(AddressInfo("icnfwd2", 0))
        self.icn_forwarder1.icnlayer.fib.add_fib_entry(Name("/test"), [fid1])

        self.icn_forwarder2.icnlayer.cs.add_content_object(Content("/test/data", "HelloWorld"), static=True)

        interest = Interest("/test/data")
        wire_data = self.encoder.encode(interest)
        self.fetchiface.send(wire_data, "icnfwd1")
        try:
            self.fetchiface.receive(timeout=4)
        except queue.Empty:
            pass
        else:
            self.fail()

    def test_creation_of_simulation_face_mgmt(self):
        """Test the creation of a simulation face using a mgmt client"""
        self.icn_forwarder1.start_forwarder()
        self.icn_forwarder2.start_forwarder()

        self.simulation_bus.start_process()

        mgmt_client1 = MgmtClient(self.icn_forwarder1.mgmt.mgmt_sock.getsockname()[1])

        mgmt_client1.add_face("icnfwd2", None, 0)
        mgmt_client1.add_forwarding_rule(Name("/test"), [0])

        mgmt_client2 = MgmtClient(self.icn_forwarder2.mgmt.mgmt_sock.getsockname()[1])
        mgmt_client2.add_new_content(Name("/test/data"), "HelloWorld")

        interest = Interest("/test/data")
        wire_data = self.encoder.encode(interest)
        self.fetchiface.send(wire_data, "icnfwd1")

        res, src = self.fetchiface.receive()
        self.assertEqual(src, "icnfwd1")
        c = self.encoder.decode(res)
        self.assertEqual(c, Content("/test/data", "HelloWorld"))

        mgmt_client1.shutdown()
        mgmt_client2.shutdown()


    def test_bandwidth_limit(self): #TODO better test here
        """Simple Test for checking the bandwidth limit"""

        self.icn_forwarder1 = ICNForwarder(port=0, encoder=self.encoder_type(),
                                           interfaces=[
                                               self.simulation_bus.add_interface("icnfwd1", max_bandwidth=1)])
        self.icn_forwarder2 = ICNForwarder(port=0, encoder=self.encoder_type(),
                                           interfaces=[
                                               self.simulation_bus.add_interface("icnfwd2", max_bandwidth=1)])

        self.icn_forwarder1.start_forwarder()
        self.icn_forwarder2.start_forwarder()

        self.simulation_bus.start_process()

        mgmt_client1 = MgmtClient(self.icn_forwarder1.mgmt.mgmt_sock.getsockname()[1])

        mgmt_client1.add_face("icnfwd2", None, 0)
        mgmt_client1.add_forwarding_rule(Name("/test"), [0])

        mgmt_client2 = MgmtClient(self.icn_forwarder2.mgmt.mgmt_sock.getsockname()[1])
        mgmt_client2.add_new_content(Name("/test/data"), "HelloWorld")

        interest = Interest("/test/data")
        wire_data = self.encoder.encode(interest)
        self.fetchiface.send(wire_data, "icnfwd1")

        res, src = self.fetchiface.receive()
        self.assertEqual(src, "icnfwd1")
        c = self.encoder.decode(res)
        self.assertEqual(c, Content("/test/data", "HelloWorld"))

        mgmt_client1.shutdown()
        mgmt_client2.shutdown()

    def test_single_nfn_interest(self):
        """Test simulation with NFN nodes and a single packet """

        self.icn_forwarder1 = NFNForwarder(port=0, encoder=self.encoder_type(),
                                           interfaces=[self.simulation_bus.add_interface("icnfwd1")])
        self.icn_forwarder2 = NFNForwarder(port=0, encoder=self.encoder_type(),
                                           interfaces=[self.simulation_bus.add_interface("icnfwd2")])
        self.icn_forwarder1.start_forwarder()
        self.icn_forwarder2.start_forwarder()
        self.simulation_bus.start_process()

        mgmt_client1 = MgmtClient(self.icn_forwarder1.mgmt.mgmt_sock.getsockname()[1])
        mgmt_client1.add_face("icnfwd2", None, 0)
        mgmt_client1.add_forwarding_rule(Name("/test"), [0])
        mgmt_client1.add_new_content(Name("/func/f1"), "PYTHON\nf\ndef f(a):\n    return a.upper()")

        mgmt_client2 = MgmtClient(self.icn_forwarder2.mgmt.mgmt_sock.getsockname()[1])
        mgmt_client2.add_new_content(Name("/test/data"), "HelloWorld")
        mgmt_client2.add_face("icnfwd1", None, 0)
        mgmt_client2.add_forwarding_rule(Name("/func"), [0])

        interest = Interest("/test/data")
        interest.name += "/func/f1(_)"
        interest.name += "NFN"
        wire_data = self.encoder.encode(interest)

        self.fetchiface.send(wire_data, "icnfwd1")

        res, src = self.fetchiface.receive()
        self.assertEqual(src, "icnfwd1")
        c = self.encoder.decode(res)
        self.assertEqual(c, Content(interest.name, "HELLOWORLD"))

        mgmt_client1.shutdown()
        mgmt_client2.shutdown()

    def test_singl_interest_repo(self):
        """Test simulation by requesting data from a repo"""
        self.path = "/tmp/repo_unit_test"
        try:
            os.stat( self.path)
        except:
            os.mkdir( self.path)
        with open( self.path + "/f1", 'w+') as content_file:
            content_file.write("A"*20000)
        self.icn_forwarder2 = ICNDataRepository(self.path, Name("/test/data"), 0, log_level=255, encoder=self.encoder_type(),
                                                interfaces=[self.simulation_bus.add_interface("icnfwd2")])


        self.icn_forwarder1.start_forwarder()
        self.icn_forwarder2.start_repo()
        self.simulation_bus.start_process()

        mgmt_client1 = MgmtClient(self.icn_forwarder1.mgmt.mgmt_sock.getsockname()[1])
        mgmt_client1.add_face("icnfwd2", None, 0)
        mgmt_client1.add_forwarding_rule(Name("/test"), [0])

        interest = Interest("/test/data/f1")
        wire_data = self.encoder.encode(interest)

        self.fetchiface.send(wire_data, "icnfwd1")
        res, src = self.fetchiface.receive()
        self.assertEqual(src, "icnfwd1")
        c = self.encoder.decode(res)
        self.assertEqual(c, Content(interest.name, "mdo:20000:/test/data/f1/c0;/test/data/f1/c1;/test/data/f1/c2;/test/data/f1/c3:/test/data/f1/m1"))

        mgmt_client1.shutdown()

    def test_single_interest_repo_fetch(self):
        """Test simulation by requesting data from a repo using the fetch program lib and chunking"""
        self.path = "/tmp/repo_unit_test"
        try:
            os.stat(self.path)
        except:
            os.mkdir(self.path)
        with open(self.path + "/f1", 'w+') as content_file:
            content_file.write("A" * 50000)
        self.icn_forwarder2 = ICNDataRepository(self.path, Name("/test/data"), 0, log_level=255,
                                                encoder=self.encoder_type(),
                                                interfaces=[self.simulation_bus.add_interface("icnfwd2")])


        self.fetchtool = Fetch("icnfwd1", None, 255, encoder=self.encoder_type(), interfaces=[self.simulation_bus.add_interface("fetchtool")])

        self.icn_forwarder1.start_forwarder()
        self.icn_forwarder2.start_repo()
        self.simulation_bus.start_process()

        mgmt_client1 = MgmtClient(self.icn_forwarder1.mgmt.mgmt_sock.getsockname()[1])
        mgmt_client1.add_face("icnfwd2", None, 0)
        mgmt_client1.add_forwarding_rule(Name("/test"), [0])

        res = self.fetchtool.fetch_data(Name("/test/data/f1"), 3)

        self.assertEqual("A"*50000, res)

        mgmt_client1.shutdown()
        self.fetchtool.stop_fetch()