blob: ab61a6a3c57c73ce6c28e19ac83cee114ae83691 [file] [log] [blame]
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -07001/*
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
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;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.group.Group;
24import org.onosproject.net.group.GroupBucket;
25import org.onosproject.net.group.GroupService;
26import org.onosproject.ui.RequestHandler;
27import org.onosproject.ui.UiMessageHandler;
28import org.onosproject.ui.table.CellFormatter;
29import org.onosproject.ui.table.TableModel;
30import org.onosproject.ui.table.TableRequestHandler;
31import org.onosproject.ui.table.cell.EnumFormatter;
Simon Hunt3e4ccaf2016-01-12 19:59:54 -080032import org.onosproject.ui.table.cell.HexFormatter;
Simon Huntbe60dde2016-01-13 12:26:56 -080033import org.onosproject.ui.table.cell.NumberFormatter;
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070034
35import java.util.Collection;
36import java.util.List;
37
38
39/**
40 * Message handler for group view related messages.
41 */
42public 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 Huntda580882015-05-12 20:58:18 -070061 protected Collection<RequestHandler> createRequestHandlers() {
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070062 return ImmutableSet.of(new GroupDataRequest());
63 }
64
65 // handler for group table requests
66 private final class GroupDataRequest extends TableRequestHandler {
67
Jian Li69f66632016-01-15 12:27:42 -080068 private static final String NO_ROWS_MESSAGE = "No groups found";
69
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070070 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 Li69f66632016-01-15 12:27:42 -080080 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 Coleff3dc672015-05-06 12:59:38 -070086 protected TableModel createTableModel() {
87 TableModel tm = super.createTableModel();
Simon Hunt3e4ccaf2016-01-12 19:59:54 -080088 tm.setFormatter(ID, HexFormatter.INSTANCE);
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070089 tm.setFormatter(TYPE, EnumFormatter.INSTANCE);
Simon Huntbe60dde2016-01-13 12:26:56 -080090 tm.setFormatter(PACKETS, NumberFormatter.INTEGER);
91 tm.setFormatter(BYTES, NumberFormatter.INTEGER);
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070092 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 Cole76f632c2015-05-28 17:06:02 -0700119 private static final String BREAK = "<br>";
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700120
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 Cole9467a232015-05-06 16:59:05 -0700132 .append(b.bytes())
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700133 .append(" Packets: ")
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700134 .append(b.packets())
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700135 .append(" Actions: ")
136 .append(b.treatment().allInstructions())
Bri Prebilic Cole76f632c2015-05-28 17:06:02 -0700137 .append(BREAK);
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700138 }
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700139
140 return sb.toString();
141 }
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700142 }
143 }
144}