Make LogicalDatastoreType a WritableObject 23/82923/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 3 Jul 2019 22:37:53 +0000 (00:37 +0200)
committerRobert Varga <nite@hq.sk>
Wed, 3 Jul 2019 23:55:05 +0000 (23:55 +0000)
This is a low-cardinality enum, which we need to serialize quite
often. Make it a simple WritableObject, so users can get common
handling. One specific user is sal-remoterpc-connector, which
needs to be able to serialize DOMDataTreeIdentifier.

JIRA: CONTROLLER-1894
Change-Id: Iee4610e093639629039a2e21ab1cfc852b756169
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit b0c200e4da8fced37726872197c510766a7ed4fd)

common/mdsal-common-api/src/main/java/org/opendaylight/mdsal/common/api/LogicalDatastoreType.java

index 31016717bcf23e263d19421e390ed41459910edb..928b27d30b1813400f7ece6080ee14d4c41e155e 100644 (file)
@@ -7,12 +7,18 @@
  */
 package org.opendaylight.mdsal.common.api;
 
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.concepts.WritableObject;
+
 /**
  * The concept of a logical data store, similar to RFC8342.
  */
 // FIXME: 3.0.0: turn this into an interface so it can be externally-defined?
 // FIXME: 3.0.0: note that mount points can have different types and policies, which can potentially be mapped
-public enum LogicalDatastoreType {
+public enum LogicalDatastoreType implements WritableObject {
     /**
      * Logical datastore representing operational state of the system and it's components. This datastore is used
      * to describe operational state of the system and it's operation related data.
@@ -26,7 +32,7 @@ public enum LogicalDatastoreType {
      *   </li>
      * </ul>
      */
-    OPERATIONAL,
+    OPERATIONAL(1),
     /**
      * Logical Datastore representing configuration state of the system and it's components. This datastore is used
      * to describe intended state of the system and intended operation mode.
@@ -43,5 +49,28 @@ public enum LogicalDatastoreType {
      *   </li>
      * </ul>
      */
-    CONFIGURATION
+    CONFIGURATION(2);
+
+    private int serialized;
+
+    LogicalDatastoreType(final int serialized) {
+        this.serialized = serialized;
+    }
+
+    @Override
+    public void writeTo(final DataOutput out) throws IOException {
+        out.writeByte(serialized);
+    }
+
+    public static @NonNull LogicalDatastoreType readFrom(final DataInput in) throws IOException {
+        final byte serialized = in.readByte();
+        switch (serialized) {
+            case 1:
+                return OPERATIONAL;
+            case 2:
+                return CONFIGURATION;
+            default:
+                throw new IOException("Unknown type " + serialized);
+        }
+    }
 }