Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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; |
| 23 | import org.onosproject.net.device.DeviceService; |
| 24 | import org.onosproject.net.device.PortStatistics; |
| 25 | import org.onosproject.ui.RequestHandler; |
| 26 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 27 | import org.onosproject.ui.table.TableModel; |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.table.TableRequestHandler; |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 29 | import org.onosproject.ui.table.cell.NumberFormatter; |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 30 | |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 31 | import java.util.Collection; |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 32 | |
| 33 | |
| 34 | /** |
| 35 | * Message handler for port view related messages. |
| 36 | */ |
| 37 | public class PortViewMessageHandler extends UiMessageHandler { |
| 38 | |
| 39 | private static final String PORT_DATA_REQ = "portDataRequest"; |
| 40 | private static final String PORT_DATA_RESP = "portDataResponse"; |
| 41 | private static final String PORTS = "ports"; |
| 42 | |
| 43 | private static final String ID = "id"; |
| 44 | private static final String PKT_RX = "pkt_rx"; |
| 45 | private static final String PKT_TX = "pkt_tx"; |
| 46 | private static final String BYTES_RX = "bytes_rx"; |
| 47 | private static final String BYTES_TX = "bytes_tx"; |
| 48 | private static final String PKT_RX_DRP = "pkt_rx_drp"; |
| 49 | private static final String PKT_TX_DRP = "pkt_tx_drp"; |
| 50 | private static final String DURATION = "duration"; |
| 51 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 52 | private static final String[] COL_IDS = { |
| 53 | ID, PKT_RX, PKT_TX, BYTES_RX, BYTES_TX, |
| 54 | PKT_RX_DRP, PKT_TX_DRP, DURATION |
| 55 | }; |
| 56 | |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 57 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 58 | protected Collection<RequestHandler> createRequestHandlers() { |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 59 | return ImmutableSet.of(new PortDataRequest()); |
| 60 | } |
| 61 | |
| 62 | // handler for port table requests |
| 63 | private final class PortDataRequest extends TableRequestHandler { |
| 64 | |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 65 | private static final String NO_ROWS_MESSAGE = "No ports found"; |
| 66 | |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 67 | private PortDataRequest() { |
| 68 | super(PORT_DATA_REQ, PORT_DATA_RESP, PORTS); |
| 69 | } |
| 70 | |
| 71 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 72 | protected String[] getColumnIds() { |
| 73 | return COL_IDS; |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 77 | protected String noRowsMessage(ObjectNode payload) { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 78 | return NO_ROWS_MESSAGE; |
| 79 | } |
| 80 | |
| 81 | @Override |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 82 | protected TableModel createTableModel() { |
| 83 | TableModel tm = super.createTableModel(); |
| 84 | tm.setFormatter(PKT_RX, NumberFormatter.INTEGER); |
| 85 | tm.setFormatter(PKT_TX, NumberFormatter.INTEGER); |
| 86 | tm.setFormatter(BYTES_RX, NumberFormatter.INTEGER); |
| 87 | tm.setFormatter(BYTES_TX, NumberFormatter.INTEGER); |
| 88 | tm.setFormatter(PKT_RX_DRP, NumberFormatter.INTEGER); |
| 89 | tm.setFormatter(PKT_TX_DRP, NumberFormatter.INTEGER); |
| 90 | return tm; |
| 91 | } |
| 92 | |
| 93 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 94 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 95 | String uri = string(payload, "devId"); |
| 96 | if (!Strings.isNullOrEmpty(uri)) { |
| 97 | DeviceId deviceId = DeviceId.deviceId(uri); |
| 98 | DeviceService ds = get(DeviceService.class); |
| 99 | for (PortStatistics stat : ds.getPortStatistics(deviceId)) { |
| 100 | populateRow(tm.addRow(), stat); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | private void populateRow(TableModel.Row row, PortStatistics stat) { |
| 106 | row.cell(ID, stat.port()) |
| 107 | .cell(PKT_RX, stat.packetsReceived()) |
| 108 | .cell(PKT_TX, stat.packetsSent()) |
| 109 | .cell(BYTES_RX, stat.bytesReceived()) |
| 110 | .cell(BYTES_TX, stat.bytesSent()) |
| 111 | .cell(PKT_RX_DRP, stat.packetsRxDropped()) |
| 112 | .cell(PKT_TX_DRP, stat.packetsTxDropped()) |
| 113 | .cell(DURATION, stat.durationSec()); |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 116 | } |