blob: f48fc81c7e819ec3dea9039728d20d868efca8c4 [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
64 private MeterDataRequest() {
65 super(METER_DATA_REQ, METER_DATA_RESP, METERS);
66 }
67
68 @Override
69 protected String[] getColumnIds() {
70 return COL_IDS;
71 }
72
73 @Override
74 protected TableModel createTableModel() {
75 TableModel tm = super.createTableModel();
Simon Huntbe60dde2016-01-13 12:26:56 -080076 tm.setFormatter(ID, HexLongFormatter.INSTANCE);
77 tm.setFormatter(PACKETS, NumberFormatter.INTEGER);
78 tm.setFormatter(BYTES, NumberFormatter.INTEGER);
Jian Li1f544732015-12-30 23:36:37 -080079 tm.setFormatter(BANDS, new BandFormatter());
80 return tm;
81 }
82
83 @Override
84 protected void populateTable(TableModel tm, ObjectNode payload) {
85 String uri = string(payload, "devId");
86 if (!Strings.isNullOrEmpty(uri)) {
87 DeviceId deviceId = DeviceId.deviceId(uri);
88 MeterService ms = get(MeterService.class);
89 for (Meter meter : ms.getMeters(deviceId)) {
90 populateRow(tm.addRow(), meter);
91 }
92 }
93 }
94
95 private void populateRow(TableModel.Row row, Meter m) {
96 row.cell(ID, m.id().id())
97 .cell(APP_ID, m.appId().name())
98 .cell(STATE, m.state())
99 .cell(PACKETS, m.packetsSeen())
100 .cell(BYTES, m.bytesSeen())
101 .cell(BANDS, m.bands());
102 }
103
104 private final class BandFormatter implements CellFormatter {
105 private static final String BREAK = "<br>";
106
107 @Override
108 public String format(Object value) {
109 StringBuilder sb = new StringBuilder();
110 Collection<Band> bands = (Collection<Band>) value;
111
112 if (bands.isEmpty()) {
113 return "(No bands for this meter)";
114 }
115
116 // TODO: re-arrange band properties based on band type
117 for (Band b : bands) {
118 sb.append("Bytes: ")
119 .append(b.bytes())
120 .append(" Packets: ")
121 .append(b.packets())
122 .append(" Type: ")
123 .append(b.type())
124 .append(BREAK);
125 }
126
127 return sb.toString();
128 }
129 }
130 }
131}