ONOS-6457 Updated bmv2.py to work with simple_switch_grpc target

Change-Id: I696daca118f631c73b747eaea8ce936cd4b1097a
diff --git a/tools/dev/mininet/bmv2.py b/tools/dev/mininet/bmv2.py
index 44f66c2..d6f96b1 100644
--- a/tools/dev/mininet/bmv2.py
+++ b/tools/dev/mininet/bmv2.py
@@ -2,9 +2,8 @@
 
 from mininet.log import error, info
 from mininet.node import Switch
-from os import environ
-from os.path import isfile
 
+BMV2_TARGET = 'simple_switch_grpc'
 
 class ONOSBmv2Switch(Switch):
     """BMv2 software switch """
@@ -12,35 +11,28 @@
     deviceId = 0
     instanceCount = 0
 
-    def __init__(self, name, thriftPort=None, deviceId=None, debugger=False,
-                 loglevel="warn", elogger=False, persistent=True, **kwargs):
+    def __init__(self, name, debugger=False, loglevel="warn", elogger=False, persistent=False,
+                 logflush=False, **kwargs):
         Switch.__init__(self, name, **kwargs)
-        self.swPath = environ['BMV2_EXE']
-        self.jsonPath = environ['BMV2_JSON']
-        if thriftPort:
-            self.thriftPort = thriftPort
+        self.thriftPort = ONOSBmv2Switch.pickUnusedPort()
+        self.grpcPort = ONOSBmv2Switch.pickUnusedPort()
+        if self.dpid:
+            self.deviceId = int(self.dpid, 0 if 'x' in self.dpid else 16)
         else:
-            self.thriftPort = ONOSBmv2Switch.pickUnusedPort()
-        if not deviceId:
-            if self.dpid:
-                self.deviceId = int(self.dpid, 0 if 'x' in self.dpid else 16)
-            else:
-                self.deviceId = ONOSBmv2Switch.deviceId
-                ONOSBmv2Switch.deviceId += 1
-        else:
-            self.deviceId = deviceId
-            ONOSBmv2Switch.deviceId = max(deviceId, ONOSBmv2Switch.deviceId)
+            self.deviceId = ONOSBmv2Switch.deviceId
+            ONOSBmv2Switch.deviceId += 1
         self.debugger = debugger
         self.loglevel = loglevel
         self.logfile = '/tmp/bmv2-%d.log' % self.deviceId
-        self.output = open(self.logfile, 'w')
         self.elogger = elogger
         self.persistent = persistent
+        self.logflush = logflush
         if persistent:
             self.exectoken = "/tmp/bmv2-%d-exec-token" % self.deviceId
             self.cmd("touch %s" % self.exectoken)
         # Store thrift port for future uses.
         self.cmd("echo %d > /tmp/bmv2-%d-thrift-port" % (self.thriftPort, self.deviceId))
+        self.cmd("echo %d > /tmp/bmv2-%d-grpc-port" % (self.grpcPort, self.deviceId))
 
     @classmethod
     def pickUnusedPort(cls):
@@ -50,26 +42,8 @@
         s.close()
         return port
 
-    @classmethod
-    def setup(cls):
-        err = False
-        if 'BMV2_EXE' not in environ:
-            error("ERROR! environment var $BMV2_EXE not set\n")
-            err = True
-        elif not isfile(environ['BMV2_EXE']):
-            error("ERROR! BMV2_EXE=%s: no such file\n" % environ['BMV2_EXE'])
-            err = True
-        if 'BMV2_JSON' not in environ:
-            error("ERROR! environment var $BMV2_JSON not set\n")
-            err = True
-        elif not isfile(environ['BMV2_JSON']):
-            error("ERROR! BMV2_JSON=%s: no such file\n" % environ['BMV2_JSON'])
-            err = True
-        if err:
-            exit(1)
-
     def start(self, controllers):
-        args = [self.swPath, '--device-id %s' % str(self.deviceId)]
+        args = [BMV2_TARGET, '--device-id %s' % str(self.deviceId)]
         for port, intf in self.intfs.items():
             if not intf.IP():
                 args.append('-i %d@%s' % (port, intf.name))
@@ -80,22 +54,14 @@
             args.append('--nanolog %s' % nanomsg)
         if self.debugger:
             args.append('--debugger')
-        args.append('--log-console -L%s' % self.loglevel)
-        args.append(self.jsonPath)
-        try:  # onos.py
-            clist = controllers[0].nodes()
-        except AttributeError:
-            clist = controllers
-        assert len(clist) > 0
-        # BMv2 can't connect to multiple controllers.
-        # Uniformly balance connections among available ones.
-        cip = clist[ONOSBmv2Switch.instanceCount % len(clist)].IP()
-        ONOSBmv2Switch.instanceCount += 1
-        # BMv2 controler port is hardcoded here as it is hardcoded also in ONOS.
-        cport = 40123
+        args.append('--log-file %s -L%s' % (self.logfile, self.loglevel))
+        if self.logflush:
+            args.append('--log-flush')
+        args.append('--no-p4')
         args.append('--')
-        args.append('--controller-ip %s' % cip)
-        args.append('--controller-port %d' % cport)
+        args.append('--enable-swap')
+        if self.grpcPort:
+            args.append('--grpc-server-addr 0.0.0.0:%d' % self.grpcPort)
 
         bmv2cmd = " ".join(args)
         info("\nStarting BMv2 target: %s\n" % bmv2cmd)
@@ -104,17 +70,17 @@
             cmdStr = "(while [ -e {} ]; " \
                      "do {} ; " \
                      "sleep 1; " \
-                     "done;) > {} 2>&1 &".format(self.exectoken, bmv2cmd, self.logfile)
+                     "done;) &".format(self.exectoken, bmv2cmd)
         else:
-            cmdStr = "{} > {} 2>&1 &".format(bmv2cmd, self.logfile)
+            cmdStr = "{} &".format(bmv2cmd)
         self.cmd(cmdStr)
 
     def stop(self):
         "Terminate switch."
-        self.output.flush()
         self.cmd("rm -f /tmp/bmv2-%d-*" % self.deviceId)
-        self.cmd("rm -f /tmp/bmv2-%d.log" % self.deviceId)
-        self.cmd('kill %' + self.swPath)
+        # wildcard end as BMv2 might create log files with .txt extension
+        self.cmd("rm -f /tmp/bmv2-%d.log*" % self.deviceId)
+        self.cmd('kill %' + BMV2_TARGET)
         self.deleteIntfs()