Ayaka Koshibe
Committed by Gerrit Code Review

Serializer for DefaultAnnotations. This allows Annotated objects to be correctly

used as keys in ConsistentMaps. Fixes ONOS-2128.

Change-Id: Ia21a25712351b99bc1b79dc231be8187cf3d3a0b
......@@ -21,6 +21,8 @@ import java.util.Map;
import java.util.Objects;
import java.util.Set;
import com.google.common.collect.Maps;
import static com.google.common.base.Preconditions.checkNotNull;
/**
......@@ -59,6 +61,15 @@ public final class DefaultAnnotations implements SparseAnnotations {
}
/**
* Returns the annotations as a map.
*
* @return a copy of the contents of the annotations as a map.
*/
public HashMap<String, String> asMap() {
return Maps.newHashMap(this.map);
}
/**
* Creates a new set of annotations using clone of the specified hash map.
*
* @param map hash map of key/value pairs
......
package org.onosproject.store.serializers;
import org.onosproject.net.DefaultAnnotations;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import java.util.HashMap;
public class AnnotationsSerializer extends Serializer<DefaultAnnotations> {
public AnnotationsSerializer() {
super(false, true);
}
@Override
public void write(Kryo kryo, Output output, DefaultAnnotations object) {
kryo.writeObject(output, object.asMap());
}
@Override
public DefaultAnnotations read(Kryo kryo, Input input, Class<DefaultAnnotations> type) {
DefaultAnnotations.Builder b = DefaultAnnotations.builder();
HashMap<String, String> map = kryo.readObject(input, HashMap.class);
map.forEach((k, v) -> b.set(k, v));
return b.build();
}
}
......@@ -264,7 +264,6 @@ public final class KryoNamespaces {
Device.Type.class,
Port.Type.class,
ChassisId.class,
DefaultAnnotations.class,
DefaultControllerNode.class,
DefaultDevice.class,
DefaultDeviceDescription.class,
......@@ -411,6 +410,7 @@ public final class KryoNamespaces {
.register(new MastershipTermSerializer(), MastershipTerm.class)
.register(new HostLocationSerializer(), HostLocation.class)
.register(new DefaultOutboundPacketSerializer(), DefaultOutboundPacket.class)
.register(new AnnotationsSerializer(), DefaultAnnotations.class)
.register(Versioned.class)
.register(MapEvent.class)
.register(MapEvent.Type.class)
......