Refactor channel and mastership handling in P4Runtime

This (big) change aims at solving the issue observed with mastership flapping
and device connection/disconnection with P4Runtime.

Channel handling is now based on the underlying gRPC channel state. Before,
channel events (open/close/error) were generated as a consequence of P4Runtime
StreamChannel events, making device availability dependent on mastership. Now
Stream Channel events only affect mastership (MASTER/STANDBY or NONE when the
SteamChannel RPC is not active).

Mastership handling has been refactored to generate P4Runtime election IDs that
are compatible with the mastership preference decided by the MastershipService.

GeneralDeviceProvider has been re-implemented to support in-order
device event processing and to reduce implementation complexity. Stats polling
has been moved to a separate component, and netcfg handling updated to only
depend on BasicDeviceConfig, augmented with a pipeconf field, and re-using the
managementAddress field to set the gRPC server endpoints (e.g.
grpc://myswitch.local:50051). Before it was depending on 3 different config
classes, making hard to detect changes.

Finally, this change affects some core interfaces:
- Adds a method to DeviceProvider and DeviceHandshaker to check for device
availability, making the meaning of availability device-specific. This is needed
in cases where the device manager needs to change the availability state of a
device (as in change #20842)
- Support device providers not capable of reconciling mastership role responses
with requests (like P4Runtime).
- Clarify the meaning of "connection" in the DeviceConnect behavior.
- Allows driver-based providers to check devices for reachability and
availability without probing the device via the network.

Change-Id: I7ff30d29f5d02ad938e3171536e54ae2916629a2
diff --git a/tools/dev/mininet/bmv2.py b/tools/dev/mininet/bmv2.py
index 56c7dcc..3dc42fd 100644
--- a/tools/dev/mininet/bmv2.py
+++ b/tools/dev/mininet/bmv2.py
@@ -35,6 +35,7 @@
 SWITCH_START_TIMEOUT = 5  # seconds
 BMV2_LOG_LINES = 5
 BMV2_DEFAULT_DEVICE_ID = 1
+DEFAULT_PIPECONF = "org.onosproject.pipelines.basic"
 
 # Stratum paths relative to stratum repo root
 STRATUM_BMV2 = 'stratum_bmv2'
@@ -119,9 +120,10 @@
 
     def __init__(self, name, json=None, debugger=False, loglevel="warn",
                  elogger=False, grpcport=None, cpuport=255, notifications=False,
-                 thriftport=None, netcfg=True, dryrun=False, pipeconf="",
-                 pktdump=False, valgrind=False, gnmi=False,
-                 portcfg=True, onosdevid=None, stratum=False, **kwargs):
+                 thriftport=None, netcfg=True, dryrun=False,
+                 pipeconf=DEFAULT_PIPECONF, pktdump=False, valgrind=False,
+                 gnmi=False, portcfg=True, onosdevid=None, stratum=False,
+                 **kwargs):
         Switch.__init__(self, name, **kwargs)
         self.grpcPort = grpcport
         self.thriftPort = thriftport
@@ -173,7 +175,10 @@
     def getDeviceConfig(self, srcIP):
 
         basicCfg = {
-            "driver": "bmv2"
+            "managementAddress": "grpc://%s:%d?device_id=%d" % (
+                srcIP, self.grpcPort, BMV2_DEFAULT_DEVICE_ID),
+            "driver": "bmv2",
+            "pipeconf": self.pipeconfId
         }
 
         if self.longitude and self.latitude:
@@ -181,30 +186,9 @@
             basicCfg["latitude"] = self.latitude
 
         cfgData = {
-            "generalprovider": {
-                "p4runtime": {
-                    "ip": srcIP,
-                    "port": self.grpcPort,
-                    "deviceId": BMV2_DEFAULT_DEVICE_ID,
-                    "deviceKeyId": "p4runtime:%s" % self.onosDeviceId
-                },
-                # "bmv2-thrift": {
-                #     "ip": srcIP,
-                #     "port": self.thriftPort
-                # }
-            },
-            "piPipeconf": {
-                "piPipeconfId": self.pipeconfId
-            },
             "basic": basicCfg
         }
 
-        if self.withGnmi:
-            cfgData["generalprovider"]["gnmi"] = {
-                "ip": srcIP,
-                "port": self.grpcPort
-            }
-
         if self.injectPorts:
             portData = {}
             portId = 1
@@ -327,7 +311,8 @@
             '-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
+            '-external_hercules_urls=0.0.0.0:%d' % self.grpcPort,
+            '-max_num_controllers_per_node=10'
         ]
         for port, intf in self.intfs.items():
             if not intf.IP():