Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | */ |
| 16 | package org.onosproject.mastership; |
| 17 | |
| 18 | import java.util.List; |
| 19 | import java.util.Map; |
| 20 | import java.util.Objects; |
| 21 | import java.util.Optional; |
| 22 | import java.util.stream.Collectors; |
| 23 | |
| 24 | import com.google.common.collect.ImmutableList; |
| 25 | import com.google.common.collect.ImmutableMap; |
| 26 | import org.onosproject.cluster.NodeId; |
| 27 | import org.onosproject.net.MastershipRole; |
| 28 | |
| 29 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 30 | |
| 31 | /** |
| 32 | * Mastership info. |
| 33 | */ |
| 34 | public final class MastershipInfo { |
| 35 | private final long term; |
| 36 | private final Optional<NodeId> master; |
| 37 | private final ImmutableMap<NodeId, MastershipRole> roles; |
| 38 | |
| 39 | public MastershipInfo() { |
| 40 | this(0, Optional.empty(), ImmutableMap.of()); |
| 41 | } |
| 42 | |
| 43 | public MastershipInfo(long term, Optional<NodeId> master, ImmutableMap<NodeId, MastershipRole> roles) { |
| 44 | this.term = term; |
| 45 | this.master = master; |
| 46 | this.roles = roles; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns the mastership term. |
| 51 | * |
| 52 | * @return the mastership term |
| 53 | */ |
| 54 | public long term() { |
| 55 | return term; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns the current master. |
| 60 | * |
| 61 | * @return the current master |
| 62 | */ |
| 63 | public Optional<NodeId> master() { |
| 64 | return master; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns a sorted list of standby nodes. |
| 69 | * |
| 70 | * @return a sorted list of standby nodes |
| 71 | */ |
| 72 | public List<NodeId> backups() { |
| 73 | return getRoles(MastershipRole.STANDBY); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns the list of nodes with the given role. |
| 78 | * |
| 79 | * @param role the role by which to filter nodes |
| 80 | * @return an immutable list of nodes with the given role sorted in priority order |
| 81 | */ |
| 82 | public List<NodeId> getRoles(MastershipRole role) { |
| 83 | return ImmutableList.copyOf(roles.entrySet() |
| 84 | .stream() |
| 85 | .filter(entry -> entry.getValue() == role) |
| 86 | .map(Map.Entry::getKey) |
| 87 | .collect(Collectors.toList())); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns the current role for the given node. |
| 92 | * |
| 93 | * @param nodeId the node for which to return the current role |
| 94 | * @return the current role for the given node |
| 95 | */ |
| 96 | public MastershipRole getRole(NodeId nodeId) { |
| 97 | return roles.get(nodeId); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public int hashCode() { |
| 102 | return Objects.hash(term, master, roles); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public boolean equals(Object object) { |
| 107 | if (object instanceof MastershipInfo) { |
| 108 | MastershipInfo that = (MastershipInfo) object; |
| 109 | return this.term == that.term |
| 110 | && Objects.equals(this.master, that.master) |
| 111 | && Objects.equals(this.roles, that.roles); |
| 112 | } |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public String toString() { |
| 118 | return toStringHelper(this) |
| 119 | .add("term", term) |
| 120 | .add("master", master) |
| 121 | .add("roles", roles) |
| 122 | .toString(); |
| 123 | } |
| 124 | } |