blob: c196642a834f5abef326ac1d8006f4607bec44c9 [file] [log] [blame]
Bri Prebilic Coleac829e42015-05-05 13:42:06 -07001/*
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
17package org.onosproject.ui.impl;
18
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.base.Strings;
21import com.google.common.collect.ImmutableSet;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.device.DeviceService;
24import org.onosproject.net.device.PortStatistics;
25import org.onosproject.ui.RequestHandler;
26import org.onosproject.ui.UiMessageHandler;
Simon Hunt3d1b0652015-05-05 17:27:24 -070027import org.onosproject.ui.table.TableModel;
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070028import org.onosproject.ui.table.TableRequestHandler;
Simon Huntbe60dde2016-01-13 12:26:56 -080029import org.onosproject.ui.table.cell.NumberFormatter;
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070030
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070031import java.util.Collection;
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070032
33
34/**
35 * Message handler for port view related messages.
36 */
37public 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 Hunt3d1b0652015-05-05 17:27:24 -070052 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 Coleac829e42015-05-05 13:42:06 -070057 @Override
Simon Huntda580882015-05-12 20:58:18 -070058 protected Collection<RequestHandler> createRequestHandlers() {
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070059 return ImmutableSet.of(new PortDataRequest());
60 }
61
62 // handler for port table requests
63 private final class PortDataRequest extends TableRequestHandler {
64
Jian Li69f66632016-01-15 12:27:42 -080065 private static final String NO_ROWS_MESSAGE = "No ports found";
66
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070067 private PortDataRequest() {
68 super(PORT_DATA_REQ, PORT_DATA_RESP, PORTS);
69 }
70
71 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070072 protected String[] getColumnIds() {
73 return COL_IDS;
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070074 }
75
76 @Override
Jian Li69f66632016-01-15 12:27:42 -080077 protected String noRowsMessage() {
78 return NO_ROWS_MESSAGE;
79 }
80
81 @Override
Simon Huntbe60dde2016-01-13 12:26:56 -080082 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 Hunt3d1b0652015-05-05 17:27:24 -070094 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 Coleac829e42015-05-05 13:42:06 -0700114 }
115 }
Bri Prebilic Coleac829e42015-05-05 13:42:06 -0700116}