[SDFAB-1048] Fix wiring issue caused by readPortId

Leverage the dep already in place between gnmi driver and gnmi protocol
and expose readPortId as cfg property of GnmiControllerImpl

Change-Id: I34defd8928589129dc84cae45033f7f7c3c673bb
diff --git a/drivers/gnmi/BUILD b/drivers/gnmi/BUILD
index 772ae88..e3535cc 100644
--- a/drivers/gnmi/BUILD
+++ b/drivers/gnmi/BUILD
@@ -6,6 +6,7 @@
     "//core/store/serializers:onos-core-serializers",
     "//protocols/gnmi/stub:onos-protocols-gnmi-stub",
     "//protocols/gnmi/api:onos-protocols-gnmi-api",
+    "//protocols/gnmi/ctl:onos-protocols-gnmi-ctl",
     "//protocols/grpc/api:onos-protocols-grpc-api",
     "//protocols/grpc/utils:onos-protocols-grpc-utils",
 ]
diff --git a/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/OpenConfigGnmiDeviceDescriptionDiscovery.java b/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/OpenConfigGnmiDeviceDescriptionDiscovery.java
index 6ae4c33..81c59b4 100644
--- a/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/OpenConfigGnmiDeviceDescriptionDiscovery.java
+++ b/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/OpenConfigGnmiDeviceDescriptionDiscovery.java
@@ -25,6 +25,7 @@
 import org.onlab.packet.ChassisId;
 import org.onosproject.gnmi.api.GnmiClient;
 import org.onosproject.gnmi.api.GnmiController;
+import org.onosproject.gnmi.ctl.GnmiControllerImpl;
 import org.onosproject.grpc.utils.AbstractGrpcHandlerBehaviour;
 import org.onosproject.net.AnnotationKeys;
 import org.onosproject.net.DefaultAnnotations;
@@ -62,15 +63,23 @@
 
     private static final String UNKNOWN = "unknown";
 
-    // FIXME temporary solution will be removed when the
-    //  transition to p4rt translation is completed
-    public static boolean readPortId = false;
+    private GnmiController gnmiController;
 
     public OpenConfigGnmiDeviceDescriptionDiscovery() {
         super(GnmiController.class);
     }
 
     @Override
