blob: 5780e9fd7124c5b81868b8b1c0429ac77263e165 [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
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 Hunt3e4ccaf2016-01-12 19:59:54 -080080 tm.setFormatter(ID, HexFormatter.INSTANCE);
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070081 tm.setFormatter(TYPE, EnumFormatter.INSTANCE);
Simon Huntbe60dde2016-01-13 12:26:56 -080082 tm.setFormatter(PACKETS, NumberFormatter.INTEGER);
83 tm.setFormatter(BYTES, NumberFormatter.INTEGER);
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070084 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 Cole76f632c2015-05-28 17:06:02 -0700111 private static final String BREAK = "<br>";
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700112
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 Cole9467a232015-05-06 16:59:05 -0700124 .append(b.bytes())
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700125 .append(" Packets: ")
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700126 .append(b.packets())
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700127 .append(" Actions: ")
128 .append(b.treatment().allInstructions())
Bri Prebilic Cole76f632c2015-05-28 17:06:02 -0700129 .append(BREAK);
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700130 }
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700131
132 return sb.toString();
133 }
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700134 }
135 }
136}