blob: c7d539b1d713655995e1a55f9fb3e5ec12359f54 [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;
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070032
33import java.util.Collection;
34import java.util.List;
35
36
37/**
38 * Message handler for group view related messages.
39 */
40public class GroupViewMessageHandler extends UiMessageHandler {
41
42 private static final String GROUP_DATA_REQ = "groupDataRequest";
43 private static final String GROUP_DATA_RESP = "groupDataResponse";
44 private static final String GROUPS = "groups";
45
46 private static final String ID = "id";
47 private static final String APP_ID = "app_id";
48 private static final String STATE = "state";
49 private static final String TYPE = "type";
50 private static final String PACKETS = "packets";
51 private static final String BYTES = "bytes";
52 private static final String BUCKETS = "buckets";
53
54 private static final String[] COL_IDS = {
55 ID, APP_ID, STATE, TYPE, PACKETS, BYTES, BUCKETS
56 };
57
58 @Override
Simon Huntda580882015-05-12 20:58:18 -070059 protected Collection<RequestHandler> createRequestHandlers() {
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070060 return ImmutableSet.of(new GroupDataRequest());
61 }
62
63 // handler for group table requests
64 private final class GroupDataRequest extends TableRequestHandler {
65
66 private GroupDataRequest() {
67 super(GROUP_DATA_REQ, GROUP_DATA_RESP, GROUPS);
68 }
69
70 @Override
71 protected String[] getColumnIds() {
72 return COL_IDS;
73 }
74
75 @Override
76 protected TableModel createTableModel() {
77 TableModel tm = super.createTableModel();
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070078 tm.setFormatter(TYPE, EnumFormatter.INSTANCE);
79 tm.setFormatter(BUCKETS, new BucketFormatter());
80 return tm;
81 }
82
83 @Override
84 protected void populateTable(TableModel tm, ObjectNode payload) {
85 String uri = string(payload, "devId");
86 if (!Strings.isNullOrEmpty(uri)) {
87 DeviceId deviceId = DeviceId.deviceId(uri);
88 GroupService gs = get(GroupService.class);
89 for (Group group : gs.getGroups(deviceId)) {
90 populateRow(tm.addRow(), group);
91 }
92 }
93 }
94
95 private void populateRow(TableModel.Row row, Group g) {
96 row.cell(ID, g.id().id())
97 .cell(APP_ID, g.appId().name())
98 .cell(STATE, g.state())
99 .cell(TYPE, g.type())
100 .cell(PACKETS, g.packets())
101 .cell(BYTES, g.bytes())
102 .cell(BUCKETS, g.buckets().buckets());
103 }
104
105 private final class BucketFormatter implements CellFormatter {
Bri Prebilic Cole76f632c2015-05-28 17:06:02 -0700106 private static final String BREAK = "<br>";
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700107
108 @Override
109 public String format(Object value) {
110 StringBuilder sb = new StringBuilder();
111 List<GroupBucket> buckets = (List<GroupBucket>) value;
112
113 if (buckets.isEmpty()) {
114 return "(No buckets for this group)";
115 }
116
117 for (GroupBucket b : buckets) {
118 sb.append("Bytes: ")
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700119 .append(b.bytes())
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700120 .append(" Packets: ")
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700121 .append(b.packets())
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700122 .append(" Actions: ")
123 .append(b.treatment().allInstructions())
Bri Prebilic Cole76f632c2015-05-28 17:06:02 -0700124 .append(BREAK);
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700125 }
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700126
127 return sb.toString();
128 }
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700129 }
130 }
131}