blob: ceb643ad6e9ee08df04dc234fb7a5facade2be9b [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;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.meter.Band;
24import org.onosproject.net.meter.Meter;
25import org.onosproject.net.meter.MeterService;
26import org.onosproject.ui.RequestHandler;
27import org.onosproject.ui.UiMessageHandler;
28import org.onosproject.ui.table.CellFormatter;
29import org.onosproject.ui.table.TableModel;
30import org.onosproject.ui.table.TableRequestHandler;
Simon Huntbe60dde2016-01-13 12:26:56 -080031import org.onosproject.ui.table.cell.HexLongFormatter;
32import org.onosproject.ui.table.cell.NumberFormatter;
Jian Li1f544732015-12-30 23:36:37 -080033
34import java.util.Collection;
35
36/**
37 * Message handler for meter view related messages.
38 */
39public class MeterViewMessageHandler extends UiMessageHandler {
40
41 private static final String METER_DATA_REQ = "meterDataRequest";
42 private static final String METER_DATA_RESP = "meterDataResponse";
43 private static final String METERS = "meters";
44
45 private static final String ID = "id";
46 private static final String APP_ID = "app_id";
47 private static final String STATE = "state";
48 private static final String PACKETS = "packets";
49 private static final String BYTES = "bytes";
50 private static final String BANDS = "bands";
51
52 private static final String[] COL_IDS = {
53 ID, APP_ID, STATE, PACKETS, BYTES, BANDS
54 };
55
56 @Override
57 protected Collection<RequestHandler> createRequestHandlers() {
58 return ImmutableSet.of(new MeterDataRequest());
59 }
60
61 // handler for meter table requests
62 private final class MeterDataRequest extends TableRequestHandler {
63
Jian Li69f66632016-01-15 12:27:42 -080064 private static final String NO_ROWS_MESSAGE = "No meters found";
65
Jian Li1f544732015-12-30 23:36:37 -080066 private MeterDataRequest() {
67 super(METER_DATA_REQ, METER_DATA_RESP, METERS);
68 }
69
70 @Override
71 protected String[] getColumnIds() {
72 return COL_IDS;
73 }
74
75 @Override
Jian Li69f66632016-01-15 12:27:42 -080076 protected String noRowsMessage() {
77 // TODO: if the device with OF 1.0, return not support message
78 return NO_ROWS_MESSAGE;
79 }
80
81 @Override
Jian Li1f544732015-12-30 23:36:37 -080082 protected TableModel createTableModel() {
83 TableModel tm = super.createTableModel();
Simon Huntbe60dde2016-01-13 12:26:56 -080084 tm.setFormatter(ID, HexLongFormatter.INSTANCE);
85 tm.setFormatter(PACKETS, NumberFormatter.INTEGER);
86 tm.setFormatter(BYTES, NumberFormatter.INTEGER);
Jian Li1f544732015-12-30 23:36:37 -080087 tm.setFormatter(BANDS, new BandFormatter());
88 return tm;
89 }
90
91 @Override
92 protected void populateTable(TableModel tm, ObjectNode payload) {
93 String uri = string(payload, "devId");
94 if (!Strings.isNullOrEmpty(uri)) {
95 DeviceId deviceId = DeviceId.deviceId(uri);
96 MeterService ms = get(MeterService.class);
97 for (Meter meter : ms.getMeters(deviceId)) {
98 populateRow(tm.addRow(), meter);
99 }
100 }
101 }
102
103 private void populateRow(TableModel.Row row, Meter m) {
104 row.cell(ID, m.id().id())
105 .cell(APP_ID, m.appId().name())
106 .cell(STATE, m.state())
107 .cell(PACKETS, m.packetsSeen())
108 .cell(BYTES, m.bytesSeen())
109 .cell(BANDS, m.bands());
110 }
111
112 private final class BandFormatter implements CellFormatter {
113 private static final String BREAK = "<br>";
114
115 @Override
116 public String format(Object value) {
117 StringBuilder sb = new StringBuilder();
118 Collection<Band> bands = (Collection<Band>) value;
119
120 if (bands.isEmpty()) {
121 return "(No bands for this meter)";
122 }
123
124 // TODO: re-arrange band properties based on band type
125 for (Band b : bands) {
126 sb.append("Bytes: ")
127 .append(b.bytes())
128 .append(" Packets: ")
129 .append(b.packets())
130 .append(" Type: ")
131 .append(b.type())
132 .append(BREAK);
133 }
134
135 return sb.toString();
136 }
137 }
138 }
139}