Fix net-trellis-p4-smoke not working with latest stratum_bmv2

- Fix scenario files to use correct deviceId
- More robust liveness check in bmv2.py
- Use different ports for stratum internal server
- Longer switch startup timeout in bmv2.py
- Ignore number of flow rules when checking summary (make scenario
independent of trellis implementation)

Change-Id: I206e5339d2e78ae9a025caa5ec4862a9d4c24871
diff --git a/tools/dev/mininet/bmv2.py b/tools/dev/mininet/bmv2.py
index 522a80c..6a7285f 100644
--- a/tools/dev/mininet/bmv2.py
+++ b/tools/dev/mininet/bmv2.py
@@ -32,7 +32,7 @@
 SIMPLE_SWITCH_GRPC = 'simple_switch_grpc'
 PKT_BYTES_TO_DUMP = 80
 VALGRIND_PREFIX = 'valgrind --leak-check=yes'
-SWITCH_START_TIMEOUT = 5  # seconds
+SWITCH_START_TIMEOUT = 10  # seconds
 BMV2_LOG_LINES = 5
 BMV2_DEFAULT_DEVICE_ID = 1
 DEFAULT_PIPECONF = "org.onosproject.pipelines.basic"
@@ -81,7 +81,8 @@
             if sw.stopped:
                 return
             with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
-                if s.connect_ex(('127.0.0.1', sw.grpcPort)) == 0:
+                port = sw.grpcPortInternal if sw.grpcPortInternal else sw.grpcPort
+                if s.connect_ex(('localhost', port)) == 0:
                     time.sleep(1)
                 else:
                     warn("\n*** WARN: switch %s died ☠️ \n" % sw.name)
@@ -126,6 +127,7 @@
                  **kwargs):
         Switch.__init__(self, name, **kwargs)
         self.grpcPort = grpcport
+        self.grpcPortInternal = None  # Needed for Stratum (local_hercules_url)
         self.thriftPort = thriftport
         self.cpuPort = cpuport
         self.json = json
@@ -303,6 +305,8 @@
             os.mkdir(config_dir)
             with open(self.chassisConfigFile, 'w') as fp:
                 fp.write(self.chassisConfig())
+            if self.grpcPortInternal is None:
+                self.grpcPortInternal = pickUnusedPort()
             cmdString = self.getStratumCmdString(config_dir)
         else:
             if self.thriftPort is None:
@@ -320,6 +324,8 @@
                 # Start the switch
                 self.stopped = False
                 self.logfd = open(self.logfile, "w")
+                self.logfd.write(cmdString + "\n\n" + "-" * 80 + "\n\n")
+                self.logfd.flush()
                 self.bmv2popen = self.popen(cmdString,
                                             stdout=self.logfd,
                                             stderr=self.logfd)
@@ -346,11 +352,12 @@
             stratumRoot + STRATUM_BINARY,
             '-device_id=%d' % self.p4DeviceId,
             '-chassis_config_file=%s' % self.chassisConfigFile,
-            '-forwarding_pipeline_configs_file=%s/config.txt' % config_dir,
+            '-forwarding_pipeline_configs_file=%s/pipeline_config.txt' % config_dir,
             '-persistent_config_dir=' + config_dir,
             '-initial_pipeline=' + stratumRoot + STRATUM_INIT_PIPELINE,
             '-cpu_port=%s' % self.cpuPort,
             '-external_hercules_urls=0.0.0.0:%d' % self.grpcPort,
+            '-local_hercules_url=localhost:%d' % self.grpcPortInternal,
             '-max_num_controllers_per_node=10'
         ]
         return " ".join(args)
@@ -390,7 +397,8 @@
         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         endtime = time.time() + SWITCH_START_TIMEOUT
         while True:
-            result = sock.connect_ex(('127.0.0.1', self.grpcPort))
+            port = self.grpcPortInternal if self.grpcPortInternal else self.grpcPort
+            result = sock.connect_ex(('localhost', port))
             if result == 0:
                 # No new line
                 sys.stdout.write("⚡️ %s @ %d" % (self.targetName, self.bmv2popen.pid))
