Refactor several gRPC translator by removing annotation redundancy

Change-Id: I895d06183857ec74272287d904163f1d7d31d269
This commit is contained in:
Jian Li 2017-11-21 11:23:19 +09:00
parent d8e720718d
commit 21fa4feb22
4 changed files with 80 additions and 117 deletions

View File

@ -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() {}
}

View File

@ -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 org.onosproject.net.region.RegionId;
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 @@ public final class RegionProtoTranslator {
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 @@ public final class RegionProtoTranslator {
.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() {}

View File

@ -19,18 +19,14 @@ 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.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 @@ public final class DeviceProtoTranslator {
hwVersion, swVersion, serialNumber,
chassis,
defaultAvailable,
asAnnotations(deviceDescription.getAnnotationsMap()));
AnnotationsTranslator.asAnnotations(deviceDescription.getAnnotationsMap()));
}
/**
@ -80,7 +76,7 @@ public final class DeviceProtoTranslator {
.setSerialNumber(deviceDescription.serialNumber())
.setChassisId(deviceDescription.chassisId().toString())
.setIsDefaultAvailable(deviceDescription.isDefaultAvailable())
.putAllAnnotations(asMap(deviceDescription.annotations()))
.putAllAnnotations(AnnotationsTranslator.asMap(deviceDescription.annotations()))
.build();
}
@ -172,44 +168,6 @@ public final class DeviceProtoTranslator {
}
}
// 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() {
}

View File

@ -19,8 +19,7 @@ import org.onosproject.grpc.net.device.models.PortDescriptionProtoOuterClass.Por
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.onosproject.net.device.PortStatistics;
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 @@ public final class PortProtoTranslator {
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 @@ public final class PortProtoTranslator {
.setIsEnabled(portDescription.isEnabled())
.setType(translate(portDescription.type()))
.setPortSpeed(portDescription.portSpeed())
.putAllAnnotations(asMap(portDescription.annotations()))
.putAllAnnotations(AnnotationsTranslator.asMap(portDescription.annotations()))
.build();
}
@ -162,44 +158,6 @@ public final class PortProtoTranslator {
.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() {}
}