blob: 11b56c14af6919ecd40cae02e66bd95a34bb5bfc [file] [log] [blame]
Madan Jampani0cb00672015-02-27 00:27:22 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16package org.onosproject.store.consistent.impl;
17
Thomas Vachuskade563cf2015-04-01 00:28:50 -070018import com.google.common.collect.ImmutableMap;
19import com.google.common.collect.ImmutableSet;
20import com.google.common.collect.Maps;
21import org.onosproject.store.cluster.impl.NodeInfo;
22
23import java.util.ArrayList;
24import java.util.Collections;
25import java.util.HashSet;
26import java.util.List;
Madan Jampani0cb00672015-02-27 00:27:22 -080027import java.util.Map;
28import java.util.Set;
29
Madan Jampani0cb00672015-02-27 00:27:22 -080030import static com.google.common.base.Preconditions.checkNotNull;
31
Madan Jampani0cb00672015-02-27 00:27:22 -080032/**
33 * Partitioned database configuration.
34 */
35public class DatabaseDefinition {
36 private Map<String, Set<NodeInfo>> partitions;
37 private Set<NodeInfo> nodes;
38
39 /**
40 * Creates a new DatabaseDefinition.
Thomas Vachuskade563cf2015-04-01 00:28:50 -070041 *
Madan Jampani0cb00672015-02-27 00:27:22 -080042 * @param partitions partition map
Thomas Vachuskade563cf2015-04-01 00:28:50 -070043 * @param nodes set of nodes
Madan Jampani0cb00672015-02-27 00:27:22 -080044 * @return database definition
45 */
Thomas Vachuskade563cf2015-04-01 00:28:50 -070046 public static DatabaseDefinition from(Map<String, Set<NodeInfo>> partitions,
47 Set<NodeInfo> nodes) {
Madan Jampani0cb00672015-02-27 00:27:22 -080048 checkNotNull(partitions);
49 checkNotNull(nodes);
50 DatabaseDefinition definition = new DatabaseDefinition();
51 definition.partitions = ImmutableMap.copyOf(partitions);
52 definition.nodes = ImmutableSet.copyOf(nodes);
53 return definition;
54 }
55
56 /**
Thomas Vachuskade563cf2015-04-01 00:28:50 -070057 * Creates a new DatabaseDefinition using default partitions.
58 *
59 * @param nodes set of nodes
60 * @return database definition
61 */
62 public static DatabaseDefinition from(Set<NodeInfo> nodes) {
63 return from(generateDefaultPartitions(nodes), nodes);
64 }
65
66 /**
Madan Jampani0cb00672015-02-27 00:27:22 -080067 * Returns the map of database partitions.
Thomas Vachuskade563cf2015-04-01 00:28:50 -070068 *
Madan Jampani0cb00672015-02-27 00:27:22 -080069 * @return db partition map
70 */
71 public Map<String, Set<NodeInfo>> getPartitions() {
72 return partitions;
73 }
74
75 /**
76 * Returns the set of nodes.
Thomas Vachuskade563cf2015-04-01 00:28:50 -070077 *
Madan Jampani0cb00672015-02-27 00:27:22 -080078 * @return nodes
79 */
80 public Set<NodeInfo> getNodes() {
81 return nodes;
82 }
Thomas Vachuskade563cf2015-04-01 00:28:50 -070083
84
85 /**
86 * Generates set of default partitions using permutations of the nodes.
87 *
88 * @param nodes information about cluster nodes
89 * @return default partition map
90 */
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();
95
96 int length = nodes.size();
97 int count = 3;
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));
102 }
103 partitions.put("p" + (i + 1), set);
104 }
105 return partitions;
106 }
107
Madan Jampani0cb00672015-02-27 00:27:22 -0800108}