blob: 30f93963ff84c345d9b128e3416cd702ba3c77a0 [file] [log] [blame]
Jordan Halterman281dbf32018-06-15 17:46:28 -07001/*
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 */
16package org.onosproject.store.flow.impl;
17
18import java.util.List;
19import java.util.Objects;
20
21import org.onosproject.cluster.NodeId;
22
23import static com.google.common.base.MoreObjects.toStringHelper;
24
25/**
26 * Device term context.
27 */
28public class DeviceReplicaInfo {
29 private final long term;
30 private final NodeId master;
31 private final List<NodeId> backups;
32
33 public DeviceReplicaInfo(long term, NodeId master, List<NodeId> backups) {
34 this.term = term;
35 this.master = master;
36 this.backups = backups;
37 }
38
39 /**
40 * Returns the mastership term.
41 *
42 * @return the mastership term
43 */
44 public long term() {
45 return term;
46 }
47
48 /**
49 * Returns the master for the {@link #term()}.
50 *
51 * @return the master {@link NodeId} for the current {@link #term()}
52 */
53 public NodeId master() {
54 return master;
55 }
56
57 /**
58 * Returns a boolean indicating whether the given {@link NodeId} is the current master.
59 *
60 * @param nodeId the node ID to check
61 * @return indicates whether the given node identifier is the identifier of the current master
62 */
63 public boolean isMaster(NodeId nodeId) {
64 return Objects.equals(master, nodeId);
65 }
66
67 /**
68 * Returns a list of all active backup nodes in priority order.
69 * <p>
70 * The returned backups are limited by the flow rule store's configured backup count.
71 *
72 * @return a list of backup nodes in priority order
73 */
74 public List<NodeId> backups() {
75 return backups;
76 }
77
78 /**
79 * Returns a boolean indicating whether the given node is a backup.
80 *
81 * @param nodeId the node identifier
82 * @return indicates whether the given node is a backup
83 */
84 public boolean isBackup(NodeId nodeId) {
85 return backups.contains(nodeId);
86 }
87
88 @Override
89 public int hashCode() {
90 return Objects.hash(term, master, backups);
91 }
92
93 @Override
94 public boolean equals(Object object) {
95 if (object instanceof DeviceReplicaInfo) {
96 DeviceReplicaInfo that = (DeviceReplicaInfo) object;
97 return this.term == that.term
98 && Objects.equals(this.master, that.master)
99 && Objects.equals(this.backups, that.backups);
100 }
101 return false;
102 }
103
104 @Override
105 public String toString() {
106 return toStringHelper(this)
107 .add("term", term())
108 .add("master", master())
109 .add("backups", backups())
110 .toString();
111 }
112}