Add new Nfdc batch helper to speed up running multiple commands
and refactoring to minimize code duplication.

Also added two demos/tests under examples to validate behavior.

Change-Id: I0700b7e204793d2a6bbf75f7c920a3e3951005be
diff --git a/minindn/helpers/nfdc.py b/minindn/helpers/nfdc.py
index 08aeccf..9aa38f4 100644
--- a/minindn/helpers/nfdc.py
+++ b/minindn/helpers/nfdc.py
@@ -23,126 +23,144 @@
 
 from mininet.log import debug, warn
 from minindn.minindn import Minindn
-from minindn.util import MACToEther
+from minindn.util import MACToEther, getPopen
 
-# If needed (e.g. to speed up the process), use a smaller (or larger value) 
+from subprocess import PIPE
+
+# If needed (e.g. to speed up the process), use a smaller (or larger value)
 # based on your machines resource (CPU, memory)
 SLEEP_TIME = 0.0015
 
-class Nfdc(object):
+class _NfdcBase(object):
     STRATEGY_ASF = 'asf'
     STRATEGY_BEST_ROUTE = 'best-route'
     STRATEGY_MULTICAST = 'multicast'
-    STRATEGY_NCC = 'ncc'
     PROTOCOL_UDP = 'udp'
     PROTOCOL_TCP = 'tcp'
     PROTOCOL_ETHER = 'ether'
 
+def _registerRoute(namePrefix, remoteNode, protocol=_NfdcBase.PROTOCOL_UDP, origin=255,
+                    cost=0, inheritFlag=True, captureFlag=False, expirationInMillis=None):
+    cmd = ""
+    if remoteNode.isdigit() and not protocol == "fd":
+        cmd = f'route add {namePrefix} {remoteNode} origin {origin} cost {cost}'
+    else:
+        if protocol == "ether":
+            remoteNode = MACToEther(remoteNode)
+        cmd = f'route add {namePrefix} {protocol}://{remoteNode} origin {origin} cost {cost}'
+    if not inheritFlag:
+        cmd += " no-inherit"
+    if captureFlag:
+        cmd += " capture"
+    if expirationInMillis:
+        cmd += f" expires {expirationInMillis}"
+    return cmd
+
+def _unregisterRoute(namePrefix, remoteNode, protocol=_NfdcBase.PROTOCOL_UDP, origin=255):
+    cmd = ""
+    if remoteNode.isdigit() and not protocol == "fd":
+        cmd = f'route remove {namePrefix} {remoteNode} origin {origin}'
+    else:
+        if protocol == "ether":
+            remoteNode = MACToEther(remoteNode)
+        cmd = f'route remove {namePrefix} {protocol}://{remoteNode} origin {origin}'
+    return cmd
+
+def _createFace(remoteNodeAddress, protocol=_NfdcBase.PROTOCOL_UDP, isPermanent=False, localInterface=''):
+    '''Create face in node's NFD instance. Returns FaceID of created face or -1 if failed.'''
+    if protocol == "ether" and not localInterface:
+        warn("Cannot create ethernet face without local interface!")
+        return
+    elif protocol != "ether" and localInterface:
+        warn("Cannot create non-ethernet face with local interface specified!")
+        return
+    elif protocol == "ether" and localInterface:
+        remoteNodeAddress = MACToEther(remoteNodeAddress)
+    cmd = (f'face create {protocol}://{remoteNodeAddress} '
+           f'{f"local dev://{localInterface} " if localInterface else ""}'
+           f'{"persistency permanent" if isPermanent else "persistency persistent"}')
+    return cmd
+
+def _destroyFace(remoteNode, protocol=_NfdcBase.PROTOCOL_UDP):
+    cmd = ""
+    if remoteNode.isdigit() and not protocol == "fd":
+        cmd = f'face destroy {remoteNode}'
+    else:
+        if protocol == "ether":
+            remoteNode = MACToEther(remoteNode)
+        cmd = f'face destroy {protocol}://{remoteNode}'
+    return cmd
+
+def _setStrategy(namePrefix, strategy):
+    cmd = f'strategy set {namePrefix} ndn:/localhost/nfd/strategy/{strategy}'
+    return cmd
+
+def _unsetStrategy(namePrefix):
+    cmd = f'strategy unset {namePrefix}'
+    return cmd
+
+class Nfdc(_NfdcBase):
     @staticmethod
