blob: 204fe8f48fadf26a449f24ed994aa3870ff65ff5 [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;
31
32import java.util.Collection;
33
34/**
35 * Message handler for meter view related messages.
36 */
37public class MeterViewMessageHandler extends UiMessageHandler {
38
39 private static final String METER_DATA_REQ = "meterDataRequest";
40 private static final String METER_DATA_RESP = "meterDataResponse";
41 private static final String METERS = "meters";
42
43 private static final String ID = "id";
44 private static final String APP_ID = "app_id";
45 private static final String STATE = "state";
46 private static final String PACKETS = "packets";
47 private static final String BYTES = "bytes";
48 private static final String BANDS = "bands";
49
50 private static final String[] COL_IDS = {
51 ID, APP_ID, STATE, PACKETS, BYTES, BANDS
52 };
53
54 @Override
55 protected Collection<RequestHandler> createRequestHandlers() {
56 return ImmutableSet.of(new MeterDataRequest());
57 }
58
59 // handler for meter table requests
60 private final class MeterDataRequest extends TableRequestHandler {
61
62 private MeterDataRequest() {
63 super(METER_DATA_REQ, METER_DATA_RESP, METERS);
64 }
65
66 @Override
67 protected String[] getColumnIds() {
68 return COL_IDS;
69 }
70
71 @Override
72 protected TableModel createTableModel() {
73 TableModel tm = super.createTableModel();
74 tm.setFormatter(BANDS, new BandFormatter());
75 return tm;
76 }
77
78 @Override
79 protected void populateTable(TableModel tm, ObjectNode payload) {
80 String uri = string(payload, "devId");
81 if (!Strings.isNullOrEmpty(uri)) {
82 DeviceId deviceId = DeviceId.deviceId(uri);
83 MeterService ms = get(MeterService.class);
84 for (Meter meter : ms.getMeters(deviceId)) {
85 populateRow(tm.addRow(), meter);
86 }
87 }
88 }
89
90 private void populateRow(TableModel.Row row, Meter m) {
91 row.cell(ID, m.id().id())
92 .cell(APP_ID, m.appId().name())
93 .cell(STATE, m.state())
94 .cell(PACKETS, m.packetsSeen())
95 .cell(BYTES, m.bytesSeen())
96 .cell(BANDS, m.bands());
97 }
98
99 private final class BandFormatter implements CellFormatter {
100 private static final String BREAK = "<br>";
101
102 @Override
103 public String format(Object value) {
104 StringBuilder sb = new StringBuilder();
105 Collection<Band> bands = (Collection<Band>) value;
106
107 if (bands.isEmpty()) {
108 return "(No bands for this meter)";
109 }
110
111 // TODO: re-arrange band properties based on band type
112 for (Band b : bands) {
113 sb.append("Bytes: ")
114 .append(b.bytes())
115 .append(" Packets: ")
116 .append(b.packets())
117 .append(" Type: ")
118 .append(b.type())
119 .append(BREAK);
120 }
121
122 return sb.toString();
123 }
124 }
125 }
126}