blob: d9ad4b51222903f38f391672e7682e8881b85b19 [file] [log] [blame]
Bri Prebilic Coleac829e42015-05-05 13:42:06 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Bri Prebilic Coleac829e42015-05-05 13:42:06 -07003 *
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;
Viswanath KSPd25440b2017-07-21 13:48:06 +053032import java.util.List;
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070033
34
35/**
36 * Message handler for port view related messages.
37 */
38public class PortViewMessageHandler extends UiMessageHandler {
39
40 private static final String PORT_DATA_REQ = "portDataRequest";
41 private static final String PORT_DATA_RESP = "portDataResponse";
42 private static final String PORTS = "ports";
Viswanath KSPd25440b2017-07-21 13:48:06 +053043 private static final String DELTA = "showDelta";
44 private static final String NZ = "nzFilter";
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070045
46 private static final String ID = "id";
47 private static final String PKT_RX = "pkt_rx";
48 private static final String PKT_TX = "pkt_tx";
49 private static final String BYTES_RX = "bytes_rx";
50 private static final String BYTES_TX = "bytes_tx";
51 private static final String PKT_RX_DRP = "pkt_rx_drp";
52 private static final String PKT_TX_DRP = "pkt_tx_drp";
53 private static final String DURATION = "duration";
54
Simon Hunt3d1b0652015-05-05 17:27:24 -070055 private static final String[] COL_IDS = {
56 ID, PKT_RX, PKT_TX, BYTES_RX, BYTES_TX,
57 PKT_RX_DRP, PKT_TX_DRP, DURATION
58 };
59
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070060 @Override
Simon Huntda580882015-05-12 20:58:18 -070061 protected Collection<RequestHandler> createRequestHandlers() {
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070062 return ImmutableSet.of(new PortDataRequest());
63 }
64
65 // handler for port table requests
66 private final class PortDataRequest extends TableRequestHandler {
67
Jian Li69f66632016-01-15 12:27:42 -080068 private static final String NO_ROWS_MESSAGE = "No ports found";
69
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070070 private PortDataRequest() {
71 super(PORT_DATA_REQ, PORT_DATA_RESP, PORTS);
72 }
73
74 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070075 protected String[] getColumnIds() {
76 return COL_IDS;
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070077 }
78
79 @Override
Jian Li8baf4472016-01-15 15:08:09 -080080 protected String noRowsMessage(ObjectNode payload) {
Jian Li69f66632016-01-15 12:27:42 -080081 return NO_ROWS_MESSAGE;
82 }
83
84 @Override
Simon Huntbe60dde2016-01-13 12:26:56 -080085 protected TableModel createTableModel() {
86 TableModel tm = super.createTableModel();
87 tm.setFormatter(PKT_RX, NumberFormatter.INTEGER);
88 tm.setFormatter(PKT_TX, NumberFormatter.INTEGER);
89 tm.setFormatter(BYTES_RX, NumberFormatter.INTEGER);
90 tm.setFormatter(BYTES_TX, NumberFormatter.INTEGER);
91 tm.setFormatter(PKT_RX_DRP, NumberFormatter.INTEGER);
92 tm.setFormatter(PKT_TX_DRP, NumberFormatter.INTEGER);
93 return tm;
94 }
95
96 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070097 protected void populateTable(TableModel tm, ObjectNode payload) {
98 String uri = string(payload, "devId");
Viswanath KSPd25440b2017-07-21 13:48:06 +053099 boolean nz = bool(payload, NZ);
100 boolean delta = bool(payload, DELTA);
Simon Hunt3d1b0652015-05-05 17:27:24 -0700101 if (!Strings.isNullOrEmpty(uri)) {
102 DeviceId deviceId = DeviceId.deviceId(uri);
103 DeviceService ds = get(DeviceService.class);
Viswanath KSPd25440b2017-07-21 13:48:06 +0530104 List<PortStatistics> stats = delta ?
105 ds.getPortDeltaStatistics(deviceId) :
106 ds.getPortStatistics(deviceId);
107 for (PortStatistics stat : stats) {
108 if (nz && stat.isZero()) {
109 continue;
110 }
Simon Hunt3d1b0652015-05-05 17:27:24 -0700111 populateRow(tm.addRow(), stat);
112 }
113 }
114 }
115
116 private void populateRow(TableModel.Row row, PortStatistics stat) {
117 row.cell(ID, stat.port())
118 .cell(PKT_RX, stat.packetsReceived())
119 .cell(PKT_TX, stat.packetsSent())
120 .cell(BYTES_RX, stat.bytesReceived())
121 .cell(BYTES_TX, stat.bytesSent())
122 .cell(PKT_RX_DRP, stat.packetsRxDropped())
123 .cell(PKT_TX_DRP, stat.packetsTxDropped())
124 .cell(DURATION, stat.durationSec());
Bri Prebilic Coleac829e42015-05-05 13:42:06 -0700125 }
126 }
Bri Prebilic Coleac829e42015-05-05 13:42:06 -0700127}