Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cluster; |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 17 | |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 18 | import java.util.List; |
| 19 | import java.util.Objects; |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 20 | import java.util.Optional; |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 21 | |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 22 | import com.google.common.base.MoreObjects; |
Yuta HIGUCHI | 40d0177 | 2014-10-21 00:08:44 -0700 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableList; |
| 24 | |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 25 | /** |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 26 | * An immutable container for role information for a device, |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 27 | * within the current cluster. Role attributes include current |
| 28 | * master and a preference-ordered list of backup nodes. |
| 29 | */ |
| 30 | public class RoleInfo { |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 31 | private final Optional<NodeId> master; |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 32 | private final List<NodeId> backups; |
| 33 | |
| 34 | public RoleInfo(NodeId master, List<NodeId> backups) { |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 35 | this.master = Optional.ofNullable(master); |
Yuta HIGUCHI | 40d0177 | 2014-10-21 00:08:44 -0700 | [diff] [blame] | 36 | this.backups = ImmutableList.copyOf(backups); |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Yuta HIGUCHI | 4b0ca2c | 2014-10-30 01:17:36 -0700 | [diff] [blame] | 39 | public RoleInfo() { |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 40 | this.master = Optional.empty(); |
Yuta HIGUCHI | 4b0ca2c | 2014-10-30 01:17:36 -0700 | [diff] [blame] | 41 | this.backups = ImmutableList.of(); |
| 42 | } |
| 43 | |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 44 | // This will return a Optional<NodeId> in the future. |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 45 | public NodeId master() { |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 46 | return master.orElseGet(() -> null); |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | public List<NodeId> backups() { |
Ayaka Koshibe | adb2d3c | 2014-10-20 23:39:51 -0700 | [diff] [blame] | 50 | return backups; |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public boolean equals(Object other) { |
Sho SHIMIZU | 5bfd4e6 | 2015-09-09 14:32:17 -0700 | [diff] [blame] | 55 | if (this == other) { |
| 56 | return true; |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 57 | } |
Sho SHIMIZU | 5bfd4e6 | 2015-09-09 14:32:17 -0700 | [diff] [blame] | 58 | |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 59 | if (!(other instanceof RoleInfo)) { |
| 60 | return false; |
| 61 | } |
| 62 | RoleInfo that = (RoleInfo) other; |
Sho SHIMIZU | 5bfd4e6 | 2015-09-09 14:32:17 -0700 | [diff] [blame] | 63 | |
| 64 | return Objects.equals(this.master, that.master) |
| 65 | && Objects.equals(this.backups, that.backups); |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public int hashCode() { |
Yuta HIGUCHI | 4b0ca2c | 2014-10-30 01:17:36 -0700 | [diff] [blame] | 70 | return Objects.hash(master, backups); |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public String toString() { |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 75 | return MoreObjects.toStringHelper(this.getClass()) |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 76 | .add("master", master.orElseGet(() -> null)) |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 77 | .add("backups", backups) |
| 78 | .toString(); |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 79 | } |
| 80 | } |