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 | |
| 68 | private GroupDataRequest() { |
| 69 | super(GROUP_DATA_REQ, GROUP_DATA_RESP, GROUPS); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | protected String[] getColumnIds() { |
| 74 | return COL_IDS; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | protected TableModel createTableModel() { |
| 79 | TableModel tm = super.createTableModel(); |
Simon Hunt | 3e4ccaf | 2016-01-12 19:59:54 -0800 | [diff] [blame] | 80 | tm.setFormatter(ID, HexFormatter.INSTANCE); |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 81 | tm.setFormatter(TYPE, EnumFormatter.INSTANCE); |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame^] | 82 | tm.setFormatter(PACKETS, NumberFormatter.INTEGER); |
| 83 | tm.setFormatter(BYTES, NumberFormatter.INTEGER); |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 84 | tm.setFormatter(BUCKETS, new BucketFormatter()); |
| 85 | return tm; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 90 | String uri = string(payload, "devId"); |
| 91 | if (!Strings.isNullOrEmpty(uri)) { |
| 92 | DeviceId deviceId = DeviceId.deviceId(uri); |
| 93 | GroupService gs = get(GroupService.class); |
| 94 | for (Group group : gs.getGroups(deviceId)) { |
| 95 | populateRow(tm.addRow(), group); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | private void populateRow(TableModel.Row row, Group g) { |
| 101 | row.cell(ID, g.id().id()) |
| 102 | .cell(APP_ID, g.appId().name()) |
| 103 | .cell(STATE, g.state()) |
| 104 | .cell(TYPE, g.type()) |
| 105 | .cell(PACKETS, g.packets()) |
| 106 | .cell(BYTES, g.bytes()) |
| 107 | .cell(BUCKETS, g.buckets().buckets()); |
| 108 | } |
| 109 | |
| 110 | private final class BucketFormatter implements CellFormatter { |
Bri Prebilic Cole | 76f632c | 2015-05-28 17:06:02 -0700 | [diff] [blame] | 111 | private static final String BREAK = "<br>"; |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 112 | |
| 113 | @Override |
| 114 | public String format(Object value) { |
| 115 | StringBuilder sb = new StringBuilder(); |
| 116 | List<GroupBucket> buckets = (List<GroupBucket>) value; |
| 117 | |
| 118 | if (buckets.isEmpty()) { |
| 119 | return "(No buckets for this group)"; |
| 120 | } |
| 121 | |
| 122 | for (GroupBucket b : buckets) { |
| 123 | sb.append("Bytes: ") |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 124 | .append(b.bytes()) |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 125 | .append(" Packets: ") |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 126 | .append(b.packets()) |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 127 | .append(" Actions: ") |
| 128 | .append(b.treatment().allInstructions()) |
Bri Prebilic Cole | 76f632c | 2015-05-28 17:06:02 -0700 | [diff] [blame] | 129 | .append(BREAK); |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 130 | } |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 131 | |
| 132 | return sb.toString(); |
| 133 | } |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | } |