Migrate NodeIdentifierWithPredicates.getKeyValues()
[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 package org.opendaylight.controller.cluster.datastore.utils;
9
10 import org.opendaylight.controller.cluster.access.concepts.MemberName;
11 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
12 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Utils for encoding prefix shard name.
21  */
22 public final class ClusterUtils {
23     private static final Logger LOG = LoggerFactory.getLogger(ClusterUtils.class);
24
25     // id for the shard used to store prefix configuration
26     public static final String PREFIX_CONFIG_SHARD_ID = "prefix-configuration-shard";
27
28     public static final QName PREFIX_SHARDS_QNAME =
29             QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:clustering:prefix-shard-configuration",
30                     "2017-01-10", "prefix-shards").intern();
31     public static final QName SHARD_LIST_QNAME =
32             QName.create(PREFIX_SHARDS_QNAME, "shard").intern();
33     public static final QName SHARD_PREFIX_QNAME =
34             QName.create(PREFIX_SHARDS_QNAME, "prefix").intern();
35     public static final QName SHARD_REPLICAS_QNAME =
36             QName.create(PREFIX_SHARDS_QNAME, "replicas").intern();
37     public static final QName SHARD_REPLICA_QNAME =
38             QName.create(PREFIX_SHARDS_QNAME, "replica").intern();
39
40     public static final YangInstanceIdentifier PREFIX_SHARDS_PATH =
41             YangInstanceIdentifier.of(PREFIX_SHARDS_QNAME).toOptimized();
42     public static final YangInstanceIdentifier SHARD_LIST_PATH =
43             PREFIX_SHARDS_PATH.node(SHARD_LIST_QNAME).toOptimized();
44
45     private ClusterUtils() {
46     }
47
48     public static ShardIdentifier getShardIdentifier(final MemberName memberName, final DOMDataTreeIdentifier prefix) {
49         final String type;
50         switch (prefix.getDatastoreType()) {
51             case OPERATIONAL:
52                 type = "operational";
53                 break;
54             case CONFIGURATION:
55                 type = "config";
56                 break;
57             default:
58                 type = prefix.getDatastoreType().name();
59                 LOG.warn("Unknown data store type {}", type);
60         }
61
62         return ShardIdentifier.create(getCleanShardName(prefix.getRootIdentifier()), memberName, type);
63     }
64
65     /**
66      * Returns an encoded shard name based on the provided path that should doesn't contain characters that cannot be
67      * present in akka actor paths.
68      *
69      * @param path Path on which to base the shard name
70      * @return encoded name that doesn't contain characters that cannot be in actor path.
71      */
72     public static String getCleanShardName(final YangInstanceIdentifier path) {
73         if (path.isEmpty()) {
74             return "default";
75         }
76
77         final StringBuilder builder = new StringBuilder();
78         // TODO need a better mapping that includes namespace, but we'll need to cleanup the string beforehand
79         // we have to fight both javax and akka url path restrictions..
80         path.getPathArguments().forEach(p -> {
81             builder.append(p.getNodeType().getLocalName());
82             if (p instanceof NodeIdentifierWithPredicates) {
83                 builder.append("-key_");
84                 ((NodeIdentifierWithPredicates) p).entrySet().forEach(entry -> {
85                     builder.append(entry.getKey().getLocalName()).append(entry.getValue()).append('-');
86                 });
87                 builder.append('_');
88             }
89             builder.append('!');
90         });
91         return builder.toString();
92     }
93 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.