diff --git a/tools/test/scenarios/bin/mininet-p4-trellis b/tools/test/scenarios/bin/mininet-p4-trellis
index b5a3593..db19cc4 100755
--- a/tools/test/scenarios/bin/mininet-p4-trellis
+++ b/tools/test/scenarios/bin/mininet-p4-trellis
@@ -4,7 +4,6 @@
 # Starts mininet in the network machine with the Trellis P4 topology
 # -----------------------------------------------------------------------------
 
-set -e
 set -x
 
 [[ ! -d "$ONOS_ROOT" ]] && echo "ONOS_ROOT is not defined" >&2 && exit 1
@@ -25,7 +24,7 @@
 "
 
 (
-    ssh -t -t ${remote} "screen -L -S mininet sudo ${trellisp4Sh}"
+    ssh -t -t ${remote} "screen -L -S mininet ${trellisp4Sh}"
     scp ${ONOS_USER}@[${OCN}]:$log /tmp/mininet.log
     ssh ${remote} rm -f $log
 ) &
diff --git a/tools/test/scenarios/net-trellis-p4-devices.xml b/tools/test/scenarios/net-trellis-p4-devices.xml
index 1b79eab..76e3632 100644
--- a/tools/test/scenarios/net-trellis-p4-devices.xml
+++ b/tools/test/scenarios/net-trellis-p4-devices.xml
@@ -21,17 +21,17 @@
         <group name="Net-Trellis-P4-Devices">
 
             <step name="Net-Trellis-P4-Devices.Device-1"
-                  exec="onos-find-device ${OC1} device:bmv2:s204"/>
+                  exec="onos-find-device ${OC1} device:s204"/>
 
             <step name="Net-Trellis-P4-Devices.Device-2"
-                  exec="onos-find-device ${OC1} device:bmv2:s205"/>
+                  exec="onos-find-device ${OC1} device:s205"/>
 
             <step name="Net-Trellis-P4-Devices.Device-3"
-                  exec="onos-find-device ${OC1} device:bmv2:s226"/>
+                  exec="onos-find-device ${OC1} device:s226"/>
 
             <step name="Net-Trellis-P4-Devices.Device-4"
-                  exec="onos-find-device ${OC1} device:bmv2:s227"/>
+                  exec="onos-find-device ${OC1} device:s227"/>
 
         </group>
 
-</scenario>
\ No newline at end of file
+</scenario>
diff --git a/tools/test/scenarios/net-trellis-p4-hosts.xml b/tools/test/scenarios/net-trellis-p4-hosts.xml
index 287cbe7..0ad2c59 100644
--- a/tools/test/scenarios/net-trellis-p4-hosts.xml
+++ b/tools/test/scenarios/net-trellis-p4-hosts.xml
@@ -20,13 +20,13 @@
 
         <group name="Net-Trellis-P4-Hosts">
             <step name="Net-Trellis-P4-Hosts.Host-1"
-                  exec="onos-find-host ${OC1} 00:AA:00:00:00:01 device:bmv2:s204/3 10.0.2.1"/>
+                  exec="onos-find-host ${OC1} 00:AA:00:00:00:01 device:s204/3 10.0.2.1"/>
             <step name="Net-Trellis-P4-Hosts.Host-2"
-                  exec="onos-find-host ${OC1} 00:AA:00:00:00:02 device:bmv2:s204/4 10.0.2.2"/>
+                  exec="onos-find-host ${OC1} 00:AA:00:00:00:02 device:s204/4 10.0.2.2"/>
             <step name="Net-Trellis-P4-Hosts.Host-3"
-                  exec="onos-find-host ${OC1} 00:AA:00:00:00:03 device:bmv2:s205/3 10.0.3.1"/>
+                  exec="onos-find-host ${OC1} 00:AA:00:00:00:03 device:s205/3 10.0.3.1"/>
             <step name="Net-Trellis-P4-Hosts.Host-4"
-                  exec="onos-find-host ${OC1} 00:AA:00:00:00:04 device:bmv2:s205/4 10.0.3.2"/>
+                  exec="onos-find-host ${OC1} 00:AA:00:00:00:04 device:s205/4 10.0.3.2"/>
         </group>
 
 </scenario>
