607e78c9d4e2a857651adca5e9dac4888c4a884a
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / utils / ClusterUtils.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.datastore.utils;
10
11 import akka.cluster.ddata.Key;
12 import akka.cluster.ddata.ORMap;
13 import akka.cluster.ddata.ORMapKey;
14 import java.util.Map;
15 import org.opendaylight.controller.cluster.access.concepts.MemberName;
16 import org.opendaylight.controller.cluster.datastore.config.PrefixShardConfiguration;
17 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
23
24 /**
25  * Utils for encoding prefix shard name.
26  */
27 public class ClusterUtils {
28
29     // key for replicated configuration key
30     public static final Key<ORMap<PrefixShardConfiguration>> CONFIGURATION_KEY =
31             ORMapKey.create("prefix-shard-configuration-config");
32
33     public static final Key<ORMap<PrefixShardConfiguration>> OPERATIONAL_KEY =
34             ORMapKey.create("prefix-shard-configuration-oper");
35
36     public static ShardIdentifier getShardIdentifier(final MemberName memberName, final DOMDataTreeIdentifier prefix) {
37         final String type;
38         switch (prefix.getDatastoreType()) {
39             case OPERATIONAL:
40                 type = "operational";
41                 break;
42             case CONFIGURATION:
43                 type = "config";
44                 break;
45             default:
46                 type = prefix.getDatastoreType().name();
47         }
48
49         return ShardIdentifier.create(getCleanShardName(prefix.getRootIdentifier()), memberName, type);
50     }
51
52     /**
53      * Returns an encoded shard name based on the provided path that should doesn't contain characters that cannot be
54      * present in akka actor paths.
55      *
56      * @param path Path on which to base the shard name
57      * @return encoded name that doesn't contain characters that cannot be in actor path.
58      */
59     public static String getCleanShardName(final YangInstanceIdentifier path) {
60         if (path.isEmpty()) {
61             return "default";
62         }
63
64         final StringBuilder builder = new StringBuilder();
65         // TODO need a better mapping that includes namespace, but we'll need to cleanup the string beforehand
66         // we have to fight both javax and akka url path restrictions..
67         path.getPathArguments().forEach(p -> {
68             builder.append(p.getNodeType().getLocalName());
69             if (p instanceof NodeIdentifierWithPredicates) {
70                 builder.append("-key_");
71                 final Map<QName, Object> key = ((NodeIdentifierWithPredicates) p).getKeyValues();
72                 key.entrySet().forEach(e -> {
73                     builder.append(e.getKey().getLocalName());
74                     builder.append(e.getValue());
75                     builder.append("-");
76                 });
77                 builder.append("_");
78             }
79             builder.append("!");
80         });
81         return builder.toString();
82     }
83
84     public static Key<ORMap<PrefixShardConfiguration>> getReplicatorKey(LogicalDatastoreType type) {
85         if (LogicalDatastoreType.CONFIGURATION.equals(type)) {
86             return CONFIGURATION_KEY;
87         } else {
88             return OPERATIONAL_KEY;
89         }
90     }
91
92     public static org.opendaylight.mdsal.common.api.LogicalDatastoreType toMDSalApi(
93             final LogicalDatastoreType logicalDatastoreType) {
94         return org.opendaylight.mdsal.common.api.LogicalDatastoreType.valueOf(logicalDatastoreType.name());
95     }
96 }