BUG-2138: Make DistributedShardFactory return Futures.
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / utils / ClusterUtils.java
index dba17611300e56898094fb0cf5a8b1342f907da5..607e78c9d4e2a857651adca5e9dac4888c4a884a 100644 (file)
@@ -8,19 +8,45 @@
 
 package org.opendaylight.controller.cluster.datastore.utils;
 
+import akka.cluster.ddata.Key;
+import akka.cluster.ddata.ORMap;
+import akka.cluster.ddata.ORMapKey;
+import java.util.Map;
 import org.opendaylight.controller.cluster.access.concepts.MemberName;
+import org.opendaylight.controller.cluster.datastore.config.PrefixShardConfiguration;
 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 
 /**
  * Utils for encoding prefix shard name.
  */
 public class ClusterUtils {
 
+    // key for replicated configuration key
+    public static final Key<ORMap<PrefixShardConfiguration>> CONFIGURATION_KEY =
+            ORMapKey.create("prefix-shard-configuration-config");
+
+    public static final Key<ORMap<PrefixShardConfiguration>> OPERATIONAL_KEY =
+            ORMapKey.create("prefix-shard-configuration-oper");
+
     public static ShardIdentifier getShardIdentifier(final MemberName memberName, final DOMDataTreeIdentifier prefix) {
-        return ShardIdentifier
-                .create(getCleanShardName(prefix.getRootIdentifier()), memberName, prefix.getDatastoreType().name());
+        final String type;
+        switch (prefix.getDatastoreType()) {
+            case OPERATIONAL:
+                type = "operational";
+                break;
+            case CONFIGURATION:
+                type = "config";
+                break;
+            default:
+                type = prefix.getDatastoreType().name();
+        }
+
+        return ShardIdentifier.create(getCleanShardName(prefix.getRootIdentifier()), memberName, type);
     }
 
     /**
@@ -31,12 +57,40 @@ public class ClusterUtils {
      * @return encoded name that doesn't contain characters that cannot be in actor path.
      */
     public static String getCleanShardName(final YangInstanceIdentifier path) {
+        if (path.isEmpty()) {
+            return "default";
+        }
+
         final StringBuilder builder = new StringBuilder();
         // TODO need a better mapping that includes namespace, but we'll need to cleanup the string beforehand
+        // we have to fight both javax and akka url path restrictions..
         path.getPathArguments().forEach(p -> {
             builder.append(p.getNodeType().getLocalName());
+            if (p instanceof NodeIdentifierWithPredicates) {
+                builder.append("-key_");
+                final Map<QName, Object> key = ((NodeIdentifierWithPredicates) p).getKeyValues();
+                key.entrySet().forEach(e -> {
+                    builder.append(e.getKey().getLocalName());
+                    builder.append(e.getValue());
+                    builder.append("-");
+                });
+                builder.append("_");
+            }
             builder.append("!");
         });
         return builder.toString();
     }
+
+    public static Key<ORMap<PrefixShardConfiguration>> getReplicatorKey(LogicalDatastoreType type) {
+        if (LogicalDatastoreType.CONFIGURATION.equals(type)) {
+            return CONFIGURATION_KEY;
+        } else {
+            return OPERATIONAL_KEY;
+        }
+    }
+
+    public static org.opendaylight.mdsal.common.api.LogicalDatastoreType toMDSalApi(
+            final LogicalDatastoreType logicalDatastoreType) {
+        return org.opendaylight.mdsal.common.api.LogicalDatastoreType.valueOf(logicalDatastoreType.name());
+    }
 }