diff --git a/tools/test/scenarios/net-trellis-p4-links.xml b/tools/test/scenarios/net-trellis-p4-links.xml
index 0a19caa..137877c 100644
--- a/tools/test/scenarios/net-trellis-p4-links.xml
+++ b/tools/test/scenarios/net-trellis-p4-links.xml
@@ -24,30 +24,30 @@
             <!-- Bi-Directional link between s204 and s226 -->
             <!-- Link 1 -->
             <step name="Net-Trellis-P4-Links.Query-204-226-Link-1"
-                  exec="find-link.py ${OC1} link_204_226_1_ device:bmv2:s204 1 device:bmv2:s226 1"/>
+                  exec="find-link.py ${OC1} link_204_226_1_ device:s204 1 device:s226 1"/>
             <step name="Net-Trellis-P4-Links.Validate-204-226-Link-1-State" requires="^"
                   exec="test '${link_204_226_1_State}' == 'ACTIVE'"/>
             <step name="Net-Trellis-P4-Links.Validate-204-226-Link-1-Type" requires="^"
                   exec="test '${link_204_226_1_Type}' == 'DIRECT'"/>
             <step name="Net-Trellis-P4-Links.Validate-204-226-Link-1-Src-Device" requires="^"
-                  exec="test '${link_204_226_1_SrcDevice}' == 'device:bmv2:s204'"/>
+                  exec="test '${link_204_226_1_SrcDevice}' == 'device:s204'"/>
             <step name="Net-Trellis-P4-Links.Validate-204-226-Link-1-Dst-Device" requires="^"
-                  exec="test '${link_204_226_1_DstDevice}' == 'device:bmv2:s226'"/>
+                  exec="test '${link_204_226_1_DstDevice}' == 'device:s226'"/>
             <step name="Net-Trellis-P4-Links.Validate-204-226-Link-1-Src-Port" requires="^"
                   exec="test '${link_204_226_1_SrcPort}' == '1'"/>
             <step name="Net-Trellis-P4-Links.Validate-204-226-Link-1-Dst-Port" requires="^"
                   exec="test '${link_204_226_1_DstPort}' == '1'"/>
 
             <step name="Net-Trellis-P4-Links.Query-226-204-Link-2"
-                  exec="find-link.py ${OC1} link_226_204_2_ device:bmv2:s226 1 device:bmv2:s204 1"/>
+                  exec="find-link.py ${OC1} link_226_204_2_ device:s226 1 device:s204 1"/>
             <step name="Net-Trellis-P4-Links.Validate-226-204-Link-2-State" requires="^"
                   exec="test '${link_226_204_2_State}' == 'ACTIVE'"/>
             <step name="Net-Trellis-P4-Links.Validate-226-204-Link-2-Type" requires="^"
                   exec="test '${link_226_204_2_Type}' == 'DIRECT'"/>
             <step name="Net-Trellis-P4-Links.Validate-226-204-Link-2-Src-Device" requires="^"
-                  exec="test '${link_226_204_2_SrcDevice}' == 'device:bmv2:s226'"/>
+                  exec="test '${link_226_204_2_SrcDevice}' == 'device:s226'"/>
             <step name="Net-Trellis-P4-Links.Validate-226-204-Link-2-Dst-Device" requires="^"
-                  exec="test '${link_226_204_2_DstDevice}' == 'device:bmv2:s204'"/>
+                  exec="test '${link_226_204_2_DstDevice}' == 'device:s204'"/>
             <step name="Net-Trellis-P4-Links.Validate-226-204-Link-2-Src-Port" requires="^"
                   exec="test '${link_226_204_2_SrcPort}' == '1'"/>
             <step name="Net-Trellis-P4-Links.Validate-226-204-Link-2-Dst-Port" requires="^"
@@ -56,30 +56,30 @@
             <!-- Bi-Directional link between s205 and s227 -->
             <!-- Link 1 -->
             <step name="Net-Trellis-P4-Links.Query-205-227-Link-1"
