blob: 0fa75294525a034e8cfb7c47664d6f84d5b9c16e [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cluster;
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070017
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070018import java.util.List;
19import java.util.Objects;
Ayaka Koshibea48f7522015-04-01 17:06:09 -070020import java.util.Optional;
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070021
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070022import com.google.common.base.MoreObjects;
Yuta HIGUCHI40d01772014-10-21 00:08:44 -070023import com.google.common.collect.ImmutableList;
24
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070025/**
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070026 * An immutable container for role information for a device,
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070027 * within the current cluster. Role attributes include current
28 * master and a preference-ordered list of backup nodes.
29 */
30public class RoleInfo {
Ayaka Koshibea48f7522015-04-01 17:06:09 -070031 private final Optional<NodeId> master;
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070032 private final List<NodeId> backups;
33
34 public RoleInfo(NodeId master, List<NodeId> backups) {
Ayaka Koshibea48f7522015-04-01 17:06:09 -070035 this.master = Optional.ofNullable(master);
Yuta HIGUCHI40d01772014-10-21 00:08:44 -070036 this.backups = ImmutableList.copyOf(backups);
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070037 }
38
Yuta HIGUCHI4b0ca2c2014-10-30 01:17:36 -070039 public RoleInfo() {
Ayaka Koshibea48f7522015-04-01 17:06:09 -070040 this.master = Optional.empty();
Yuta HIGUCHI4b0ca2c2014-10-30 01:17:36 -070041 this.backups = ImmutableList.of();
42 }
43
Ayaka Koshibea48f7522015-04-01 17:06:09 -070044 // This will return a Optional<NodeId> in the future.
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070045 public NodeId master() {
Ayaka Koshibea48f7522015-04-01 17:06:09 -070046 return master.orElseGet(() -> null);
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070047 }
48
49 public List<NodeId> backups() {
Ayaka Koshibeadb2d3c2014-10-20 23:39:51 -070050 return backups;
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070051 }
52
53 @Override
54 public boolean equals(Object other) {
Sho SHIMIZU5bfd4e62015-09-09 14:32:17 -070055 if (this == other) {
56 return true;
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070057 }
Sho SHIMIZU5bfd4e62015-09-09 14:32:17 -070058
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070059 if (!(other instanceof RoleInfo)) {
60 return false;
61 }
62 RoleInfo that = (RoleInfo) other;
Sho SHIMIZU5bfd4e62015-09-09 14:32:17 -070063
64 return Objects.equals(this.master, that.master)
65 && Objects.equals(this.backups, that.backups);
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070066 }
67
68 @Override
69 public int hashCode() {
Yuta HIGUCHI4b0ca2c2014-10-30 01:17:36 -070070 return Objects.hash(master, backups);
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070071 }
72
73 @Override
74 public String toString() {
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070075 return MoreObjects.toStringHelper(this.getClass())
Ayaka Koshibea48f7522015-04-01 17:06:09 -070076 .add("master", master.orElseGet(() -> null))
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070077 .add("backups", backups)
78 .toString();
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070079 }
80}