blob: feb517ce2ab98fc1f5bdafb23a6b47705eec7ce8 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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.store.flow;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070017
Sho SHIMIZU19af19d2016-08-12 16:08:04 -070018import com.google.common.base.Objects;
19import org.onosproject.cluster.NodeId;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070020
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070021import java.util.Collections;
Madan Jampani86940d92015-05-06 11:47:57 -070022import java.util.List;
Sho SHIMIZU19af19d2016-08-12 16:08:04 -070023import java.util.Optional;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070024
Sho SHIMIZU19af19d2016-08-12 16:08:04 -070025import static com.google.common.base.Preconditions.checkNotNull;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070026
27/**
28 * Class to represent placement information about Master/Backup copy.
29 */
30public final class ReplicaInfo {
31
Jordan Halterman0a2bd452018-06-13 17:24:58 -070032 private final long term;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070033 private final Optional<NodeId> master;
Madan Jampani86940d92015-05-06 11:47:57 -070034 private final List<NodeId> backups;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070035
36 /**
37 * Creates a ReplicaInfo instance.
38 *
Jordan Halterman0a2bd452018-06-13 17:24:58 -070039 * @param term monotonically increasing unique mastership term
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070040 * @param master NodeId of the node where the master copy should be
Madan Jampani86940d92015-05-06 11:47:57 -070041 * @param backups list of NodeId, where backup copies should be placed
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070042 */
Jordan Halterman0a2bd452018-06-13 17:24:58 -070043 public ReplicaInfo(long term, NodeId master, List<NodeId> backups) {
44 this.term = term;
Sho SHIMIZU19af19d2016-08-12 16:08:04 -070045 this.master = Optional.ofNullable(master);
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070046 this.backups = checkNotNull(backups);
47 }
48
49 /**
Jordan Halterman0a2bd452018-06-13 17:24:58 -070050 * Returns the mastership term.
51 *
52 * @return the mastership term
53 */
54 public long term() {
55 return term;
56 }
57
58 /**
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070059 * Returns the NodeId, if there is a Node where the master copy should be.
60 *
61 * @return NodeId, where the master copy should be placed
62 */
63 public Optional<NodeId> master() {
64 return master;
65 }
66
67 /**
68 * Returns the collection of NodeId, where backup copies should be placed.
69 *
70 * @return collection of NodeId, where backup copies should be placed
71 */
Madan Jampani86940d92015-05-06 11:47:57 -070072 public List<NodeId> backups() {
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070073 return backups;
74 }
75
Madan Jampani0f6ad142015-05-13 17:10:04 -070076 @Override
77 public int hashCode() {
78 return Objects.hashCode(master, backups);
79 }
80
81 @Override
82 public boolean equals(Object other) {
83 if (!(other instanceof ReplicaInfo)) {
84 return false;
85 }
86 ReplicaInfo that = (ReplicaInfo) other;
87 return Objects.equal(this.master, that.master) &&
88 Objects.equal(this.backups, that.backups);
89 }
90
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070091 // for Serializer
92 private ReplicaInfo() {
Jordan Halterman0a2bd452018-06-13 17:24:58 -070093 this.term = 0;
Sho SHIMIZU19af19d2016-08-12 16:08:04 -070094 this.master = Optional.empty();
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070095 this.backups = Collections.emptyList();
96 }
97}