Further evolution of these two classes will require allowing for two
distinct specializations.
In order to be able to do that, we need to disconnect them from their
serial form.
This patch instroduces serialization proxies, which know how to write
out and read in these objects without referencing their class name.
JIRA: MDSAL-845
Change-Id: I2b50d3aa5f752d6181dab991a5a8282b079b16df
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit
9286037f2852d197c4384511fce97c551e2b8dc3)
(cherry picked from commit
9e23304d266d00210f1aeec57a99a1d8694caffe)
--- /dev/null
+/*
+ * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.mdsal.binding.api;
+
+import static com.google.common.base.Verify.verifyNotNull;
+import static java.util.Objects.requireNonNull;
+
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+/**
+ * A serialization proxy for {@link DataTreeIdentifier}.
+ */
+final class DTIv1 implements Externalizable {
+ @java.io.Serial
+ private static final long serialVersionUID = 1L;
+
+ private DataTreeIdentifier<?> id;
+
+ @SuppressWarnings("redundantModifier")
+ public DTIv1() {
+ // For Externalizable
+ }
+
+ DTIv1(final DataTreeIdentifier<?> id) {
+ this.id = requireNonNull(id);
+ }
+
+ @Override
+ public void writeExternal(final ObjectOutput out) throws IOException {
+ id.getDatastoreType().writeTo(out);
+ out.writeObject(id.getRootIdentifier());
+ }
+
+ @Override
+ public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
+ id = DataTreeIdentifier.create(LogicalDatastoreType.readFrom(in), (InstanceIdentifier<?>) in.readObject());
+ }
+
+ @java.io.Serial
+ Object readResolve() {
+ return verifyNotNull(id);
+ }
+}
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import org.junit.Test;
import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
import org.opendaylight.yangtools.yang.binding.ChildOf;
assertFalse("Different object", TEST_IDENTIFIER1.equals(new Object()));
}
+ @Test
+ public void serializationTest() throws Exception {
+ final var bos = new ByteArrayOutputStream();
+ try (var oos = new ObjectOutputStream(bos)) {
+ oos.writeObject(TEST_IDENTIFIER1);
+ }
+
+ final var bytes = bos.toByteArray();
+ assertEquals(728, bytes.length);
+
+ try (var ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
+ assertEquals(TEST_IDENTIFIER1, ois.readObject());
+ }
+ }
+
private interface TestDataObject1 extends ChildOf<DataRoot> {
@Override
default Class<? extends DataObject> implementedInterface() {
--- /dev/null
+/*
+ * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.mdsal.dom.api;
+
+import static com.google.common.base.Verify.verifyNotNull;
+import static java.util.Objects.requireNonNull;
+
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+
+/**
+ * A serialization proxy for {@link DOMDataTreeIdentifier}.
+ */
+final class DTIv1 implements Externalizable {
+ @java.io.Serial
+ private static final long serialVersionUID = 1L;
+
+ private DOMDataTreeIdentifier id;
+
+ @SuppressWarnings("redundantModifier")
+ public DTIv1() {
+ // For Externalizable
+ }
+
+ DTIv1(final DOMDataTreeIdentifier id) {
+ this.id = requireNonNull(id);
+ }
+
+ @Override
+ public void writeExternal(final ObjectOutput out) throws IOException {
+ id.getDatastoreType().writeTo(out);
+ out.writeObject(id.getRootIdentifier());
+ }
+
+ @Override
+ public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
+ id = new DOMDataTreeIdentifier(LogicalDatastoreType.readFrom(in), (YangInstanceIdentifier) in.readObject());
+ }
+
+ @java.io.Serial
+ Object readResolve() {
+ return verifyNotNull(id);
+ }
+}
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import org.junit.Test;
import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
import org.opendaylight.yangtools.yang.common.QName;
assertTrue("ToString", REF_TREE.toString().contains(REF_TREE.getRootIdentifier().toString()) && REF_TREE
.toString().contains(REF_TREE.getDatastoreType().toString()));
}
+
+ @Test
+ public void serializationTest() throws Exception {
+ final var bos = new ByteArrayOutputStream();
+ try (var oos = new ObjectOutputStream(bos)) {
+ oos.writeObject(REF_TREE);
+ }
+
+ final var bytes = bos.toByteArray();
+ assertEquals(561, bytes.length);
+
+ try (var ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
+ assertEquals(REF_TREE, ois.readObject());
+ }
+ }
}