Use the nlsr.conf file installed in the system

Use infoedit to edit nlsr.conf file

refs: #4386, #4038

Change-Id: I4ad1e1efe92b20f444f5f9346679574888a456ed
diff --git a/ndn/nlsr.py b/ndn/nlsr.py
index 0fb73a7..2d72775 100644
--- a/ndn/nlsr.py
+++ b/ndn/nlsr.py
@@ -45,8 +45,8 @@
         self.confFile = "%s/nlsr.conf" % node.homeFolder
 
         # Make directory for log file
-        self.logDir = "%s/log" % node.homeFolder
-        self.node.cmd("mkdir %s" % self.logDir)
+        self.logDir = "{}/log".format(node.homeFolder)
+        self.node.cmd("mkdir {}".format(self.logDir))
 
         # Create faces in NFD
         self.createFaces()
@@ -144,6 +144,7 @@
         self.node = node
         self.isSecurityEnabled = isSecurityEnabled
         self.faceType = faceType
+        self.infocmd = "infoedit -f nlsr.conf"
 
         parameters = node.nlsrParameters
 
@@ -153,55 +154,29 @@
         self.hyperAngle = parameters.get("angle", 0.0)
         self.logLevel = parameters.get("nlsr-log-level", "DEBUG")
         self.neighborIPs = []
+        self.node.cmd("sudo cp /usr/local/etc/ndn/nlsr.conf.sample nlsr.conf")
 
     def createConfigFile(self):
 
-        tmp_conf = "/tmp/nlsr.conf"
+        self.__editGeneralSection()
+        self.__editNeighborsSection()
+        self.__editHyperbolicSection()
+        self.__editFibSection()
+        self.__editAdvertisingSection()
+        self.__editSecuritySection()
 
-        configFile = open(tmp_conf, 'w')
-        configFile.write(self.__getConfig())
-        configFile.close()
+    def __editGeneralSection(self):
 
-        # 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)
+        self.node.cmd("{} -s general.network -v {}".format(self.infocmd, NETWORK))
+        self.node.cmd("{} -s general.site -v /{}-site".format(self.infocmd, self.node.name))
+        self.node.cmd("{} -s general.router -v /%C1.Router/cs/{}".format(self.infocmd, self.node.name))
+        self.node.cmd("{} -s general.log-level -v {}".format(self.infocmd, self.logLevel))
+        self.node.cmd("{} -s general.log-dir -v {}/log".format(self.infocmd, self.node.homeFolder))
+        self.node.cmd("{} -s general.seq-dir -v {}/log".format(self.infocmd, self.node.homeFolder))
 
-        # Copy nlsr.conf to home folder
-        self.node.cmd("mv %s nlsr.conf" % tmp_conf)
+    def __editNeighborsSection(self):
 
-    def __getConfig(self):
-
-        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
-
-    def __getGeneralSection(self):
-
-        general =  "general\n"
-        general += "{\n"
-        general += "  network {}\n".format(NETWORK)
-        general += "  site /{}-site\n".format(self.node.name)
-        general += "  router /%C1.Router/cs/" + self.node.name + "\n"
-        general += "  log-level " + self.logLevel + "\n"
-        general += "  log-dir " + self.node.homeFolder + "/log\n"
-        general += "  seq-dir " + self.node.homeFolder + "/log\n"
-        general += "}\n"
-
-        return general
-
-    def __getNeighborsSection(self):
-
-        neighbors =  "neighbors\n"
-        neighbors += "{\n"
-
+        self.node.cmd("{} -d neighbors.neighbor".format(self.infocmd))
         for intf in self.node.intfList():
             link = intf.link
             if link:
@@ -219,237 +194,37 @@
                 # To be used later to create faces
                 self.neighborIPs.append(ip)
 
-                neighbors += "neighbor\n"
-                neighbors += "{\n"
-                neighbors += "  name " + NETWORK + other.name + "-site/%C1.Router/cs/" + other.name + "\n"
-                neighbors += "  face-uri {}://{}\n".format(self.faceType, ip)
-                neighbors += "  link-cost " + linkCost + "\n"
-                neighbors += "}\n"
+                self.node.cmd("{} -a neighbors.neighbor \
+                              <<<\'name {}{}-site/%C1.Router/cs/{} face-uri {}://{}\n cost {}\'"
+                              .format(self.infocmd, NETWORK, other.name, other.name, self.faceType, ip, linkCost))
 
