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