+    protected boolean setupBehaviour(String opName) {
+        if (!super.setupBehaviour(opName)) {
+            return false;
+        }
+
+        gnmiController = handler().get(GnmiController.class);
+        return true;
+    }
+
+    @Override
     public DeviceDescription discoverDeviceDetails() {
         return new DefaultDeviceDescription(
                 data().deviceId().uri(),
@@ -125,7 +134,7 @@
             }
             /* Override port number if read port-id is enabled
                and /interfaces/interface/state/id is available */
-            if (readPortId && portIds.containsKey(key)) {
+            if (readPortId() && portIds.containsKey(key)) {
                 value.withPortNumber(portIds.get(key));
             }
             DefaultAnnotations annotation = annotations.get(key).build();
@@ -135,6 +144,13 @@
         return portDescriptionList;
     }
 
+    private boolean readPortId() {
+        // FIXME temporary solution will be substituted by
+        //  an XML driver property when the transition to
+        //  p4rt translation is completed
+        return ((GnmiControllerImpl) gnmiController).readPortId();
+    }
+
     private GetRequest buildPortStateRequest() {
         Path path = Path.newBuilder()
                 .addElem(PathElem.newBuilder().setName("interfaces").build())
diff --git a/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/GnmiControllerImpl.java b/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/GnmiControllerImpl.java
index 42d31db..448ea81 100644
--- a/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/GnmiControllerImpl.java
+++ b/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/GnmiControllerImpl.java
@@ -17,30 +17,90 @@
 package org.onosproject.gnmi.ctl;
 
 import io.grpc.ManagedChannel;
+import org.onlab.util.Tools;
+import org.onosproject.cfg.ComponentConfigService;
 import org.onosproject.gnmi.api.GnmiClient;
 import org.onosproject.gnmi.api.GnmiController;
 import org.onosproject.gnmi.api.GnmiEvent;
 import org.onosproject.gnmi.api.GnmiEventListener;
 import org.onosproject.grpc.ctl.AbstractGrpcClientController;
 import org.onosproject.net.DeviceId;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Modified;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.slf4j.Logger;
+
+import java.util.Dictionary;
+
+import static org.onosproject.gnmi.ctl.OsgiPropertyConstants.READ_PORT_ID;
+import static org.onosproject.gnmi.ctl.OsgiPropertyConstants.READ_PORT_ID_DEFAULT;
+import static org.slf4j.LoggerFactory.getLogger;
 
 /**
  * Implementation of gNMI controller.
  */
-@Component(immediate = true, service = GnmiController.class)
+@Component(immediate = true,
+        service = GnmiController.class,
+        property = {
+                READ_PORT_ID + ":Boolean=" + READ_PORT_ID_DEFAULT,
+        })
 public class GnmiControllerImpl
         extends AbstractGrpcClientController
         <GnmiClient, GnmiEvent, GnmiEventListener>
         implements GnmiController {
 
+    private final Logger log = getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    private ComponentConfigService componentConfigService;
+
+    /**
+     * Configure read port-id for gnmi drivers; default is false.
+     */
+    private boolean readPortId = READ_PORT_ID_DEFAULT;
+
     public GnmiControllerImpl() {
         super(GnmiEvent.class, "gNMI");
     }
 
+    @Activate
+    public void activate(ComponentContext context) {
+        super.activate();
+        componentConfigService.registerProperties(getClass());
+        modified(context);
+    }
+
+    @Modified
+    public void modified(ComponentContext context) {
+        if (context == null) {
+            return;
+        }
+
+        Dictionary<?, ?> properties = context.getProperties();
+        String strReadPortId = Tools.get(properties, READ_PORT_ID);
+        // FIXME temporary solution will be substituted by
+        //  an XML driver property when the transition to
+        //  p4rt translation is completed
+        readPortId = Boolean.parseBoolean(strReadPortId);
+        log.info("Configured. {} is configured to {}",
+                READ_PORT_ID, readPortId);
+    }
+
     @Override
     protected GnmiClient createClientInstance(
             DeviceId deviceId, ManagedChannel channel) {
         return new GnmiClientImpl(deviceId, channel, this);
     }
+
+    /**
+     * Returns whether or not readPortId is enabled.
+     *
+     * @return true if readPortId is enabled, false otherwise.
+     */
+    public boolean readPortId() {
+        return readPortId;
+    }
 }
diff --git a/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/OsgiPropertyConstants.java b/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/OsgiPropertyConstants.java
new file mode 100644
index 0000000..8751ad5
--- /dev/null
+++ b/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/OsgiPropertyConstants.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2022-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.gnmi.ctl;
+
+/**
+ * Constants for default values of configurable properties.
+ */
+public final class OsgiPropertyConstants {
+
+    private OsgiPropertyConstants() {}
+
+    public static final String READ_PORT_ID = "readPortId";
+    public static final boolean READ_PORT_ID_DEFAULT = false;
+
+}
diff --git a/providers/general/device/BUILD b/providers/general/device/BUILD
index 6e1d53e..511201a 100644
--- a/providers/general/device/BUILD
+++ b/providers/general/device/BUILD
@@ -3,7 +3,6 @@
     "//protocols/gnmi/api:onos-protocols-gnmi-api",
     "//deps:com_google_protobuf_protobuf_java",
     "//protocols/grpc/api:onos-protocols-grpc-api",
-    "//drivers/gnmi:onos-drivers-gnmi",
 ]
 
 osgi_jar_with_tests(
diff --git a/providers/general/device/src/main/java/org/onosproject/provider/general/device/impl/GeneralDeviceProvider.java b/providers/general/device/src/main/java/org/onosproject/provider/general/device/impl/GeneralDeviceProvider.java
index 4c3018a..ac0fda7 100644
--- a/providers/general/device/src/main/java/org/onosproject/provider/general/device/impl/GeneralDeviceProvider.java
+++ b/providers/general/device/src/main/java/org/onosproject/provider/general/device/impl/GeneralDeviceProvider.java
@@ -27,7 +27,6 @@
 import org.onosproject.cluster.ClusterService;
 import org.onosproject.cluster.NodeId;
 import org.onosproject.core.CoreService;
-import org.onosproject.drivers.gnmi.OpenConfigGnmiDeviceDescriptionDiscovery;
 import org.onosproject.gnmi.api.GnmiController;
 import org.onosproject.mastership.MastershipInfo;
 import org.onosproject.mastership.MastershipService;
@@ -98,8 +97,6 @@
 import static org.onlab.util.Tools.groupedThreads;
 import static org.onosproject.provider.general.device.impl.OsgiPropertyConstants.CHECKUP_INTERVAL;
 import static org.onosproject.provider.general.device.impl.OsgiPropertyConstants.CHECKUP_INTERVAL_DEFAULT;
-import static org.onosproject.provider.general.device.impl.OsgiPropertyConstants.READ_PORT_ID;
-import static org.onosproject.provider.general.device.impl.OsgiPropertyConstants.READ_PORT_ID_DEFAULT;
 import static org.onosproject.provider.general.device.impl.OsgiPropertyConstants.STATS_POLL_INTERVAL;
 import static org.onosproject.provider.general.device.impl.OsgiPropertyConstants.STATS_POLL_INTERVAL_DEFAULT;
 import static org.slf4j.LoggerFactory.getLogger;
@@ -113,8 +110,7 @@
 @Component(immediate = true,
         property = {
                 CHECKUP_INTERVAL + ":Integer=" + CHECKUP_INTERVAL_DEFAULT,
-                STATS_POLL_INTERVAL + ":Integer=" + STATS_POLL_INTERVAL_DEFAULT,
-                READ_PORT_ID + ":Boolean=" + READ_PORT_ID_DEFAULT,
+                STATS_POLL_INTERVAL + ":Integer=" + STATS_POLL_INTERVAL_DEFAULT
         })
 public class GeneralDeviceProvider extends AbstractProvider
         implements DeviceProvider {
@@ -178,11 +174,6 @@
      */
     private int statsPollInterval = STATS_POLL_INTERVAL_DEFAULT;
 
-    /**
-     * Configure read port-id for gnmi drivers; default is false.
-     */
-    private boolean readPortId = READ_PORT_ID_DEFAULT;
-
     private final Map<DeviceId, DeviceHandshaker> handshakersWithListeners = Maps.newConcurrentMap();
     private final Map<DeviceId, Long> lastCheckups = Maps.newConcurrentMap();
     private final InternalPipeconfWatchdogListener pipeconfWatchdogListener = new InternalPipeconfWatchdogListener();
@@ -242,11 +233,6 @@
                 properties, STATS_POLL_INTERVAL, STATS_POLL_INTERVAL_DEFAULT);
         log.info("Configured. {} is configured to {} seconds",
                  STATS_POLL_INTERVAL, statsPollInterval);
-        final boolean oldReaPortId = readPortId;
-        String strReadPortId = Tools.get(properties, READ_PORT_ID);
-        readPortId = Boolean.parseBoolean(strReadPortId);
-        log.info("Configured. {} is configured to {}",
-                READ_PORT_ID, readPortId);
 
         if (oldCheckupInterval != checkupInterval) {
             startOrReschedulePeriodicCheckupTasks();
@@ -255,12 +241,6 @@
         if (oldStatsPollFrequency != statsPollInterval) {
             statsPoller.reschedule(statsPollInterval);
         }
-
-        if (oldReaPortId != readPortId) {
-            // FIXME temporary solution will be removed when the
-            //  transition to p4rt translation is completed
-            OpenConfigGnmiDeviceDescriptionDiscovery.readPortId = readPortId;
-        }
     }
 
     @Deactivate
diff --git a/providers/general/device/src/main/java/org/onosproject/provider/general/device/impl/OsgiPropertyConstants.java b/providers/general/device/src/main/java/org/onosproject/provider/general/device/impl/OsgiPropertyConstants.java
index ce18b0f..f2f2d08 100644
--- a/providers/general/device/src/main/java/org/onosproject/provider/general/device/impl/OsgiPropertyConstants.java
+++ b/providers/general/device/src/main/java/org/onosproject/provider/general/device/impl/OsgiPropertyConstants.java
@@ -29,7 +29,4 @@
     public static final String CHECKUP_INTERVAL = "checkupInterval";
     public static final int CHECKUP_INTERVAL_DEFAULT = 10;
 
-    public static final String READ_PORT_ID = "readPortId";
-    public static final boolean READ_PORT_ID_DEFAULT = false;
-
 }