Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -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 | ac829e4 | 2015-05-05 13:42:06 -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 | |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.base.Strings; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.onosproject.net.DeviceId; |
Simon Hunt | c9e8321 | 2017-10-09 16:52:07 -0700 | [diff] [blame] | 23 | import org.onosproject.net.Port; |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 24 | import org.onosproject.net.device.DeviceService; |
| 25 | import org.onosproject.net.device.PortStatistics; |
| 26 | import org.onosproject.ui.RequestHandler; |
| 27 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.table.TableModel; |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.table.TableRequestHandler; |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 30 | import org.onosproject.ui.table.cell.NumberFormatter; |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 31 | |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 32 | import java.util.Collection; |
Viswanath KSP | d25440b | 2017-07-21 13:48:06 +0530 | [diff] [blame] | 33 | import java.util.List; |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 34 | |
Simon Hunt | c9e8321 | 2017-10-09 16:52:07 -0700 | [diff] [blame] | 35 | import static org.onosproject.net.DeviceId.deviceId; |
| 36 | import static org.onosproject.net.PortNumber.portNumber; |
| 37 | |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Message handler for port view related messages. |
| 41 | */ |
| 42 | public class PortViewMessageHandler extends UiMessageHandler { |
| 43 | |
| 44 | private static final String PORT_DATA_REQ = "portDataRequest"; |
| 45 | private static final String PORT_DATA_RESP = "portDataResponse"; |
| 46 | private static final String PORTS = "ports"; |
Viswanath KSP | d25440b | 2017-07-21 13:48:06 +0530 | [diff] [blame] | 47 | private static final String DELTA = "showDelta"; |
| 48 | private static final String NZ = "nzFilter"; |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 49 | |
Simon Hunt | c9e8321 | 2017-10-09 16:52:07 -0700 | [diff] [blame] | 50 | private static final String PORT_DETAILS_REQ = "portDetailsRequest"; |
| 51 | private static final String PORT_DETAILS_RESP = "portDetailsResponse"; |
| 52 | private static final String DETAILS = "details"; |
| 53 | private static final String PORT = "port"; |
| 54 | |
| 55 | private static final String DEV_ID = "devId"; |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 56 | private static final String ID = "id"; |
| 57 | private static final String PKT_RX = "pkt_rx"; |
| 58 | private static final String PKT_TX = "pkt_tx"; |
| 59 | private static final String BYTES_RX = "bytes_rx"; |
| 60 | private static final String BYTES_TX = "bytes_tx"; |
| 61 | private static final String PKT_RX_DRP = "pkt_rx_drp"; |
| 62 | private static final String PKT_TX_DRP = "pkt_tx_drp"; |
| 63 | private static final String DURATION = "duration"; |
Simon Hunt | c9e8321 | 2017-10-09 16:52:07 -0700 | [diff] [blame] | 64 | private static final String SPEED = "speed"; |
| 65 | private static final String ENABLED = "enabled"; |
| 66 | private static final String TYPE = "type"; |
| 67 | private static final String TYPE_IID = "_iconid_type"; |
| 68 | private static final String PORT_ICON_PREFIX = "portIcon_"; |
| 69 | |
| 70 | |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 71 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 72 | private static final String[] COL_IDS = { |
| 73 | ID, PKT_RX, PKT_TX, BYTES_RX, BYTES_TX, |
| 74 | PKT_RX_DRP, PKT_TX_DRP, DURATION |
| 75 | }; |
| 76 | |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 77 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 78 | protected Collection<RequestHandler> createRequestHandlers() { |
Simon Hunt | c9e8321 | 2017-10-09 16:52:07 -0700 | [diff] [blame] | 79 | return ImmutableSet.of( |
| 80 | new PortDataRequest(), |
| 81 | new DetailRequestHandler() |
| 82 | ); |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // handler for port table requests |
| 86 | private final class PortDataRequest extends TableRequestHandler { |
| 87 | |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 88 | private static final String NO_ROWS_MESSAGE = "No ports found"; |
| 89 | |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 90 | private PortDataRequest() { |
| 91 | super(PORT_DATA_REQ, PORT_DATA_RESP, PORTS); |
| 92 | } |
| 93 | |
| 94 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 95 | protected String[] getColumnIds() { |
| 96 | return COL_IDS; |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 100 | protected String noRowsMessage(ObjectNode payload) { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 101 | return NO_ROWS_MESSAGE; |
| 102 | } |
| 103 | |
| 104 | @Override |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 105 | protected TableModel createTableModel() { |
| 106 | TableModel tm = super.createTableModel(); |
| 107 | tm.setFormatter(PKT_RX, NumberFormatter.INTEGER); |
| 108 | tm.setFormatter(PKT_TX, NumberFormatter.INTEGER); |
| 109 | tm.setFormatter(BYTES_RX, NumberFormatter.INTEGER); |
| 110 | tm.setFormatter(BYTES_TX, NumberFormatter.INTEGER); |
| 111 | tm.setFormatter(PKT_RX_DRP, NumberFormatter.INTEGER); |
| 112 | tm.setFormatter(PKT_TX_DRP, NumberFormatter.INTEGER); |
| 113 | return tm; |
| 114 | } |
| 115 | |
| 116 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 117 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 118 | String uri = string(payload, "devId"); |
Viswanath KSP | d25440b | 2017-07-21 13:48:06 +0530 | [diff] [blame] | 119 | boolean nz = bool(payload, NZ); |
| 120 | boolean delta = bool(payload, DELTA); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 121 | if (!Strings.isNullOrEmpty(uri)) { |
| 122 | DeviceId deviceId = DeviceId.deviceId(uri); |
| 123 | DeviceService ds = get(DeviceService.class); |
Viswanath KSP | d25440b | 2017-07-21 13:48:06 +0530 | [diff] [blame] | 124 | List<PortStatistics> stats = delta ? |
| 125 | ds.getPortDeltaStatistics(deviceId) : |
| 126 | ds.getPortStatistics(deviceId); |
| 127 | for (PortStatistics stat : stats) { |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 128 | populateRow(tm.addRow(), stat); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | private void populateRow(TableModel.Row row, PortStatistics stat) { |
| 134 | row.cell(ID, stat.port()) |
| 135 | .cell(PKT_RX, stat.packetsReceived()) |
| 136 | .cell(PKT_TX, stat.packetsSent()) |
| 137 | .cell(BYTES_RX, stat.bytesReceived()) |
| 138 | .cell(BYTES_TX, stat.bytesSent()) |
| 139 | .cell(PKT_RX_DRP, stat.packetsRxDropped()) |
| 140 | .cell(PKT_TX_DRP, stat.packetsTxDropped()) |
| 141 | .cell(DURATION, stat.durationSec()); |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
Simon Hunt | c9e8321 | 2017-10-09 16:52:07 -0700 | [diff] [blame] | 144 | |
| 145 | private final class DetailRequestHandler extends RequestHandler { |
| 146 | private DetailRequestHandler() { |
| 147 | super(PORT_DETAILS_REQ); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void process(ObjectNode payload) { |
| 152 | String id = string(payload, ID); |
| 153 | String devId = string(payload, DEV_ID); |
| 154 | |
| 155 | DeviceService deviceService = get(DeviceService.class); |
| 156 | Port port = deviceService.getPort(deviceId(devId), portNumber(id)); |
| 157 | |
| 158 | ObjectNode data = objectNode(); |
| 159 | |
| 160 | data.put(ID, id); |
| 161 | data.put(DEV_ID, devId); |
| 162 | data.put(TYPE, displayType(port.type())); |
| 163 | data.put(SPEED, displaySpeed(port.portSpeed())); |
| 164 | data.put(ENABLED, port.isEnabled()); |
| 165 | |
| 166 | data.put(TYPE_IID, getIconIdForPortType(port.type())); |
| 167 | |
| 168 | ObjectNode rootNode = objectNode(); |
| 169 | rootNode.set(DETAILS, data); |
| 170 | |
| 171 | // NOTE: ... an alternate way of getting all the details of an item: |
| 172 | // Use the codec context to get a JSON of the port. See ONOS-5976. |
| 173 | rootNode.set(PORT, getJsonCodecContext().encode(port, Port.class)); |
| 174 | |
| 175 | sendMessage(PORT_DETAILS_RESP, rootNode); |
| 176 | } |
| 177 | |
| 178 | private String getIconIdForPortType(Port.Type type) { |
| 179 | String typeStr = "DEFAULT"; |
| 180 | // TODO: consider providing alternate icon ID for different types |
| 181 | return PORT_ICON_PREFIX + typeStr; |
| 182 | // NOTE: look in icon.js for glyphMapping structure to see which |
| 183 | // glyph will be used for the port type. |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Returns the port type as a displayable string. |
| 188 | * |
| 189 | * @param type the port type |
| 190 | * @return human readable port type |
| 191 | */ |
| 192 | private String displayType(Port.Type type) { |
| 193 | // TODO: consider better display values? |
| 194 | return type.toString(); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Returns port speed as a displayable string. |
| 199 | * |
| 200 | * @param portSpeed port speed in Mbps |
| 201 | * @return human readable port speed |
| 202 | */ |
| 203 | private String displaySpeed(long portSpeed) { |
| 204 | // TODO: better conversion between Gbps, Mbps, Kbps, etc. |
| 205 | return "" + portSpeed + " Mbps"; |
| 206 | } |
| 207 | } |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 208 | } |