Refactor gRPC northbound device service

Change-Id: I2f15074ab086f9a5e3294b49e09976f0dd77f51e
diff --git a/incubator/protobuf/services/nb/src/main/java/org/onosproject/grpc/nb/net/device/GrpcNbDeviceService.java b/incubator/protobuf/services/nb/src/main/java/org/onosproject/grpc/nb/net/device/GrpcNbDeviceService.java
new file mode 100644
index 0000000..1e1bdc9
--- /dev/null
+++ b/incubator/protobuf/services/nb/src/main/java/org/onosproject/grpc/nb/net/device/GrpcNbDeviceService.java
@@ -0,0 +1,356 @@
+/*
+ * 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.grpc.nb.net.device;
+
+
+import com.google.common.annotations.Beta;
+import io.grpc.stub.StreamObserver;
+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.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.grpc.net.device.models.PortEnumsProto;
+import org.onosproject.grpc.net.device.models.PortStatisticsProtoOuterClass.PortStatisticsProto;
+import org.onosproject.grpc.nb.net.device.DeviceServiceGrpc.DeviceServiceImplBase;
+import org.onosproject.grpc.net.models.PortProtoOuterClass.PortProto;
+import org.onosproject.grpc.net.device.models.DeviceEnumsProto;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.MastershipRole;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.grpc.nb.utils.GrpcNbDeviceServiceUtil;
+
+import static org.onosproject.grpc.nb.net.device.DeviceServiceNb.*;
+
+
+/**
+ * A server that provides access to the methods exposed by {@link DeviceService}.
+ * TODO this requires major refactoring, translation should be delegated to calls to
+ * TODO{@link GrpcNbDeviceServiceUtil}.
+ */
+@Beta
+@Component(immediate = true)
+public class GrpcNbDeviceService {
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DeviceService deviceService;
+
+    @Activate
+    public void activate() {
+        //TODO this should contact the registry service and register an instance
+        // of this service.
+    }
+
+    @Deactivate
+    public void deactivate() {
+    }
+
+    private class DeviceServiceNbServerInternal extends DeviceServiceImplBase {
+
+        public DeviceServiceNbServerInternal() {
+            super();
+        }
+
+
+        @Override
+        public void getDeviceCount(
+                getDeviceCountRequest request,
+                StreamObserver<DeviceServiceNb.getDeviceCountReply> responseObserver) {
+            responseObserver
+                    .onNext(getDeviceCountReply
+                            .newBuilder()
+                            .setDeviceCount(
+                                    deviceService.getDeviceCount())
+                            .build());
+            responseObserver.onCompleted();
+        }
+
+        //FIXME NOTE: this will be switched to a streaming version.
+        @Override
+        public void getDevices(getDevicesRequest request,
+                               StreamObserver<DeviceServiceNb.getDevicesReply> responseObserver) {
+            getDevicesReply.Builder replyBuilder = getDevicesReply.newBuilder();
+            deviceService.getDevices().forEach(d -> {
+                replyBuilder.addDevice(
+                        org.onosproject.grpc.net.models.DeviceProtoOuterClass.DeviceProto
+                                .newBuilder()
+                                .setDeviceId(d.id().toString())
+                                .setType(
+                                        DeviceEnumsProto.DeviceTypeProto
+                                                .valueOf(d.type().toString()))
+                                .setManufacturer(d.manufacturer())
+                                .setHwVersion(d.hwVersion())
+                                .setSwVersion(d.swVersion())
+                                .setSerialNumber(d.serialNumber())
+                                .setChassisId(d.chassisId().toString())
+                                .build());
+            });
+            responseObserver.onNext(replyBuilder.build());
+            responseObserver.onCompleted();
+        }
+
+        //FIXME NOTE: this will be switched to a streaming version.
+        @Override
+        public void getAvailableDevices(getAvailableDevicesRequest request,
+                                        StreamObserver
+                                                <DeviceServiceNb.getAvailableDevicesReply> responseObserver) {
+            getAvailableDevicesReply.Builder replyBuilder = getAvailableDevicesReply.newBuilder();
+            deviceService.getAvailableDevices().forEach(d -> {
+                replyBuilder.addDevice(
+                        org.onosproject.grpc.net.models.DeviceProtoOuterClass.DeviceProto
+                                .newBuilder()
+                                .setDeviceId(d.id().toString())
+                                .setType(DeviceEnumsProto.DeviceTypeProto.valueOf(
+                                        d.type().toString()))
+                                .setManufacturer(d.manufacturer())
+                                .setHwVersion(d.hwVersion())
+                                .setSwVersion(d.swVersion())
+                                .setSerialNumber(d.serialNumber())
+                                .setChassisId(d.chassisId().toString())
+                                .build());
+            });
+            responseObserver.onNext(replyBuilder.build());
+            responseObserver.onCompleted();
+        }
+
+        @Override
+        public void getDevice(getDeviceRequest request,
+                              io.grpc.stub.StreamObserver<DeviceServiceNb.getDeviceReply> responseObserver) {
+            org.onosproject.net.Device device = deviceService.getDevice(
+                    DeviceId.deviceId(request.getDeviceId()));
+            responseObserver.onNext(
+                    getDeviceReply.newBuilder().setDevice(
+                            org.onosproject.grpc.net.models.DeviceProtoOuterClass.DeviceProto
+                                    .newBuilder()
+                                    .setDeviceId(device.id().toString())
+                                    .setType(
+                                            //TODO check for better approach to mapping between enum varieties
+                                            DeviceEnumsProto.DeviceTypeProto.valueOf(device.type().toString()))
+                                    .setManufacturer(device.manufacturer())
+                                    .setHwVersion(device.hwVersion())
+                                    .setSwVersion(device.swVersion())
+                                    .setSerialNumber(device.serialNumber())
+                                    .setChassisId(device.chassisId().toString())
+                                    .build()).build());
+            responseObserver.onCompleted();
+        }
+
+        @Override
+        public void getRole(getRoleRequest request,
+                            StreamObserver<DeviceServiceNb.getRoleReply> responseObserver) {
+            DeviceId deviceId = DeviceId.deviceId(request.getDeviceId());
+            MastershipRole role = deviceService.getRole(deviceId);
+            DeviceEnumsProto.MastershipRoleProto mastershipRole =
+                    DeviceEnumsProto.MastershipRoleProto.valueOf(role.toString());
+            responseObserver.onNext(getRoleReply.newBuilder()
+                    .setRole(mastershipRole).build());
+            responseObserver.onCompleted();
+        }
+
+        //FIXME NOTE: this may be switched to a streaming version.
+        @Override
+        public void getPorts(getPortsRequest request, StreamObserver<getPortsReply> responseObserver) {
+            getPortsReply.Builder replyBuilder = getPortsReply.newBuilder();
+            deviceService.getPorts(
+                    DeviceId.deviceId(request.getDeviceId()))
+                    .forEach(port -> {
+                        PortProto.Builder portBuilder = PortProto
+                                .newBuilder()
+                                .setPortNumber(port.number().toString())
+                                .setIsEnabled(port.isEnabled())
+                                .setType(PortEnumsProto.PortTypeProto.valueOf(port.type().toString()))
+                                .setPortSpeed(port.portSpeed());
+                        port.annotations().keys().forEach(key -> portBuilder
+                                .putAnnotations(key, port.annotations().value(key)));
+
+                        replyBuilder.addPort(portBuilder.build());
+                    });
+            responseObserver.onNext(replyBuilder.build());
+            responseObserver.onCompleted();
+        }
+
+        //FIXME NOTE: this may be switched to a streaming version.
+        @Override
+        public void getPortStatistics(getPortStatisticsRequest request,
+                                      StreamObserver<getPortStatisticsReply> responseObserver) {
+            getPortStatisticsReply.Builder replyBuilder = getPortStatisticsReply.newBuilder();
+            deviceService.getPortStatistics(DeviceId.deviceId(request.getDeviceId()))
+                    .forEach(statistic -> {
+                        replyBuilder.addPortStatistics(
+                                PortStatisticsProto
+                                        .newBuilder()
+                                        .setPort(statistic.port())
+                                        .setPacketsReceived(statistic.packetsReceived())
+                                        .setPacketsSent(statistic.packetsSent())
+                                        .setBytesReceived(statistic.bytesReceived())
+                                        .setBytesSent(statistic.bytesSent())
+                                        .setPacketsRxDropped(statistic.packetsRxDropped())
+                                        .setPacketsTxDropped(statistic.packetsTxDropped())
+                                        .setPacketsRxErrors(statistic.packetsRxErrors())
+                                        .setPacketsTxErrors(statistic.packetsTxErrors())
+                                        .setDurationSec(statistic.durationSec())
+                                        .setDurationNano(statistic.durationNano())
+                                        .setIsZero(statistic.isZero())
+                                        .build());
+                    });
+            responseObserver.onNext(replyBuilder.build());
+            responseObserver.onCompleted();
+        }
+
+        //FIXME NOTE: this may be switched to a streaming version.
+        @Override
+        public void getPortDeltaStatistics(getPortDeltaStatisticsRequest request,
+                                           StreamObserver<getPortDeltaStatisticsReply> responseObserver) {
+            getPortDeltaStatisticsReply.Builder replyBuilder = getPortDeltaStatisticsReply.newBuilder();
+            deviceService.getPortDeltaStatistics(DeviceId.deviceId(request.getDeviceId()))
+                    .forEach(statistic -> {
+                        replyBuilder.addPortStatistics(
+                                PortStatisticsProto
+                                        .newBuilder()
+                                        .setPort(statistic.port())
+                                        .setPacketsReceived(statistic.packetsReceived())
+                                        .setPacketsSent(statistic.packetsSent())
+                                        .setBytesReceived(statistic.bytesReceived())
+                                        .setBytesSent(statistic.bytesSent())
+                                        .setPacketsRxDropped(statistic.packetsRxDropped())
+                                        .setPacketsTxDropped(statistic.packetsTxDropped())
+                                        .setPacketsRxErrors(statistic.packetsRxErrors())
+                                        .setPacketsTxErrors(statistic.packetsTxErrors())
+                                        .setDurationSec(statistic.durationSec())
+                                        .setDurationNano(statistic.durationNano())
+                                        .setIsZero(statistic.isZero())
+                                        .build());
+                    });
+            responseObserver.onNext(replyBuilder.build());
+            responseObserver.onCompleted();
+        }
+
+        @Override
+        public void getStatisticsForPort(getStatisticsForPortRequest request,
+                                         StreamObserver<getStatisticsForPortReply> responseObserver) {
+            org.onosproject.net.device.PortStatistics statistics = deviceService
+                    .getStatisticsForPort(DeviceId.deviceId(request.getDeviceId()),
+                            PortNumber.portNumber(request.getPortNumber()));
+            responseObserver.onNext(
+                    getStatisticsForPortReply
+                            .newBuilder()
+                            .setPortStatistics(
+                                    PortStatisticsProto
+                                            .newBuilder()
+                                            .setPort(statistics.port())
+                                            .setPacketsReceived(statistics.packetsReceived())
+                                            .setPacketsSent(statistics.packetsSent())
+                                            .setBytesReceived(statistics.bytesReceived())
+                                            .setBytesSent(statistics.bytesSent())
+                                            .setPacketsRxDropped(statistics.packetsRxDropped())
+                                            .setPacketsTxDropped(statistics.packetsTxDropped())
+                                            .setPacketsRxErrors(statistics.packetsRxErrors())
+                                            .setPacketsTxErrors(statistics.packetsTxErrors())
+                                            .setDurationSec(statistics.durationSec())
+                                            .setDurationNano(statistics.durationNano())
+                                            .setIsZero(statistics.isZero())
+                                            .build()).build());
+            responseObserver.onCompleted();
+
+        }
+
+        @Override
+        public void getDeltaStatisticsForPort(getDeltaStatisticsForPortRequest request,
+                                              StreamObserver<getDeltaStatisticsForPortReply> responseObserver) {
+            org.onosproject.net.device.PortStatistics statistics = deviceService
+                    .getDeltaStatisticsForPort(DeviceId.deviceId(request.getDeviceId()),
+                            PortNumber.portNumber(request.getPortNumber()));
+            responseObserver.onNext(
+                    getDeltaStatisticsForPortReply
+                            .newBuilder()
+                            .setPortStatistics(
+                                    PortStatisticsProto
+                                            .newBuilder()
+                                            .setPort(statistics.port())
+                                            .setPacketsReceived(statistics.packetsReceived())
+                                            .setPacketsSent(statistics.packetsSent())
+                                            .setBytesReceived(statistics.bytesReceived())
+                                            .setBytesSent(statistics.bytesSent())
+                                            .setPacketsRxDropped(statistics.packetsRxDropped())
+                                            .setPacketsTxDropped(statistics.packetsTxDropped())
+                                            .setPacketsRxErrors(statistics.packetsRxErrors())
+                                            .setPacketsTxErrors(statistics.packetsTxErrors())
+                                            .setDurationSec(statistics.durationSec())
+                                            .setDurationNano(statistics.durationNano())
+                                            .setIsZero(statistics.isZero())
+                                            .build()).build());
+            responseObserver.onCompleted();
+        }
+
+        @Override
+        public void getPort(getPortRequest request,
+                            StreamObserver<getPortReply> responseObserver) {
+            //FIXME getting deviceId here is dangerous because it is not guaranteed to be populated as port of a OneOf
+            org.onosproject.net.Port port = deviceService.getPort(
+                    new ConnectPoint(DeviceId.deviceId(
+                            request.getConnectPoint().getDeviceId()),
+                            PortNumber.portNumber(
+                                    request.getConnectPoint()
+                                            .getPortNumber())));
+            PortProto.Builder portBuilder =
+                    PortProto.newBuilder()
+                            .setPortNumber(port.number().toString())
+                            .setIsEnabled(port.isEnabled())
+                            .setType(
+                                    PortEnumsProto.PortTypeProto
+                                            .valueOf(port.type().toString()))
+                            .setPortSpeed(port.portSpeed());
+
+            port.annotations().keys().forEach(key -> portBuilder
+                    .putAnnotations(key, port.annotations().value(key)));
+
+            responseObserver.onNext(getPortReply
+                    .newBuilder()
+                    .setPort(portBuilder.build())
+                    .build());
+            responseObserver.onCompleted();
+        }
+
+        @Override
+        public void isAvailable(isAvailableRequest request,
+                                StreamObserver<isAvailableReply> responseObserver) {
+            responseObserver.onNext(
+                    isAvailableReply
+                            .newBuilder()
+                            .setIsAvailable(
+                                    deviceService.isAvailable(
+                                            DeviceId.deviceId(
+                                                    request.getDeviceId())))
+                            .build());
+            responseObserver.onCompleted();
+        }
+
+        @Override
+        public void localStatus(localStatusRequest request,
+                                StreamObserver<localStatusReply> responseObserver) {
+            responseObserver.onNext(
+                    localStatusReply
+                            .newBuilder()
+                            .setStatus(
+                                    deviceService.localStatus(
+                                            DeviceId.deviceId(request.getDeviceId())))
+                            .build());
+            responseObserver.onCompleted();
+        }
+    }
+}
\ No newline at end of file
diff --git a/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/package-info.java b/incubator/protobuf/services/nb/src/main/java/org/onosproject/grpc/nb/net/device/package-info.java
similarity index 92%
rename from incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/package-info.java
rename to incubator/protobuf/services/nb/src/main/java/org/onosproject/grpc/nb/net/device/package-info.java
index 51cb983..0814b10 100644
--- a/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/package-info.java
+++ b/incubator/protobuf/services/nb/src/main/java/org/onosproject/grpc/nb/net/device/package-info.java
@@ -16,4 +16,4 @@
 /**
  * gRPC server implementations for northbound services.
  */
