modification to use cluster version

Change-Id: Iaf169507577deba20d548348532a2e0b91a03249
refs: #3652
diff --git a/ndn/nlsr.py b/ndn/nlsr.py
index 43176b4..3a4b522 100644
--- a/ndn/nlsr.py
+++ b/ndn/nlsr.py
@@ -1,6 +1,6 @@
 # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
 #
-# Copyright (C) 2015-2016, The University of Memphis,
+# Copyright (C) 2015-2017, The University of Memphis,
 #                          Arizona Board of Regents,
 #                          Regents of the University of California.
 #
@@ -22,12 +22,15 @@
 # If not, see <http://www.gnu.org/licenses/>.
 
 from mininet.clean import sh
+from mininet.examples.cluster import RemoteMixin
 
 from ndn.ndn_application import NdnApplication
+from ndn.util import ssh, scp
 
-import os
 import shutil
+import os
 import textwrap
+from subprocess import call
 
 class Nlsr(NdnApplication):
     def __init__(self, node):
@@ -60,7 +63,7 @@
         securityDir = "{}/security".format(workDir)
 
         if not os.path.exists(securityDir):
-                os.mkdir(securityDir)
+            os.mkdir(securityDir)
 
         # Create root certificate
         rootName = "/ndn"
@@ -71,8 +74,12 @@
         for host in net.hosts:
             nodeSecurityFolder = "{}/security".format(host.homeFolder)
 
-            if not os.path.exists(nodeSecurityFolder):
-                os.mkdir(nodeSecurityFolder)
+            host.cmd("mkdir -p %s" % nodeSecurityFolder)
+
+            # Create temp folders for remote nodes on this machine (localhost) to store site.key file
+            # from RemoteNodes
+            if not os.path.exists(nodeSecurityFolder) and isinstance(host, RemoteMixin) and host.isRemote:
+                os.makedirs(nodeSecurityFolder)
 
             shutil.copyfile("{}/root.cert".format(securityDir), "{}/root.cert".format(nodeSecurityFolder))
 
@@ -82,8 +89,25 @@
             siteCertFile = "{}/site.cert".format(nodeSecurityFolder)
             Nlsr.createKey(host, siteName, siteKeyFile)
 
+            # Copy siteKeyFile from remote for ndnsec-certgen
+            if isinstance(host, RemoteMixin) and host.isRemote:
+                login = "mininet@{}".format(host.server)
+                src = "{}:{}".format(login, siteKeyFile)
+                dst = siteKeyFile
+                scp(src, dst)
+
             # Root key is in root namespace, must sign site key and then install on host
             sh("ndnsec-certgen -N {} -s {} -p {} {} > {}".format(siteName, rootName, siteName, siteKeyFile, siteCertFile))
+
+            # Copy root.cert and site.cert from localhost to remote host
+            if isinstance(host, RemoteMixin) and host.isRemote:
+                login = "mininet@{}".format(host.server)
+                src = "{}/site.cert".format(nodeSecurityFolder)
+                src2 = "{}/root.cert".format(nodeSecurityFolder)
+                dst = "{}:/tmp/".format(login)
+                scp(src, src2, dst)
+                host.cmd("mv /tmp/*.cert {}".format(nodeSecurityFolder))
+
             host.cmd("ndnsec-cert-install -f {}".format(siteCertFile))
 
             # Create operator certificate
@@ -106,7 +130,6 @@
     ROUTING_HYPERBOLIC = "hr"
 
     def __init__(self, node, isSecurityEnabled):
-        node.cmd("sudo cp /usr/local/etc/mini-ndn/nlsr.conf nlsr.conf")
         self.node = node
         self.isSecurityEnabled = isSecurityEnabled
 
@@ -120,30 +143,29 @@
 
     def createConfigFile(self):
 
-        filePath = "%s/nlsr.conf" % self.node.homeFolder
+        tmp_conf = "/tmp/nlsr.conf"
 
-        configFile = open(filePath, 'r')
-        oldContent = configFile.read()
+        configFile = open(tmp_conf, 'w')
+        configFile.write(self.__getConfig())
         configFile.close()
 
-        newContent = oldContent.replace("$GENERAL_SECTION", self.__getGeneralSection())
-        newContent = newContent.replace("$NEIGHBORS_SECTION", self.__getNeighborsSection())
-        newContent = newContent.replace("$HYPERBOLIC_SECTION", self.__getHyperbolicSection())
-        newContent = newContent.replace("$FIB_SECTION", self.__getFibSection())
-        newContent = newContent.replace("$ADVERTISING_SECTION", self.__getAdvertisingSection())
-        newContent = newContent.replace("$SECURITY_SECTION", self.__getSecuritySection())
+        # If this node is a remote node scp the nlsr.conf file to its /tmp/nlsr.conf
+        if isinstance(self.node, RemoteMixin) and self.node.isRemote:
+            login = "mininet@%s" % self.node.server
+            src = tmp_conf
+            dst = "%s:%s" % (login, tmp_conf)
+            scp(src, dst)
 
-        configFile = open(filePath, 'w')
-        configFile.write(newContent)
-        configFile.close()
+        # Copy nlsr.conf to home folder
+        self.node.cmd("mv %s nlsr.conf" % tmp_conf)
 
     def __getConfig(self):
 
-        config =  self.__getGeneralSection()
-        config += self.__getNeighborsSection()
-        config += self.__getHyperbolicSection()
-        config += self.__getFibSection()
-        config += self.__getAdvertisingSection()
+        config  = self.__getGeneralSection() + "\n"
+        config += self.__getNeighborsSection() + "\n"
+        config += self.__getHyperbolicSection() + "\n"
+        config += self.__getFibSection() + "\n"
+        config += self.__getAdvertisingSection() + "\n"
         config += self.__getSecuritySection()
 
         return config