blob: 311d44f9c8af630223d5705cc8178ec23ef194a3 [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;
Simon Huntc9e83212017-10-09 16:52:07 -070023import org.onosproject.net.Port;
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070024import org.onosproject.net.device.DeviceService;
25import org.onosproject.net.device.PortStatistics;
26import org.onosproject.ui.RequestHandler;
27import org.onosproject.ui.UiMessageHandler;
Simon Hunt3d1b0652015-05-05 17:27:24 -070028import org.onosproject.ui.table.TableModel;
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070029import org.onosproject.ui.table.TableRequestHandler;
Simon Huntbe60dde2016-01-13 12:26:56 -080030import org.onosproject.ui.table.cell.NumberFormatter;
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070031
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070032import java.util.Collection;
Viswanath KSPd25440b2017-07-21 13:48:06 +053033import java.util.List;
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070034
Simon Huntc9e83212017-10-09 16:52:07 -070035import static org.onosproject.net.DeviceId.deviceId;
36import static org.onosproject.net.PortNumber.portNumber;
37
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070038
39/**
40 * Message handler for port view related messages.
41 */
42public class PortViewMessageHandler extends UiMessageHandler {
43
44 private static final String PORT_DATA_REQ = "portDataRequest";
45 private static final String PORT_DATA_RESP = "portDataResponse";
46 private static final String PORTS = "ports";
Viswanath KSPd25440b2017-07-21 13:48:06 +053047 private static final String DELTA = "showDelta";
48 private static final String NZ = "nzFilter";
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070049
Simon Huntc9e83212017-10-09 16:52:07 -070050 private static final String PORT_DETAILS_REQ = "portDetailsRequest";
51 private static final String PORT_DETAILS_RESP = "portDetailsResponse";
52 private static final String DETAILS = "details";
53 private static final String PORT = "port";
54
55 private static final String DEV_ID = "devId";
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070056 private static final String ID = "id";
57 private static final String PKT_RX = "pkt_rx";
58 private static final String PKT_TX = "pkt_tx";
59 private static final String BYTES_RX = "bytes_rx";
60 private static final String BYTES_TX = "bytes_tx";
61 private static final String PKT_RX_DRP = "pkt_rx_drp";
62 private static final String PKT_TX_DRP = "pkt_tx_drp";
63 private static final String DURATION = "duration";
Simon Huntc9e83212017-10-09 16:52:07 -070064 private static final String SPEED = "speed";
65 private static final String ENABLED = "enabled";
66 private static final String TYPE = "type";
67 private static final String TYPE_IID = "_iconid_type";
68 private static final String PORT_ICON_PREFIX = "portIcon_";
69
70
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070071
Simon Hunt3d1b0652015-05-05 17:27:24 -070072 private static final String[] COL_IDS = {
73 ID, PKT_RX, PKT_TX, BYTES_RX, BYTES_TX,
74 PKT_RX_DRP, PKT_TX_DRP, DURATION
75 };
76
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070077 @Override
Simon Huntda580882015-05-12 20:58:18 -070078 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntc9e83212017-10-09 16:52:07 -070079 return ImmutableSet.of(
80 new PortDataRequest(),
81 new DetailRequestHandler()
82 );
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070083 }
84
85 // handler for port table requests
86 private final class PortDataRequest extends TableRequestHandler {
87
Jian Li69f66632016-01-15 12:27:42 -080088 private static final String NO_ROWS_MESSAGE = "No ports found";
89
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070090 private PortDataRequest() {
91 super(PORT_DATA_REQ, PORT_DATA_RESP, PORTS);
92 }
93
94 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070095 protected String[] getColumnIds() {
96 return COL_IDS;
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070097 }
98
99 @Override
Jian Li8baf4472016-01-15 15:08:09 -0800100 protected String noRowsMessage(ObjectNode payload) {
Jian Li69f66632016-01-15 12:27:42 -0800101 return NO_ROWS_MESSAGE;
102 }
103
104 @Override
Simon Huntbe60dde2016-01-13 12:26:56 -0800105 protected TableModel createTableModel() {
106 TableModel tm = super.createTableModel();
107 tm.setFormatter(PKT_RX, NumberFormatter.INTEGER);
108 tm.setFormatter(PKT_TX, NumberFormatter.INTEGER);
109 tm.setFormatter(BYTES_RX, NumberFormatter.INTEGER);
110 tm.setFormatter(BYTES_TX, NumberFormatter.INTEGER);
111 tm.setFormatter(PKT_RX_DRP, NumberFormatter.INTEGER);
112 tm.setFormatter(PKT_TX_DRP, NumberFormatter.INTEGER);
113 return tm;
114 }
115
116 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700117 protected void populateTable(TableModel tm, ObjectNode payload) {
118 String uri = string(payload, "devId");
Viswanath KSPd25440b2017-07-21 13:48:06 +0530119 boolean nz = bool(payload, NZ);
120 boolean delta = bool(payload, DELTA);
Simon Hunt3d1b0652015-05-05 17:27:24 -0700121 if (!Strings.isNullOrEmpty(uri)) {
122 DeviceId deviceId = DeviceId.deviceId(uri);
123 DeviceService ds = get(DeviceService.class);
Viswanath KSPd25440b2017-07-21 13:48:06 +0530124 List<PortStatistics> stats = delta ?
125 ds.getPortDeltaStatistics(deviceId) :
126 ds.getPortStatistics(deviceId);
127 for (PortStatistics stat : stats) {
Simon Hunt3d1b0652015-05-05 17:27:24 -0700128 populateRow(tm.addRow(), stat);
129 }
130 }
131 }
132
133 private void populateRow(TableModel.Row row, PortStatistics stat) {
Ray Milkey5ec42082019-02-13 09:56:07 -0800134 row.cell(ID, stat.portNumber().toLong())
Simon Hunt3d1b0652015-05-05 17:27:24 -0700135 .cell(PKT_RX, stat.packetsReceived())
136 .cell(PKT_TX, stat.packetsSent())
137 .cell(BYTES_RX, stat.bytesReceived())
138 .cell(BYTES_TX, stat.bytesSent())
139 .cell(PKT_RX_DRP, stat.packetsRxDropped())
140 .cell(PKT_TX_DRP, stat.packetsTxDropped())
141 .cell(DURATION, stat.durationSec());
Bri Prebilic Coleac829e42015-05-05 13:42:06 -0700142 }
143 }
Simon Huntc9e83212017-10-09 16:52:07 -0700144
145 private final class DetailRequestHandler extends RequestHandler {
146 private DetailRequestHandler() {
147 super(PORT_DETAILS_REQ);
148 }
149
150 @Override
151 public void process(ObjectNode payload) {
152 String id = string(payload, ID);
153 String devId = string(payload, DEV_ID);
154
155 DeviceService deviceService = get(DeviceService.class);
156 Port port = deviceService.getPort(deviceId(devId), portNumber(id));
157
158 ObjectNode data = objectNode();
159
160 data.put(ID, id);
161 data.put(DEV_ID, devId);
162 data.put(TYPE, displayType(port.type()));
163 data.put(SPEED, displaySpeed(port.portSpeed()));
164 data.put(ENABLED, port.isEnabled());
165
166 data.put(TYPE_IID, getIconIdForPortType(port.type()));
167
168 ObjectNode rootNode = objectNode();
169 rootNode.set(DETAILS, data);
170
171 // NOTE: ... an alternate way of getting all the details of an item:
172 // Use the codec context to get a JSON of the port. See ONOS-5976.
173 rootNode.set(PORT, getJsonCodecContext().encode(port, Port.class));
174
175 sendMessage(PORT_DETAILS_RESP, rootNode);
176 }
177
178 private String getIconIdForPortType(Port.Type type) {
179 String typeStr = "DEFAULT";
180 // TODO: consider providing alternate icon ID for different types
181 return PORT_ICON_PREFIX + typeStr;
182 // NOTE: look in icon.js for glyphMapping structure to see which
183 // glyph will be used for the port type.
184 }
185
186 /**
187 * Returns the port type as a displayable string.
188 *
189 * @param type the port type
190 * @return human readable port type
191 */
192 private String displayType(Port.Type type) {
193 // TODO: consider better display values?
194 return type.toString();
195 }
196
197 /**
198 * Returns port speed as a displayable string.
199 *
200 * @param portSpeed port speed in Mbps
201 * @return human readable port speed
202 */
203 private String displaySpeed(long portSpeed) {
204 // TODO: better conversion between Gbps, Mbps, Kbps, etc.
205 return "" + portSpeed + " Mbps";
206 }
207 }
Bri Prebilic Coleac829e42015-05-05 13:42:06 -0700208}