Remove depenency on internal BMv2 device ID when starting bmv2.py

Needed to support TestON-based Segment Routing tests. All instances
started with internal ID = 0 (one PI/P4Runtime server procees is
executed for each simple_switch_grpc instance, i.e, there's no need to
use different internal IDs to distinguish between switch instances).
Log/conf files and bm-* commands are now based on switch name.

Change-Id: I34d3079d6dff5933ceb4d95f04863426af24eb81
diff --git a/tools/dev/mininet/bmv2.py b/tools/dev/mininet/bmv2.py
index ddbdac4..eadb21b 100644
--- a/tools/dev/mininet/bmv2.py
+++ b/tools/dev/mininet/bmv2.py
@@ -21,6 +21,7 @@
 VALGRIND_PREFIX = 'valgrind --leak-check=yes'
 SWITCH_START_TIMEOUT = 5  # seconds
 BMV2_LOG_LINES = 5
+BMV2_DEFAULT_DEVICE_ID = 0
 
 
 def parseBoolean(value):
@@ -78,8 +79,6 @@
 class ONOSBmv2Switch(Switch):
     """BMv2 software switch with gRPC server"""
 
-    deviceId = 0
-
     def __init__(self, name, json=None, debugger=False, loglevel="warn",
                  elogger=False, grpcport=None, cpuport=255,
                  thriftport=None, netcfg=True, dryrun=False, pipeconf="",
@@ -88,11 +87,6 @@
         Switch.__init__(self, name, **kwargs)
         self.grpcPort = pickUnusedPort() if not grpcport else grpcport
         self.thriftPort = pickUnusedPort() if not thriftport else thriftport
-        if self.dpid:
-            self.deviceId = int(self.dpid, 0 if 'x' in self.dpid else 16)
-        else:
-            self.deviceId = ONOSBmv2Switch.deviceId
-            ONOSBmv2Switch.deviceId += 1
         self.cpuPort = cpuport
         self.json = json
         self.debugger = parseBoolean(debugger)
@@ -100,19 +94,19 @@
         # Important: Mininet removes all /tmp/*.log files in case of exceptions.
         # We want to be able to see the bmv2 log if anything goes wrong, hence
         # avoid the .log extension.
-        self.logfile = '/tmp/bmv2-%d-log' % self.deviceId
+        self.logfile = '/tmp/bmv2-%s-log' % self.name
         self.elogger = parseBoolean(elogger)
         self.pktdump = parseBoolean(pktdump)
         self.netcfg = parseBoolean(netcfg)
         self.dryrun = parseBoolean(dryrun)
         self.valgrind = parseBoolean(valgrind)
-        self.netcfgfile = '/tmp/bmv2-%d-netcfg.json' % self.deviceId
+        self.netcfgfile = '/tmp/bmv2-%s-netcfg.json' % self.name
         self.pipeconfId = pipeconf
         self.injectPorts = parseBoolean(portcfg)
         self.withGnmi = parseBoolean(gnmi)
         self.longitude = kwargs['longitude'] if 'longitude' in kwargs else None
         self.latitude = kwargs['latitude'] if 'latitude' in kwargs else None
-        self.onosDeviceId = "device:bmv2:%d" % self.deviceId
+        self.onosDeviceId = "device:bmv2:%s" % self.name
         self.logfd = None
         self.bmv2popen = None
         self.stopped = False
@@ -120,8 +114,8 @@
         # Remove files from previous executions
         self.cleanupTmpFiles()
 
-        writeToFile("/tmp/bmv2-%d-grpc-port" % self.deviceId, self.grpcPort)
-        writeToFile("/tmp/bmv2-%d-thrift-port" % self.deviceId, self.thriftPort)
+        writeToFile("/tmp/bmv2-%s-grpc-port" % self.name, self.grpcPort)
+        writeToFile("/tmp/bmv2-%s-thrift-port" % self.name, self.thriftPort)
 
     def getSourceIp(self, dstIP):
         """
@@ -147,7 +141,7 @@
                 "p4runtime": {
                     "ip": srcIP,
                     "port": self.grpcPort,
-                    "deviceId": self.deviceId,
+                    "deviceId": BMV2_DEFAULT_DEVICE_ID,
                     "deviceKeyId": "p4runtime:%s" % self.onosDeviceId
                 }
             },
@@ -253,20 +247,23 @@
             raise ex
 
     def grpcTargetArgs(self):
-        args = ['--device-id %s' % str(self.deviceId)]
+        args = ['--device-id %s' % str(BMV2_DEFAULT_DEVICE_ID)]
         for port, intf in self.intfs.items():
             if not intf.IP():
                 args.append('-i %d@%s' % (port, intf.name))
+        args.append('--thrift-port %s' % self.thriftPort)
+        ntfaddr = 'ipc:///tmp/bmv2-%s-notifications.ipc' % self.name
+        args.append('--notifications-addr %s' % ntfaddr)
         if self.elogger:
-            nanomsg = 'ipc:///tmp/bmv2-%d-log.ipc' % self.deviceId
-            args.append('--nanolog %s' % nanomsg)
+            nanologaddr = 'ipc:///tmp/bmv2-%s-nanolog.ipc' % self.name
+            args.append('--nanolog %s' % nanologaddr)
         if self.debugger:
-            args.append('--debugger')
+            dbgaddr = 'ipc:///tmp/bmv2-%s-debug.ipc' % self.name
+            args.append('--debugger-addr %s' % dbgaddr)
         args.append('--log-console')
         if self.pktdump:
             args.append('--pcap --dump-packet-data %s' % PKT_BYTES_TO_DUMP)
         args.append('-L%s' % self.loglevel)
-        args.append('--thrift-port %s' % self.thriftPort)
         if not self.json:
             args.append('--no-p4')
         else:
@@ -298,7 +295,7 @@
     def printBmv2Log(self):
         if os.path.isfile(self.logfile):
             print "-" * 80
-            print "BMv2 %d log (from %s):" % (self.deviceId, self.logfile)
+            print "%s log (from %s):" % (self.name, self.logfile)
             with open(self.logfile, 'r') as f:
                 lines = f.readlines()
                 if len(lines) > BMV2_LOG_LINES:
@@ -325,7 +322,7 @@
             self.logfd.close()
 
     def cleanupTmpFiles(self):
-        self.cmd("rm -f /tmp/bmv2-%d-*" % self.deviceId)
+        self.cmd("rm -f /tmp/bmv2-%s-*" % self.name)
 
     def stop(self, deleteIntfs=True):
         """Terminate switch."""