ReadData message using protocol buffer 15/8815/6
authorBasheeruddin Ahmed <syedbahm@cisco.com>
Tue, 8 Jul 2014 18:40:40 +0000 (11:40 -0700)
committerMoiz Raja <moraja@cisco.com>
Mon, 28 Jul 2014 20:56:29 +0000 (13:56 -0700)
and InstanceIdentifierUtils

Change-Id: I57100033200fc38d69ab6aa04841e8bc7546839c
Signed-off-by: Basheeruddin Ahmed <syedbahm@cisco.com>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/ReadData.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/InstanceIdentifierUtils.java [new file with mode: 0644]

index 2f56a9740b2d4872a116dbccc31ceeb33e6425f1..f3d623e17b27b7d3ff7e3691b4fff0b29966e33f 100644 (file)
@@ -8,9 +8,12 @@
 
 package org.opendaylight.controller.cluster.datastore.messages;
 
+import org.opendaylight.controller.cluster.datastore.utils.InstanceIdentifierUtils;
+import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
 
 public class ReadData {
+  public static final Class SERIALIZABLE_CLASS = ShardTransactionMessages.ReadData.class;
   private final InstanceIdentifier path;
 
   public ReadData(InstanceIdentifier path) {
@@ -20,4 +23,15 @@ public class ReadData {
   public InstanceIdentifier getPath() {
     return path;
   }
+
+  public Object toSerializable(){
+    return ShardTransactionMessages.ReadData.newBuilder()
+        .setInstanceIdentifierPathArguments(InstanceIdentifierUtils.getParentPath(path.toString()))
+        .build();
+  }
+
+  public static ReadData fromSerializable(Object serializable){
+    ShardTransactionMessages.ReadData o = (ShardTransactionMessages.ReadData) serializable;
+    return new ReadData(InstanceIdentifierUtils.from(o.getInstanceIdentifierPathArguments()));
+  }
 }
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/InstanceIdentifierUtils.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/InstanceIdentifierUtils.java
new file mode 100644 (file)
index 0000000..fafeba5
--- /dev/null
@@ -0,0 +1,42 @@
+package org.opendaylight.controller.cluster.datastore.utils;
+
+import org.opendaylight.controller.cluster.datastore.node.utils.NodeIdentifierFactory;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author: syedbahm
+ */
+public class InstanceIdentifierUtils {
+  public static String getParentPath(String currentElementPath) {
+    String parentPath = "";
+
+    if (currentElementPath != null) {
+      String[] parentPaths = currentElementPath.split("/");
+      if (parentPaths.length > 2) {
+        for (int i = 0; i < parentPaths.length - 1; i++) {
+          if (parentPaths[i].length() > 0) {
+            parentPath += "/" + parentPaths[i];
+          }
+        }
+      }
+    }
+    return parentPath;
+  }
+
+  public static InstanceIdentifier from(String path) {
+    String[] ids = path.split("/");
+
+    List<InstanceIdentifier.PathArgument> pathArguments = new ArrayList<>();
+    for (String nodeId : ids) {
+      if (!"".equals(nodeId)) {
+        pathArguments.add(NodeIdentifierFactory.getArgument(nodeId));
+      }
+    }
+    final InstanceIdentifier instanceIdentifier =
+        new InstanceIdentifier(pathArguments);
+    return instanceIdentifier;
+  }
+}