-                  exec="find-link.py ${OC1} link_205_227_1_ device:bmv2:s205 2 device:bmv2:s227 2"/>
+                  exec="find-link.py ${OC1} link_205_227_1_ device:s205 2 device:s227 2"/>
             <step name="Net-Trellis-P4-Links.Validate-205-227-Link-1-State" requires="^"
                   exec="test '${link_205_227_1_State}' == 'ACTIVE'"/>
             <step name="Net-Trellis-P4-Links.Validate-205-227-Link-1-Type" requires="^"
                   exec="test '${link_205_227_1_Type}' == 'DIRECT'"/>
             <step name="Net-Trellis-P4-Links.Validate-205-227-Link-1-Src-Device" requires="^"
-                  exec="test '${link_205_227_1_SrcDevice}' == 'device:bmv2:s205'"/>
+                  exec="test '${link_205_227_1_SrcDevice}' == 'device:s205'"/>
             <step name="Net-Trellis-P4-Links.Validate-205-227-Link-1-Dst-Device" requires="^"
-                  exec="test '${link_205_227_1_DstDevice}' == 'device:bmv2:s227'"/>
+                  exec="test '${link_205_227_1_DstDevice}' == 'device:s227'"/>
             <step name="Net-Trellis-P4-Links.Validate-205-227-Link-1-Src-Port" requires="^"
                   exec="test '${link_205_227_1_SrcPort}' == '2'"/>
             <step name="Net-Trellis-P4-Links.Validate-205-227-Link-1-Dst-Port" requires="^"
                   exec="test '${link_205_227_1_DstPort}' == '2'"/>
 
             <step name="Net-Trellis-P4-Links.Query-227-205-Link-2"
-                  exec="find-link.py ${OC1} link_227_205_2_ device:bmv2:s227 2 device:bmv2:s205 2"/>
+                  exec="find-link.py ${OC1} link_227_205_2_ device:s227 2 device:s205 2"/>
             <step name="Net-Trellis-P4-Links.Validate-227-205-Link-2-State" requires="^"
                   exec="test '${link_227_205_2_State}' == 'ACTIVE'"/>
             <step name="Net-Trellis-P4-Links.Validate-227-205-Link-2-Type" requires="^"
                   exec="test '${link_227_205_2_Type}' == 'DIRECT'"/>
             <step name="Net-Trellis-P4-Links.Validate-227-205-Link-2-Src-Device" requires="^"
-                  exec="test '${link_227_205_2_SrcDevice}' == 'device:bmv2:s227'"/>
+                  exec="test '${link_227_205_2_SrcDevice}' == 'device:s227'"/>
             <step name="Net-Trellis-P4-Links.Validate-227-205-Link-2-Dst-Device" requires="^"
-                  exec="test '${link_227_205_2_DstDevice}' == 'device:bmv2:s205'"/>
+                  exec="test '${link_227_205_2_DstDevice}' == 'device:s205'"/>
             <step name="Net-Trellis-P4-Links.Validate-227-205-Link-2-Src-Port" requires="^"
                   exec="test '${link_227_205_2_SrcPort}' == '2'"/>
             <step name="Net-Trellis-P4-Links.Validate-227-205-Link-2-Dst-Port" requires="^"
diff --git a/tools/test/scenarios/net-trellis-p4-smoke.xml b/tools/test/scenarios/net-trellis-p4-smoke.xml
index 0033aeb..cb80cd7 100644
--- a/tools/test/scenarios/net-trellis-p4-smoke.xml
+++ b/tools/test/scenarios/net-trellis-p4-smoke.xml
@@ -24,7 +24,7 @@
 
         <step name="Net-Trellis-P4-Smoke.Check-Summary"
               requires="Trellis-Net-P4-Setup.Wait-For-Mininet" delay="5"
-              exec="onos-check-summary ${OC1} [0-9]* 4 8 0 112"/>
+              exec="onos-check-summary ${OC1} [0-9]* 4 8 0 [0-9]*"/>
 
         <import file="${ONOS_SCENARIOS}/net-trellis-p4-devices.xml"/>
         <dependency name="Net-Trellis-P4-Devices"