blob: 46f9f9ec87d056bbab2b4102b31a5cccecdefb59 [file] [log] [blame]
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -07003 *
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;
Viswanath KSPd1212c72016-10-21 01:03:35 +053022import org.onosproject.core.ApplicationId;
23import org.onosproject.core.DefaultApplicationId;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070024import org.onosproject.net.DeviceId;
25import org.onosproject.net.flow.FlowEntry;
Viswanath KSPd1212c72016-10-21 01:03:35 +053026import org.onosproject.net.flow.FlowRule;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070027import org.onosproject.net.flow.FlowRuleService;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070028import org.onosproject.net.flow.criteria.Criterion;
29import org.onosproject.net.flow.instructions.Instruction;
Simon Huntd2747a02015-04-30 22:41:16 -070030import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070031import org.onosproject.ui.UiMessageHandler;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070032import org.onosproject.ui.table.CellFormatter;
Simon Hunt3d1b0652015-05-05 17:27:24 -070033import org.onosproject.ui.table.TableModel;
Simon Huntabd16f62015-05-01 13:14:40 -070034import org.onosproject.ui.table.TableRequestHandler;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070035import org.onosproject.ui.table.cell.EnumFormatter;
Simon Hunt3e4ccaf2016-01-12 19:59:54 -080036import org.onosproject.ui.table.cell.HexFormatter;
37import org.onosproject.ui.table.cell.HexLongFormatter;
Simon Huntbe60dde2016-01-13 12:26:56 -080038import org.onosproject.ui.table.cell.NumberFormatter;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070039
Simon Huntd2747a02015-04-30 22:41:16 -070040import java.util.Collection;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070041import java.util.List;
42import java.util.Set;
43
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070044/**
45 * Message handler for flow view related messages.
46 */
Simon Hunta0ddb022015-05-01 09:53:01 -070047public class FlowViewMessageHandler extends UiMessageHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070048
49 private static final String FLOW_DATA_REQ = "flowDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070050 private static final String FLOW_DATA_RESP = "flowDataResponse";
51 private static final String FLOWS = "flows";
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070052
Viswanath KSPd1212c72016-10-21 01:03:35 +053053 private static final String FLOW_DETAILS_REQ = "flowDetailsRequest";
54 private static final String FLOW_DETAILS_RESP = "flowDetailsResponse";
55 private static final String DETAILS = "details";
56 private static final String FLOW_PRIORITY = "priority";
57
Simon Huntabd16f62015-05-01 13:14:40 -070058 private static final String ID = "id";
Viswanath KSPd1212c72016-10-21 01:03:35 +053059 private static final String FLOW_ID = "flowId";
Simon Huntabd16f62015-05-01 13:14:40 -070060 private static final String APP_ID = "appId";
61 private static final String GROUP_ID = "groupId";
62 private static final String TABLE_ID = "tableId";
63 private static final String PRIORITY = "priority";
64 private static final String SELECTOR = "selector";
65 private static final String TREATMENT = "treatment";
66 private static final String TIMEOUT = "timeout";
67 private static final String PERMANENT = "permanent";
68 private static final String STATE = "state";
Bri Prebilic Cole641b97b2015-05-05 14:47:40 -070069 private static final String PACKETS = "packets";
70 private static final String BYTES = "bytes";
Simon Huntabd16f62015-05-01 13:14:40 -070071
72 private static final String COMMA = ", ";
Viswanath KSP70e0d142016-10-28 21:58:32 +053073 private static final String OX = "0x";
74 private static final String EMPTY = "";
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070075
Simon Hunt3d1b0652015-05-05 17:27:24 -070076 private static final String[] COL_IDS = {
77 ID, APP_ID, GROUP_ID, TABLE_ID, PRIORITY, SELECTOR,
78 TREATMENT, TIMEOUT, PERMANENT, STATE, PACKETS, BYTES
79 };
80
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070081 @Override
Simon Huntda580882015-05-12 20:58:18 -070082 protected Collection<RequestHandler> createRequestHandlers() {
Viswanath KSPd1212c72016-10-21 01:03:35 +053083 return ImmutableSet.of(
84 new FlowDataRequest(),
85 new DetailRequestHandler()
86 );
Simon Huntd2747a02015-04-30 22:41:16 -070087 }
88
Simon Huntabd16f62015-05-01 13:14:40 -070089 // handler for flow table requests
90 private final class FlowDataRequest extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070091
Jian Li69f66632016-01-15 12:27:42 -080092 private static final String NO_ROWS_MESSAGE = "No flows found";
93
Simon Huntd2747a02015-04-30 22:41:16 -070094 private FlowDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070095 super(FLOW_DATA_REQ, FLOW_DATA_RESP, FLOWS);
Simon Huntd2747a02015-04-30 22:41:16 -070096 }
97
98 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070099 protected String[] getColumnIds() {
100 return COL_IDS;
Simon Hunt44aa2f82015-04-30 15:01:35 -0700101 }
Simon Hunt44aa2f82015-04-30 15:01:35 -0700102
Simon Hunt3d1b0652015-05-05 17:27:24 -0700103 @Override
Jian Li8baf4472016-01-15 15:08:09 -0800104 protected String noRowsMessage(ObjectNode payload) {
Jian Li69f66632016-01-15 12:27:42 -0800105 return NO_ROWS_MESSAGE;
106 }
107
108 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700109 protected TableModel createTableModel() {
110 TableModel tm = super.createTableModel();
Simon Hunt3e4ccaf2016-01-12 19:59:54 -0800111 tm.setFormatter(ID, HexLongFormatter.INSTANCE);
112 tm.setFormatter(GROUP_ID, HexFormatter.INSTANCE);
Simon Huntbe60dde2016-01-13 12:26:56 -0800113 tm.setFormatter(STATE, EnumFormatter.INSTANCE);
114 tm.setFormatter(PACKETS, NumberFormatter.INTEGER);
115 tm.setFormatter(BYTES, NumberFormatter.INTEGER);
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700116 tm.setFormatter(SELECTOR, new SelectorFormatter());
117 tm.setFormatter(TREATMENT, new TreatmentFormatter());
Simon Hunt3d1b0652015-05-05 17:27:24 -0700118 return tm;
119 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700120
Simon Hunt3d1b0652015-05-05 17:27:24 -0700121 @Override
122 protected void populateTable(TableModel tm, ObjectNode payload) {
123 String uri = string(payload, "devId");
124 if (!Strings.isNullOrEmpty(uri)) {
125 DeviceId deviceId = DeviceId.deviceId(uri);
126 FlowRuleService frs = get(FlowRuleService.class);
127 for (FlowEntry flow : frs.getFlowEntries(deviceId)) {
128 populateRow(tm.addRow(), flow);
129 }
130 }
131 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700132
Simon Hunt3d1b0652015-05-05 17:27:24 -0700133 private void populateRow(TableModel.Row row, FlowEntry flow) {
134 row.cell(ID, flow.id().value())
Simon Hunt8a0429a2017-01-06 16:52:47 -0800135 .cell(APP_ID, flow.appId())
136 .cell(GROUP_ID, flow.groupId().id())
137 .cell(TABLE_ID, flow.tableId())
138 .cell(PRIORITY, flow.priority())
139 .cell(TIMEOUT, flow.timeout())
140 .cell(PERMANENT, flow.isPermanent())
141 .cell(STATE, flow.state())
142 .cell(PACKETS, flow.packets())
143 .cell(BYTES, flow.bytes())
144 .cell(SELECTOR, flow)
145 .cell(TREATMENT, flow);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700146 }
147
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700148 private final class SelectorFormatter implements CellFormatter {
149 @Override
150 public String format(Object value) {
151 FlowEntry flow = (FlowEntry) value;
152 Set<Criterion> criteria = flow.selector().criteria();
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700153
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700154 if (criteria.isEmpty()) {
155 return "(No traffic selector criteria for this flow)";
156 }
157 StringBuilder sb = new StringBuilder("Criteria: ");
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700158 for (Criterion c : criteria) {
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700159 sb.append(c).append(COMMA);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700160 }
161 removeTrailingComma(sb);
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700162
163 return sb.toString();
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700164 }
165 }
166
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700167 private final class TreatmentFormatter implements CellFormatter {
168 @Override
169 public String format(Object value) {
170 FlowEntry flow = (FlowEntry) value;
171 List<Instruction> instructions = flow.treatment().allInstructions();
172
Konstantinos Kanonakisa7592bc2016-04-13 10:24:15 -0500173 if (instructions.isEmpty()
174 && flow.treatment().metered() == null
175 && flow.treatment().tableTransition() == null) {
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700176 return "(No traffic treatment instructions for this flow)";
177 }
178 StringBuilder sb = new StringBuilder("Treatment Instructions: ");
179 for (Instruction i : instructions) {
180 sb.append(i).append(COMMA);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700181 }
Konstantinos Kanonakis845c3282016-04-01 17:34:20 -0500182 if (flow.treatment().metered() != null) {
183 sb.append(flow.treatment().metered().toString()).append(COMMA);
184 }
185 if (flow.treatment().tableTransition() != null) {
186 sb.append(flow.treatment().tableTransition().toString()).append(COMMA);
187 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700188 removeTrailingComma(sb);
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700189
190 return sb.toString();
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700191 }
192 }
193
194 private StringBuilder removeTrailingComma(StringBuilder sb) {
195 int pos = sb.lastIndexOf(COMMA);
196 sb.delete(pos, sb.length());
197 return sb;
198 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700199 }
Viswanath KSPd1212c72016-10-21 01:03:35 +0530200
201 private final class DetailRequestHandler extends RequestHandler {
202 private DetailRequestHandler() {
203 super(FLOW_DETAILS_REQ);
204 }
205
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530206 private FlowEntry findFlowById(String appIdText, String flowId) {
Viswanath KSP70e0d142016-10-28 21:58:32 +0530207 String strippedFlowId = flowId.replaceAll(OX, EMPTY);
Viswanath KSPd1212c72016-10-21 01:03:35 +0530208 FlowRuleService fs = get(FlowRuleService.class);
209 int appIdInt = Integer.parseInt(appIdText);
Viswanath KSP70e0d142016-10-28 21:58:32 +0530210 ApplicationId appId = new DefaultApplicationId(appIdInt, DETAILS);
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530211 Iterable<FlowEntry> entries = fs.getFlowEntriesById(appId);
Viswanath KSPd1212c72016-10-21 01:03:35 +0530212
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530213 for (FlowEntry entry : entries) {
214 if (entry.id().toString().equals(strippedFlowId)) {
215 return entry;
Viswanath KSPd1212c72016-10-21 01:03:35 +0530216 }
217 }
218
219 return null;
220 }
221
Viswanath KSP70e0d142016-10-28 21:58:32 +0530222 private String decorateFlowId(FlowRule flow) {
223 return OX + flow.id();
224 }
225
Viswanath KSPed55ecf2016-12-22 18:15:45 +0530226 private String decorateGroupId(FlowRule flow) {
227 return OX + flow.groupId().id();
228 }
229
230 private String getCriteriaString(FlowRule flow) {
231 Set<Criterion> criteria = flow.selector().criteria();
232 StringBuilder sb = new StringBuilder();
233 for (Criterion c : criteria) {
234 sb.append(c).append(COMMA);
235 }
236 int pos = sb.lastIndexOf(COMMA);
237 sb.delete(pos, sb.length());
238
239 return sb.toString();
240 }
241
242 private String getTreatmentString(FlowRule flow) {
243 List<Instruction> instructions = flow.treatment().allInstructions();
244 StringBuilder sb = new StringBuilder();
245 for (Instruction inst : instructions) {
246 sb.append(inst).append(COMMA);
247 }
248 if (flow.treatment().metered() != null) {
249 sb.append(flow.treatment().metered().toString()).append(COMMA);
250 }
251 if (flow.treatment().tableTransition() != null) {
252 sb.append(flow.treatment().tableTransition().toString()).append(COMMA);
253 }
254 int pos = sb.lastIndexOf(COMMA);
255 sb.delete(pos, sb.length());
256
257 return sb.toString();
258 }
259
Viswanath KSPd1212c72016-10-21 01:03:35 +0530260 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800261 public void process(ObjectNode payload) {
Viswanath KSPd1212c72016-10-21 01:03:35 +0530262
263 String flowId = string(payload, FLOW_ID);
264 String appId = string(payload, APP_ID);
265 FlowRule flow = findFlowById(appId, flowId);
Viswanath KSP70e0d142016-10-28 21:58:32 +0530266 if (flow != null) {
267 ObjectNode data = objectNode();
Viswanath KSPd1212c72016-10-21 01:03:35 +0530268
Viswanath KSPed55ecf2016-12-22 18:15:45 +0530269
Viswanath KSP70e0d142016-10-28 21:58:32 +0530270 data.put(FLOW_ID, decorateFlowId(flow));
271 data.put(FLOW_PRIORITY, flow.priority());
Viswanath KSPed55ecf2016-12-22 18:15:45 +0530272 data.put(GROUP_ID, decorateGroupId(flow));
273 data.put(APP_ID, flow.appId());
274 data.put(TABLE_ID, flow.tableId());
275 data.put(TIMEOUT, flow.hardTimeout());
276 data.put(PERMANENT, Boolean.toString(flow.isPermanent()));
277 data.put(SELECTOR, getCriteriaString(flow));
278 data.put(TREATMENT, getTreatmentString(flow));
279
Viswanath KSPd1212c72016-10-21 01:03:35 +0530280
Viswanath KSP70e0d142016-10-28 21:58:32 +0530281 //TODO put more detail info to data
Viswanath KSPd1212c72016-10-21 01:03:35 +0530282
Viswanath KSP70e0d142016-10-28 21:58:32 +0530283 ObjectNode rootNode = objectNode();
284 rootNode.set(DETAILS, data);
285 sendMessage(FLOW_DETAILS_RESP, rootNode);
286 }
Viswanath KSPd1212c72016-10-21 01:03:35 +0530287 }
288 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700289}