Add RoutingHelper to allow IP communication in experiments

Change-Id: I267e264b2583909a47229279076655a8080ace22
diff --git a/install.sh b/install.sh
index 44429f5..c5fe190 100755
--- a/install.sh
+++ b/install.sh
@@ -198,6 +198,9 @@
 }
 
 function minindn {
+    $install libigraph0-dev
+    sudo pip install -r requirements.txt
+
     if [[ updated != true ]]; then
         if [ ! -d "build" ]; then
             $update
diff --git a/ndn/apps/routing_helper.py b/ndn/apps/routing_helper.py
new file mode 100644
index 0000000..4c89e48
--- /dev/null
+++ b/ndn/apps/routing_helper.py
@@ -0,0 +1,136 @@
+# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+#
+# Copyright (C) 2015-2019, 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 igraph import *
+from mininet.log import info
+
+
+class LinkInfo:
+    """
+    This class is used to encapsule link information (IP and interface names).
+    """
+
+    def __init__(self, start_intf_name, start_ip, end_intf_name, end_ip):
+        self.start_intf_name = start_intf_name
+        self.start_intf_ip = start_ip
+        self.end_intf_name = end_intf_name
+        self.end_ip = end_ip
+
+
+class IPRoutingHelper:
+    """The routing helper allows to run IP-based evaluations with Mini-NDN. It configures static IP
+    routes to all nodes, which means that all nodes can reach all other nodes in the network
+    reachable, even when relaying is required.
+
+     Usage from Experiment folder: `IPRoutingHelper.calcAllRoutes(self.net)`
+    """
+
+    @staticmethod
+    def findLinkInformation(links, first_node, second_node):
+        """ This method returns link information of a link connecting two nodes.
+
+        :param links: All links in the emulation topology
+        :param first_node: Current node which is looked at
+        :param second_node: Target node (neighbour  of first_node)
+        :return: Link information as LinkInfo object, or returns null None if the
+            nodes are not directly connected
+        """
+        for link in links:
+            if link.intf1.node.name == first_node and link.intf2.node.name == second_node:
+                return LinkInfo(link.intf1.name, link.intf1.ip, link.intf2.name, link.intf2.ip)
+            elif link.intf2.node.name == first_node and link.intf1.node.name == second_node:
+                return LinkInfo(link.intf2.name, link.intf2.ip, link.intf1.name, link.intf1.ip)
+
+        return None
+
+    @staticmethod
+    def calcAllRoutes(net):
+        """ Configures IP routes between all nodes in the emulation topology. This is done in three
+         steps:
+
+        1) IP forwarding is enabled on all nodes
+        2) The igraph lib is used to calculate all shortest paths between the nodes
+        3) Route add commands are used to actually configure the ip routes
+
+        :param net:
+        """
+
+        mini_nodes = net.hosts
+        mini_links = net.links
+
+        # Enabling IP forwaring on all nodes
+        info("Configure IP forwarding on all nodes\n")
+        for node in mini_nodes:
+            node.cmd("sysctl -w net.ipv4.ip_forward=1")
+
+        # Calculate igraph to calculate all shortest paths between nodes
+        node_names = [node.name for node in mini_nodes]
+        links = []
+        for link in mini_links:
+            links.append((link.intf1.node.name, link.intf2.node.name))
+            links.append((link.intf2.node.name, link.intf1.node.name))
+
+        networkGraph = Graph()
+        networkGraph = networkGraph.as_directed()
+        for node in node_names:
+            networkGraph.add_vertex(node)
+        for (a, b) in links:
+            networkGraph.add_edges([(a, b), (b, a)])
+
+        named_paths = []
+        for from_node in node_names:
+            for to_node in node_names:
+                if from_node != to_node:
+                    paths = networkGraph.get_all_shortest_paths(from_node, to_node)
+                    if len(paths) == 0:
+                        continue
+                    shortest_path = paths[0]
+                    shortest_path_with_nodenames = []
+                    for node in shortest_path:
+                        shortest_path_with_nodenames.append(networkGraph.vs["name"][node])
+                    named_paths.append(shortest_path_with_nodenames)
+
+        # Iterate over all paths and configure the routes using the "route add"
+        info("Configure routes on all nodes\n")
+        for path in named_paths:
+            start_node = path[0]
+            end_node = path[-1]
+            mini_start = net.get(start_node)
+            mini_end = net.get(end_node)
+
+            link_info = IPRoutingHelper.findLinkInformation(mini_links, path[0], path[1])
+            start_intf = link_info.start_intf_name
+
+            for intf in mini_end.intfs:
+                addr = mini_end.intfs[intf].ip
+                if len(path) == 2:
+                    # For direct connection, configure exit interface
+                    info("[{}] route add -host {} dev {}\n".format(start_node, addr, start_intf))
+                    mini_start.cmd("route add -host {} dev {}".format(addr, start_intf))
+                elif len(path) > 2:
+                    # For longer paths, configure next hop as gateway
+                    gateway_ip = link_info.end_ip
+                    info("[{}] route add -host {} dev {} gw {}\n"
+                         .format(start_node, addr, start_intf, gateway_ip))
+                    mini_start.cmd("route add -host {} dev {} gw {}"
+                                   .format(addr, start_intf, gateway_ip))
diff --git a/ndn/experiments/ip_rounting_experiment.py b/ndn/experiments/ip_rounting_experiment.py
new file mode 100644
index 0000000..75c87ce
--- /dev/null
+++ b/ndn/experiments/ip_rounting_experiment.py
@@ -0,0 +1,47 @@
+# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+#
+# Copyright (C) 2015-2019, 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.routing_helper import IPRoutingHelper
+
+from mininet.log import info
+
+
+class IpRoutingExperiment(Experiment):
+
+    def __init__(self, args):
+        Experiment.__init__(self, args)
+
+    def setup(self):
+        pass
+
+    def run(self):
+
+        # Calculate all routes for IP routing
+        IPRoutingHelper.calcAllRoutes(self.net)
+        info("IP routes configured, start ping\n")
+
+        self.net.pingAll()
+
+
+Experiment.register("ip-routing", IpRoutingExperiment)
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..be5de0f
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1 @@
+python-igraph
\ No newline at end of file