Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.ui.impl; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.base.Strings; |
| 21 | import com.google.common.collect.ImmutableSet; |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 22 | import org.onosproject.net.Device; |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 23 | import org.onosproject.net.DeviceId; |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 24 | import org.onosproject.net.device.DeviceService; |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 25 | import org.onosproject.net.meter.Band; |
| 26 | import org.onosproject.net.meter.Meter; |
| 27 | import org.onosproject.net.meter.MeterService; |
| 28 | import org.onosproject.ui.RequestHandler; |
| 29 | import org.onosproject.ui.UiMessageHandler; |
| 30 | import org.onosproject.ui.table.CellFormatter; |
| 31 | import org.onosproject.ui.table.TableModel; |
| 32 | import org.onosproject.ui.table.TableRequestHandler; |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 33 | import org.onosproject.ui.table.cell.HexLongFormatter; |
| 34 | import org.onosproject.ui.table.cell.NumberFormatter; |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 35 | |
| 36 | import java.util.Collection; |
Jian Li | be6de22 | 2016-01-25 16:17:25 -0800 | [diff] [blame] | 37 | import java.util.Set; |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Message handler for meter view related messages. |
| 41 | */ |
| 42 | public 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 Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 48 | private static final String PROTOCOL = "protocol"; |
| 49 | private static final String OF_10 = "OF_10"; |
Jian Li | be6de22 | 2016-01-25 16:17:25 -0800 | [diff] [blame] | 50 | 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 Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 54 | |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 55 | 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 Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 74 | private static final String NO_ROWS_MESSAGE = "No meters found"; |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 75 | private static final String NOT_SUPPORT_MESSAGE = "Meters not supported"; |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 76 | |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 77 | 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 Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 87 | 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 Li | be6de22 | 2016-01-25 16:17:25 -0800 | [diff] [blame] | 93 | if (meterNotSupported(dev)) { |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 94 | return NOT_SUPPORT_MESSAGE; |
| 95 | } |
| 96 | } |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 97 | return NO_ROWS_MESSAGE; |
| 98 | } |
| 99 | |
| 100 | @Override |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 101 | protected TableModel createTableModel() { |
| 102 | TableModel tm = super.createTableModel(); |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 103 | tm.setFormatter(ID, HexLongFormatter.INSTANCE); |
| 104 | tm.setFormatter(PACKETS, NumberFormatter.INTEGER); |
| 105 | tm.setFormatter(BYTES, NumberFormatter.INTEGER); |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 106 | 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 Li | be6de22 | 2016-01-25 16:17:25 -0800 | [diff] [blame] | 131 | private boolean meterNotSupported(Device dev) { |
| 132 | String protocol = dev.annotations().value(PROTOCOL); |
| 133 | return UNSUPPORTED_PROTOCOLS.contains(protocol); |
| 134 | } |
| 135 | |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 136 | 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 | } |