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
Showing
3 changed files
with
44 additions
and
1 deletions
| ... | @@ -21,6 +21,8 @@ import java.util.Map; | ... | @@ -21,6 +21,8 @@ import java.util.Map; |
| 21 | import java.util.Objects; | 21 | import java.util.Objects; |
| 22 | import java.util.Set; | 22 | import java.util.Set; |
| 23 | 23 | ||
| 24 | +import com.google.common.collect.Maps; | ||
| 25 | + | ||
| 24 | import static com.google.common.base.Preconditions.checkNotNull; | 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | 27 | ||
| 26 | /** | 28 | /** |
| ... | @@ -59,6 +61,15 @@ public final class DefaultAnnotations implements SparseAnnotations { | ... | @@ -59,6 +61,15 @@ public final class DefaultAnnotations implements SparseAnnotations { |
| 59 | } | 61 | } |
| 60 | 62 | ||
| 61 | /** | 63 | /** |
| 64 | + * Returns the annotations as a map. | ||
| 65 | + * | ||
| 66 | + * @return a copy of the contents of the annotations as a map. | ||
| 67 | + */ | ||
| 68 | + public HashMap<String, String> asMap() { | ||
| 69 | + return Maps.newHashMap(this.map); | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + /** | ||
| 62 | * Creates a new set of annotations using clone of the specified hash map. | 73 | * Creates a new set of annotations using clone of the specified hash map. |
| 63 | * | 74 | * |
| 64 | * @param map hash map of key/value pairs | 75 | * @param map hash map of key/value pairs | ... | ... |
core/store/serializers/src/main/java/org/onosproject/store/serializers/AnnotationsSerializer.java
0 → 100644
| 1 | +package org.onosproject.store.serializers; | ||
| 2 | + | ||
| 3 | +import org.onosproject.net.DefaultAnnotations; | ||
| 4 | + | ||
| 5 | +import com.esotericsoftware.kryo.Kryo; | ||
| 6 | +import com.esotericsoftware.kryo.Serializer; | ||
| 7 | +import com.esotericsoftware.kryo.io.Input; | ||
| 8 | +import com.esotericsoftware.kryo.io.Output; | ||
| 9 | + | ||
| 10 | +import java.util.HashMap; | ||
| 11 | + | ||
| 12 | +public class AnnotationsSerializer extends Serializer<DefaultAnnotations> { | ||
| 13 | + | ||
| 14 | + public AnnotationsSerializer() { | ||
| 15 | + super(false, true); | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + @Override | ||
| 19 | + public void write(Kryo kryo, Output output, DefaultAnnotations object) { | ||
| 20 | + kryo.writeObject(output, object.asMap()); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + @Override | ||
| 24 | + public DefaultAnnotations read(Kryo kryo, Input input, Class<DefaultAnnotations> type) { | ||
| 25 | + DefaultAnnotations.Builder b = DefaultAnnotations.builder(); | ||
| 26 | + HashMap<String, String> map = kryo.readObject(input, HashMap.class); | ||
| 27 | + map.forEach((k, v) -> b.set(k, v)); | ||
| 28 | + | ||
| 29 | + return b.build(); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | +} |
| ... | @@ -264,7 +264,6 @@ public final class KryoNamespaces { | ... | @@ -264,7 +264,6 @@ public final class KryoNamespaces { |
| 264 | Device.Type.class, | 264 | Device.Type.class, |
| 265 | Port.Type.class, | 265 | Port.Type.class, |
| 266 | ChassisId.class, | 266 | ChassisId.class, |
| 267 | - DefaultAnnotations.class, | ||
| 268 | DefaultControllerNode.class, | 267 | DefaultControllerNode.class, |
| 269 | DefaultDevice.class, | 268 | DefaultDevice.class, |
| 270 | DefaultDeviceDescription.class, | 269 | DefaultDeviceDescription.class, |
| ... | @@ -411,6 +410,7 @@ public final class KryoNamespaces { | ... | @@ -411,6 +410,7 @@ public final class KryoNamespaces { |
| 411 | .register(new MastershipTermSerializer(), MastershipTerm.class) | 410 | .register(new MastershipTermSerializer(), MastershipTerm.class) |
| 412 | .register(new HostLocationSerializer(), HostLocation.class) | 411 | .register(new HostLocationSerializer(), HostLocation.class) |
| 413 | .register(new DefaultOutboundPacketSerializer(), DefaultOutboundPacket.class) | 412 | .register(new DefaultOutboundPacketSerializer(), DefaultOutboundPacket.class) |
| 413 | + .register(new AnnotationsSerializer(), DefaultAnnotations.class) | ||
| 414 | .register(Versioned.class) | 414 | .register(Versioned.class) |
| 415 | .register(MapEvent.class) | 415 | .register(MapEvent.class) |
| 416 | .register(MapEvent.Type.class) | 416 | .register(MapEvent.Type.class) | ... | ... |
-
Please register or login to post a comment