-    def registerRoute(node, namePrefix, remoteNode, protocol=PROTOCOL_UDP, origin=255,
+    def registerRoute(node, namePrefix, remoteNode, protocol=_NfdcBase.PROTOCOL_UDP, origin=255,
                       cost=0, inheritFlag=True, captureFlag=False, expirationInMillis=None):
-        cmd = ""
-        if remoteNode.isdigit() and not protocol == "fd":
-            cmd = ('nfdc route add {} {} origin {} cost {} {}{}{}').format(
-                namePrefix,
-                remoteNode,
-                origin,
-                cost,
-                'no-inherit ' if not inheritFlag else '',
-                'capture ' if captureFlag else '',
-                'expires {}'.format(expirationInMillis) if expirationInMillis else ''
-            )
-        else:
-            if protocol == "ether":
-                remoteNode = MACToEther(remoteNode)
-            cmd = ('nfdc route add {} {}://{} origin {} cost {} {}{}{}').format(
-                namePrefix,
-                protocol,
-                remoteNode,
-                origin,
-                cost,
-                'no-inherit ' if not inheritFlag else '',
-                'capture ' if captureFlag else '',
-                'expires {}'.format(expirationInMillis) if expirationInMillis else ''
-            )
+        cmd = "nfdc " + _registerRoute(namePrefix, remoteNode, protocol, origin, cost, inheritFlag, captureFlag, expirationInMillis)
         debug(node.cmd(cmd))
         Minindn.sleep(SLEEP_TIME)
 
     @staticmethod
-    def unregisterRoute(node, namePrefix, remoteNode, protocol=PROTOCOL_UDP, origin=255):
-        cmd = ""
-        if remoteNode.isdigit() and not protocol == "fd":
-            cmd = 'nfdc route remove {} {} origin {}'.format(namePrefix, remoteNode, origin)
-        else:
-            if protocol == "ether":
-                remoteNode = MACToEther(remoteNode)
-            cmd = 'nfdc route remove {} {}://{} origin {}'.format(namePrefix, protocol, remoteNode, origin)
+    def unregisterRoute(node, namePrefix, remoteNode, protocol=_NfdcBase.PROTOCOL_UDP, origin=255):
+        cmd = "nfdc " + _unregisterRoute(namePrefix, remoteNode, protocol, origin)
         debug(node.cmd(cmd))
         Minindn.sleep(SLEEP_TIME)
 
     @staticmethod
-    def createFace(node, remoteNodeAddress, protocol=PROTOCOL_UDP, isPermanent=False, localInterface='', allowExisting=True):
+    def createFace(node, remoteNodeAddress, protocol=_NfdcBase.PROTOCOL_UDP, isPermanent=False, localInterface='', allowExisting=True):
         '''Create face in node's NFD instance. Returns FaceID of created face or -1 if failed.'''
-        if protocol == "ether" and not localInterface:
-            warn("Cannot create ethernet face without local interface!")
-            return
-        elif protocol != "ether" and localInterface:
-            warn("Cannot create non-ethernet face with local interface specified!")
-            return
-        elif protocol == "ether" and localInterface:
-            remoteNodeAddress = MACToEther(remoteNodeAddress)
-        cmd = ('nfdc face create {}://{} {}{}'.format(
-            protocol,
-            remoteNodeAddress,
-            'local dev://{} '.format(localInterface) if localInterface else '',
-            'persistency permanent' if isPermanent else 'persistency persistent'
-        ))
+        cmd = "nfdc " + _createFace(remoteNodeAddress, protocol, isPermanent, localInterface)
         output = node.cmd(cmd)
         debug(output)
         Minindn.sleep(SLEEP_TIME)
         if "face-created" in output or (allowExisting and ("face-exists" in output or "face-updated" in output)):
             faceID = output.split(" ")[1][3:]
             if "face-exists" in output or "face-updated" in output:
-               debug("[{}] Existing face found: {}\n".format(node.name, faceID))
+               debug(f'[{node.name}] Existing face found: {faceID}\n')
             return faceID
-        warn("[{}] Face register failed: {}\n".format(node.name, output))
+        warn(f'[{node.name}] Face register failed: {output}\n')
         return -1
 
     @staticmethod
-    def destroyFace(node, remoteNode, protocol=PROTOCOL_UDP):
-        if remoteNode.isdigit() and not protocol == "fd":
-            debug(node.cmd('nfdc face destroy {}'.format(remoteNode)))
-        else:
-            if protocol == "ether":
-                remoteNode = MACToEther(remoteNode)
-            debug(node.cmd('nfdc face destroy {}://{}'.format(protocol, remoteNode)))
+    def destroyFace(node, remoteNode, protocol=_NfdcBase.PROTOCOL_UDP):
+        cmd = "nfdc " + _destroyFace(remoteNode, protocol)
+        debug(node.cmd(cmd))
         Minindn.sleep(SLEEP_TIME)
 
     @staticmethod
     def setStrategy(node, namePrefix, strategy):
-        cmd = 'nfdc strategy set {} ndn:/localhost/nfd/strategy/{}'.format(namePrefix, strategy)
+        cmd = "nfdc " + _setStrategy(namePrefix, strategy)
         out = node.cmd(cmd)
         if out.find('error') != -1:
-            warn("[" + node.name + "] Error on strategy set out: " + out)
+            warn(f'[{node.name}] Error on strategy set out: {out}')
+        debug(out)
         Minindn.sleep(SLEEP_TIME)
 
     @staticmethod
     def unsetStrategy(node, namePrefix):
-        debug(node.cmd("nfdc strategy unset {}".format(namePrefix)))
+        cmd = "nfdc " + _unsetStrategy(namePrefix)
+        debug(node.cmd(cmd))
         Minindn.sleep(SLEEP_TIME)
 
     @staticmethod
-    def getFaceId(node, remoteNodeAddress, localEndpoint=None, protocol=PROTOCOL_UDP, portNum="6363"):
+    def getFaceId(node, remoteNodeAddress, localEndpoint=None, protocol=_NfdcBase.PROTOCOL_UDP, portNum="6363"):
         '''Returns the faceId for a remote node based on FaceURI, or -1 if a face is not found'''
-        #Should this be cached or is the hit not worth it?
+        # Because this is an interactive helper method, we don't split this into _NfdcBase.
         local = ""
         if localEndpoint:
-            local = " local {}".format(localEndpoint)
+            local = f' local {localEndpoint}'
         if protocol == "ether":
             remoteNodeAddress = MACToEther(remoteNodeAddress)
-            output = node.cmd("nfdc face list remote {}://{}{}".format(protocol, remoteNodeAddress, local))
+            output = node.cmd(f'nfdc face list remote {protocol}://{remoteNodeAddress}{local}')
         else:
-            output = node.cmd("nfdc face list remote {}://{}:{}{}".format(protocol, remoteNodeAddress, portNum, local))
+            output = node.cmd(f'nfdc face list remote {protocol}://{remoteNodeAddress}:{portNum}{local}')
         debug(output)
         Minindn.sleep(SLEEP_TIME)
         # This is fragile but we don't have that many better options
@@ -150,3 +168,43 @@
             return -1
         faceId = output.split(" ")[0][7:]
         return faceId
+
+class NfdcBatch(_NfdcBase):
+    '''Helper for writing and passing an Nfdc batch file to Nfd'''
+    def __init__(self):
+        self.batch_commands = []
+
+    def executeBatch(self, node, batch_file_name = None):
+        '''Execute batch file on node given as argument.
+        Optional: batch_file_name is the name of the file that will be created in the node's home dir.'''
+        # The intended use of this method is to either use it for a universal configuration or to use an
+        # individual object for each node.
+        if batch_file_name == None:
+            batch_file_name = "nfdc_helper.batch"
+        batch_str = "\n".join(self.batch_commands)
+        file_path = f'{node.params["params"]["homeDir"]}/{batch_file_name}'
+        with open(file_path, "w") as f:
+            f.write(batch_str)
+        process = getPopen(node, f'nfdc --batch {file_path}')
+        # End user can poll if process has finished if desirable; this is also why we do not clean up the
+        # temporary files.
+        return process
+
+    def registerRoute(self, namePrefix, remoteNode, protocol=_NfdcBase.PROTOCOL_UDP, origin=255,
+                      cost=0, inheritFlag=True, captureFlag=False, expirationInMillis=None):
+        self.batch_commands.append(_registerRoute(namePrefix, remoteNode, protocol, origin, cost, inheritFlag, captureFlag, expirationInMillis))
+
+    def unregisterRoute(self, namePrefix, remoteNode, protocol=_NfdcBase.PROTOCOL_UDP, origin=255):
+        self.batch_commands.append(_unregisterRoute(namePrefix, remoteNode, protocol, origin))
+
+    def createFace(self, remoteNodeAddress, protocol=_NfdcBase.PROTOCOL_UDP, isPermanent=False, localInterface=''):
+        self.batch_commands.append(_createFace(remoteNodeAddress, protocol, isPermanent, localInterface))
+
+    def destroyFace(self, remoteNode, protocol=_NfdcBase.PROTOCOL_UDP):
+        self.batch_commands.append(_destroyFace(remoteNode, protocol))
+
+    def setStrategy(self, namePrefix, strategy):
+        self.batch_commands.append(_setStrategy(namePrefix, strategy))
+
+    def unsetStrategy(self, namePrefix):
+        self.batch_commands.append(_unsetStrategy(namePrefix))
\ No newline at end of file