blob: 54f23cd3f9009a287c96c2520868fe4a8cdd1891 [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.cluster;
tom73d6d1e2014-09-17 20:08:01 -070017
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070018import java.time.Instant;
19import java.util.Optional;
Jordan Halterman28183ee2017-10-17 17:29:10 -070020import java.util.Set;
21
22import org.joda.time.DateTime;
Ray Milkey054e23d2018-03-22 13:37:11 -070023import org.onlab.util.Tools;
Jordan Halterman28183ee2017-10-17 17:29:10 -070024import org.onosproject.core.Version;
25import org.onosproject.event.ListenerService;
26
tom73d6d1e2014-09-17 20:08:01 -070027/**
Jordan Halterman980a8c12017-09-22 18:01:19 -070028 * Service for obtaining information about the individual nodes within the controller cluster.
tom73d6d1e2014-09-17 20:08:01 -070029 */
Jordan Halterman28183ee2017-10-17 17:29:10 -070030public interface ClusterService extends ListenerService<ClusterEvent, ClusterEventListener> {
31
32 /**
33 * Returns the local controller node.
34 *
35 * @return local controller node
36 */
37 ControllerNode getLocalNode();
38
39 /**
40 * Returns the set of current cluster members.
41 *
42 * @return set of cluster members
43 */
44 Set<ControllerNode> getNodes();
45
46 /**
47 * Returns the specified controller node.
48 *
49 * @param nodeId controller node identifier
50 * @return controller node
51 */
52 ControllerNode getNode(NodeId nodeId);
53
54 /**
55 * Returns the availability state of the specified controller node. Note
56 * that this does not imply that all the core and application components
57 * have been fully activated; only that the node has joined the cluster.
58 *
59 * @param nodeId controller node identifier
60 * @return availability state
61 */
62 ControllerNode.State getState(NodeId nodeId);
63
64 /**
65 * Returns the version of the given controller node.
66 *
67 * @param nodeId controller node identifier
68 * @return controller version
69 */
70 Version getVersion(NodeId nodeId);
71
72 /**
73 * Returns the system time when the availability state was last updated.
74 *
75 * @param nodeId controller node identifier
76 * @return system time when the availability state was last updated.
77 */
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070078 default Instant getLastUpdatedInstant(NodeId nodeId) {
79 return Optional.ofNullable(getLastUpdated(nodeId))
80 .map(DateTime::getMillis)
81 .map(Instant::ofEpochMilli)
82 .orElse(null);
83 }
84
85 /**
Ray Milkey054e23d2018-03-22 13:37:11 -070086 * Returns a human readable form of the system time when the availability state was last updated.
87 *
88 * @param nodeId controller node identifier
89 * @return human readable string for system time when the availability state was last updated.
90 */
91 default String localStatus(NodeId nodeId) {
92 Instant lastUpdated = getLastUpdatedInstant(nodeId);
93 String timeAgo = "Never";
94 if (lastUpdated != null) {
95 timeAgo = Tools.timeAgo(lastUpdated.toEpochMilli());
96 }
97 return timeAgo;
98 }
99
100 /**
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700101 * Returns the system time when the availability state was last updated.
102 *
103 * @param nodeId controller node identifier
104 * @return system time when the availability state was last updated.
105 *
106 * @deprecated in 1.12.0
107 */
108 @Deprecated
109 default DateTime getLastUpdated(NodeId nodeId) {
110 return Optional.ofNullable(getLastUpdatedInstant(nodeId))
111 .map(Instant::toEpochMilli)
112 .map(DateTime::new)
113 .orElse(null);
114 }
Jordan Halterman28183ee2017-10-17 17:29:10 -0700115
tom73d6d1e2014-09-17 20:08:01 -0700116}