blob: 38b811eff4e749d1f4afe6f455e0d6e605e0a037 [file] [log] [blame]
Madan Jampanie14a09c2016-02-11 10:43:21 -08001/*
2 * Copyright 2016 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.primitives.impl;
17
18import io.atomix.copycat.server.cluster.Member;
19
20import java.util.Collection;
21import java.util.Set;
22
23import org.onosproject.cluster.PartitionId;
24import org.onosproject.store.service.PartitionInfo;
25
26import com.google.common.base.MoreObjects;
27import com.google.common.collect.ImmutableList;
28import com.google.common.collect.ImmutableSet;
29import com.google.common.collect.Lists;
30
31/**
32 * Operational details for a {@code StoragePartition}.
33 */
34public class StoragePartitionDetails {
35
36 private final PartitionId partitionId;
37 private final Set<Member> activeMembers;
38 private final Set<Member> configuredMembers;
39 private final Member leader;
40 private final long leaderTerm;
41
42 public StoragePartitionDetails(PartitionId partitionId,
43 Collection<Member> activeMembers,
44 Collection<Member> configuredMembers,
45 Member leader,
46 long leaderTerm) {
47 this.partitionId = partitionId;
48 this.activeMembers = ImmutableSet.copyOf(activeMembers);
49 this.configuredMembers = ImmutableSet.copyOf(configuredMembers);
50 this.leader = leader;
51 this.leaderTerm = leaderTerm;
52 }
53
54 /**
55 * Returns the set of active members.
56 * @return active members
57 */
58 public Set<Member> activeMembers() {
59 return activeMembers;
60 }
61
62 /**
63 * Returns the set of configured members.
64 * @return configured members
65 */
66 public Set<Member> configuredMembers() {
67 return configuredMembers;
68 }
69
70 /**
71 * Returns the partition leader.
72 * @return leader
73 */
74 public Member leader() {
75 return leader;
76 }
77
78 /**
79 * Returns the partition leader term.
80 * @return leader term
81 */
82 public long leaderTerm() {
83 return leaderTerm;
84 }
85
86 @Override
87 public String toString() {
88 return MoreObjects.toStringHelper(getClass())
89 .add("activeMembers", activeMembers)
90 .add("configuredMembers", configuredMembers)
91 .add("leader", leader)
92 .add("leaderTerm", leaderTerm)
93 .toString();
94 }
95
96 /**
97 * Returns the details as an instance of {@code PartitionInfo}.
98 * @return partition info
99 */
100 public PartitionInfo toPartitionInfo() {
101 return new PartitionInfo(partitionId.toString(),
102 leaderTerm,
103 Lists.transform(ImmutableList.copyOf(activeMembers), m -> m.address().toString()),
104 leader == null ? "none" : leader.address().toString());
105 }
106}