blob: 95fdd5320da97549330d6b357f391bc3542a692f [file] [log] [blame]
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -07003 *
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;
Jian Li8baf4472016-01-15 15:08:09 -080022import org.onosproject.net.Device;
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070023import org.onosproject.net.DeviceId;
Jian Li8baf4472016-01-15 15:08:09 -080024import org.onosproject.net.device.DeviceService;
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070025import org.onosproject.net.group.Group;
26import org.onosproject.net.group.GroupBucket;
27import org.onosproject.net.group.GroupService;
28import org.onosproject.ui.RequestHandler;
29import org.onosproject.ui.UiMessageHandler;
30import org.onosproject.ui.table.CellFormatter;
31import org.onosproject.ui.table.TableModel;
32import org.onosproject.ui.table.TableRequestHandler;
33import org.onosproject.ui.table.cell.EnumFormatter;
Simon Hunt3e4ccaf2016-01-12 19:59:54 -080034import org.onosproject.ui.table.cell.HexFormatter;
Simon Huntbe60dde2016-01-13 12:26:56 -080035import org.onosproject.ui.table.cell.NumberFormatter;
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070036
37import java.util.Collection;
38import java.util.List;
39
40
41/**
42 * Message handler for group view related messages.
43 */
44public 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 Li8baf4472016-01-15 15:08:09 -080050 private static final String PROTOCOL = "protocol";
51 private static final String OF_10 = "OF_10";
52
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070053 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 Huntda580882015-05-12 20:58:18 -070066 protected Collection<RequestHandler> createRequestHandlers() {
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070067 return ImmutableSet.of(new GroupDataRequest());
68 }
69
Jian Li8baf4472016-01-15 15:08:09 -080070 private static String deviceProtocol(Device device) {
71 String protocol = device.annotations().value(PROTOCOL);
72 return protocol != null ? protocol : "";
73 }
74
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070075 // handler for group table requests
76 private final class GroupDataRequest extends TableRequestHandler {
77
Jian Li69f66632016-01-15 12:27:42 -080078 private static final String NO_ROWS_MESSAGE = "No groups found";
Jian Li8baf4472016-01-15 15:08:09 -080079 private static final String NOT_SUPPORT_MESSAGE = "Groups not supported";
Jian Li69f66632016-01-15 12:27:42 -080080
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070081 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 Li8baf4472016-01-15 15:08:09 -080091 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 Li69f66632016-01-15 12:27:42 -0800102 return NO_ROWS_MESSAGE;
103 }
104
105 @Override
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700106 protected TableModel createTableModel() {
107 TableModel tm = super.createTableModel();
Simon Hunt3e4ccaf2016-01-12 19:59:54 -0800108 tm.setFormatter(ID, HexFormatter.INSTANCE);
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700109 tm.setFormatter(TYPE, EnumFormatter.INSTANCE);
Simon Huntbe60dde2016-01-13 12:26:56 -0800110 tm.setFormatter(PACKETS, NumberFormatter.INTEGER);
111 tm.setFormatter(BYTES, NumberFormatter.INTEGER);
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700112 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 Hunt8a0429a2017-01-06 16:52:47 -0800130 .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 Coleff3dc672015-05-06 12:59:38 -0700136 }
137
138 private final class BucketFormatter implements CellFormatter {
Bri Prebilic Cole76f632c2015-05-28 17:06:02 -0700139 private static final String BREAK = "<br>";
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700140
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 Cole9467a232015-05-06 16:59:05 -0700152 .append(b.bytes())
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700153 .append(" Packets: ")
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700154 .append(b.packets())
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700155 .append(" Actions: ")
156 .append(b.treatment().allInstructions())
Bri Prebilic Cole76f632c2015-05-28 17:06:02 -0700157 .append(BREAK);
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700158 }
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700159
160 return sb.toString();
161 }
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700162 }
163 }
164}