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