blob: d3ba9c508c1c8d09885dddbd2a2f68e13b56d66a [file] [log] [blame]
Jian Li1f544732015-12-30 23:36:37 -08001/*
2 * Copyright 2016 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;
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;
37
38/**
39 * Message handler for meter view related messages.
40 */
41public class MeterViewMessageHandler extends UiMessageHandler {
42
43 private static final String METER_DATA_REQ = "meterDataRequest";
44 private static final String METER_DATA_RESP = "meterDataResponse";
45 private static final String METERS = "meters";
46
Jian Li8baf4472016-01-15 15:08:09 -080047 private static final String PROTOCOL = "protocol";
48 private static final String OF_10 = "OF_10";
49
Jian Li1f544732015-12-30 23:36:37 -080050 private static final String ID = "id";
51 private static final String APP_ID = "app_id";
52 private static final String STATE = "state";
53 private static final String PACKETS = "packets";
54 private static final String BYTES = "bytes";
55 private static final String BANDS = "bands";
56
57 private static final String[] COL_IDS = {
58 ID, APP_ID, STATE, PACKETS, BYTES, BANDS
59 };
60
61 @Override
62 protected Collection<RequestHandler> createRequestHandlers() {
63 return ImmutableSet.of(new MeterDataRequest());
64 }
65
Jian Li8baf4472016-01-15 15:08:09 -080066 private static String deviceProtocol(Device device) {
67 String protocol = device.annotations().value(PROTOCOL);
68 return protocol != null ? protocol : "";
69 }
70
Jian Li1f544732015-12-30 23:36:37 -080071 // 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
93 // TODO: replace with a less brittle solution...
94 if (deviceProtocol(dev).equals(OF_10)) {
95 return NOT_SUPPORT_MESSAGE;
96 }
97 }
Jian Li69f66632016-01-15 12:27:42 -080098 return NO_ROWS_MESSAGE;
99 }
100
101 @Override
Jian Li1f544732015-12-30 23:36:37 -0800102 protected TableModel createTableModel() {
103 TableModel tm = super.createTableModel();
Simon Huntbe60dde2016-01-13 12:26:56 -0800104 tm.setFormatter(ID, HexLongFormatter.INSTANCE);
105 tm.setFormatter(PACKETS, NumberFormatter.INTEGER);
106 tm.setFormatter(BYTES, NumberFormatter.INTEGER);
Jian Li1f544732015-12-30 23:36:37 -0800107 tm.setFormatter(BANDS, new BandFormatter());
108 return tm;
109 }
110
111 @Override
112 protected void populateTable(TableModel tm, ObjectNode payload) {
113 String uri = string(payload, "devId");
114 if (!Strings.isNullOrEmpty(uri)) {
115 DeviceId deviceId = DeviceId.deviceId(uri);
116 MeterService ms = get(MeterService.class);
117 for (Meter meter : ms.getMeters(deviceId)) {
118 populateRow(tm.addRow(), meter);
119 }
120 }
121 }
122
123 private void populateRow(TableModel.Row row, Meter m) {
124 row.cell(ID, m.id().id())
125 .cell(APP_ID, m.appId().name())
126 .cell(STATE, m.state())
127 .cell(PACKETS, m.packetsSeen())
128 .cell(BYTES, m.bytesSeen())
129 .cell(BANDS, m.bands());
130 }
131
132 private final class BandFormatter implements CellFormatter {
133 private static final String BREAK = "<br>";
134
135 @Override
136 public String format(Object value) {
137 StringBuilder sb = new StringBuilder();
138 Collection<Band> bands = (Collection<Band>) value;
139
140 if (bands.isEmpty()) {
141 return "(No bands for this meter)";
142 }
143
144 // TODO: re-arrange band properties based on band type
145 for (Band b : bands) {
146 sb.append("Bytes: ")
147 .append(b.bytes())
148 .append(" Packets: ")
149 .append(b.packets())
150 .append(" Type: ")
151 .append(b.type())
152 .append(BREAK);
153 }
154
155 return sb.toString();
156 }
157 }
158 }
159}