-package org.onosproject.incubator.protobuf.services.nb;
+package org.onosproject.grpc.nb.net.device;
diff --git a/incubator/protobuf/services/nb/src/main/java/org/onosproject/grpc/nb/utils/GrpcNbDeviceServiceUtil.java b/incubator/protobuf/services/nb/src/main/java/org/onosproject/grpc/nb/utils/GrpcNbDeviceServiceUtil.java
new file mode 100644
index 0000000..0ad1b3f
--- /dev/null
+++ b/incubator/protobuf/services/nb/src/main/java/org/onosproject/grpc/nb/utils/GrpcNbDeviceServiceUtil.java
@@ -0,0 +1,389 @@
+/*
+ * 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.grpc.nb.utils;
+
+import org.onlab.packet.ChassisId;
+import org.onosproject.grpc.net.device.models.DeviceDescriptionProtoOuterClass;
+import org.onosproject.grpc.net.device.models.DeviceDescriptionProtoOuterClass.DeviceDescriptionProto;
+import org.onosproject.grpc.net.device.models.DeviceEnumsProto.DeviceTypeProto;
+import org.onosproject.grpc.net.device.models.DeviceEnumsProto.MastershipRoleProto;
+import org.onosproject.grpc.net.device.models.PortDescriptionProtoOuterClass.PortDescriptionProto;
+import org.onosproject.grpc.net.device.models.PortEnumsProto;
+
+import org.onosproject.grpc.net.device.models.PortStatisticsProtoOuterClass;
+import org.onosproject.grpc.net.device.models.PortStatisticsProtoOuterClass.PortStatisticsProto;
+import org.onosproject.net.Annotations;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.Device.Type;
+import org.onosproject.net.MastershipRole;
+import org.onosproject.net.Port;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.SparseAnnotations;
+import org.onosproject.net.device.DefaultDeviceDescription;
+import org.onosproject.net.device.DefaultPortDescription;
+import org.onosproject.net.device.DefaultPortStatistics;
+import org.onosproject.net.device.DeviceDescription;
+import org.onosproject.net.device.PortDescription;
+import org.onosproject.net.device.PortStatistics;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * gRPC message conversion related utilities for device service.
+ */
+public final class GrpcNbDeviceServiceUtil {
+
+    private static final Logger log = LoggerFactory.getLogger(GrpcNbDeviceServiceUtil.class);
+
+    /**
+     * Translates gRPC enum MastershipRole to ONOS enum.
+     *
+     * @param role mastership role in gRPC enum
+     * @return equivalent in ONOS enum
+     */
+    public static MastershipRole translate(MastershipRoleProto role) {
+        switch (role) {
+            case NONE:
+                return MastershipRole.NONE;
+            case MASTER:
+                return MastershipRole.MASTER;
+            case STANDBY:
+                return MastershipRole.STANDBY;
+            case UNRECOGNIZED:
+                log.warn("Unrecognized MastershipRole gRPC message: {}", role);
+                return MastershipRole.NONE;
+            default:
+                return MastershipRole.NONE;
+        }
+    }
+
+    /**
+     * Translates ONOS enum MastershipRole to gRPC enum.
+     *
+     * @param newRole ONOS' mastership role
+     * @return equivalent in gRPC message enum
+     */
+    public static MastershipRoleProto translate(MastershipRole newRole) {
+        switch (newRole) {
+            case MASTER:
+                return MastershipRoleProto.MASTER;
+            case STANDBY:
+                return MastershipRoleProto.STANDBY;
+            case NONE:
+            default:
+                return MastershipRoleProto.NONE;
+        }
+    }
+
+
+    /**
+     * Translates gRPC DeviceDescription to {@link DeviceDescriptionProtoOuterClass}.
+     *
+     * @param deviceDescription gRPC message
+     * @return {@link DeviceDescriptionProtoOuterClass}
+     */
+    public static DeviceDescription translate(
+            DeviceDescriptionProto deviceDescription) {
+        URI uri = URI.create(deviceDescription.getDeviceUri());
+        Type type = translate(deviceDescription.getType());
+        String manufacturer = deviceDescription.getManufacturer();
+        String hwVersion = deviceDescription.getHwVersion();
+        String swVersion = deviceDescription.getSwVersion();
+        String serialNumber = deviceDescription.getSerialNumber();
+        ChassisId chassis = new ChassisId(deviceDescription.getChassisId());
+        boolean defaultAvailable = deviceDescription.getIsDefaultAvailable();
+        return new DefaultDeviceDescription(uri, type, manufacturer,
+                hwVersion, swVersion, serialNumber,
+                chassis,
+                defaultAvailable,
+                asAnnotations(deviceDescription.getAnnotationsMap()));
+    }
+
+    /**
+     * Translates {@link DeviceDescription} to gRPC DeviceDescription message.
+     *
+     * @param deviceDescription {@link DeviceDescription}
+     * @return gRPC DeviceDescription message
+     */
+    public static DeviceDescriptionProto translate(
+            DeviceDescription deviceDescription) {
+
+        return DeviceDescriptionProto.newBuilder()
+                .setDeviceUri(deviceDescription.deviceUri().toString())
+                .setType(translate(deviceDescription.type()))
+                .setManufacturer(deviceDescription.manufacturer())
+                .setHwVersion(deviceDescription.hwVersion())
+                .setSwVersion(deviceDescription.swVersion())
+                .setSerialNumber(deviceDescription.serialNumber())
+                .setChassisId(deviceDescription.chassisId().toString())
+                .setIsDefaultAvailable(deviceDescription.isDefaultAvailable())
+                .putAllAnnotations(asMap(deviceDescription.annotations()))
+                .build();
+    }
+
+
+    /**
+     * Translates gRPC DeviceType to {@link Type}.
+     *
+     * @param type      gRPC message
+     * @return  {@link Type}
+     */
+    public static Type translate(DeviceTypeProto type) {
+        switch (type) {
+            case BALANCER:
+                return Type.BALANCER;
+            case CONTROLLER:
+                return Type.CONTROLLER;
+            case FIBER_SWITCH:
+                return Type.FIBER_SWITCH;
+            case FIREWALL:
+                return Type.FIREWALL;
+            case IDS:
+                return Type.IDS;
+            case IPS:
+                return Type.IPS;
+            case MICROWAVE:
+                return Type.MICROWAVE;
+            case OTHER:
+                return Type.OTHER;
+            case OTN:
+                return Type.OTN;
+            case ROADM:
+                return Type.ROADM;
+            case ROADM_OTN:
+                return Type.ROADM_OTN;
+            case ROUTER:
+                return Type.ROUTER;
+            case SWITCH:
+                return Type.SWITCH;
+            case VIRTUAL_DEVICE:
+                return Type.VIRTUAL;
+
+            case UNRECOGNIZED:
+            default:
+                log.warn("Unexpected DeviceType: {}", type);
+                return Type.OTHER;
+        }
+    }
+
+    /**
+     * Translates {@link Type} to gRPC DeviceType.
+     *
+     * @param type {@link Type}
+     * @return  gRPC message
+     */
+    public static DeviceTypeProto translate(Type type) {
+        switch (type) {
+            case BALANCER:
+                return DeviceTypeProto.BALANCER;
+            case CONTROLLER:
+                return DeviceTypeProto.CONTROLLER;
+            case FIBER_SWITCH:
+                return DeviceTypeProto.FIBER_SWITCH;
+            case FIREWALL:
+                return DeviceTypeProto.FIREWALL;
+            case IDS:
+                return DeviceTypeProto.IDS;
+            case IPS:
+                return DeviceTypeProto.IPS;
+            case MICROWAVE:
+                return DeviceTypeProto.MICROWAVE;
+            case OTHER:
+                return DeviceTypeProto.OTHER;
+            case OTN:
+                return DeviceTypeProto.OTN;
+            case ROADM:
+                return DeviceTypeProto.ROADM;
+            case ROADM_OTN:
+                return DeviceTypeProto.ROADM_OTN;
+            case ROUTER:
+                return DeviceTypeProto.ROUTER;
+            case SWITCH:
+                return DeviceTypeProto.SWITCH;
+            case VIRTUAL:
+                return DeviceTypeProto.VIRTUAL_DEVICE;
+
+            default:
+                log.warn("Unexpected Device.Type: {}", type);
+                return DeviceTypeProto.OTHER;
+        }
+    }
+
+    /**
+     * Translates gRPC PortDescription message to {@link PortDescription}.
+     *
+     * @param portDescription gRPC message
+     * @return {@link PortDescription}
+     */
+    public static PortDescription translate(PortDescriptionProto portDescription) {
+        PortNumber number = PortNumber.fromString(portDescription.getPortNumber());
+        boolean isEnabled = portDescription.getIsEnabled();
+        Port.Type type = translate(portDescription.getType());
+        long portSpeed = portDescription.getPortSpeed();
+        SparseAnnotations annotations = asAnnotations(portDescription.getAnnotationsMap());
+        // TODO How to deal with more specific Port...
+        return new DefaultPortDescription(number, isEnabled, type, portSpeed, annotations);
+    }
+
+    /**
+     * Translates {@link PortDescription} to gRPC PortDescription message.
+     *
+     * @param portDescription {@link PortDescription}
+     * @return gRPC PortDescription message
+     */
+    public static PortDescriptionProto translate(PortDescription portDescription) {
+        return PortDescriptionProto.newBuilder()
+                .setPortNumber(portDescription.portNumber().toString())
+                .setIsEnabled(portDescription.isEnabled())
+                .setType(translate(portDescription.type()))
+                .setPortSpeed(portDescription.portSpeed())
+                .putAllAnnotations(asMap(portDescription.annotations()))
+                .build();
+    }
+
+    /**
+     * Translates gRPC PortType to {@link Port.Type}.
+     *
+     * @param type      gRPC message
+     * @return  {@link Port.Type}
+     */
+    public static Port.Type translate(PortEnumsProto.PortTypeProto type) {
+        switch (type) {
+            case COPPER:
+                return Port.Type.COPPER;
+            case FIBER:
+                return Port.Type.FIBER;
+            case OCH:
+                return Port.Type.OCH;
+            case ODUCLT:
+                return Port.Type.ODUCLT;
+            case OMS:
+                return Port.Type.OMS;
+            case PACKET:
+                return Port.Type.PACKET;
+            case VIRTUAL_PORT:
+                return Port.Type.VIRTUAL;
+
+            case UNRECOGNIZED:
+            default:
+                log.warn("Unexpected PortType: {}", type);
+                return Port.Type.COPPER;
+        }
+    }
+
+    /**
+     * Translates {@link Port.Type} to gRPC PortType.
+     *
+     * @param type      {@link Port.Type}
+     * @return  gRPC message
+     */
+    public static PortEnumsProto.PortTypeProto translate(Port.Type type) {
+        switch (type) {
+            case COPPER:
+                return PortEnumsProto.PortTypeProto.COPPER;
+            case FIBER:
+                return PortEnumsProto.PortTypeProto.FIBER;
+            case OCH:
+                return PortEnumsProto.PortTypeProto.OCH;
+            case ODUCLT:
+                return PortEnumsProto.PortTypeProto.ODUCLT;
+            case OMS:
+                return PortEnumsProto.PortTypeProto.OMS;
+            case PACKET:
+                return PortEnumsProto.PortTypeProto.PACKET;
+            case VIRTUAL:
+                return PortEnumsProto.PortTypeProto.VIRTUAL_PORT;
+
+            default:
+                log.warn("Unexpected Port.Type: {}", type);
+                return PortEnumsProto.PortTypeProto.COPPER;
+        }
+    }
+
+    /**
+     * Translates gRPC PortStatistics message to {@link PortStatisticsProtoOuterClass}.
+     *
+     * @param portStatistics gRPC PortStatistics message
+     * @return {@link PortStatisticsProtoOuterClass}
+     */
+    public static PortStatistics translate(PortStatisticsProto portStatistics) {
+        // TODO implement adding missing fields
+        return DefaultPortStatistics.builder()
+                .setPort(portStatistics.getPort())
+                .setPacketsReceived(portStatistics.getPacketsReceived())
+                .setPacketsSent(portStatistics.getPacketsSent())
+                .build();
+    }
+
+    /**
+     * Translates {@link PortStatistics} to gRPC PortStatistics message.
+     *
+     * @param portStatistics {@link PortStatistics}
+     * @return gRPC PortStatistics message
+     */
+    public static PortStatisticsProto translate(PortStatistics portStatistics) {
+        // TODO implement adding missing fields
+        return PortStatisticsProto.newBuilder()
+                .setPort(portStatistics.port())
+                .setPacketsReceived(portStatistics.packetsReceived())
+                .setPacketsSent(portStatistics.packetsSent())
+                .build();
+    }
+
+    // may be this can be moved to Annotation itself or AnnotationsUtils
+    /**
+     * Converts Annotations to Map of Strings.
+     *
+     * @param annotations {@link Annotations}
+     * @return Map of annotation key and values
+     */
+    public static Map<String, String> asMap(Annotations annotations) {
+        if (annotations instanceof DefaultAnnotations) {
+            return ((DefaultAnnotations) annotations).asMap();
+        }
+        Map<String, String> map = new HashMap<>();
+        annotations.keys()
+                .forEach(k -> map.put(k, annotations.value(k)));
+
+        return map;
+    }
+
+    // may be this can be moved to Annotation itself or AnnotationsUtils
+    /**
+     * Converts Map of Strings to {@link SparseAnnotations}.
+     *
+     * @param annotations Map of annotation key and values
+     * @return {@link SparseAnnotations}
+     */
+    public static SparseAnnotations asAnnotations(Map<String, String> annotations) {
+        DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
+        annotations.entrySet().forEach(e -> {
+            if (e.getValue() != null) {
+                builder.set(e.getKey(), e.getValue());
+            } else {
+                builder.remove(e.getKey());
+            }
+        });
+        return builder.build();
+    }
+
+    // Utility class not intended for instantiation.
+    private GrpcNbDeviceServiceUtil() {}
+}
diff --git a/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/package-info.java b/incubator/protobuf/services/nb/src/main/java/org/onosproject/grpc/nb/utils/package-info.java
similarity index 80%
copy from incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/package-info.java
copy to incubator/protobuf/services/nb/src/main/java/org/onosproject/grpc/nb/utils/package-info.java
index 51cb983..341744c 100644
--- a/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/package-info.java
+++ b/incubator/protobuf/services/nb/src/main/java/org/onosproject/grpc/nb/utils/package-info.java
@@ -13,7 +13,9 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 /**
- * gRPC server implementations for northbound services.
+ * A package contains a set of utilities that are used to convert gRPC model
+ * object to ONOS data model object.
  */
