Refactor several gRPC translator by removing annotation redundancy

Change-Id: I895d06183857ec74272287d904163f1d7d31d269
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/device/DeviceProtoTranslator.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/device/DeviceProtoTranslator.java
index 495da82..90d10f5 100644
--- a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/device/DeviceProtoTranslator.java
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/device/DeviceProtoTranslator.java
@@ -19,18 +19,14 @@
 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.net.Annotations;
-import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.incubator.protobuf.models.net.AnnotationsTranslator;
 import org.onosproject.net.Device.Type;
-import org.onosproject.net.SparseAnnotations;
 import org.onosproject.net.device.DefaultDeviceDescription;
 import org.onosproject.net.device.DeviceDescription;
 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.
@@ -59,7 +55,7 @@
                 hwVersion, swVersion, serialNumber,
                 chassis,
                 defaultAvailable,
-                asAnnotations(deviceDescription.getAnnotationsMap()));
+                AnnotationsTranslator.asAnnotations(deviceDescription.getAnnotationsMap()));
     }
 
     /**
@@ -80,7 +76,7 @@
                 .setSerialNumber(deviceDescription.serialNumber())
                 .setChassisId(deviceDescription.chassisId().toString())
                 .setIsDefaultAvailable(deviceDescription.isDefaultAvailable())
-                .putAllAnnotations(asMap(deviceDescription.annotations()))
+                .putAllAnnotations(AnnotationsTranslator.asMap(deviceDescription.annotations()))
                 .build();
     }
 
@@ -172,44 +168,6 @@
         }
     }
 
-
-    // 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 DeviceProtoTranslator() {
     }
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/device/PortProtoTranslator.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/device/PortProtoTranslator.java
index 91bcd32..0d0e0fa 100644
--- a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/device/PortProtoTranslator.java
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/device/PortProtoTranslator.java
@@ -19,8 +19,7 @@
 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.incubator.protobuf.models.net.AnnotationsTranslator;
 import org.onosproject.net.Port;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.SparseAnnotations;
@@ -31,9 +30,6 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.HashMap;
-import java.util.Map;
-
 /**
  * gRPC message conversion related utilities for port service.
  */
@@ -52,7 +48,7 @@
         boolean isEnabled = portDescription.getIsEnabled();
         Port.Type type = translate(portDescription.getType());
         long portSpeed = portDescription.getPortSpeed();
-        SparseAnnotations annotations = asAnnotations(portDescription.getAnnotationsMap());
+        SparseAnnotations annotations = AnnotationsTranslator.asAnnotations(portDescription.getAnnotationsMap());
         // TODO How to deal with more specific Port...
         return new DefaultPortDescription(number, isEnabled, type, portSpeed, annotations);
     }
@@ -69,7 +65,7 @@
                 .setIsEnabled(portDescription.isEnabled())
                 .setType(translate(portDescription.type()))
                 .setPortSpeed(portDescription.portSpeed())
-                .putAllAnnotations(asMap(portDescription.annotations()))
+                .putAllAnnotations(AnnotationsTranslator.asMap(portDescription.annotations()))
                 .build();
     }
 
@@ -162,44 +158,6 @@
                 .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 PortProtoTranslator() {}
 }