Add new MutableCompositeModification version 58/93858/14
authortadei.bilan <tadei.bilan@pantheon.tech>
Tue, 17 Nov 2020 13:39:03 +0000 (15:39 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 2 Jul 2021 10:42:56 +0000 (12:42 +0200)
Add new version of MutableCompositeModification, that will write NNDO
header right after number of modifications, forming proper embedded
block of modifications.

For communicating this, we are renaming
DataStoreVersions.MAGNESIUM_VERSION and reusing it for the bump.

JIRA: CONTROLLER-1939
Change-Id: Ic5c2a8f91fcc41e78682ec202442308d6dc1191a
Signed-off-by: tadei.bilan <tadei.bilan@pantheon.tech>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DataStoreVersions.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/VersionedExternalizableMessage.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/modification/MutableCompositeModification.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/MutableCompositeModificationTest.java

index 79eedd3ef3e7511166b5dd4c9c00b0436ed6db95..d9a8e2c740d3ccb120025823b0f725f7b8b323a9 100644 (file)
@@ -14,19 +14,19 @@ package org.opendaylight.controller.cluster.datastore;
  */
 public final class DataStoreVersions {
     @Deprecated
-    public static final short BASE_HELIUM_VERSION = 0;
+    public static final short BASE_HELIUM_VERSION =  0;
     @Deprecated
-    public static final short HELIUM_1_VERSION = 1;
+    public static final short HELIUM_1_VERSION    =  1;
     @Deprecated
-    public static final short HELIUM_2_VERSION = 2;
+    public static final short HELIUM_2_VERSION    =  2;
     @Deprecated
-    public static final short LITHIUM_VERSION = 3;
-    public static final short BORON_VERSION = 5;
-    public static final short FLUORINE_VERSION = 9;
-    public static final short NEON_SR2_VERSION = 10;
-    public static final short SODIUM_SR1_VERSION = 11;
-    public static final short MAGNESIUM_VERSION = 12;
-    public static final short CURRENT_VERSION = SODIUM_SR1_VERSION;
+    public static final short LITHIUM_VERSION     =  3;
+    public static final short BORON_VERSION       =  5;
+    public static final short FLUORINE_VERSION    =  9;
+    public static final short NEON_SR2_VERSION    = 10;
+    public static final short SODIUM_SR1_VERSION  = 11;
+    public static final short PHOSPHORUS_VERSION  = 12;
+    public static final short CURRENT_VERSION     = SODIUM_SR1_VERSION;
 
     private DataStoreVersions() {
 
index a1b93392079b3d24792e6e439e60546a7bbd453d..16f03133d095141228f2f8f74dc3130674985f46 100644 (file)
@@ -26,6 +26,7 @@ public abstract class VersionedExternalizableMessage implements Externalizable,
     private short version = DataStoreVersions.CURRENT_VERSION;
 
     public VersionedExternalizableMessage() {
+
     }
 
     public VersionedExternalizableMessage(final short version) {
@@ -37,7 +38,7 @@ public abstract class VersionedExternalizableMessage implements Externalizable,
     }
 
     protected final @NonNull NormalizedNodeStreamVersion getStreamVersion() {
-        if (version >= DataStoreVersions.MAGNESIUM_VERSION) {
+        if (version >= DataStoreVersions.PHOSPHORUS_VERSION) {
             return NormalizedNodeStreamVersion.MAGNESIUM;
         } else if (version == DataStoreVersions.SODIUM_SR1_VERSION) {
             return NormalizedNodeStreamVersion.SODIUM_SR1;
index a7556744f4682cba8c954376f47d17304d9c4df2..a9ffe9b1ba877c40718a946df5ecf6bf591d4ca2 100644 (file)
@@ -88,29 +88,12 @@ public class MutableCompositeModification extends VersionedExternalizableMessage
     @Override
     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
         super.readExternal(in);
-
-        int size = in.readInt();
+        final int size = in.readInt();
         if (size > 0) {
-            final NormalizedNodeDataInput input = NormalizedNodeDataInput.newDataInputWithoutValidation(in);
-            final ReusableStreamReceiver receiver = ReusableImmutableNormalizedNodeStreamWriter.create();
-
-            for (int i = 0; i < size; i++) {
-                byte type = in.readByte();
-                switch (type) {
-                    case Modification.WRITE:
-                        modifications.add(WriteModification.fromStream(input, getVersion(), receiver));
-                        break;
-
-                    case Modification.MERGE:
-                        modifications.add(MergeModification.fromStream(input, getVersion(), receiver));
-                        break;
-
-                    case Modification.DELETE:
-                        modifications.add(DeleteModification.fromStream(input, getVersion()));
-                        break;
-                    default:
-                        break;
-                }
+            if (getVersion() >= DataStoreVersions.PHOSPHORUS_VERSION) {
+                readExternalModern(NormalizedNodeDataInput.newDataInput(in), size);
+            } else {
+                readExternalLegacy(in, size);
             }
         }
     }
@@ -118,15 +101,72 @@ public class MutableCompositeModification extends VersionedExternalizableMessage
     @Override
     public void writeExternal(final ObjectOutput out) throws IOException {
         super.writeExternal(out);
-
         final int size = modifications.size();
         out.writeInt(size);
         if (size > 0) {
-            try (NormalizedNodeDataOutput stream = getStreamVersion().newDataOutput(out)) {
-                for (Modification mod : modifications) {
-                    out.writeByte(mod.getType());
-                    mod.writeTo(stream);
-                }
+            if (getVersion() >= DataStoreVersions.PHOSPHORUS_VERSION) {
+                writeExternalModern(out);
+            } else {
+                writeExternalLegacy(out);
+            }
+        }
+    }
+
+    private void readExternalLegacy(final ObjectInput in, final int size) throws IOException {
+        final NormalizedNodeDataInput input = NormalizedNodeDataInput.newDataInputWithoutValidation(in);
+        final ReusableStreamReceiver receiver = ReusableImmutableNormalizedNodeStreamWriter.create();
+        for (int i = 0; i < size; i++) {
+            final byte type = in.readByte();
+            switch (type) {
+                case Modification.WRITE:
+                    modifications.add(WriteModification.fromStream(input, getVersion(), receiver));
+                    break;
+                case Modification.MERGE:
+                    modifications.add(MergeModification.fromStream(input, getVersion(), receiver));
+                    break;
+                case Modification.DELETE:
+                    modifications.add(DeleteModification.fromStream(input, getVersion()));
+                    break;
+                default:
+                    break;
+            }
+        }
+    }
+
+    private void writeExternalLegacy(final ObjectOutput out) throws IOException {
+        try (NormalizedNodeDataOutput stream = getStreamVersion().newDataOutput(out)) {
+            for (Modification mod : modifications) {
+                out.writeByte(mod.getType());
+                mod.writeTo(stream);
+            }
+        }
+    }
+
+    private void readExternalModern(final NormalizedNodeDataInput in, final int size) throws IOException {
+        final ReusableStreamReceiver receiver = ReusableImmutableNormalizedNodeStreamWriter.create();
+        for (int i = 0; i < size; i++) {
+            final byte type = in.readByte();
+            switch (type) {
+                case Modification.WRITE:
+                    modifications.add(WriteModification.fromStream(in, getVersion(), receiver));
+                    break;
+                case Modification.MERGE:
+                    modifications.add(MergeModification.fromStream(in, getVersion(), receiver));
+                    break;
+                case Modification.DELETE:
+                    modifications.add(DeleteModification.fromStream(in, getVersion()));
+                    break;
+                default:
+                    break;
+            }
+        }
+    }
+
+    private void writeExternalModern(final ObjectOutput out) throws IOException {
+        try (NormalizedNodeDataOutput stream = getStreamVersion().newDataOutput(out)) {
+            for (Modification mod : modifications) {
+                stream.writeByte(mod.getType());
+                mod.writeTo(stream);
             }
         }
     }
index c4fcaab0e4523d1abc60704367ef8088fd57ec83..35ee8fc5bb61077377c6d580efc30d18c75e50b3 100644 (file)
@@ -5,13 +5,10 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.controller.cluster.datastore.modification;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
 
-import java.util.Optional;
 import org.apache.commons.lang.SerializationUtils;
 import org.junit.Test;
 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
@@ -23,10 +20,8 @@ import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
 
 public class MutableCompositeModificationTest extends AbstractModificationTest {
-
     @Test
     public void testApply() throws Exception {
-
         MutableCompositeModification compositeModification = new MutableCompositeModification();
         compositeModification.addModification(new WriteModification(TestModel.TEST_PATH,
             ImmutableNodes.containerNode(TestModel.TEST_QNAME)));
@@ -35,10 +30,7 @@ public class MutableCompositeModificationTest extends AbstractModificationTest {
         compositeModification.apply(transaction);
         commitTransaction(transaction);
 
-        Optional<NormalizedNode> data = readData(TestModel.TEST_PATH);
-
-        assertNotNull(data.get());
-        assertEquals(TestModel.TEST_QNAME, data.get().getIdentifier().getNodeType());
+        assertEquals(TestModel.TEST_QNAME, readData(TestModel.TEST_PATH).get().getIdentifier().getNodeType());
     }
 
     @Test
@@ -54,13 +46,57 @@ public class MutableCompositeModificationTest extends AbstractModificationTest {
 
         YangInstanceIdentifier deletePath = TestModel.TEST_PATH;
 
-        MutableCompositeModification compositeModification = new MutableCompositeModification();
+        MutableCompositeModification compositeModification =
+            new MutableCompositeModification(DataStoreVersions.SODIUM_SR1_VERSION);
+        compositeModification.addModification(new WriteModification(writePath, writeData));
+        compositeModification.addModification(new MergeModification(mergePath, mergeData));
+        compositeModification.addModification(new DeleteModification(deletePath));
+
+        final byte[] bytes = SerializationUtils.serialize(compositeModification);
+        assertEquals(360, bytes.length);
+        MutableCompositeModification clone = (MutableCompositeModification) SerializationUtils.deserialize(bytes);
+
+        assertEquals("getVersion", DataStoreVersions.SODIUM_SR1_VERSION, clone.getVersion());
+
+        assertEquals("getModifications size", 3, clone.getModifications().size());
+
+        WriteModification write = (WriteModification)clone.getModifications().get(0);
+        assertEquals("getVersion", DataStoreVersions.SODIUM_SR1_VERSION, write.getVersion());
+        assertEquals("getPath", writePath, write.getPath());
+        assertEquals("getData", writeData, write.getData());
+
+        MergeModification merge = (MergeModification)clone.getModifications().get(1);
+        assertEquals("getVersion", DataStoreVersions.SODIUM_SR1_VERSION, merge.getVersion());
+        assertEquals("getPath", mergePath, merge.getPath());
+        assertEquals("getData", mergeData, merge.getData());
+
+        DeleteModification delete = (DeleteModification)clone.getModifications().get(2);
+        assertEquals("getVersion", DataStoreVersions.SODIUM_SR1_VERSION, delete.getVersion());
+        assertEquals("getPath", deletePath, delete.getPath());
+    }
+
+    @Test
+    public void testSerializationModern() {
+        YangInstanceIdentifier writePath = TestModel.TEST_PATH;
+        NormalizedNode writeData = ImmutableContainerNodeBuilder.create()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
+                .withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
+
+        YangInstanceIdentifier mergePath = TestModel.OUTER_LIST_PATH;
+        NormalizedNode mergeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
+                new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME)).build();
+
+        YangInstanceIdentifier deletePath = TestModel.TEST_PATH;
+
+        MutableCompositeModification compositeModification =
+            new MutableCompositeModification();
         compositeModification.addModification(new WriteModification(writePath, writeData));
         compositeModification.addModification(new MergeModification(mergePath, mergeData));
         compositeModification.addModification(new DeleteModification(deletePath));
 
-        MutableCompositeModification clone = (MutableCompositeModification)
-                SerializationUtils.clone(compositeModification);
+        final byte[] bytes = SerializationUtils.serialize(compositeModification);
+        assertEquals(360, bytes.length);
+        MutableCompositeModification clone = (MutableCompositeModification) SerializationUtils.deserialize(bytes);
 
         assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, clone.getVersion());