-        neighbors += "}\n"
+    def __editHyperbolicSection(self):
 
-        return neighbors
+        self.node.cmd("{} -s hyperbolic.state -v {}".format(self.infocmd, self.hyperbolicState))
+        self.node.cmd("{} -s hyperbolic.radius -v {}".format(self.infocmd, self.hyperRadius))
+        self.node.cmd("{} -s hyperbolic.angle -v {}".format(self.infocmd, self.hyperAngle))
 
-    def __getHyperbolicSection(self):
+    def __editFibSection(self):
 
-        hyper =  "hyperbolic\n"
-        hyper += "{\n"
-        hyper += "state %s\n" % self.hyperbolicState
-        hyper += "radius " + str(self.hyperRadius) + "\n"
-        hyper += "angle " + str(self.hyperAngle) + "\n"
-        hyper += "}\n"
+        self.node.cmd("{} -s fib.max-faces-per-prefix  -v {}".format(self.infocmd, self.nFaces))
 
-        return hyper
+    def __editAdvertisingSection(self):
 
-    def __getFibSection(self):
+        self.node.cmd("{} -d advertising.prefix".format(self.infocmd))
+        self.node.cmd("{} -s advertising.prefix -v {}{}-site/{}"
+                      .format(self.infocmd, NETWORK, self.node.name, self.node.name))
 
-        fib =  "fib\n"
-        fib += "{\n"
-        fib += "  max-faces-per-prefix " + str(self.nFaces) + "\n"
-        fib += "}\n"
+    def __editSecuritySection(self):
 
-        return fib
-
-    def __getAdvertisingSection(self):
-
-        advertising =  "advertising\n"
-        advertising += "{\n"
-        advertising += "  prefix "+ NETWORK + self.node.name + "-site/" + self.node.name + "\n"
-        advertising += "}\n"
-
-        return advertising
-
-    def __getSecuritySection(self):
+        self.node.cmd("{} -d security.cert-to-publish".format(self.infocmd))
         if self.isSecurityEnabled is False:
-            security = textwrap.dedent("""\
-                security
-                {
-                  validator
-                  {
-                    trust-anchor
-                    {
-                      type any
-                    }
-                  }
-                  prefix-update-validator
-                  {
-                    trust-anchor
-                    {
-                      type any
-                    }
-                  }
-                }""")
+            self.node.cmd("{} -s security.validator.trust-anchor.type -v any".format(self.infocmd))
+            self.node.cmd("{} -d security.validator.trust-anchor.file-name".format(self.infocmd))
+            self.node.cmd("{} -s security.prefix-update-validator.trust-anchor.type -v any".format(self.infocmd))
+            self.node.cmd("{} -d security.prefix-update-validator.trust-anchor.file-name".format(self.infocmd))
         else:
