Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -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 | */ |
| 16 | |
| 17 | package org.onosproject.ui.impl; |
| 18 | |
Viswanath KSP | 116f0c1 | 2016-11-21 00:02:51 +0530 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.ImmutableSet; |
pierventre | 55a5f39 | 2021-11-05 15:37:32 +0100 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 23 | import org.onosproject.cluster.ClusterService; |
| 24 | import org.onosproject.cluster.ControllerNode; |
| 25 | import org.onosproject.cluster.NodeId; |
Viswanath KSP | 3232286 | 2017-01-16 18:33:05 +0530 | [diff] [blame] | 26 | import org.onosproject.mastership.MastershipService; |
Viswanath KSP | 116f0c1 | 2016-11-21 00:02:51 +0530 | [diff] [blame] | 27 | import org.onosproject.net.Device; |
| 28 | import org.onosproject.net.device.DeviceService; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.RequestHandler; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.table.TableModel; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 32 | import org.onosproject.ui.table.TableRequestHandler; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 33 | import org.onosproject.ui.table.cell.TimeFormatter; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 34 | |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 35 | import java.time.Instant; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 36 | import java.util.Collection; |
Viswanath KSP | 116f0c1 | 2016-11-21 00:02:51 +0530 | [diff] [blame] | 37 | import java.util.List; |
| 38 | import java.util.stream.Collectors; |
| 39 | |
| 40 | import static com.google.common.collect.ImmutableList.copyOf; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 41 | |
| 42 | |
| 43 | /** |
| 44 | * Message handler for cluster view related messages. |
| 45 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 46 | public class ClusterViewMessageHandler extends UiMessageHandler { |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 47 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 48 | private static final String CLUSTER_DATA_REQ = "clusterDataRequest"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 49 | private static final String CLUSTER_DATA_RESP = "clusterDataResponse"; |
| 50 | private static final String CLUSTERS = "clusters"; |
| 51 | |
Simon Hunt | a271e0a | 2016-10-26 10:59:14 -0700 | [diff] [blame] | 52 | private static final String CLUSTER_DETAILS_REQ = "clusterDetailsRequest"; |
| 53 | private static final String CLUSTER_DETAILS_RESP = "clusterDetailsResponse"; |
Viswanath KSP | df11ea8 | 2016-10-25 01:15:16 +0530 | [diff] [blame] | 54 | private static final String DETAILS = "details"; |
| 55 | |
Viswanath KSP | 116f0c1 | 2016-11-21 00:02:51 +0530 | [diff] [blame] | 56 | private static final String DEVICES = "devices"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 57 | private static final String ID = "id"; |
| 58 | private static final String IP = "ip"; |
| 59 | private static final String TCP_PORT = "tcp"; |
| 60 | private static final String STATE_IID = "_iconid_state"; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 61 | private static final String STARTED_IID = "_iconid_started"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 62 | private static final String UPDATED = "updated"; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 63 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 64 | private static final String[] COL_IDS = { |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 65 | ID, IP, TCP_PORT, STATE_IID, STARTED_IID, UPDATED |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Viswanath KSP | 116f0c1 | 2016-11-21 00:02:51 +0530 | [diff] [blame] | 68 | private static final String URI = "id"; |
| 69 | private static final String TYPE = "type"; |
| 70 | private static final String CHASSIS_ID = "chassisid"; |
| 71 | private static final String HW = "hw"; |
| 72 | private static final String SW = "sw"; |
| 73 | private static final String MFR = "mfr"; |
| 74 | private static final String PROTOCOL = "protocol"; |
| 75 | private static final String SERIAL = "serial"; |
| 76 | |
| 77 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 78 | private static final String ICON_ID_ONLINE = "active"; |
| 79 | private static final String ICON_ID_OFFLINE = "inactive"; |
| 80 | |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 81 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 82 | protected Collection<RequestHandler> createRequestHandlers() { |
Viswanath KSP | df11ea8 | 2016-10-25 01:15:16 +0530 | [diff] [blame] | 83 | return ImmutableSet.of( |
| 84 | new ClusterDataRequest(), |
| 85 | new DetailRequestHandler()); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 88 | // handler for cluster table requests |
| 89 | private final class ClusterDataRequest extends TableRequestHandler { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 90 | private static final String NO_ROWS_MESSAGE = "No cluster nodes found"; |
| 91 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 92 | private ClusterDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 93 | super(CLUSTER_DATA_REQ, CLUSTER_DATA_RESP, CLUSTERS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 97 | protected String[] getColumnIds() { |
| 98 | return COL_IDS; |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 102 | protected String noRowsMessage(ObjectNode payload) { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 103 | return NO_ROWS_MESSAGE; |
| 104 | } |
| 105 | |
| 106 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 107 | protected TableModel createTableModel() { |
| 108 | TableModel tm = super.createTableModel(); |
Simon Hunt | 5939e65 | 2015-05-06 16:20:23 -0700 | [diff] [blame] | 109 | tm.setFormatter(UPDATED, new TimeFormatter()); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 110 | return tm; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 115 | ClusterService cs = get(ClusterService.class); |
| 116 | for (ControllerNode node : cs.getNodes()) { |
| 117 | populateRow(tm.addRow(), node, cs); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | private void populateRow(TableModel.Row row, ControllerNode node, |
| 122 | ClusterService cs) { |
| 123 | NodeId id = node.id(); |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 124 | Instant lastUpdated = cs.getLastUpdatedInstant(id); |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 125 | ControllerNode.State state = cs.getState(id); |
| 126 | String iconId = state.isActive() ? ICON_ID_ONLINE : ICON_ID_OFFLINE; |
| 127 | String startedId = state.isReady() ? ICON_ID_ONLINE : ICON_ID_OFFLINE; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 128 | |
| 129 | row.cell(ID, id) |
| 130 | .cell(IP, node.ip()) |
| 131 | .cell(TCP_PORT, node.tcpPort()) |
| 132 | .cell(STATE_IID, iconId) |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 133 | .cell(STARTED_IID, startedId) |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 134 | .cell(UPDATED, lastUpdated); |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
Viswanath KSP | df11ea8 | 2016-10-25 01:15:16 +0530 | [diff] [blame] | 137 | |
| 138 | private final class DetailRequestHandler extends RequestHandler { |
| 139 | |
| 140 | public DetailRequestHandler() { |
| 141 | super(CLUSTER_DETAILS_REQ); |
| 142 | } |
| 143 | |
Viswanath KSP | 3232286 | 2017-01-16 18:33:05 +0530 | [diff] [blame] | 144 | private List<Device> populateDevices(ControllerNode node) { |
Viswanath KSP | 116f0c1 | 2016-11-21 00:02:51 +0530 | [diff] [blame] | 145 | DeviceService ds = get(DeviceService.class); |
Viswanath KSP | 3232286 | 2017-01-16 18:33:05 +0530 | [diff] [blame] | 146 | MastershipService ms = get(MastershipService.class); |
Viswanath KSP | 116f0c1 | 2016-11-21 00:02:51 +0530 | [diff] [blame] | 147 | return copyOf(ds.getDevices()).stream() |
Viswanath KSP | 3232286 | 2017-01-16 18:33:05 +0530 | [diff] [blame] | 148 | .filter(d -> ms.getMasterFor(d.id()).equals(node.id())) |
Viswanath KSP | 116f0c1 | 2016-11-21 00:02:51 +0530 | [diff] [blame] | 149 | .collect(Collectors.toList()); |
| 150 | } |
| 151 | |
| 152 | private String deviceProtocol(Device device) { |
| 153 | String protocol = device.annotations().value(PROTOCOL); |
| 154 | return protocol != null ? protocol : ""; |
| 155 | } |
| 156 | |
| 157 | private ObjectNode deviceData(Device d) { |
| 158 | ObjectNode device = objectNode(); |
| 159 | |
| 160 | device.put(URI, d.id().toString()); |
| 161 | device.put(TYPE, d.type().toString()); |
| 162 | device.put(CHASSIS_ID, d.chassisId().toString()); |
| 163 | device.put(MFR, d.manufacturer()); |
| 164 | device.put(HW, d.hwVersion()); |
| 165 | device.put(SW, d.swVersion()); |
| 166 | device.put(PROTOCOL, deviceProtocol(d)); |
| 167 | device.put(SERIAL, d.serialNumber()); |
| 168 | |
| 169 | return device; |
| 170 | } |
| 171 | |
Viswanath KSP | df11ea8 | 2016-10-25 01:15:16 +0530 | [diff] [blame] | 172 | @Override |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 173 | public void process(ObjectNode payload) { |
pierventre | 55a5f39 | 2021-11-05 15:37:32 +0100 | [diff] [blame] | 174 | ObjectNode rootNode = objectNode(); |
| 175 | ObjectNode data = objectNode(); |
| 176 | ArrayNode devices = arrayNode(); |
Viswanath KSP | df11ea8 | 2016-10-25 01:15:16 +0530 | [diff] [blame] | 177 | |
| 178 | String id = string(payload, ID); |
| 179 | ClusterService cs = get(ClusterService.class); |
| 180 | ControllerNode node = cs.getNode(new NodeId(id)); |
pierventre | 55a5f39 | 2021-11-05 15:37:32 +0100 | [diff] [blame] | 181 | if (node != null) { |
| 182 | IpAddress nodeIp = node.ip(); |
| 183 | List<Device> deviceList = populateDevices(node); |
Viswanath KSP | df11ea8 | 2016-10-25 01:15:16 +0530 | [diff] [blame] | 184 | |
pierventre | 55a5f39 | 2021-11-05 15:37:32 +0100 | [diff] [blame] | 185 | data.put(ID, node.id().toString()); |
| 186 | data.put(IP, nodeIp != null ? nodeIp.toString() : node.host()); |
Viswanath KSP | 116f0c1 | 2016-11-21 00:02:51 +0530 | [diff] [blame] | 187 | |
pierventre | 55a5f39 | 2021-11-05 15:37:32 +0100 | [diff] [blame] | 188 | for (Device d : deviceList) { |
| 189 | devices.add(deviceData(d)); |
| 190 | } |
Viswanath KSP | df11ea8 | 2016-10-25 01:15:16 +0530 | [diff] [blame] | 191 | |
pierventre | 55a5f39 | 2021-11-05 15:37:32 +0100 | [diff] [blame] | 192 | } else { |
| 193 | data.put(ID, "NONE"); |
| 194 | data.put(IP, "NONE"); |
Viswanath KSP | 116f0c1 | 2016-11-21 00:02:51 +0530 | [diff] [blame] | 195 | } |
Viswanath KSP | 116f0c1 | 2016-11-21 00:02:51 +0530 | [diff] [blame] | 196 | data.set(DEVICES, devices); |
| 197 | |
Viswanath KSP | df11ea8 | 2016-10-25 01:15:16 +0530 | [diff] [blame] | 198 | //TODO put more detail info to data |
Viswanath KSP | df11ea8 | 2016-10-25 01:15:16 +0530 | [diff] [blame] | 199 | rootNode.set(DETAILS, data); |
| 200 | sendMessage(CLUSTER_DETAILS_RESP, rootNode); |
| 201 | } |
| 202 | } |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 203 | } |