-package org.onosproject.incubator.protobuf.services.nb;
+package org.onosproject.grpc.nb.utils;
\ No newline at end of file
diff --git a/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/DeleteMe.java b/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/DeleteMe.java
deleted file mode 100644
index 947a0e4..0000000
--- a/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/DeleteMe.java
+++ /dev/null
@@ -1,26 +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.incubator.protobuf.services.nb;
-
-/**
- * Delete this file.  It is just a placeholder for structure.
- */
-public class DeleteMe {
-    public DeleteMe() {
-        throw new RuntimeException("This is not a class that should be instantiated.");
-    }
-}
diff --git a/incubator/protobuf/services/nb/src/main/proto/PlaceHolderToDelete.proto b/incubator/protobuf/services/nb/src/main/proto/PlaceHolderToDelete.proto
deleted file mode 100644
index d1207dd..0000000
--- a/incubator/protobuf/services/nb/src/main/proto/PlaceHolderToDelete.proto
+++ /dev/null
@@ -1,6 +0,0 @@
-syntax="proto3";
-option java_package = "org.onosproject.grpc.net.services.nb";
-
-message ToDelete{
-
-}
\ No newline at end of file
diff --git a/incubator/protobuf/services/nb/src/main/proto/net/device/DeviceServiceNb.proto b/incubator/protobuf/services/nb/src/main/proto/net/device/DeviceServiceNb.proto
new file mode 100644
index 0000000..1b1dad5
--- /dev/null
+++ b/incubator/protobuf/services/nb/src/main/proto/net/device/DeviceServiceNb.proto
@@ -0,0 +1,133 @@
+syntax="proto3";
+option java_package = "org.onosproject.grpc.nb.net.device";
+
+package nb.net.device;
+
+import "net/DeviceProto.proto";
+import "net/device/DeviceEnumsProto.proto";
+import "net/device/PortStatisticsProto.proto";
+import "net/PortProto.proto";
+import "net/ConnectPointProto.proto";
+
+message getDeviceCountRequest {
+}
+
+message getDeviceCountReply {
+    int32 device_count = 1;
+}
+
+message getDevicesRequest {
+}
+
+message getDevicesReply {
+    repeated .net.DeviceProto device = 1;
+}
+
+message getAvailableDevicesReply {
+    repeated .net.DeviceProto device = 1;
+}
+
+message getAvailableDevicesRequest {
+    .net.device.DeviceTypeProto type = 1;
+}
+
+message getDeviceRequest {
+    string device_id = 1;
+}
+
+message getDeviceReply {
+    .net.DeviceProto device = 1;
+}
+
+message getRoleRequest {
+    string device_id = 1;
+}
+
+message getRoleReply {
+    .net.device.MastershipRoleProto role = 1;
+}
+
+message getPortsRequest {
+    string device_id = 1;
+}
+
+message getPortsReply {
+    repeated .net.PortProto port = 1;
+}
+
+message getPortStatisticsRequest {
+    string device_id = 1;
+}
+
+message getPortStatisticsReply {
+    repeated .net.device.PortStatisticsProto port_statistics = 1;
+}
+
+message getPortDeltaStatisticsRequest {
+    string device_id = 1;
+}
+
+message getPortDeltaStatisticsReply {
+    repeated .net.device.PortStatisticsProto port_statistics = 1;
+}
+
+message getStatisticsForPortRequest {
+    string device_id = 1;
+    uint64 port_number = 2;
+}
+
+message getStatisticsForPortReply {
+    .net.device.PortStatisticsProto port_statistics = 1;
+}
+
+message getDeltaStatisticsForPortRequest {
+    string device_id = 1;
+    uint64 port_number = 2;
+}
+
+message getDeltaStatisticsForPortReply {
+    .net.device.PortStatisticsProto port_statistics = 1;
+}
+
+message getPortRequest {
+    .net.ConnectPointProto connect_point = 1;
+}
+
+message getPortReply {
+    .net.PortProto port = 1;
+}
+
+message isAvailableRequest {
+    string device_id = 1;
+}
+
+message isAvailableReply {
+    bool is_available = 1;
+}
+
+message localStatusRequest {
+    string device_id = 1;
+}
+
+message localStatusReply {
+    string status = 1;
+}
+
+service DeviceService {
+    rpc getDeviceCount(getDeviceCountRequest) returns (getDeviceCountReply) {}
+    rpc getDevices(getDevicesRequest) returns (getDevicesReply) {}
+    rpc getAvailableDevices(getAvailableDevicesRequest) returns (getAvailableDevicesReply) {}
+    rpc getDevice(getDeviceRequest) returns (getDeviceReply) {}
+    rpc getRole(getRoleRequest) returns (getRoleReply) {}
+    rpc getPorts(getPortsRequest) returns (getPortsReply) {}
+    rpc getPortStatistics(getPortStatisticsRequest) returns (getPortStatisticsReply) {}
+    rpc getPortDeltaStatistics(getPortDeltaStatisticsRequest)
+    returns (getPortDeltaStatisticsReply) {}
+    rpc getStatisticsForPort(getStatisticsForPortRequest)
+    returns (getStatisticsForPortReply) {}
+    rpc getDeltaStatisticsForPort(getDeltaStatisticsForPortRequest)
+    returns (getDeltaStatisticsForPortReply) {}
+    rpc getPort(getPortRequest) returns (getPortReply) {}
+    rpc isAvailable(isAvailableRequest) returns (isAvailableReply) {}
+    rpc localStatus(localStatusRequest) returns (localStatusReply) {}
+}
\ No newline at end of file