-            security = textwrap.dedent("""\
-                security
-                {
-                  validator
-                  {
-                    rule
-                    {
-                      id "NSLR Hello Rule"
-                      for data
-                      filter
-                      {
-                        type name
-                        regex ^[^<NLSR><INFO>]*<NLSR><INFO><><>$
-                      }
-                      checker
-                      {
-                        type customized
-                        sig-type rsa-sha256
-                        key-locator
-                        {
-                          type name
-                          hyper-relation
-                          {
-                            k-regex ^([^<KEY><NLSR>]*)<NLSR><KEY><>$
-                            k-expand \\\\1
-                            h-relation equal
-                            p-regex ^([^<NLSR><INFO>]*)<NLSR><INFO><><>$
-                            p-expand \\\\1
-                          }
-                        }
-                      }
-                    }
-
-                    rule
-                    {
-                      id "NSLR LSA Rule"
-                      for data
-                      filter
-                      {
-                        type name
-                        regex ^[^<NLSR><LSA>]*<NLSR><LSA>
-                      }
-                      checker
-                      {
-                        type customized
-                        sig-type rsa-sha256
-                        key-locator
-                        {
-                          type name
-                          hyper-relation
-                          {
-                            k-regex  ^([^<KEY><NLSR>]*)<NLSR><KEY><>$
-                            k-expand \\\\1
-                            h-relation equal
-                            p-regex ^<localhop>([^<NLSR><LSA>]*)<NLSR><LSA>(<>*)<><><><>$
-                            p-expand \\\\1\\\\2
-                          }
-                        }
-                      }
-                    }
-
-                    rule
-                    {
-                      id "NSLR Hierarchy Exception Rule"
-                      for data
-                      filter
-                      {
-                        type name
-                        regex ^[^<KEY><%C1.Router>]*<%C1.Router>[^<KEY><NLSR>]*<KEY><><><>$
-                      }
-                      checker
-                      {
-                        type customized
-                        sig-type rsa-sha256
-                        key-locator
-                        {
-                          type name
-                          hyper-relation
-                          {
-                            k-regex ^([^<KEY><%C1.Operator>]*)<%C1.Operator>[^<KEY>]*<KEY><>$
-                            k-expand \\\\1
-                            h-relation equal
-                            p-regex ^([^<KEY><%C1.Router>]*)<%C1.Router>[^<KEY>]*<KEY><><><>$
-                            p-expand \\\\1
-                          }
-                        }
-                      }
-                    }
-
-                    rule
-                    {
-                      id "NSLR Hierarchical Rule"
-                      for data
-                      filter
-                      {
-                        type name
-                        regex ^[^<KEY>]*<KEY><><><>$
-                      }
-                      checker
-                      {
-                        type hierarchical
-                        sig-type rsa-sha256
-                      }
-                    }
-
-                    trust-anchor
-                    {
-                      type file
-                      file-name "security/root.cert"
-                    }
-                  }
-
-                  prefix-update-validator
-                  {
-                    rule
-                    {
-                      id "NLSR ControlCommand Rule"
-                      for interest
-                      filter
-                      {
-                        type name
-                        regex ^<localhost><nlsr><prefix-update>[<advertise><withdraw>]<><><>$
-                      }
-                      checker
-                      {
-                        type customized
-                        sig-type rsa-sha256
-                        key-locator
-                        {
-                          type name
-                          regex ^([^<KEY><%C1.Operator>]*)<%C1.Operator>[^<KEY>]*<KEY><>$
-                        }
-                      }
-                    }
-
-                    rule
-                    {
-                      id "NLSR Hierarchy Rule"
-                      for data
-                      filter
-                      {
-                        type name
-                        regex ^[^<KEY>]*<KEY><><><>$
-                      }
-                      checker
-                      {
-                        type hierarchical
-                        sig-type rsa-sha256
-                      }
-                    }
-
-                    trust-anchor
-                    {
-                      type file
-                      file-name "security/site.cert"
-                    }
-                  }
-                  ; cert-to-publish "security/root.cert"  ; optional, a file containing the root certificate
-                                                 ; Only the router that is designated to publish the root cert
-                                                 ; needs to specify this
-
-                  cert-to-publish "security/site.cert"  ; optional, a file containing the site certificate
-                                                 ; Only the router that is designated to publish the site cert
-                                                 ; needs to specify this
-
-                  cert-to-publish "security/op.cert" ; optional, a file containing the operator certificate
-                                                    ; Only the router that is designated to publish the operator
-                                                    ; cert needs to specify this
-
-                  cert-to-publish "security/router.cert"  ; required, a file containing the router certificate.
-                }""")
-
-        return security
+            self.node.cmd("{} -s security.validator.trust-anchor.file-name -v security/root.cert".format(self.infocmd))
+            self.node.cmd("{} -s security.prefix-update-validator.trust-anchor.file-name -v security/site.cert".format(self.infocmd))
+            self.node.cmd("{} -p security.cert-to-publish -v security/site.cert".format(self.infocmd))
+            self.node.cmd("{} -p security.cert-to-publish -v security/op.cert".format(self.infocmd))
+            self.node.cmd("{} -p security.cert-to-publish -v security/router.cert".format(self.infocmd))
\ No newline at end of file