blob: 0d36b4912daa5dc485d269d453e60ed9adb0d864 [file] [log] [blame]
Jian Li1f544732015-12-30 23:36:37 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jian Li1f544732015-12-30 23:36:37 -08003 *
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;
Jian Li8baf4472016-01-15 15:08:09 -080022import org.onosproject.net.Device;
Jian Li1f544732015-12-30 23:36:37 -080023import org.onosproject.net.DeviceId;
Jian Li8baf4472016-01-15 15:08:09 -080024import org.onosproject.net.device.DeviceService;
Jian Li1f544732015-12-30 23:36:37 -080025import org.onosproject.net.meter.Band;
26import org.onosproject.net.meter.Meter;
27import org.onosproject.net.meter.MeterService;
28import org.onosproject.ui.RequestHandler;
29import org.onosproject.ui.UiMessageHandler;
30import org.onosproject.ui.table.CellFormatter;
31import org.onosproject.ui.table.TableModel;
32import org.onosproject.ui.table.TableRequestHandler;
Simon Huntbe60dde2016-01-13 12:26:56 -080033import org.onosproject.ui.table.cell.HexLongFormatter;
34import org.onosproject.ui.table.cell.NumberFormatter;
Jian Li1f544732015-12-30 23:36:37 -080035
36import java.util.Collection;
Jian Libe6de222016-01-25 16:17:25 -080037import java.util.Set;
Jian Li1f544732015-12-30 23:36:37 -080038
39/**
40 * Message handler for meter view related messages.
41 */
42public class MeterViewMessageHandler extends UiMessageHandler {
43
44 private static final String METER_DATA_REQ = "meterDataRequest";
45 private static final String METER_DATA_RESP = "meterDataResponse";
46 private static final String METERS = "meters";
47
Jian Li8baf4472016-01-15 15:08:09 -080048 private static final String PROTOCOL = "protocol";
49 private static final String OF_10 = "OF_10";
Jian Libe6de222016-01-25 16:17:25 -080050 private static final String OF_11 = "OF_11";
51 private static final String OF_12 = "OF_12";
52 private static final Set<String> UNSUPPORTED_PROTOCOLS =
53 ImmutableSet.of(OF_10, OF_11, OF_12);
Jian Li8baf4472016-01-15 15:08:09 -080054
Jian Li1f544732015-12-30 23:36:37 -080055 private static final String ID = "id";
56 private static final String APP_ID = "app_id";
57 private static final String STATE = "state";
58 private static final String PACKETS = "packets";
59 private static final String BYTES = "bytes";
60 private static final String BANDS = "bands";
61
62 private static final String[] COL_IDS = {
63 ID, APP_ID, STATE, PACKETS, BYTES, BANDS
64 };
65
66 @Override
67 protected Collection<RequestHandler> createRequestHandlers() {
68 return ImmutableSet.of(new MeterDataRequest());
69 }
70
71 // handler for meter table requests
72 private final class MeterDataRequest extends TableRequestHandler {
73
Jian Li69f66632016-01-15 12:27:42 -080074 private static final String NO_ROWS_MESSAGE = "No meters found";
Jian Li8baf4472016-01-15 15:08:09 -080075 private static final String NOT_SUPPORT_MESSAGE = "Meters not supported";
Jian Li69f66632016-01-15 12:27:42 -080076
Jian Li1f544732015-12-30 23:36:37 -080077 private MeterDataRequest() {
78 super(METER_DATA_REQ, METER_DATA_RESP, METERS);
79 }
80
81 @Override
82 protected String[] getColumnIds() {
83 return COL_IDS;
84 }
85
86 @Override
Jian Li8baf4472016-01-15 15:08:09 -080087 protected String noRowsMessage(ObjectNode payload) {
88 String uri = string(payload, "devId");
89 if (!Strings.isNullOrEmpty(uri)) {
90 DeviceService ds = get(DeviceService.class);
91 Device dev = ds.getDevice(DeviceId.deviceId(uri));
92
Jian Libe6de222016-01-25 16:17:25 -080093 if (meterNotSupported(dev)) {
Jian Li8baf4472016-01-15 15:08:09 -080094 return NOT_SUPPORT_MESSAGE;
95 }
96 }
Jian Li69f66632016-01-15 12:27:42 -080097 return NO_ROWS_MESSAGE;
98 }
99
100 @Override
Jian Li1f544732015-12-30 23:36:37 -0800101 protected TableModel createTableModel() {
102 TableModel tm = super.createTableModel();
Simon Huntbe60dde2016-01-13 12:26:56 -0800103 tm.setFormatter(ID, HexLongFormatter.INSTANCE);
104 tm.setFormatter(PACKETS, NumberFormatter.INTEGER);
105 tm.setFormatter(BYTES, NumberFormatter.INTEGER);
Jian Li1f544732015-12-30 23:36:37 -0800106 tm.setFormatter(BANDS, new BandFormatter());
107 return tm;
108 }
109
110 @Override
111 protected void populateTable(TableModel tm, ObjectNode payload) {
112 String uri = string(payload, "devId");
113 if (!Strings.isNullOrEmpty(uri)) {
114 DeviceId deviceId = DeviceId.deviceId(uri);
115 MeterService ms = get(MeterService.class);
116 for (Meter meter : ms.getMeters(deviceId)) {
117 populateRow(tm.addRow(), meter);
118 }
119 }
120 }
121
122 private void populateRow(TableModel.Row row, Meter m) {
123 row.cell(ID, m.id().id())
124 .cell(APP_ID, m.appId().name())
125 .cell(STATE, m.state())
126 .cell(PACKETS, m.packetsSeen())
127 .cell(BYTES, m.bytesSeen())
128 .cell(BANDS, m.bands());
129 }
130
Jian Libe6de222016-01-25 16:17:25 -0800131 private boolean meterNotSupported(Device dev) {
132 String protocol = dev.annotations().value(PROTOCOL);
133 return UNSUPPORTED_PROTOCOLS.contains(protocol);
134 }
135
Jian Li1f544732015-12-30 23:36:37 -0800136 private final class BandFormatter implements CellFormatter {
137 private static final String BREAK = "<br>";
138
139 @Override
140 public String format(Object value) {
141 StringBuilder sb = new StringBuilder();
142 Collection<Band> bands = (Collection<Band>) value;
143
144 if (bands.isEmpty()) {
145 return "(No bands for this meter)";
146 }
147
148 // TODO: re-arrange band properties based on band type
149 for (Band b : bands) {
150 sb.append("Bytes: ")
151 .append(b.bytes())
152 .append(" Packets: ")
153 .append(b.packets())
154 .append(" Type: ")
155 .append(b.type())
156 .append(BREAK);
157 }
158
159 return sb.toString();
160 }
161 }
162 }
163}