blob: f30d9dcf0d688722c5f39a83838de086024eba62 [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;
22
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070023import static com.google.common.base.Preconditions.checkNotNull;
Simon Hunt642bc452016-05-04 19:34:45 -070024import static org.onosproject.cluster.ControllerNode.State.INACTIVE;
25
Simon Hunt5f6dbf82016-03-30 08:53:33 -070026/**
27 * Represents an individual member of the cluster (ONOS instance).
28 */
Simon Huntcda9c032016-04-11 10:32:54 -070029public class UiClusterMember extends UiElement {
Simon Hunt338a3b42016-04-14 09:43:52 -070030
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070031 private static final String NODE_CANNOT_BE_NULL = "Node cannot be null";
32
Simon Huntc0f20c12016-05-09 09:30:20 -070033 private final UiTopology topology;
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070034 private final NodeId nodeId;
Simon Hunt338a3b42016-04-14 09:43:52 -070035
Simon Hunt642bc452016-05-04 19:34:45 -070036 private ControllerNode.State state = INACTIVE;
37
Simon Hunt338a3b42016-04-14 09:43:52 -070038 /**
Simon Huntc0f20c12016-05-09 09:30:20 -070039 * Constructs a UI cluster member, with a reference to the parent
40 * topology instance and the specified controller node instance.
Simon Hunt338a3b42016-04-14 09:43:52 -070041 *
Simon Huntc0f20c12016-05-09 09:30:20 -070042 * @param topology parent topology containing this cluster member
Simon Huntb0582492016-09-20 18:26:38 -070043 * @param cnode underlying controller node
Simon Hunt338a3b42016-04-14 09:43:52 -070044 */
Simon Huntc0f20c12016-05-09 09:30:20 -070045 public UiClusterMember(UiTopology topology, ControllerNode cnode) {
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070046 checkNotNull(cnode, NODE_CANNOT_BE_NULL);
Simon Huntc0f20c12016-05-09 09:30:20 -070047 this.topology = topology;
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070048 this.nodeId = cnode.id();
Simon Hunt338a3b42016-04-14 09:43:52 -070049 }
50
Simon Hunt642bc452016-05-04 19:34:45 -070051 @Override
52 public String toString() {
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070053 return "UiClusterMember{" + nodeId +
Simon Hunt642bc452016-05-04 19:34:45 -070054 ", online=" + isOnline() +
55 ", ready=" + isReady() +
Simon Hunt642bc452016-05-04 19:34:45 -070056 "}";
57 }
58
Simon Huntc0f20c12016-05-09 09:30:20 -070059 @Override
60 public String idAsString() {
61 return id().toString();
62 }
63
64 /**
65 * Returns the controller node instance backing this UI cluster member.
66 *
67 * @return the backing controller node instance
68 */
69 public ControllerNode backingNode() {
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070070 return topology.services.cluster().getNode(nodeId);
Simon Huntc0f20c12016-05-09 09:30:20 -070071 }
Simon Hunt642bc452016-05-04 19:34:45 -070072
Simon Hunt338a3b42016-04-14 09:43:52 -070073 /**
Simon Hunt642bc452016-05-04 19:34:45 -070074 * Sets the state of this cluster member.
Simon Hunt338a3b42016-04-14 09:43:52 -070075 *
Simon Hunt642bc452016-05-04 19:34:45 -070076 * @param state the state
Simon Hunt338a3b42016-04-14 09:43:52 -070077 */
Simon Hunt642bc452016-05-04 19:34:45 -070078 public void setState(ControllerNode.State state) {
79 this.state = state;
80 }
81
Simon Hunt642bc452016-05-04 19:34:45 -070082 /**
Simon Hunt338a3b42016-04-14 09:43:52 -070083 * Returns the identity of the cluster member.
84 *
85 * @return member identifier
86 */
87 public NodeId id() {
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070088 return nodeId;
Simon Hunt338a3b42016-04-14 09:43:52 -070089 }
Simon Hunt642bc452016-05-04 19:34:45 -070090
91 /**
92 * Returns the IP address of the cluster member.
93 *
94 * @return the IP address
95 */
96 public IpAddress ip() {
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070097 return backingNode().ip();
Simon Hunt642bc452016-05-04 19:34:45 -070098 }
99
100 /**
101 * Returns true if this cluster member is online (active).
102 *
103 * @return true if online, false otherwise
104 */
105 public boolean isOnline() {
106 return state.isActive();
107 }
108
109 /**
110 * Returns true if this cluster member is considered ready.
111 *
112 * @return true if ready, false otherwise
113 */
114 public boolean isReady() {
115 return state.isReady();
116 }
Simon Hunt5f6dbf82016-03-30 08:53:33 -0700117}