blob: 92ebcf75ce5289af8416b82c816153fe5917ef29 [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;
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070029
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070030import java.util.Collection;
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070031
32
33/**
34 * Message handler for port view related messages.
35 */
36public class PortViewMessageHandler extends UiMessageHandler {
37
38 private static final String PORT_DATA_REQ = "portDataRequest";
39 private static final String PORT_DATA_RESP = "portDataResponse";
40 private static final String PORTS = "ports";
41
42 private static final String ID = "id";
43 private static final String PKT_RX = "pkt_rx";
44 private static final String PKT_TX = "pkt_tx";
45 private static final String BYTES_RX = "bytes_rx";
46 private static final String BYTES_TX = "bytes_tx";
47 private static final String PKT_RX_DRP = "pkt_rx_drp";
48 private static final String PKT_TX_DRP = "pkt_tx_drp";
49 private static final String DURATION = "duration";
50
Simon Hunt3d1b0652015-05-05 17:27:24 -070051 private static final String[] COL_IDS = {
52 ID, PKT_RX, PKT_TX, BYTES_RX, BYTES_TX,
53 PKT_RX_DRP, PKT_TX_DRP, DURATION
54 };
55
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070056 @Override
Simon Huntda580882015-05-12 20:58:18 -070057 protected Collection<RequestHandler> createRequestHandlers() {
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070058 return ImmutableSet.of(new PortDataRequest());
59 }
60
61 // handler for port table requests
62 private final class PortDataRequest extends TableRequestHandler {
63
64 private PortDataRequest() {
65 super(PORT_DATA_REQ, PORT_DATA_RESP, PORTS);
66 }
67
68 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070069 protected String[] getColumnIds() {
70 return COL_IDS;
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070071 }
72
73 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070074 protected void populateTable(TableModel tm, ObjectNode payload) {
75 String uri = string(payload, "devId");
76 if (!Strings.isNullOrEmpty(uri)) {
77 DeviceId deviceId = DeviceId.deviceId(uri);
78 DeviceService ds = get(DeviceService.class);
79 for (PortStatistics stat : ds.getPortStatistics(deviceId)) {
80 populateRow(tm.addRow(), stat);
81 }
82 }
83 }
84
85 private void populateRow(TableModel.Row row, PortStatistics stat) {
86 row.cell(ID, stat.port())
87 .cell(PKT_RX, stat.packetsReceived())
88 .cell(PKT_TX, stat.packetsSent())
89 .cell(BYTES_RX, stat.bytesReceived())
90 .cell(BYTES_TX, stat.bytesSent())
91 .cell(PKT_RX_DRP, stat.packetsRxDropped())
92 .cell(PKT_TX_DRP, stat.packetsTxDropped())
93 .cell(DURATION, stat.durationSec());
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070094 }
95 }
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070096}