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/AnnotationsTranslator.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/AnnotationsTranslator.java
new file mode 100644
index 0000000..0b8cb03
--- /dev/null
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/AnnotationsTranslator.java
@@ -0,0 +1,71 @@
+/*
+ * 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.models.net;
+
+import org.onosproject.net.Annotations;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.SparseAnnotations;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * gRPC message conversion related utilities for annotations service.
+ */
+public final class AnnotationsTranslator {
+
+ private static final Logger log = LoggerFactory.getLogger(AnnotationsTranslator.class);
+
+ /**
+ * 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;
+ }
+
+ /**
+ * 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 AnnotationsTranslator() {}
+}
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/RegionProtoTranslator.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/RegionProtoTranslator.java
index 16b5557..bd43c0b 100644
--- a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/RegionProtoTranslator.java
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/RegionProtoTranslator.java
@@ -14,16 +14,12 @@
* limitations under the License.
*/
package org.onosproject.incubator.protobuf.models.net;
+
import com.google.common.base.Strings;
-import org.onosproject.incubator.protobuf.models.net.region.RegionEnumsProtoTranslator;
-
-
-
import org.onosproject.cluster.NodeId;
import org.onosproject.grpc.net.models.RegionProtoOuterClass;
+import org.onosproject.incubator.protobuf.models.net.region.RegionEnumsProtoTranslator;
import org.onosproject.net.Annotations;
-import org.onosproject.net.DefaultAnnotations;
-import org.onosproject.net.SparseAnnotations;
import org.onosproject.net.region.DefaultRegion;
import org.onosproject.net.region.Region;
import org.onosproject.net.region.RegionId;
@@ -31,7 +27,6 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
-import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@@ -61,7 +56,7 @@
masters.add(nodeIdSet);
});
- Annotations annots = asAnnotations(region.getAnnotations());
+ Annotations annots = AnnotationsTranslator.asAnnotations(region.getAnnotations());
return new DefaultRegion(id, name, type, annots, masters);
}
@@ -89,25 +84,6 @@
.build();
}
- // 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 RegionProtoTranslator() {}
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() {}
}