Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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; |
| 27 | import org.onosproject.ui.table.AbstractTableRow; |
| 28 | import org.onosproject.ui.table.TableRequestHandler; |
| 29 | import org.onosproject.ui.table.TableRow; |
| 30 | |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.Collection; |
| 33 | import java.util.List; |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * Message handler for port view related messages. |
| 38 | */ |
| 39 | public class PortViewMessageHandler extends UiMessageHandler { |
| 40 | |
| 41 | private static final String PORT_DATA_REQ = "portDataRequest"; |
| 42 | private static final String PORT_DATA_RESP = "portDataResponse"; |
| 43 | private static final String PORTS = "ports"; |
| 44 | |
| 45 | private static final String ID = "id"; |
| 46 | private static final String PKT_RX = "pkt_rx"; |
| 47 | private static final String PKT_TX = "pkt_tx"; |
| 48 | private static final String BYTES_RX = "bytes_rx"; |
| 49 | private static final String BYTES_TX = "bytes_tx"; |
| 50 | private static final String PKT_RX_DRP = "pkt_rx_drp"; |
| 51 | private static final String PKT_TX_DRP = "pkt_tx_drp"; |
| 52 | private static final String DURATION = "duration"; |
| 53 | |
| 54 | @Override |
| 55 | protected Collection<RequestHandler> getHandlers() { |
| 56 | return ImmutableSet.of(new PortDataRequest()); |
| 57 | } |
| 58 | |
| 59 | // handler for port table requests |
| 60 | private final class PortDataRequest extends TableRequestHandler { |
| 61 | |
| 62 | private PortDataRequest() { |
| 63 | super(PORT_DATA_REQ, PORT_DATA_RESP, PORTS); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | protected TableRow[] generateTableRows(ObjectNode payload) { |
| 68 | String uri = string(payload, "devId"); |
| 69 | if (Strings.isNullOrEmpty(uri)) { |
| 70 | return new TableRow[0]; |
| 71 | } |
| 72 | DeviceId deviceId = DeviceId.deviceId(uri); |
| 73 | DeviceService service = get(DeviceService.class); |
| 74 | List<TableRow> list = new ArrayList<>(); |
| 75 | for (PortStatistics stat : service.getPortStatistics(deviceId)) { |
| 76 | list.add(new PortTableRow(stat)); |
| 77 | } |
| 78 | return list.toArray(new TableRow[list.size()]); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * TableRow implementation for |
| 84 | * {@link org.onosproject.net.device.PortStatistics port statistics}. |
| 85 | */ |
| 86 | private static class PortTableRow extends AbstractTableRow { |
| 87 | |
| 88 | private static final String[] COL_IDS = { |
| 89 | ID, PKT_RX, PKT_TX, BYTES_RX, BYTES_TX, |
| 90 | PKT_RX_DRP, PKT_TX_DRP, DURATION |
| 91 | }; |
| 92 | |
| 93 | public PortTableRow(PortStatistics stat) { |
| 94 | add(ID, Integer.toString(stat.port())); |
| 95 | add(PKT_RX, Long.toString(stat.packetsReceived())); |
| 96 | add(PKT_TX, Long.toString(stat.packetsSent())); |
| 97 | add(BYTES_RX, Long.toString(stat.bytesReceived())); |
| 98 | add(BYTES_TX, Long.toString(stat.bytesSent())); |
| 99 | add(PKT_RX_DRP, Long.toString(stat.packetsRxDropped())); |
| 100 | add(PKT_TX_DRP, Long.toString(stat.packetsTxDropped())); |
| 101 | add(DURATION, Long.toString(stat.durationSec())); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | protected String[] columnIds() { |
| 106 | return COL_IDS; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | } |