blob: 1601dadaeafccdc7599cc6100a238efdb4c4d069 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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) {
55 if (other == null) {
56 return false;
57 }
58 if (!(other instanceof RoleInfo)) {
59 return false;
60 }
61 RoleInfo that = (RoleInfo) other;
62 if (!Objects.equals(this.master, that.master)) {
63 return false;
64 }
65 if (!Objects.equals(this.backups, that.backups)) {
66 return false;
67 }
68 return true;
69 }
70
71 @Override
72 public int hashCode() {
Yuta HIGUCHI4b0ca2c2014-10-30 01:17:36 -070073 return Objects.hash(master, backups);
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070074 }
75
76 @Override
77 public String toString() {
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070078 return MoreObjects.toStringHelper(this.getClass())
Ayaka Koshibea48f7522015-04-01 17:06:09 -070079 .add("master", master.orElseGet(() -> null))
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070080 .add("backups", backups)
81 .toString();
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070082 }
83}