Merge remote-tracking branch 'origin/master' into upan-connect18

Change-Id: I668a901765f084cf07096e2390c59c846bd555f7
diff --git a/drivers/gnmi/BUILD b/drivers/gnmi/BUILD
index 63b11c6..4a4c100 100644
--- a/drivers/gnmi/BUILD
+++ b/drivers/gnmi/BUILD
@@ -5,6 +5,7 @@
     "@io_grpc_grpc_java//stub",
     "//core/store/serializers:onos-core-serializers",
     "//protocols/gnmi/stub:onos-protocols-gnmi-stub",
+    "//protocols/gnmi/api:onos-protocols-gnmi-api",
     "//protocols/grpc/api:onos-protocols-grpc-api",
     "//protocols/grpc/proto:onos-protocols-grpc-proto",
 ]
diff --git a/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/AbstractGnmiHandlerBehaviour.java b/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/AbstractGnmiHandlerBehaviour.java
new file mode 100644
index 0000000..e363f4a
--- /dev/null
+++ b/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/AbstractGnmiHandlerBehaviour.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2018-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.drivers.gnmi;
+
+import org.onosproject.gnmi.api.GnmiClient;
+import org.onosproject.gnmi.api.GnmiClientKey;
+import org.onosproject.gnmi.api.GnmiController;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Abstract implementation of a behaviour handler for a gNMI device.
+ */
+public class AbstractGnmiHandlerBehaviour extends AbstractHandlerBehaviour {
+
+    public static final String GNMI_SERVER_ADDR_KEY = "gnmi_ip";
+    public static final String GNMI_SERVER_PORT_KEY = "gnmi_port";
+    private static final String GNMI_SERVICE_NAME = "gnmi";
+
+    protected final Logger log = LoggerFactory.getLogger(getClass());
+    protected DeviceId deviceId;
+    protected DeviceService deviceService;
+    protected Device device;
+    protected GnmiController controller;
+    protected GnmiClient client;
+
+    protected boolean setupBehaviour() {
+        // FIXME: Should create GnmiHandshaker which initialize the client
+        // instead of create client here.
+        deviceId = handler().data().deviceId();
+
+        controller = handler().get(GnmiController.class);
+        client = controller.getClient(deviceId);
+
+        if (client == null) {
+            client = createClient();
+        }
+
+        if (client == null) {
+            log.warn("Can not create client for {} (see log above)", deviceId);
+            return false;
+        }
+
+        return true;
+    }
+
+    protected GnmiClient createClient() {
+        deviceId = handler().data().deviceId();
+        controller = handler().get(GnmiController.class);
+
+        final String serverAddr = this.data().value(GNMI_SERVER_ADDR_KEY);
+        final String serverPortString = this.data().value(GNMI_SERVER_PORT_KEY);
+
+        if (serverAddr == null || serverPortString == null) {
+            log.warn("Unable to create client for {}, missing driver data key (required is {}, {}, and {})",
+                    deviceId, GNMI_SERVER_ADDR_KEY, GNMI_SERVER_PORT_KEY);
+            return null;
+        }
+
+        final int serverPort;
+        try {
+            serverPort = Integer.parseUnsignedInt(serverPortString);
+        } catch (NumberFormatException e) {
+            log.error("{} is not a valid gNMI port number", serverPortString);
+            return null;
+        }
+        GnmiClientKey clientKey =
+                new GnmiClientKey(GNMI_SERVICE_NAME, deviceId, serverAddr, serverPort);
+        if (!controller.createClient(clientKey)) {
+            log.warn("Unable to create client for {}, aborting operation", deviceId);
+            return null;
+        }
+        return controller.getClient(deviceId);
+    }
+}
diff --git a/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/GnmiDeviceDescriptionDiscovery.java b/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/GnmiDeviceDescriptionDiscovery.java
deleted file mode 100644
index 934d76d..0000000
--- a/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/GnmiDeviceDescriptionDiscovery.java
+++ /dev/null
@@ -1,310 +0,0 @@
-/*
- * Copyright 2017-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.drivers.gnmi;
-
-import com.google.common.collect.ImmutableList;
-import gnmi.gNMIGrpc;
-import io.grpc.ManagedChannel;
-import io.grpc.ManagedChannelBuilder;
-import io.grpc.Status;
-import io.grpc.StatusRuntimeException;
-import io.grpc.netty.NettyChannelBuilder;
-import io.grpc.stub.StreamObserver;
-import org.onosproject.grpc.api.GrpcChannelController;
-import org.onosproject.grpc.api.GrpcChannelId;
-import org.onosproject.net.DefaultAnnotations;
-import org.onosproject.net.DeviceId;
-import org.onosproject.net.Port;
-import org.onosproject.net.PortNumber;
-import org.onosproject.net.device.DefaultPortDescription;
-import org.onosproject.net.device.DeviceDescription;
-import org.onosproject.net.device.DeviceDescriptionDiscovery;
-import org.onosproject.net.device.PortDescription;
-import org.onosproject.net.driver.AbstractHandlerBehaviour;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.IOException;
-import java.nio.charset.Charset;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-
-import static gnmi.Gnmi.Path;
-import static gnmi.Gnmi.PathElem;
-import static gnmi.Gnmi.SubscribeRequest;
-import static gnmi.Gnmi.SubscribeResponse;
-import static gnmi.Gnmi.Subscription;
-import static gnmi.Gnmi.SubscriptionList;
-import static gnmi.Gnmi.Update;
-
-/**
- * Class that discovers the device description and ports of a device that
- * supports the gNMI protocol and Openconfig models.
- */
-public class GnmiDeviceDescriptionDiscovery
-        extends AbstractHandlerBehaviour
-        implements DeviceDescriptionDiscovery {
-
-    private static final int REQUEST_TIMEOUT_SECONDS = 5;
-
-    private static final Logger log = LoggerFactory
-            .getLogger(GnmiDeviceDescriptionDiscovery.class);
-
-    private static final String GNMI_SERVER_ADDR_KEY = "gnmi_ip";
-    private static final String GNMI_SERVER_PORT_KEY = "gnmi_port";
-
-    @Override
-    public DeviceDescription discoverDeviceDetails() {
-        return null;
-    }
-
-    @Override
-    public List<PortDescription> discoverPortDetails() {
-        log.info("Discovering port details on device {}", handler().data().deviceId());
-
-        String serverAddr = this.data().value(GNMI_SERVER_ADDR_KEY);
-        String serverPortString = this.data().value(GNMI_SERVER_PORT_KEY);
-
-        if (serverAddr == null || serverPortString == null ||
-                serverAddr.isEmpty() || serverPortString.isEmpty()) {
-            log.warn("gNMI server information not provided, can't discover ports");
-            return ImmutableList.of();
-        }
-
-        // Get the channel
-        ManagedChannel channel = getChannel(serverAddr, serverPortString);
-
-        if (channel == null) {
-            return ImmutableList.of();
-        }
-
-        // Build the subscribe request
-        SubscribeRequest request = subscribeRequest();
-
-        // New stub
-        gNMIGrpc.gNMIStub gnmiStub = gNMIGrpc.newStub(channel);
-
-        final CompletableFuture<List<PortDescription>>
-                reply = new CompletableFuture<>();
-
-        // Subscribe to the replies
-        StreamObserver<SubscribeRequest> subscribeRequest = gnmiStub
-                .subscribe(new SubscribeResponseObserver(reply));
-        log.debug("Interfaces request {}", request);
-
-        List<PortDescription> ports;
-        try {
-            // Issue the request
-            subscribeRequest.onNext(request);
-            ports = reply.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS);
-        } catch (InterruptedException | ExecutionException | TimeoutException
-                | StatusRuntimeException e) {
-            log.warn("Unable to discover ports from {}: {}",
-                    data().deviceId(), e.getMessage());
-            log.debug("{}", e);
-            return ImmutableList.of();
-        } finally {
-            subscribeRequest.onCompleted();
-        }
-
-        return ports;
-    }
-
-    /**
-     * Obtains the ManagedChannel to be used for the communication.
-     *
-     * @return the managed channel
-     */
-    private ManagedChannel getChannel(String serverAddr, String serverPortString) {
-
-        DeviceId deviceId = handler().data().deviceId();
-
-        GrpcChannelController controller = handler().get(GrpcChannelController.class);
-        ManagedChannel channel = null;
-
-        //FIXME can be optimized
-        //getting a channel if exists.
-        ManagedChannel managedChannel = controller
-                .getChannels(handler().data().deviceId()).stream().filter(c -> {
-                    String[] authority = c.authority().split(":");
-                    String host = authority[0];
-                    String port = authority[1];
-                    return host.equals(serverAddr) && port.equals(serverPortString);
-                }).findAny().orElse(null);
-
-        if (managedChannel != null) {
-            log.debug("Reusing Channel");
-            channel = managedChannel;
-        } else {
-            log.debug("Creating Channel");
-            GrpcChannelId newChannelId = GrpcChannelId.of(deviceId, "gnmi");
-
-            ManagedChannelBuilder channelBuilder = NettyChannelBuilder
-                    .forAddress(serverAddr, Integer.valueOf(serverPortString))
-                    .usePlaintext(true);
-
-            try {
-                channel = controller.connectChannel(newChannelId, channelBuilder);
-            } catch (IOException e) {
-                log.warn("Unable to connect to gRPC server of {}: {}",
-                        deviceId, e.getMessage());
-            }
-        }
-        return channel;
-    }
-
-    /**
-     * Creates the subscribe request for the interfaces.
-     *
-     * @return subscribe request
-     */
-    private SubscribeRequest subscribeRequest() {
-        Path path = Path.newBuilder()
-                .addElem(PathElem.newBuilder().setName("interfaces").build())
-                .addElem(PathElem.newBuilder().setName("interface").build())
-                .addElem(PathElem.newBuilder().setName("...").build())
-                .build();
-        Subscription subscription = Subscription.newBuilder().setPath(path).build();
-        SubscriptionList list = SubscriptionList.newBuilder().setMode(SubscriptionList.Mode.ONCE)
-                .addSubscription(subscription).build();
-        return SubscribeRequest.newBuilder().setSubscribe(list).build();
-    }
-
-    /**
-     * Handles messages received from the device on the stream channel.
-     */
-    private final class SubscribeResponseObserver
-            implements StreamObserver<SubscribeResponse> {
-
-        private final CompletableFuture<List<PortDescription>> reply;
-
-        private SubscribeResponseObserver(CompletableFuture<List<PortDescription>> reply) {
-            this.reply = reply;
-        }
-
-        @Override
-        public void onNext(SubscribeResponse message) {
-            Map<String, DefaultPortDescription.Builder> ports = new HashMap<>();
-            Map<String, DefaultAnnotations.Builder> portsAnnotations = new HashMap<>();
-            log.debug("Response {} ", message.getUpdate().toString());
-            message.getUpdate().getUpdateList().forEach(update -> {
-                parseUpdate(ports, portsAnnotations, update);
-            });
-
-            List<PortDescription> portDescriptionList = new ArrayList<>();
-            ports.forEach((k, v) -> {
-//                v.portSpeed(1000L);
-                v.type(Port.Type.COPPER);
-                v.annotations(portsAnnotations.get(k).set("name", k).build());
-                portDescriptionList.add(v.build());
-            });
-
-            reply.complete(portDescriptionList);
-        }
-
-
-        @Override
-        public void onError(Throwable throwable) {
-            log.warn("Error on stream channel for {}: {}",
-                    data().deviceId(), Status.fromThrowable(throwable));
-            log.debug("{}", throwable);
-        }
-
-        @Override
-        public void onCompleted() {
-            log.debug("SubscribeResponseObserver completed");
-        }
-    }
-
-    /**
-     * Parses the update received from the device.
-     *
-     * @param ports            the ports description to build
-     * @param portsAnnotations the ports annotations list to populate
-     * @param update           the update received
-     */
-    private void parseUpdate(Map<String, DefaultPortDescription.Builder> ports,
-                             Map<String, DefaultAnnotations.Builder> portsAnnotations,
-                             Update update) {
-
-        //FIXME crude parsing, can be done via object (de)serialization
-        if (update.getPath().getElemList().size() > 3) {
-            String name = update.getPath().getElem(3).getName();
-            String portId = update.getPath().getElem(1).getKeyMap().get("name");
-            if (!ports.containsKey(portId)) {
-                int number = Character.getNumericValue(portId.charAt(portId.length() - 1));
-                PortNumber portNumber = PortNumber.portNumber(number, portId);
-                ports.put(portId, DefaultPortDescription.builder()
-                        .withPortNumber(portNumber));
-            }
-            if (name.equals("enabled")) {
-                DefaultPortDescription.Builder builder = ports.get(portId);
-                builder = builder.isEnabled(update.getVal().getBoolVal());
-                ports.put(portId, builder);
-            } else if (name.equals("state")) {
-                String speedName = update.getPath().getElem(4).getName();
-                if (speedName.equals("negotiated-port-speed")) {
-                    DefaultPortDescription.Builder builder = ports.get(portId);
-                    long speed = parsePortSpeed(update.getVal().getStringVal());
-                    builder = builder.portSpeed(speed);
-                    ports.put(portId, builder);
-                }
-            } else if (!name.equals("ifindex")) {
-                if (!portsAnnotations.containsKey(portId)) {
-                    portsAnnotations.put(portId, DefaultAnnotations.builder()
-                            .set(name, update.getVal().toByteString()
-                                    .toString(Charset.defaultCharset()).trim()));
-                } else {
-                    DefaultAnnotations.Builder builder = portsAnnotations.get(portId);
-                    builder = builder.set(name, update.getVal().toByteString().
-                            toString(Charset.defaultCharset()).trim());
-                    portsAnnotations.put(portId, builder);
-                }
-            }
-        }
-    }
-
-    private long parsePortSpeed(String speed) {
-        log.debug("Speed from config {}", speed);
-        switch (speed) {
-            case "SPEED_10MB":
-                return 10;
-            case "SPEED_100MB":
-                return 100;
-            case "SPEED_1GB":
-                return 1000;
-            case "SPEED_10GB":
-                return 10000;
-            case "SPEED_25GB":
-                return 25000;
-            case "SPEED_40GB":
-                return 40000;
-            case "SPEED_50GB":
-                return 50000;
-            case "SPEED_100GB":
-                return 100000;
-            default:
-                return 1000;
-        }
-    }
-}
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
new file mode 100644
index 0000000..f9d2236
--- /dev/null
+++ b/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/OpenConfigGnmiDeviceDescriptionDiscovery.java
@@ -0,0 +1,207 @@
+/*
+ * Copyright 2017-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.drivers.gnmi;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import gnmi.Gnmi;
+import gnmi.Gnmi.GetRequest;
+import gnmi.Gnmi.GetResponse;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.device.DefaultPortDescription;
+import org.onosproject.net.device.DeviceDescription;
+import org.onosproject.net.device.DeviceDescriptionDiscovery;
+import org.onosproject.net.device.PortDescription;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.charset.Charset;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import static gnmi.Gnmi.Path;
+import static gnmi.Gnmi.PathElem;
+import static gnmi.Gnmi.Update;
+
+/**
+ * Class that discovers the device description and ports of a device that
+ * supports the gNMI protocol and Openconfig models.
+ */
+public class OpenConfigGnmiDeviceDescriptionDiscovery
+        extends AbstractGnmiHandlerBehaviour
+        implements DeviceDescriptionDiscovery {
+
+    private static final int REQUEST_TIMEOUT_SECONDS = 5;
+
+    private static final Logger log = LoggerFactory
+            .getLogger(OpenConfigGnmiDeviceDescriptionDiscovery.class);
+
+    @Override
+    public DeviceDescription discoverDeviceDetails() {
+        return null;
+    }
+
+    @Override
+    public List<PortDescription> discoverPortDetails() {
+        if (!setupBehaviour()) {
+            return Collections.emptyList();
+        }
+        log.debug("Discovering port details on device {}", handler().data().deviceId());
+
+        GetResponse response;
+        try {
+            response = client.get(buildPortStateRequest())
+                    .get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS);
+        } catch (InterruptedException | ExecutionException | TimeoutException e) {
+            log.warn("Unable to discover ports from {}: {}", deviceId, e.getMessage());
+            log.debug("{}", e);
+            return Collections.emptyList();
+        }
+
+        Map<String, DefaultPortDescription.Builder> ports = Maps.newHashMap();
+        Map<String, DefaultAnnotations.Builder> annotations = Maps.newHashMap();
+
+        // Creates port descriptions with port name and port number
+        response.getNotificationList()
+                .stream()
+                .flatMap(notification -> notification.getUpdateList().stream())
+                .forEach(update -> {
+                    // /interfaces/interface[name=ifName]/state/...
+                    String ifName = update.getPath().getElem(1).getKeyMap().get("name");
+                    if (!ports.containsKey(ifName)) {
+                        ports.put(ifName, DefaultPortDescription.builder());
+                        annotations.put(ifName, DefaultAnnotations.builder());
+                    }
+
+
+                    DefaultPortDescription.Builder builder = ports.get(ifName);
+                    DefaultAnnotations.Builder annotationsBuilder = annotations.get(ifName);
+                    parseInterfaceInfo(update, ifName, builder, annotationsBuilder);
+                });
+
+        List<PortDescription> portDescriptionList = Lists.newArrayList();
+        ports.forEach((key, value) -> {
+            DefaultAnnotations annotation = annotations.get(key).build();
+            portDescriptionList.add(value.annotations(annotation).build());
+        });
+        return portDescriptionList;
+    }
+
+    private GetRequest buildPortStateRequest() {
+        Path path = Path.newBuilder()
+                .addElem(PathElem.newBuilder().setName("interfaces").build())
+                .addElem(PathElem.newBuilder().setName("interface").putKey("name", "...").build())
+                .addElem(PathElem.newBuilder().setName("state").build())
+                .build();
+        return GetRequest.newBuilder()
+                .addPath(path)
+                .setType(GetRequest.DataType.ALL)
+                .setEncoding(Gnmi.Encoding.PROTO)
+                .build();
+    }
+
+    /**
+     * Parses the interface information.
+     *
+     * @param update           the update received
+     */
+    private void parseInterfaceInfo(Update update,
+                                    String ifName,
+                                    DefaultPortDescription.Builder builder,
+                                    DefaultAnnotations.Builder annotationsBuilder) {
+
+
+        Path path = update.getPath();
+        List<PathElem> elems = path.getElemList();
+        Gnmi.TypedValue val = update.getVal();
+        if (elems.size() == 4) {
+            // /interfaces/interface/state/ifindex
+            // /interfaces/interface/state/oper-status
+            String pathElemName = elems.get(3).getName();
+            switch (pathElemName) {
+                case "ifindex": // port number
+                    builder.withPortNumber(PortNumber.portNumber(val.getUintVal(), ifName));
+                    break;
+                case "oper-status":
+                    builder.isEnabled(parseOperStatus(val.getStringVal()));
+                    break;
+                default:
+                    String valueString = val.toByteString().toString(Charset.defaultCharset()).trim();
+                    if (!valueString.isEmpty()) {
+                        annotationsBuilder.set(pathElemName, valueString);
+                    }
+                    log.debug("Unknown path: {}", path);
+                    break;
+            }
+        }
+        if (elems.size() == 5) {
+            // /interfaces/interface/ethernet/config/port-speed
+            String pathElemName = elems.get(4).getName();
+            switch (pathElemName) {
+                case "port-speed":
+                    builder.portSpeed(parsePortSpeed(val.getStringVal()));
+                    break;
+                default:
+                    String valueString = val.toByteString().toString(Charset.defaultCharset()).trim();
+                    if (!valueString.isEmpty()) {
+                        annotationsBuilder.set(pathElemName, valueString);
+                    }
+                    log.debug("Unknown path: {}", path);
+                    break;
+            }
+        }
+    }
+
+    private boolean parseOperStatus(String operStatus) {
+        switch (operStatus) {
+            case "UP":
+                return true;
+            case "DOWN":
+            default:
+                return false;
+        }
+    }
+
+    private long parsePortSpeed(String speed) {
+        log.debug("Speed from config {}", speed);
+        switch (speed) {
+            case "SPEED_10MB":
+                return 10;
+            case "SPEED_100MB":
+                return 100;
+            case "SPEED_1GB":
+                return 1000;
+            case "SPEED_10GB":
+                return 10000;
+            case "SPEED_25GB":
+                return 25000;
+            case "SPEED_40GB":
+                return 40000;
+            case "SPEED_50GB":
+                return 50000;
+            case "SPEED_100GB":
+                return 100000;
+            default:
+                return 1000;
+        }
+    }
+}
diff --git a/drivers/gnmi/src/main/resources/gnmi-drivers.xml b/drivers/gnmi/src/main/resources/gnmi-drivers.xml
index 3744781..4ca539e 100644
--- a/drivers/gnmi/src/main/resources/gnmi-drivers.xml
+++ b/drivers/gnmi/src/main/resources/gnmi-drivers.xml
@@ -17,7 +17,7 @@
 <drivers>
     <driver name="gnmi" manufacturer="gnmi" hwVersion="master" swVersion="master">
         <behaviour api="org.onosproject.net.device.DeviceDescriptionDiscovery"
