Add startup experiments for NLSR and current testbed topology

refs: #4785

Change-Id: I957b8c229ed0696b2f3fca9445f9f27274b0e197
diff --git a/ndn/experiments/nlsr/__init__.py b/ndn/experiments/nlsr/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/ndn/experiments/nlsr/__init__.py
diff --git a/ndn/experiments/nlsr/advertise-delayed-start.py b/ndn/experiments/nlsr/advertise-delayed-start.py
new file mode 100644
index 0000000..cbd8ac8
--- /dev/null
+++ b/ndn/experiments/nlsr/advertise-delayed-start.py
@@ -0,0 +1,73 @@
+# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+#
+# Copyright (C) 2015-2018, The University of Memphis,
+#                          Arizona Board of Regents,
+#                          Regents of the University of California.
+#
+# This file is part of Mini-NDN.
+# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
+#
+# Mini-NDN is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Mini-NDN is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Mini-NDN, e.g., in COPYING.md file.
+# If not, see <http://www.gnu.org/licenses/>.
+
+from ndn.experiments.experiment import Experiment
+from ndn.apps.nlsr import Nlsr, NlsrConfigGenerator
+
+from mininet.log import info
+
+import time, sys
+
+class AdvertiseDelayedStartExperiment(Experiment):
+    '''Tests Name LSA data segmentation'''
+
+    def __init__(self, args):
+        Experiment.__init__(self, args)
+
+    def setup(self):
+        pass
+
+    def run(self):
+        pass
+
+    def startNlsr(self, checkConvergence = True):
+        # NLSR Security
+        if self.options.nlsrSecurity is True:
+            Nlsr.createKeysAndCertificates(self.net, self.options.workDir)
+
+        host1 = self.net.hosts[0]
+        host1.nlsr = Nlsr(host1, self.options)
+        host1.nlsr.start()
+
+        expectedTotalCount = 500
+        for i in range(0, expectedTotalCount):
+            host1.cmd("nlsrc advertise /long/name/to/exceed/max/packet/size/host1/{}".format(i))
+
+        time.sleep(60)
+
+        host2 = self.net.hosts[1]
+        host2.nlsr = Nlsr(host2, self.options)
+        host2.nlsr.start()
+
+        time.sleep(60)
+
+        advertiseCount = int(host2.cmd("nfdc fib | grep host1 | wc -l"))
+        info(advertiseCount)
+        if advertiseCount == expectedTotalCount:
+            info('\nSuccessfully advertised {} prefixes\n'.format(expectedTotalCount))
+        else:
+            info('\nAdvertising {} prefixes failed. Exiting...\n'.format(expectedTotalCount))
+            self.net.stop()
+            sys.exit(1)
+
+Experiment.register("advertise-delayed-start", AdvertiseDelayedStartExperiment)
diff --git a/ndn/experiments/nlsr/delayed-start.py b/ndn/experiments/nlsr/delayed-start.py
new file mode 100644
index 0000000..e995c50
--- /dev/null
+++ b/ndn/experiments/nlsr/delayed-start.py
@@ -0,0 +1,66 @@
+# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+#
+# Copyright (C) 2015-2018, The University of Memphis,
+#                          Arizona Board of Regents,
+#                          Regents of the University of California.
+#
+# This file is part of Mini-NDN.
+# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
+#
+# Mini-NDN is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Mini-NDN is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Mini-NDN, e.g., in COPYING.md file.
+# If not, see <http://www.gnu.org/licenses/>.
+
+from ndn.experiments.experiment import Experiment
+from ndn.apps.nlsr import Nlsr, NlsrConfigGenerator
+
+from mininet.log import info
+
+import time
+
+class NlsrDelayedStartExperiment(Experiment):
+
+    def __init__(self, args):
+        Experiment.__init__(self, args)
+
+    def setup(self):
+        pass
+
+    def run(self):
+        pass
+
+    def startNlsr(self, checkConvergence = True):
+        # NLSR Security
+        if self.options.nlsrSecurity is True:
+            Nlsr.createKeysAndCertificates(self.net, self.options.workDir)
+
+        i = 1
+        # NLSR initialization
+        info('Starting NLSR on nodes\n')
+        for host in self.net.hosts:
+            host.nlsr = Nlsr(host, self.options)
+            host.nlsr.start()
+
+            # Wait 1/2 minute between starting NLSRs
+            # Wait 1 hour before starting last NLSR
+            if i == len(self.net.hosts) - 1:
+                info('Sleeping 1 hour before starting last NLSR')
+                time.sleep(3600)
+            else:
+                time.sleep(30)
+            i += 1
+
+        if checkConvergence:
+            self.checkConvergence()
+
+Experiment.register("nlsr-delayed-start", NlsrDelayedStartExperiment)
diff --git a/ndn/experiments/nlsr/failure_experiment.py b/ndn/experiments/nlsr/failure_experiment.py
new file mode 100644
index 0000000..783bac4
--- /dev/null
+++ b/ndn/experiments/nlsr/failure_experiment.py
@@ -0,0 +1,65 @@
+# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+#
+# Copyright (C) 2015-2018, The University of Memphis,
+#                          Arizona Board of Regents,
+#                          Regents of the University of California.
+#
+# This file is part of Mini-NDN.
+# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
+#
+# Mini-NDN is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Mini-NDN is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Mini-NDN, e.g., in COPYING.md file.
+# If not, see <http://www.gnu.org/licenses/>.
+
+from ndn.experiments.experiment import Experiment
+from ndn.apps.ndn_ping_client import NDNPingClient
+
+import time
+
+class FailureExperiment(Experiment):
+
+    def __init__(self, args):
+        args["options"].nPings = 300
+        Experiment.__init__(self, args)
+
+        self.PING_COLLECTION_TIME_BEFORE_FAILURE = 60
+        self.PING_COLLECTION_TIME_AFTER_RECOVERY = 120
+
+    def run(self):
+        self.startPctPings()
+
+        # After the pings are scheduled, collect pings for 1 minute
+        time.sleep(self.PING_COLLECTION_TIME_BEFORE_FAILURE)
+
+        # Bring down CSU
+        for host in self.net.hosts:
+            if host.name == "csu":
+                self.failNode(host)
+                break
+
+        # CSU is down for 2 minutes
+        time.sleep(120)
+
+        # Bring CSU back up
+        for host in self.net.hosts:
+            if host.name == "csu":
+                self.recoverNode(host)
+
+                for other in self.net.hosts:
+                    if host.name != other.name:
+                        NDNPingClient.ping(host, other, self.PING_COLLECTION_TIME_AFTER_RECOVERY)
+
+        # Collect pings for more seconds after CSU is up
+        time.sleep(self.PING_COLLECTION_TIME_AFTER_RECOVERY)
+
+Experiment.register("failure", FailureExperiment)
diff --git a/ndn/experiments/nlsr/mcn_failure_experiment.py b/ndn/experiments/nlsr/mcn_failure_experiment.py
new file mode 100644
index 0000000..cd448cc
--- /dev/null
+++ b/ndn/experiments/nlsr/mcn_failure_experiment.py
@@ -0,0 +1,79 @@
+# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+#
+# Copyright (C) 2015-2018, The University of Memphis,
+#                          Arizona Board of Regents,
+#                          Regents of the University of California.
+#
+# This file is part of Mini-NDN.
+# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
+#
+# Mini-NDN is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Mini-NDN is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Mini-NDN, e.g., in COPYING.md file.
+# If not, see <http://www.gnu.org/licenses/>.
+
+from ndn.experiments.experiment import Experiment
+from ndn.apps.ndn_ping_client import NDNPingClient
+
+import time
+
+class MCNFailureExperiment(Experiment):
+
+    def __init__(self, args):
+        Experiment.__init__(self, args)
+
+        self.PING_COLLECTION_TIME_BEFORE_FAILURE = 60
+        self.PING_COLLECTION_TIME_AFTER_RECOVERY = 120
+
+    def getMostConnectedNode(self):
+        mcn = max(self.net.hosts, key=lambda host: len(host.intfNames()))
+        print "The most connected node is: {}".format(mcn.name)
+        return mcn
+
+    def setup(self):
+        if self.options.nPings != 0:
+            Experiment.setup(self)
+
+    def run(self):
+        mostConnectedNode = self.getMostConnectedNode()
+
+        if self.options.nPings != 0:
+            self.startPctPings()
+
+        # After the pings are scheduled, collect pings for 1 minute
+        time.sleep(self.PING_COLLECTION_TIME_BEFORE_FAILURE)
+
+        # Bring down MCN
+        self.failNode(mostConnectedNode)
+
+        # MCN is down for 2 minutes
+        time.sleep(int(self.options.arguments.waitTime))
+
+        # Bring MCN back up
+        self.recoverNode(mostConnectedNode)
+
+        # Restart pings
+        if self.options.nPings != 0:
+            for nodeToPing in self.pingedDict[mostConnectedNode]:
+                 NDNPingClient.ping(mostConnectedNode, nodeToPing, self.PING_COLLECTION_TIME_AFTER_RECOVERY)
+
+            # Collect pings for more seconds after MCN is up
+            time.sleep(self.PING_COLLECTION_TIME_AFTER_RECOVERY)
+        else:
+            self.checkConvergence()
+
+    @staticmethod
+    def parseArguments(parser):
+        parser.add_argument("--wait-time", dest="waitTime", default="120",
+                            help="[Experiment] Generic wait time for experiment use")
+
+Experiment.register("mcn-failure", MCNFailureExperiment)
diff --git a/ndn/experiments/nlsr/multiple_failure_experiment.py b/ndn/experiments/nlsr/multiple_failure_experiment.py
new file mode 100644
index 0000000..efbba60
--- /dev/null
+++ b/ndn/experiments/nlsr/multiple_failure_experiment.py
@@ -0,0 +1,80 @@
+# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+#
+# Copyright (C) 2015-2018, The University of Memphis,
+#                          Arizona Board of Regents,
+#                          Regents of the University of California.
+#
+# This file is part of Mini-NDN.
+# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
+#
+# Mini-NDN is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Mini-NDN is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Mini-NDN, e.g., in COPYING.md file.
+# If not, see <http://www.gnu.org/licenses/>.
+
+from ndn.experiments.experiment import Experiment
+from ndn.apps.ndn_ping_client import NDNPingClient
+
+import time
+
+class MultipleFailureExperiment(Experiment):
+
+    def __init__(self, args):
+
+        self.PING_COLLECTION_TIME_BEFORE_FAILURE = 60
+
+        self.FAILURE_INTERVAL = 60
+        self.RECOVERY_INTERVAL = 60
+
+        # This is the number of pings required to make it through the full experiment
+        nInitialPings = (self.PING_COLLECTION_TIME_BEFORE_FAILURE +
+                         len(args["net"].hosts) * (self.FAILURE_INTERVAL + self.RECOVERY_INTERVAL))
+        print("Scheduling with {} initial pings".format(nInitialPings))
+
+        Experiment.__init__(self, args)
+        self.options.nPings = nInitialPings
+
+    def run(self):
+        self.startPctPings()
+
+        # After the pings are scheduled, collect pings for 1 minute
+        time.sleep(self.PING_COLLECTION_TIME_BEFORE_FAILURE)
+
+        nNodesRemainingToFail = len(self.net.hosts)
+
+        # Fail and recover each node
+        for host in self.net.hosts:
+            # Fail the node
+            self.failNode(host)
+
+            # Stay in failure state for FAILURE_INTERVAL seconds
+            time.sleep(self.FAILURE_INTERVAL)
+
+            # Bring the node back up
+            start_time = time.time()
+            self.recoverNode(host)
+            recovery_time = int(time.time() - start_time)
+
+            # Number of pings required to reach the end of the test
+            nNodesRemainingToFail -= 1
+            nPings = ((self.RECOVERY_INTERVAL - recovery_time) +
+                      nNodesRemainingToFail*(self.FAILURE_INTERVAL + self.RECOVERY_INTERVAL))
+
+            print("Scheduling with {} remaining pings".format(nPings))
+
+            # Restart pings
+            for nodeToPing in self.pingedDict[host]:
+                NDNPingClient.ping(host, nodeToPing, nPings)
+
+            time.sleep(self.RECOVERY_INTERVAL - recovery_time)
+
+Experiment.register("multiple-failure", MultipleFailureExperiment)
diff --git a/ndn/experiments/nlsr/pingall_experiment.py b/ndn/experiments/nlsr/pingall_experiment.py
new file mode 100644
index 0000000..56a70bd
--- /dev/null
+++ b/ndn/experiments/nlsr/pingall_experiment.py
@@ -0,0 +1,49 @@
+# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+#
+# Copyright (C) 2015-2018, The University of Memphis,
+#                          Arizona Board of Regents,
+#                          Regents of the University of California.
+#
+# This file is part of Mini-NDN.
+# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
+#
+# Mini-NDN is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Mini-NDN is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Mini-NDN, e.g., in COPYING.md file.
+# If not, see <http://www.gnu.org/licenses/>.
+
+from ndn.experiments.experiment import Experiment
+
+import time
+
+class PingallExperiment(Experiment):
+
+    def __init__(self, args):
+
+        Experiment.__init__(self, args)
+        self.COLLECTION_PERIOD_BUFFER = 10
+        print "Using {} traffic".format(self.options.pctTraffic)
+
+    def setup(self):
+        if self.options.nPings != 0:
+            Experiment.setup(self)
+
+    def run(self):
+        if self.options.nPings == 0:
+            return
+
+        self.startPctPings()
+
+        # For pingall experiment sleep for the number of pings + some offset
+        time.sleep(self.options.nPings + self.COLLECTION_PERIOD_BUFFER)
+
+Experiment.register("pingall", PingallExperiment)
diff --git a/ndn/experiments/nlsr/prefix_propogation.py b/ndn/experiments/nlsr/prefix_propogation.py
new file mode 100644
index 0000000..ae8301a
--- /dev/null
+++ b/ndn/experiments/nlsr/prefix_propogation.py
@@ -0,0 +1,66 @@
+# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+#
+# Copyright (C) 2015-2018, The University of Memphis,
+#                          Arizona Board of Regents,
+#                          Regents of the University of California.
+#
+# This file is part of Mini-NDN.
+# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
+#
+# Mini-NDN is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Mini-NDN is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Mini-NDN, e.g., in COPYING.md file.
+# If not, see <http://www.gnu.org/licenses/>.
+
+import sys
+import time
+from ndn.experiments.experiment import Experiment
+
+class PrefixPropogationExperiment(Experiment):
+
+    def __init__(self, args):
+        Experiment.__init__(self, args)
+
+    def setup(self):
+        self.checkConvergence()
+
+    def run(self):
+        firstNode = self.net.hosts[0]
+
+        if self.options.nlsrSecurity:
+            firstNode.cmd("ndnsec-set-default /ndn/{}-site/%C1.Operator/op".format(firstNode.name))
+
+        print("Testing advertise")
+        firstNode.cmd("nlsrc advertise /testPrefix")
+        time.sleep(30)
+
+        for host in self.net.hosts:
+            if host.name != firstNode.name:
+                if (int(host.cmd("nfdc fib | grep testPrefix | wc -l")) != 1 or
+                   int(host.cmd("nlsrc status | grep testPrefix | wc -l")) != 1):
+                    print("Advertise test failed")
+                    self.net.stop()
+                    sys.exit(1)
+
+        print("Testing withdraw")
+        firstNode.cmd("nlsrc withdraw /testPrefix")
+        time.sleep(30)
+
+        for host in self.net.hosts:
+            if host.name != firstNode.name:
+                if (int(host.cmd("nfdc fib | grep testPrefix | wc -l")) != 0 or
+                   int(host.cmd("nlsrc status | grep testPrefix | wc -l")) != 0):
+                    print("Withdraw test failed")
+                    self.net.stop()
+                    sys.exit(1)
+
+Experiment.register("prefix-propogation", PrefixPropogationExperiment)