Allow use of NFD and NLSR PPA with Mini-NDN.

Refs: #3992

Change-Id: I329d0303bb4e03ec2296dfb7f7aa00cac3dadfbd
diff --git a/ndn/nfd.py b/ndn/nfd.py
index 09e28d5..480e320 100644
--- a/ndn/nfd.py
+++ b/ndn/nfd.py
@@ -23,6 +23,7 @@
 
 import time, sys, os
 from ndn.ndn_application import NdnApplication
+from ndn.util import copyExistentFile
 
 class Nfd(NdnApplication):
     STRATEGY_BEST_ROUTE = "best-route"
@@ -39,15 +40,11 @@
         self.ndnFolder = "{}/.ndn".format(node.homeFolder)
         self.clientConf = "{}/client.conf".format(self.ndnFolder)
 
-        # Copy nfd.conf file from /usr/local/etc/ndn to the node's home
-
+        # Copy nfd.conf file from /usr/local/etc/ndn or /etc/ndn to the node's home directory
         # Use nfd.conf as default configuration for NFD, else use the sample
-        if os.path.isfile("/usr/local/etc/ndn/nfd.conf") == True:
-            node.cmd("sudo cp /usr/local/etc/ndn/nfd.conf {}".format(self.confFile))
-        elif os.path.isfile("/usr/local/etc/ndn/nfd.conf.sample") == True:
-            node.cmd("sudo cp /usr/local/etc/ndn/nfd.conf.sample {}".format(self.confFile))
-        else:
-            sys.exit("nfd.conf or nfd.conf.sample cannot be found in the expected directory. Exit.")
+        possibleConfPaths = ["/usr/local/etc/ndn/nfd.conf.sample", "/usr/local/etc/ndn/nfd.conf",
+                             "/etc/ndn/nfd.conf.sample", "/etc/ndn/nfd.conf"]
+        copyExistentFile(node, possibleConfPaths, self.confFile)
 
         # Set log level
         node.cmd("infoedit -f {} -s log.default_level -v {}".format(self.confFile, self.logLevel))
@@ -60,9 +57,11 @@
         # Make NDN folder
         node.cmd("sudo mkdir {}".format(self.ndnFolder))
 
-        # Copy the client.conf file and change the unix socket
-        node.cmd("sudo cp /usr/local/etc/ndn/client.conf.sample {}".format(self.clientConf))
+        # Copy client configuration to host
+        possibleClientConfPaths = ["/usr/local/etc/ndn/client.conf.sample", "/etc/ndn/client.conf.sample"]
+        copyExistentFile(node, possibleClientConfPaths, self.clientConf)
 
+        # Change the unix socket
         node.cmd("sudo sed -i 's|nfd.sock|{}.sock|g' {}".format(node.name, self.clientConf))
 
         # Change home folder
@@ -75,4 +74,4 @@
 
     def setStrategy(self, name, strategy):
         self.node.cmd("nfdc strategy set {} ndn:/localhost/nfd/strategy/{}".format(name, strategy))
-        time.sleep(0.5)
+        time.sleep(0.5)
\ No newline at end of file
diff --git a/ndn/nlsr.py b/ndn/nlsr.py
index ab3c648..6c5572e 100644
--- a/ndn/nlsr.py
+++ b/ndn/nlsr.py
@@ -25,7 +25,7 @@
 from mininet.examples.cluster import RemoteMixin
 
 from ndn.ndn_application import NdnApplication
-from ndn.util import ssh, scp
+from ndn.util import ssh, scp, copyExistentFile
 
 import shutil
 import os
@@ -78,7 +78,7 @@
         # Create root certificate
         rootName = NETWORK
         sh("ndnsec-keygen {}".format(rootName)) # Installs a self-signed cert into the system
-        sh("ndnsec-cert-dump -i {} > {}/root.cert".format(rootName, securityDir, securityDir))
+        sh("ndnsec-cert-dump -i {} > {}/root.cert".format(rootName, securityDir))
 
         # Create necessary certificates for each site
         for host in net.hosts:
@@ -154,7 +154,8 @@
         self.hyperRadius = parameters.get("radius", 0.0)
         self.hyperAngle = parameters.get("angle", 0.0)
         self.neighborIPs = []
-        self.node.cmd("sudo cp /usr/local/etc/ndn/nlsr.conf.sample nlsr.conf")
+        possibleConfPaths = ["/usr/local/etc/ndn/nlsr.conf.sample", "/etc/ndn/nlsr.conf.sample"]
+        copyExistentFile(node, possibleConfPaths, "{}/nlsr.conf".format(self.node.homeFolder))
 
     def createConfigFile(self):
 
diff --git a/ndn/util.py b/ndn/util.py
index 7ccf5d0..0ed570d 100644
--- a/ndn/util.py
+++ b/ndn/util.py
@@ -24,6 +24,7 @@
 from subprocess import call
 from mininet.cli import CLI
 import sys
+from os.path import isfile
 
 sshbase = [ 'ssh', '-q', '-t', '-i/home/mininet/.ssh/id_rsa' ]
 scpbase = [ 'scp', '-i', '/home/mininet/.ssh/id_rsa' ]
@@ -40,6 +41,16 @@
     rcmd = scpbase + tmp
     call(rcmd, stdout=devnull, stderr=devnull)
 
+def copyExistentFile(node, fileList, destination):
+    for file in fileList:
+        if isfile(file):
+            node.cmd("cp {} {}".format(file, destination))
+            break
+    if not isfile(destination):
+        fileName = destination.split("/")[-1]
+        raise IOError("{} not found in expected directory.".format(fileName))
+
+
 class MiniNDNCLI(CLI):
     prompt = 'mini-ndn> '
     def __init__(self, mininet, stdin=sys.stdin, script=None):