Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cluster; |
tom | 73d6d1e | 2014-09-17 20:08:01 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 18 | import java.time.Instant; |
| 19 | import java.util.Optional; |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 20 | import java.util.Set; |
| 21 | |
| 22 | import org.joda.time.DateTime; |
Ray Milkey | 054e23d | 2018-03-22 13:37:11 -0700 | [diff] [blame] | 23 | import org.onlab.util.Tools; |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 24 | import org.onosproject.core.Version; |
| 25 | import org.onosproject.event.ListenerService; |
| 26 | |
tom | 73d6d1e | 2014-09-17 20:08:01 -0700 | [diff] [blame] | 27 | /** |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 28 | * Service for obtaining information about the individual nodes within the controller cluster. |
tom | 73d6d1e | 2014-09-17 20:08:01 -0700 | [diff] [blame] | 29 | */ |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 30 | public 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 | /** |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 47 | * Returns the set of consensus nodes. |
| 48 | * |
| 49 | * @return the set of consensus nodes |
| 50 | */ |
| 51 | Set<Node> getConsensusNodes(); |
| 52 | |
| 53 | /** |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 54 | * Returns the specified controller node. |
| 55 | * |
| 56 | * @param nodeId controller node identifier |
| 57 | * @return controller node |
| 58 | */ |
| 59 | ControllerNode getNode(NodeId nodeId); |
| 60 | |
| 61 | /** |
| 62 | * Returns the availability state of the specified controller node. Note |
| 63 | * that this does not imply that all the core and application components |
| 64 | * have been fully activated; only that the node has joined the cluster. |
| 65 | * |
| 66 | * @param nodeId controller node identifier |
| 67 | * @return availability state |
| 68 | */ |
| 69 | ControllerNode.State getState(NodeId nodeId); |
| 70 | |
| 71 | /** |
| 72 | * Returns the version of the given controller node. |
| 73 | * |
| 74 | * @param nodeId controller node identifier |
| 75 | * @return controller version |
| 76 | */ |
| 77 | Version getVersion(NodeId nodeId); |
| 78 | |
| 79 | /** |
| 80 | * Returns the system time when the availability state was last updated. |
| 81 | * |
| 82 | * @param nodeId controller node identifier |
| 83 | * @return system time when the availability state was last updated. |
| 84 | */ |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 85 | default Instant getLastUpdatedInstant(NodeId nodeId) { |
| 86 | return Optional.ofNullable(getLastUpdated(nodeId)) |
| 87 | .map(DateTime::getMillis) |
| 88 | .map(Instant::ofEpochMilli) |
| 89 | .orElse(null); |
| 90 | } |
| 91 | |
| 92 | /** |
Ray Milkey | 054e23d | 2018-03-22 13:37:11 -0700 | [diff] [blame] | 93 | * Returns a human readable form of the system time when the availability state was last updated. |
| 94 | * |
| 95 | * @param nodeId controller node identifier |
| 96 | * @return human readable string for system time when the availability state was last updated. |
| 97 | */ |
| 98 | default String localStatus(NodeId nodeId) { |
| 99 | Instant lastUpdated = getLastUpdatedInstant(nodeId); |
| 100 | String timeAgo = "Never"; |
| 101 | if (lastUpdated != null) { |
| 102 | timeAgo = Tools.timeAgo(lastUpdated.toEpochMilli()); |
| 103 | } |
| 104 | return timeAgo; |
| 105 | } |
| 106 | |
| 107 | /** |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 108 | * Returns the system time when the availability state was last updated. |
| 109 | * |
| 110 | * @param nodeId controller node identifier |
| 111 | * @return system time when the availability state was last updated. |
| 112 | * |
| 113 | * @deprecated in 1.12.0 |
| 114 | */ |
| 115 | @Deprecated |
| 116 | default DateTime getLastUpdated(NodeId nodeId) { |
| 117 | return Optional.ofNullable(getLastUpdatedInstant(nodeId)) |
| 118 | .map(Instant::toEpochMilli) |
| 119 | .map(DateTime::new) |
| 120 | .orElse(null); |
| 121 | } |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 122 | |
tom | 73d6d1e | 2014-09-17 20:08:01 -0700 | [diff] [blame] | 123 | } |