-                   impl="org.onosproject.drivers.gnmi.GnmiDeviceDescriptionDiscovery"/>
+                   impl="org.onosproject.drivers.gnmi.OpenConfigGnmiDeviceDescriptionDiscovery"/>
     </driver>
 </drivers>
 
diff --git a/protocols/gnmi/BUILD b/protocols/gnmi/BUILD
index 8a3510d..0b6b6e6 100644
--- a/protocols/gnmi/BUILD
+++ b/protocols/gnmi/BUILD
@@ -1,11 +1,13 @@
 BUNDLES = [
     "//protocols/gnmi/stub:onos-protocols-gnmi-stub",
+    "//protocols/gnmi/ctl:onos-protocols-gnmi-ctl",
+    "//protocols/gnmi/api:onos-protocols-gnmi-api",
 ]
 
 onos_app(
     app_name = "org.onosproject.protocols.gnmi",
     category = "Protocol",
-    description = "ONOS gNMI protocol subsystem",
+    description = "gNMI protocol subsystem",
     included_bundles = BUNDLES,
     required_apps = [
         "org.onosproject.protocols.grpc",
diff --git a/protocols/gnmi/api/BUILD b/protocols/gnmi/api/BUILD
new file mode 100644
index 0000000..1cf1a3d
--- /dev/null
+++ b/protocols/gnmi/api/BUILD
@@ -0,0 +1,8 @@
+COMPILE_DEPS = CORE_DEPS + [
+    "//protocols/grpc/api:onos-protocols-grpc-api",
+    "//protocols/gnmi/stub:onos-protocols-gnmi-stub",
+]
+
+osgi_jar(
+    deps = COMPILE_DEPS,
+)
diff --git a/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiClient.java b/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiClient.java
new file mode 100644
index 0000000..7f43d1f
--- /dev/null
+++ b/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiClient.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2018-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.api;
+
+import com.google.common.annotations.Beta;
+
+import gnmi.Gnmi.CapabilityResponse;
+import gnmi.Gnmi.GetResponse;
+import gnmi.Gnmi.GetRequest;
+import gnmi.Gnmi.SetRequest;
+import gnmi.Gnmi.SetResponse;
+import org.onosproject.grpc.api.GrpcClient;
+
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Client to control a gNMI server.
+ */
+@Beta
+public interface GnmiClient extends GrpcClient {
+
+    /**
+     * Gets capability from a target. This allows device driver behavior
+     * to validate the service version and models which gNMI device supported.
+     *
+     * @return the capability response
+     */
+    CompletableFuture<CapabilityResponse> capability();
+
+    /**
+     * Retrieve a snapshot of data from the device.
+     *
+     * @param request the get request
+     * @return the snapshot of data from the device
+     */
+    CompletableFuture<GetResponse> get(GetRequest request);
+
+    /**
+     * Modifies the state of data on the device.
+     *
+     * @param request the set request
+     * @return the set result
+     */
+    CompletableFuture<SetResponse> set(SetRequest request);
+
+    // TODO: Support gNMI subscription
+}
diff --git a/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiClientKey.java b/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiClientKey.java
new file mode 100644
index 0000000..26ce113
--- /dev/null
+++ b/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiClientKey.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2018-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.api;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.grpc.api.GrpcClientKey;
+import org.onosproject.net.DeviceId;
+
+/**
+ * Key that uniquely identifies a gNMI client.
+ */
+@Beta
+public class GnmiClientKey extends GrpcClientKey {
+
+    /**
+     * Creates a new gNMI client key.
+     *
+     * @param serviceName gNMI service name of the client
+     * @param deviceId    ONOS device ID
+     * @param serverAddr  gNMI server address
+     * @param serverPort  gNMI server port
+     */
+    public GnmiClientKey(String serviceName, DeviceId deviceId, String serverAddr, int serverPort) {
+        super(serviceName, deviceId, serverAddr, serverPort);
+    }
+}
diff --git a/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiController.java b/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiController.java
new file mode 100644
index 0000000..f4964ed
--- /dev/null
+++ b/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiController.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2018-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.api;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.grpc.api.GrpcClientController;
+
+/**
+ * Controller of gNMI devices.
+ */
+@Beta
+public interface GnmiController
+        extends GrpcClientController<GnmiClientKey, GnmiClient> {
+}
diff --git a/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiEvent.java b/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiEvent.java
new file mode 100644
index 0000000..5129926
--- /dev/null
+++ b/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiEvent.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2018-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.api;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.event.AbstractEvent;
+
+/**
+ * Representation of an event received from a gNMI device.
+ */
+@Beta
+public final class GnmiEvent extends AbstractEvent<GnmiEvent.Type, GnmiEventSubject> {
+
+    /**
+     * Type of gNMI event.
+     */
+    public enum Type {
+        /**
+         * Update.
+         */
+        UPDATE,
+
+        /**
+         * Sync response.
+         */
+        SYNC_RESPONSE
+    }
+
+    protected GnmiEvent(Type type, GnmiEventSubject subject) {
+        super(type, subject);
+    }
+}
diff --git a/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiEventListener.java b/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiEventListener.java
new file mode 100644
index 0000000..0fe2fd3
--- /dev/null
+++ b/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiEventListener.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2018-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.api;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.event.EventListener;
+
+/**
+ * A listener of events received from gNMI devices.
+ */
+@Beta
+public interface GnmiEventListener extends EventListener<GnmiEvent> {
+}
diff --git a/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiEventSubject.java b/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiEventSubject.java
new file mode 100644
index 0000000..d1563ed
--- /dev/null
+++ b/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiEventSubject.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2018-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.api;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.net.DeviceId;
+
+/**
+ * Information about an event generated by a gNMI device .
+ */
+@Beta
+public interface GnmiEventSubject {
+
+    /**
+     * Returns the deviceId associated to this subject.
+     *
+     * @return the device ID
+     */
+    DeviceId deviceId();
+}
diff --git a/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/package-info.java b/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/package-info.java
new file mode 100644
index 0000000..5f07b46
--- /dev/null
+++ b/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-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.
+ */
+
+/**
+ * gNMI protocol API.
+ */
+package org.onosproject.gnmi.api;
\ No newline at end of file
diff --git a/protocols/gnmi/ctl/BUILD b/protocols/gnmi/ctl/BUILD
new file mode 100644
index 0000000..db4cd44
--- /dev/null
+++ b/protocols/gnmi/ctl/BUILD
@@ -0,0 +1,23 @@
+COMPILE_DEPS = CORE_DEPS + KRYO + [
+    "//protocols/gnmi/api:onos-protocols-gnmi-api",
+    "//protocols/gnmi/stub:onos-protocols-gnmi-stub",
+    "//protocols/grpc/api:onos-protocols-grpc-api",
+    "//protocols/grpc/ctl:onos-protocols-grpc-ctl",
+    "//protocols/grpc:grpc-core-repkg",
+    "@com_google_protobuf//:protobuf_java",
+    "@io_grpc_grpc_java//netty",
+    "@io_grpc_grpc_java//protobuf-lite",
+    "@io_grpc_grpc_java//stub",
+    "@com_google_api_grpc_proto_google_common_protos//jar",
+]
+
+TEST_DEPS = TEST + [
+    "@minimal_json//jar",
+    "@io_grpc_grpc_java//core:inprocess",
+    "@io_grpc_grpc_java//protobuf-lite",
+]
+
+osgi_jar_with_tests(
+    test_deps = TEST_DEPS,
+    deps = COMPILE_DEPS,
+)
diff --git a/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/GnmiClientImpl.java b/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/GnmiClientImpl.java
new file mode 100644
index 0000000..74a9ec0
--- /dev/null
+++ b/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/GnmiClientImpl.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2018-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 protocols.gnmi.ctl.java.org.onosproject.gnmi.ctl;
+
+import gnmi.Gnmi.CapabilityRequest;
+import gnmi.Gnmi.CapabilityResponse;
+import gnmi.Gnmi.GetRequest;
+import gnmi.Gnmi.GetResponse;
+import gnmi.Gnmi.SetRequest;
+import gnmi.Gnmi.SetResponse;
+import gnmi.gNMIGrpc;
+import io.grpc.ManagedChannel;
+import io.grpc.StatusRuntimeException;
+import org.onosproject.gnmi.api.GnmiClientKey;
+import org.onosproject.grpc.ctl.AbstractGrpcClient;
+import org.slf4j.Logger;
+import org.onosproject.gnmi.api.GnmiClient;
+
+import java.util.concurrent.CompletableFuture;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of gNMI client.
+ */
+public class GnmiClientImpl extends AbstractGrpcClient implements GnmiClient {
+    private final Logger log = getLogger(getClass());
+    private final gNMIGrpc.gNMIBlockingStub blockingStub;
+
+    public GnmiClientImpl(GnmiClientKey clientKey, ManagedChannel managedChannel) {
+        super(clientKey, managedChannel);
+        this.blockingStub = gNMIGrpc.newBlockingStub(managedChannel);
+    }
+
+    @Override
+    public CompletableFuture<CapabilityResponse> capability() {
+        return supplyInContext(this::doCapability, "capability");
+    }
+
+    @Override
+    public CompletableFuture<GetResponse> get(GetRequest request) {
+        return supplyInContext(() -> doGet(request), "get");
+    }
+
+    @Override
+    public CompletableFuture<SetResponse> set(SetRequest request) {
+        return supplyInContext(() -> doSet(request), "set");
+    }
+
+    private CapabilityResponse doCapability() {
+        CapabilityRequest request = CapabilityRequest.newBuilder().build();
+        try {
+            return blockingStub.capabilities(request);
+        } catch (StatusRuntimeException e) {
+            log.warn("Unable to get capability from {}: {}", deviceId, e.getMessage());
+            return null;
+        }
+    }
+
+    private GetResponse doGet(GetRequest request) {
+        try {
+            return blockingStub.get(request);
+        } catch (StatusRuntimeException e) {
+            log.warn("Unable to get data from {}: {}", deviceId, e.getMessage());
+            return null;
+        }
+    }
+
+    private SetResponse doSet(SetRequest request) {
+        try {
+            return blockingStub.set(request);
+        } catch (StatusRuntimeException e) {
+            log.warn("Unable to set data to {}: {}", deviceId, e.getMessage());
+            return null;
+        }
+    }
+}
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
new file mode 100644
index 0000000..7ddc3f0
--- /dev/null
+++ b/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/GnmiControllerImpl.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2018-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 protocols.gnmi.ctl.java.org.onosproject.gnmi.ctl;
+
+import io.grpc.ManagedChannel;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Service;
+import org.onosproject.gnmi.api.GnmiEvent;
+import org.onosproject.gnmi.api.GnmiEventListener;
+import org.onosproject.grpc.ctl.AbstractGrpcClientController;
+import org.onosproject.gnmi.api.GnmiClient;
+import org.onosproject.gnmi.api.GnmiClientKey;
+import org.onosproject.gnmi.api.GnmiController;
+import org.slf4j.Logger;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of gNMI controller.
+ */
+@Component(immediate = true)
+@Service
+public class GnmiControllerImpl
+        extends AbstractGrpcClientController<GnmiClientKey, GnmiClient, GnmiEvent, GnmiEventListener>
+        implements GnmiController {
+    private final Logger log = getLogger(getClass());
+
+    @Activate
+    public void activate() {
+        super.activate();
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        super.deactivate();
+        log.info("Stopped");
+    }
+
+    @Override
+    protected GnmiClient createClientInstance(GnmiClientKey clientKey, ManagedChannel channel) {
+        return new GnmiClientImpl(clientKey, channel);
+    }
+}
diff --git a/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/package-info.java b/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/package-info.java
new file mode 100644
index 0000000..ae46aa4
--- /dev/null
+++ b/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-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.
+ */
+
+/**
+ * Implementation classes of the gNMI protocol subsystem.
+ */
+package protocols.gnmi.ctl.java.org.onosproject.gnmi.ctl;
\ No newline at end of file