Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [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; |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [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; |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 25 | import org.onosproject.net.group.Group; |
| 26 | import org.onosproject.net.group.GroupBucket; |
| 27 | import org.onosproject.net.group.GroupService; |
| 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; |
| 33 | import org.onosproject.ui.table.cell.EnumFormatter; |
Simon Hunt | 3e4ccaf | 2016-01-12 19:59:54 -0800 | [diff] [blame] | 34 | import org.onosproject.ui.table.cell.HexFormatter; |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 35 | import org.onosproject.ui.table.cell.NumberFormatter; |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 36 | |
| 37 | import java.util.Collection; |
| 38 | import java.util.List; |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * Message handler for group view related messages. |
| 43 | */ |
| 44 | public class GroupViewMessageHandler extends UiMessageHandler { |
| 45 | |
| 46 | private static final String GROUP_DATA_REQ = "groupDataRequest"; |
| 47 | private static final String GROUP_DATA_RESP = "groupDataResponse"; |
| 48 | private static final String GROUPS = "groups"; |
| 49 | |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 50 | private static final String PROTOCOL = "protocol"; |
| 51 | private static final String OF_10 = "OF_10"; |
| 52 | |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 53 | private static final String ID = "id"; |
| 54 | private static final String APP_ID = "app_id"; |
| 55 | private static final String STATE = "state"; |
| 56 | private static final String TYPE = "type"; |
| 57 | private static final String PACKETS = "packets"; |
| 58 | private static final String BYTES = "bytes"; |
| 59 | private static final String BUCKETS = "buckets"; |
| 60 | |
| 61 | private static final String[] COL_IDS = { |
| 62 | ID, APP_ID, STATE, TYPE, PACKETS, BYTES, BUCKETS |
| 63 | }; |
| 64 | |
| 65 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 66 | protected Collection<RequestHandler> createRequestHandlers() { |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 67 | return ImmutableSet.of(new GroupDataRequest()); |
| 68 | } |
| 69 | |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 70 | private static String deviceProtocol(Device device) { |
| 71 | String protocol = device.annotations().value(PROTOCOL); |
| 72 | return protocol != null ? protocol : ""; |
| 73 | } |
| 74 | |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 75 | // handler for group table requests |
| 76 | private final class GroupDataRequest extends TableRequestHandler { |
| 77 | |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 78 | private static final String NO_ROWS_MESSAGE = "No groups found"; |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 79 | private static final String NOT_SUPPORT_MESSAGE = "Groups not supported"; |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 80 | |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 81 | private GroupDataRequest() { |
| 82 | super(GROUP_DATA_REQ, GROUP_DATA_RESP, GROUPS); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | protected String[] getColumnIds() { |
| 87 | return COL_IDS; |
| 88 | } |
| 89 | |
| 90 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 91 | protected String noRowsMessage(ObjectNode payload) { |
| 92 | String uri = string(payload, "devId"); |
| 93 | if (!Strings.isNullOrEmpty(uri)) { |
| 94 | DeviceService ds = get(DeviceService.class); |
| 95 | Device dev = ds.getDevice(DeviceId.deviceId(uri)); |
| 96 | |
| 97 | // TODO: replace with a less brittle solution... |
| 98 | if (deviceProtocol(dev).equals(OF_10)) { |
| 99 | return NOT_SUPPORT_MESSAGE; |
| 100 | } |
| 101 | } |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 102 | return NO_ROWS_MESSAGE; |
| 103 | } |
| 104 | |
| 105 | @Override |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 106 | protected TableModel createTableModel() { |
| 107 | TableModel tm = super.createTableModel(); |
Simon Hunt | 3e4ccaf | 2016-01-12 19:59:54 -0800 | [diff] [blame] | 108 | tm.setFormatter(ID, HexFormatter.INSTANCE); |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 109 | tm.setFormatter(TYPE, EnumFormatter.INSTANCE); |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 110 | tm.setFormatter(PACKETS, NumberFormatter.INTEGER); |
| 111 | tm.setFormatter(BYTES, NumberFormatter.INTEGER); |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 112 | tm.setFormatter(BUCKETS, new BucketFormatter()); |
| 113 | return tm; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 118 | String uri = string(payload, "devId"); |
| 119 | if (!Strings.isNullOrEmpty(uri)) { |
| 120 | DeviceId deviceId = DeviceId.deviceId(uri); |
| 121 | GroupService gs = get(GroupService.class); |
| 122 | for (Group group : gs.getGroups(deviceId)) { |
| 123 | populateRow(tm.addRow(), group); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | private void populateRow(TableModel.Row row, Group g) { |
| 129 | row.cell(ID, g.id().id()) |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 130 | .cell(APP_ID, g.appId().name()) |
| 131 | .cell(STATE, g.state()) |
| 132 | .cell(TYPE, g.type()) |
| 133 | .cell(PACKETS, g.packets()) |
| 134 | .cell(BYTES, g.bytes()) |
| 135 | .cell(BUCKETS, g.buckets().buckets()); |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | private final class BucketFormatter implements CellFormatter { |
Bri Prebilic Cole | 76f632c | 2015-05-28 17:06:02 -0700 | [diff] [blame] | 139 | private static final String BREAK = "<br>"; |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 140 | |
| 141 | @Override |
| 142 | public String format(Object value) { |
| 143 | StringBuilder sb = new StringBuilder(); |
| 144 | List<GroupBucket> buckets = (List<GroupBucket>) value; |
| 145 | |
| 146 | if (buckets.isEmpty()) { |
| 147 | return "(No buckets for this group)"; |
| 148 | } |
| 149 | |
| 150 | for (GroupBucket b : buckets) { |
| 151 | sb.append("Bytes: ") |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 152 | .append(b.bytes()) |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 153 | .append(" Packets: ") |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 154 | .append(b.packets()) |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 155 | .append(" Actions: ") |
| 156 | .append(b.treatment().allInstructions()) |
Bri Prebilic Cole | 76f632c | 2015-05-28 17:06:02 -0700 | [diff] [blame] | 157 | .append(BREAK); |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 158 | } |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 159 | |
| 160 | return sb.toString(); |
| 161 | } |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | } |