2 * Copyright 2015 Open Networking Laboratory
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onosproject.store.consistent.impl;
18 import com.google.common.collect.ImmutableMap;
19 import com.google.common.collect.ImmutableSet;
20 import com.google.common.collect.Maps;
21 import org.onosproject.store.cluster.impl.NodeInfo;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.List;
30 import static com.google.common.base.Preconditions.checkNotNull;
33 * Partitioned database configuration.
35 public class DatabaseDefinition {
36 private Map<String, Set<NodeInfo>> partitions;
37 private Set<NodeInfo> nodes;
40 * Creates a new DatabaseDefinition.
42 * @param partitions partition map
43 * @param nodes set of nodes
44 * @return database definition
46 public static DatabaseDefinition from(Map<String, Set<NodeInfo>> partitions,
47 Set<NodeInfo> nodes) {
48 checkNotNull(partitions);
50 DatabaseDefinition definition = new DatabaseDefinition();
51 definition.partitions = ImmutableMap.copyOf(partitions);
52 definition.nodes = ImmutableSet.copyOf(nodes);
57 * Creates a new DatabaseDefinition using default partitions.
59 * @param nodes set of nodes
60 * @return database definition
62 public static DatabaseDefinition from(Set<NodeInfo> nodes) {
63 return from(generateDefaultPartitions(nodes), nodes);
67 * Returns the map of database partitions.
69 * @return db partition map
71 public Map<String, Set<NodeInfo>> getPartitions() {
76 * Returns the set of nodes.
80 public Set<NodeInfo> getNodes() {
86 * Generates set of default partitions using permutations of the nodes.
88 * @param nodes information about cluster nodes
89 * @return default partition map
91 private static Map<String, Set<NodeInfo>> generateDefaultPartitions(Set<NodeInfo> nodes) {
92 List<NodeInfo> sorted = new ArrayList<>(nodes);
93 Collections.sort(sorted, (o1, o2) -> o1.getId().compareTo(o2.getId()));
94 Map<String, Set<NodeInfo>> partitions = Maps.newHashMap();
96 int length = nodes.size();
98 for (int i = 0; i < length; i++) {
99 Set<NodeInfo> set = new HashSet<>(count);
100 for (int j = 0; j < count; j++) {
101 set.add(sorted.get((i + j) % length));
103 partitions.put("p" + (i + 1), set);