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