blob: 5860a6de2f26bd6f20a3cf836c7348a2463aa481 [file] [log] [blame]
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -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
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070019import com.fasterxml.jackson.databind.node.ObjectNode;
Simon Huntabd16f62015-05-01 13:14:40 -070020import com.google.common.base.Strings;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070021import com.google.common.collect.ImmutableSet;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070022import org.onosproject.net.DeviceId;
23import org.onosproject.net.flow.FlowEntry;
24import org.onosproject.net.flow.FlowRuleService;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070025import org.onosproject.net.flow.criteria.Criterion;
26import org.onosproject.net.flow.instructions.Instruction;
Simon Huntd2747a02015-04-30 22:41:16 -070027import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070028import org.onosproject.ui.UiMessageHandler;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070029import org.onosproject.ui.table.CellFormatter;
Simon Hunt3d1b0652015-05-05 17:27:24 -070030import org.onosproject.ui.table.TableModel;
Simon Huntabd16f62015-05-01 13:14:40 -070031import org.onosproject.ui.table.TableRequestHandler;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070032import org.onosproject.ui.table.cell.EnumFormatter;
Simon Hunt3e4ccaf2016-01-12 19:59:54 -080033import org.onosproject.ui.table.cell.HexFormatter;
34import org.onosproject.ui.table.cell.HexLongFormatter;
Simon Huntbe60dde2016-01-13 12:26:56 -080035import org.onosproject.ui.table.cell.NumberFormatter;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070036
Simon Huntd2747a02015-04-30 22:41:16 -070037import java.util.Collection;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070038import java.util.List;
39import java.util.Set;
40
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070041/**
42 * Message handler for flow view related messages.
43 */
Simon Hunta0ddb022015-05-01 09:53:01 -070044public class FlowViewMessageHandler extends UiMessageHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070045
46 private static final String FLOW_DATA_REQ = "flowDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070047 private static final String FLOW_DATA_RESP = "flowDataResponse";
48 private static final String FLOWS = "flows";
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070049
Simon Huntabd16f62015-05-01 13:14:40 -070050 private static final String ID = "id";
51 private static final String APP_ID = "appId";
52 private static final String GROUP_ID = "groupId";
53 private static final String TABLE_ID = "tableId";
54 private static final String PRIORITY = "priority";
55 private static final String SELECTOR = "selector";
56 private static final String TREATMENT = "treatment";
57 private static final String TIMEOUT = "timeout";
58 private static final String PERMANENT = "permanent";
59 private static final String STATE = "state";
Bri Prebilic Cole641b97b2015-05-05 14:47:40 -070060 private static final String PACKETS = "packets";
61 private static final String BYTES = "bytes";
Simon Huntabd16f62015-05-01 13:14:40 -070062
63 private static final String COMMA = ", ";
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070064
Simon Hunt3d1b0652015-05-05 17:27:24 -070065 private static final String[] COL_IDS = {
66 ID, APP_ID, GROUP_ID, TABLE_ID, PRIORITY, SELECTOR,
67 TREATMENT, TIMEOUT, PERMANENT, STATE, PACKETS, BYTES
68 };
69
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070070 @Override
Simon Huntda580882015-05-12 20:58:18 -070071 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -070072 return ImmutableSet.of(new FlowDataRequest());
73 }
74
Simon Huntabd16f62015-05-01 13:14:40 -070075 // handler for flow table requests
76 private final class FlowDataRequest extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070077
Jian Li69f66632016-01-15 12:27:42 -080078 private static final String NO_ROWS_MESSAGE = "No flows found";
79
Simon Huntd2747a02015-04-30 22:41:16 -070080 private FlowDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070081 super(FLOW_DATA_REQ, FLOW_DATA_RESP, FLOWS);
Simon Huntd2747a02015-04-30 22:41:16 -070082 }
83
84 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070085 protected String[] getColumnIds() {
86 return COL_IDS;
Simon Hunt44aa2f82015-04-30 15:01:35 -070087 }
Simon Hunt44aa2f82015-04-30 15:01:35 -070088
Simon Hunt3d1b0652015-05-05 17:27:24 -070089 @Override
Jian Li69f66632016-01-15 12:27:42 -080090 protected String noRowsMessage() {
91 return NO_ROWS_MESSAGE;
92 }
93
94 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070095 protected TableModel createTableModel() {
96 TableModel tm = super.createTableModel();
Simon Hunt3e4ccaf2016-01-12 19:59:54 -080097 tm.setFormatter(ID, HexLongFormatter.INSTANCE);
98 tm.setFormatter(GROUP_ID, HexFormatter.INSTANCE);
Simon Huntbe60dde2016-01-13 12:26:56 -080099 tm.setFormatter(STATE, EnumFormatter.INSTANCE);
100 tm.setFormatter(PACKETS, NumberFormatter.INTEGER);
101 tm.setFormatter(BYTES, NumberFormatter.INTEGER);
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700102 tm.setFormatter(SELECTOR, new SelectorFormatter());
103 tm.setFormatter(TREATMENT, new TreatmentFormatter());
Simon Hunt3d1b0652015-05-05 17:27:24 -0700104 return tm;
105 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700106
Simon Hunt3d1b0652015-05-05 17:27:24 -0700107 @Override
108 protected void populateTable(TableModel tm, ObjectNode payload) {
109 String uri = string(payload, "devId");
110 if (!Strings.isNullOrEmpty(uri)) {
111 DeviceId deviceId = DeviceId.deviceId(uri);
112 FlowRuleService frs = get(FlowRuleService.class);
113 for (FlowEntry flow : frs.getFlowEntries(deviceId)) {
114 populateRow(tm.addRow(), flow);
115 }
116 }
117 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700118
Simon Hunt3d1b0652015-05-05 17:27:24 -0700119 private void populateRow(TableModel.Row row, FlowEntry flow) {
120 row.cell(ID, flow.id().value())
121 .cell(APP_ID, flow.appId())
122 .cell(GROUP_ID, flow.groupId().id())
123 .cell(TABLE_ID, flow.tableId())
124 .cell(PRIORITY, flow.priority())
Simon Hunt3d1b0652015-05-05 17:27:24 -0700125 .cell(TIMEOUT, flow.timeout())
126 .cell(PERMANENT, flow.isPermanent())
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700127 .cell(STATE, flow.state())
Simon Hunt3d1b0652015-05-05 17:27:24 -0700128 .cell(PACKETS, flow.packets())
Simon Huntbe60dde2016-01-13 12:26:56 -0800129 .cell(BYTES, flow.bytes())
130 .cell(SELECTOR, flow)
131 .cell(TREATMENT, flow);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700132 }
133
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700134 private final class SelectorFormatter implements CellFormatter {
135 @Override
136 public String format(Object value) {
137 FlowEntry flow = (FlowEntry) value;
138 Set<Criterion> criteria = flow.selector().criteria();
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700139
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700140 if (criteria.isEmpty()) {
141 return "(No traffic selector criteria for this flow)";
142 }
143 StringBuilder sb = new StringBuilder("Criteria: ");
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700144 for (Criterion c : criteria) {
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700145 sb.append(c).append(COMMA);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700146 }
147 removeTrailingComma(sb);
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700148
149 return sb.toString();
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700150 }
151 }
152
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700153 private final class TreatmentFormatter implements CellFormatter {
154 @Override
155 public String format(Object value) {
156 FlowEntry flow = (FlowEntry) value;
157 List<Instruction> instructions = flow.treatment().allInstructions();
158
159 if (instructions.isEmpty()) {
160 return "(No traffic treatment instructions for this flow)";
161 }
162 StringBuilder sb = new StringBuilder("Treatment Instructions: ");
163 for (Instruction i : instructions) {
164 sb.append(i).append(COMMA);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700165 }
166 removeTrailingComma(sb);
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700167
168 return sb.toString();
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700169 }
170 }
171
172 private StringBuilder removeTrailingComma(StringBuilder sb) {
173 int pos = sb.lastIndexOf(COMMA);
174 sb.delete(pos, sb.length());
175 return sb;
176 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700177 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700178}