blob: f0aae017f7b079178a56f44257cb9467bb9dfab9 [file] [log] [blame]
Simon Hunt5f6dbf82016-03-30 08:53:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Simon Hunt5f6dbf82016-03-30 08:53:33 -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 */
16
17package org.onosproject.ui.model.topo;
18
Simon Hunt642bc452016-05-04 19:34:45 -070019import org.onlab.packet.IpAddress;
Simon Hunt338a3b42016-04-14 09:43:52 -070020import org.onosproject.cluster.ControllerNode;
21import org.onosproject.cluster.NodeId;
Simon Huntc0f20c12016-05-09 09:30:20 -070022import org.onosproject.net.DeviceId;
23
24import java.util.Collections;
25import java.util.HashSet;
26import java.util.Set;
Simon Hunt338a3b42016-04-14 09:43:52 -070027
Simon Hunt642bc452016-05-04 19:34:45 -070028import static org.onosproject.cluster.ControllerNode.State.INACTIVE;
29
Simon Hunt5f6dbf82016-03-30 08:53:33 -070030/**
31 * Represents an individual member of the cluster (ONOS instance).
32 */
Simon Huntcda9c032016-04-11 10:32:54 -070033public class UiClusterMember extends UiElement {
Simon Hunt338a3b42016-04-14 09:43:52 -070034
Simon Huntc0f20c12016-05-09 09:30:20 -070035 private final UiTopology topology;
Simon Hunt338a3b42016-04-14 09:43:52 -070036 private final ControllerNode cnode;
37
Simon Hunt642bc452016-05-04 19:34:45 -070038 private ControllerNode.State state = INACTIVE;
Simon Huntc0f20c12016-05-09 09:30:20 -070039 private final Set<DeviceId> mastership = new HashSet<>();
Simon Hunt642bc452016-05-04 19:34:45 -070040
Simon Hunt338a3b42016-04-14 09:43:52 -070041 /**
Simon Huntc0f20c12016-05-09 09:30:20 -070042 * Constructs a UI cluster member, with a reference to the parent
43 * topology instance and the specified controller node instance.
Simon Hunt338a3b42016-04-14 09:43:52 -070044 *
Simon Huntc0f20c12016-05-09 09:30:20 -070045 * @param topology parent topology containing this cluster member
Simon Huntb0582492016-09-20 18:26:38 -070046 * @param cnode underlying controller node
Simon Hunt338a3b42016-04-14 09:43:52 -070047 */
Simon Huntc0f20c12016-05-09 09:30:20 -070048 public UiClusterMember(UiTopology topology, ControllerNode cnode) {
49 this.topology = topology;
Simon Hunt338a3b42016-04-14 09:43:52 -070050 this.cnode = cnode;
51 }
52
Simon Hunt642bc452016-05-04 19:34:45 -070053 @Override
54 public String toString() {
55 return "UiClusterMember{" + cnode +
56 ", online=" + isOnline() +
57 ", ready=" + isReady() +
Simon Huntc0f20c12016-05-09 09:30:20 -070058 ", #devices=" + deviceCount() +
Simon Hunt642bc452016-05-04 19:34:45 -070059 "}";
60 }
61
Simon Huntc0f20c12016-05-09 09:30:20 -070062 @Override
63 public String idAsString() {
64 return id().toString();
65 }
66
67 /**
68 * Returns the controller node instance backing this UI cluster member.
69 *
70 * @return the backing controller node instance
71 */
72 public ControllerNode backingNode() {
73 return cnode;
74 }
Simon Hunt642bc452016-05-04 19:34:45 -070075
Simon Hunt338a3b42016-04-14 09:43:52 -070076 /**
Simon Hunt642bc452016-05-04 19:34:45 -070077 * Sets the state of this cluster member.
Simon Hunt338a3b42016-04-14 09:43:52 -070078 *
Simon Hunt642bc452016-05-04 19:34:45 -070079 * @param state the state
Simon Hunt338a3b42016-04-14 09:43:52 -070080 */
Simon Hunt642bc452016-05-04 19:34:45 -070081 public void setState(ControllerNode.State state) {
82 this.state = state;
83 }
84
Simon Hunt642bc452016-05-04 19:34:45 -070085 /**
Simon Huntc0f20c12016-05-09 09:30:20 -070086 * Sets the collection of identities of devices for which this
87 * controller node is master.
Simon Hunt642bc452016-05-04 19:34:45 -070088 *
Simon Huntc0f20c12016-05-09 09:30:20 -070089 * @param mastership device IDs
Simon Hunt642bc452016-05-04 19:34:45 -070090 */
Simon Huntc0f20c12016-05-09 09:30:20 -070091 public void setMastership(Set<DeviceId> mastership) {
92 this.mastership.clear();
93 this.mastership.addAll(mastership);
Simon Hunt338a3b42016-04-14 09:43:52 -070094 }
95
96 /**
97 * Returns the identity of the cluster member.
98 *
99 * @return member identifier
100 */
101 public NodeId id() {
102 return cnode.id();
103 }
Simon Hunt642bc452016-05-04 19:34:45 -0700104
105 /**
106 * Returns the IP address of the cluster member.
107 *
108 * @return the IP address
109 */
110 public IpAddress ip() {
111 return cnode.ip();
112 }
113
114 /**
115 * Returns true if this cluster member is online (active).
116 *
117 * @return true if online, false otherwise
118 */
119 public boolean isOnline() {
120 return state.isActive();
121 }
122
123 /**
124 * Returns true if this cluster member is considered ready.
125 *
126 * @return true if ready, false otherwise
127 */
128 public boolean isReady() {
129 return state.isReady();
130 }
131
132 /**
133 * Returns the number of devices for which this cluster member is master.
134 *
135 * @return number of devices for which this member is master
136 */
137 public int deviceCount() {
Simon Huntc0f20c12016-05-09 09:30:20 -0700138 return mastership.size();
Simon Hunt642bc452016-05-04 19:34:45 -0700139 }
140
Simon Huntc0f20c12016-05-09 09:30:20 -0700141 /**
142 * Returns the list of devices for which this cluster member is master.
143 *
144 * @return list of devices for which this member is master
145 */
146 public Set<DeviceId> masterOf() {
147 return Collections.unmodifiableSet(mastership);
148 }
149
150 /**
151 * Returns true if the specified device is one for which this cluster
152 * member is master.
153 *
154 * @param deviceId device ID
155 * @return true if this cluster member is master for the given device
156 */
157 public boolean masterOf(DeviceId deviceId) {
158 return mastership.contains(deviceId);
Simon Hunt642bc452016-05-04 19:34:45 -0700159 }
Simon Hunt5f6dbf82016-03-30 08:53:33 -0700160}