blob: 8b33c40312526dae81aa229e4820263915adf483 [file] [log] [blame]
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Simon Hunt8f057ee2017-07-19 14:05:26 -070019import com.fasterxml.jackson.databind.node.ArrayNode;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070020import com.fasterxml.jackson.databind.node.ObjectNode;
Simon Huntabd16f62015-05-01 13:14:40 -070021import com.google.common.base.Strings;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070022import com.google.common.collect.ImmutableSet;
Simon Hunt36b658d2017-07-14 16:20:11 -070023import org.onosproject.app.ApplicationService;
24import org.onosproject.core.Application;
Viswanath KSPd1212c72016-10-21 01:03:35 +053025import org.onosproject.core.ApplicationId;
Thomas Vachuskafbf08642017-10-04 13:49:12 -070026import org.onosproject.core.CoreService;
Viswanath KSPd1212c72016-10-21 01:03:35 +053027import org.onosproject.core.DefaultApplicationId;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070028import org.onosproject.net.DeviceId;
29import org.onosproject.net.flow.FlowEntry;
Viswanath KSPd1212c72016-10-21 01:03:35 +053030import org.onosproject.net.flow.FlowRule;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070031import org.onosproject.net.flow.FlowRuleService;
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +053032import org.onosproject.net.flow.TrafficTreatment;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070033import org.onosproject.net.flow.criteria.Criterion;
34import org.onosproject.net.flow.instructions.Instruction;
Simon Hunt8f057ee2017-07-19 14:05:26 -070035import org.onosproject.net.flow.instructions.Instructions;
Simon Huntd2747a02015-04-30 22:41:16 -070036import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070037import org.onosproject.ui.UiMessageHandler;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070038import org.onosproject.ui.table.CellFormatter;
Simon Hunt3d1b0652015-05-05 17:27:24 -070039import org.onosproject.ui.table.TableModel;
Simon Huntabd16f62015-05-01 13:14:40 -070040import org.onosproject.ui.table.TableRequestHandler;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070041import org.onosproject.ui.table.cell.EnumFormatter;
Simon Hunt3e4ccaf2016-01-12 19:59:54 -080042import org.onosproject.ui.table.cell.HexLongFormatter;
Simon Huntbe60dde2016-01-13 12:26:56 -080043import org.onosproject.ui.table.cell.NumberFormatter;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070044
Simon Huntd2747a02015-04-30 22:41:16 -070045import java.util.Collection;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070046import java.util.List;
Simon Hunt36b658d2017-07-14 16:20:11 -070047import java.util.Map;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070048import java.util.Set;
Simon Hunt36b658d2017-07-14 16:20:11 -070049import java.util.stream.Collectors;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070050
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +053051
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070052/**
53 * Message handler for flow view related messages.
54 */
Simon Hunta0ddb022015-05-01 09:53:01 -070055public class FlowViewMessageHandler extends UiMessageHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070056
57 private static final String FLOW_DATA_REQ = "flowDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070058 private static final String FLOW_DATA_RESP = "flowDataResponse";
59 private static final String FLOWS = "flows";
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070060
Viswanath KSPd1212c72016-10-21 01:03:35 +053061 private static final String FLOW_DETAILS_REQ = "flowDetailsRequest";
62 private static final String FLOW_DETAILS_RESP = "flowDetailsResponse";
63 private static final String DETAILS = "details";
64 private static final String FLOW_PRIORITY = "priority";
65
Simon Hunt8f057ee2017-07-19 14:05:26 -070066 private static final String DEV_ID = "devId";
67
Simon Huntabd16f62015-05-01 13:14:40 -070068 private static final String ID = "id";
Viswanath KSPd1212c72016-10-21 01:03:35 +053069 private static final String FLOW_ID = "flowId";
Simon Huntabd16f62015-05-01 13:14:40 -070070 private static final String APP_ID = "appId";
Simon Hunt36b658d2017-07-14 16:20:11 -070071 private static final String APP_NAME = "appName";
Simon Huntabd16f62015-05-01 13:14:40 -070072 private static final String GROUP_ID = "groupId";
73 private static final String TABLE_ID = "tableId";
74 private static final String PRIORITY = "priority";
Simon Hunt9bff5952017-07-13 14:05:09 -070075 private static final String SELECTOR_C = "selector_c"; // for table column
Simon Huntabd16f62015-05-01 13:14:40 -070076 private static final String SELECTOR = "selector";
Simon Hunt9bff5952017-07-13 14:05:09 -070077 private static final String TREATMENT_C = "treatment_c"; // for table column
Simon Huntabd16f62015-05-01 13:14:40 -070078 private static final String TREATMENT = "treatment";
Thomas Vachuskafbf08642017-10-04 13:49:12 -070079 private static final String IDLE_TIMEOUT = "idleTimeout";
80 private static final String HARD_TIMEOUT = "hardTimeout";
Simon Huntabd16f62015-05-01 13:14:40 -070081 private static final String PERMANENT = "permanent";
82 private static final String STATE = "state";
Bri Prebilic Cole641b97b2015-05-05 14:47:40 -070083 private static final String PACKETS = "packets";
Simon Hunt9bff5952017-07-13 14:05:09 -070084 private static final String DURATION = "duration";
Bri Prebilic Cole641b97b2015-05-05 14:47:40 -070085 private static final String BYTES = "bytes";
Simon Huntabd16f62015-05-01 13:14:40 -070086
87 private static final String COMMA = ", ";
Viswanath KSP70e0d142016-10-28 21:58:32 +053088 private static final String OX = "0x";
89 private static final String EMPTY = "";
Simon Hunt8f057ee2017-07-19 14:05:26 -070090 private static final String SPACE = " ";
91 private static final String COLON = ":";
92 private static final String ANGLE_O = "<";
93 private static final String ANGLE_C = ">";
94 private static final String SQUARE_O = "[";
95 private static final String SQUARE_C = "]";
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070096
Simon Hunt36b658d2017-07-14 16:20:11 -070097 private static final String ONOS_PREFIX = "org.onosproject.";
98 private static final String ONOS_MARKER = "*";
99
Simon Hunt8f057ee2017-07-19 14:05:26 -0700100 // TODO: replace the use of the following constants with localized text
101 private static final String MSG_NO_SELECTOR =
102 "(No traffic selector criteria for this flow)";
103 private static final String MSG_NO_TREATMENT =
104 "(No traffic treatment instructions for this flow)";
105 private static final String NO_ROWS_NO_FLOWS = "No flows found";
106
107 private static final String CRITERIA = "Criteria";
108 private static final String TREATMENT_INSTRUCTIONS = "Treatment Instructions";
109 private static final String IMM = "imm";
110 private static final String DEF = "def";
111 private static final String METERED = "metered";
112 private static final String TRANSITION = "transition";
113 private static final String METADATA = "metadata";
114 private static final String CLEARED = "cleared";
115 private static final String UNKNOWN = "Unknown";
116
117
Simon Hunt3d1b0652015-05-05 17:27:24 -0700118 private static final String[] COL_IDS = {
Simon Hunt9bff5952017-07-13 14:05:09 -0700119 ID,
120 STATE,
121 BYTES,
122 PACKETS,
123 DURATION,
124 PRIORITY,
125 TABLE_ID,
126 APP_ID,
Simon Hunt36b658d2017-07-14 16:20:11 -0700127 APP_NAME,
Simon Hunt9bff5952017-07-13 14:05:09 -0700128
129 GROUP_ID,
Thomas Vachuskafbf08642017-10-04 13:49:12 -0700130 IDLE_TIMEOUT,
Simon Hunt9bff5952017-07-13 14:05:09 -0700131 PERMANENT,
132
133 SELECTOR_C,
134 SELECTOR,
135 TREATMENT_C,
136 TREATMENT,
Simon Hunt3d1b0652015-05-05 17:27:24 -0700137 };
138
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700139 @Override
Simon Huntda580882015-05-12 20:58:18 -0700140 protected Collection<RequestHandler> createRequestHandlers() {
Viswanath KSPd1212c72016-10-21 01:03:35 +0530141 return ImmutableSet.of(
142 new FlowDataRequest(),
143 new DetailRequestHandler()
144 );
Simon Huntd2747a02015-04-30 22:41:16 -0700145 }
146
Simon Hunt8f057ee2017-07-19 14:05:26 -0700147 private void removeTrailingComma(StringBuilder sb) {
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530148 int pos = sb.lastIndexOf(COMMA);
149 sb.delete(pos, sb.length());
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530150 }
151
Simon Hunt36b658d2017-07-14 16:20:11 -0700152 // Generate a map of shorts->application IDs
153 // (working around deficiency(?) in Application Service API)
154 private Map<Short, ApplicationId> appShortMap() {
155 Set<Application> apps =
156 get(ApplicationService.class).getApplications();
157
158 return apps.stream()
159 .collect(Collectors.toMap(a -> a.id().id(), Application::id));
160 }
161
162 // Return an application name, based on a lookup of the internal short ID
163 private String makeAppName(short id, Map<Short, ApplicationId> lookup) {
164 ApplicationId appId = lookup.get(id);
165 if (appId == null) {
Thomas Vachuskafbf08642017-10-04 13:49:12 -0700166 appId = get(CoreService.class).getAppId(id);
167 if (appId == null) {
168 return UNKNOWN + SPACE + ANGLE_O + id + ANGLE_C;
169 }
170 lookup.put(id, appId);
Simon Hunt36b658d2017-07-14 16:20:11 -0700171 }
172 String appName = appId.name();
173 return appName.startsWith(ONOS_PREFIX)
174 ? appName.replaceFirst(ONOS_PREFIX, ONOS_MARKER) : appName;
175 }
176
Simon Huntabd16f62015-05-01 13:14:40 -0700177 // handler for flow table requests
178 private final class FlowDataRequest extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -0700179
180 private FlowDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -0700181 super(FLOW_DATA_REQ, FLOW_DATA_RESP, FLOWS);
Simon Huntd2747a02015-04-30 22:41:16 -0700182 }
183
184 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700185 protected String[] getColumnIds() {
186 return COL_IDS;
Simon Hunt44aa2f82015-04-30 15:01:35 -0700187 }
Simon Hunt44aa2f82015-04-30 15:01:35 -0700188
Simon Hunt3d1b0652015-05-05 17:27:24 -0700189 @Override
Jian Li8baf4472016-01-15 15:08:09 -0800190 protected String noRowsMessage(ObjectNode payload) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700191 return NO_ROWS_NO_FLOWS;
Jian Li69f66632016-01-15 12:27:42 -0800192 }
193
194 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700195 protected TableModel createTableModel() {
196 TableModel tm = super.createTableModel();
Simon Hunt3e4ccaf2016-01-12 19:59:54 -0800197 tm.setFormatter(ID, HexLongFormatter.INSTANCE);
Simon Huntbe60dde2016-01-13 12:26:56 -0800198 tm.setFormatter(STATE, EnumFormatter.INSTANCE);
Simon Huntbe60dde2016-01-13 12:26:56 -0800199 tm.setFormatter(BYTES, NumberFormatter.INTEGER);
Simon Hunt9bff5952017-07-13 14:05:09 -0700200 tm.setFormatter(PACKETS, NumberFormatter.INTEGER);
201 tm.setFormatter(DURATION, NumberFormatter.INTEGER);
202
203 tm.setFormatter(SELECTOR_C, new SelectorShortFormatter());
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700204 tm.setFormatter(SELECTOR, new SelectorFormatter());
Simon Hunt9bff5952017-07-13 14:05:09 -0700205 tm.setFormatter(TREATMENT_C, new TreatmentShortFormatter());
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700206 tm.setFormatter(TREATMENT, new TreatmentFormatter());
Simon Hunt3d1b0652015-05-05 17:27:24 -0700207 return tm;
208 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700209
Simon Hunt3d1b0652015-05-05 17:27:24 -0700210 @Override
211 protected void populateTable(TableModel tm, ObjectNode payload) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700212 String uri = string(payload, DEV_ID);
Simon Hunt3d1b0652015-05-05 17:27:24 -0700213 if (!Strings.isNullOrEmpty(uri)) {
214 DeviceId deviceId = DeviceId.deviceId(uri);
Simon Hunt36b658d2017-07-14 16:20:11 -0700215 Map<Short, ApplicationId> lookup = appShortMap();
Simon Hunt3d1b0652015-05-05 17:27:24 -0700216 FlowRuleService frs = get(FlowRuleService.class);
Simon Hunt36b658d2017-07-14 16:20:11 -0700217
Simon Hunt3d1b0652015-05-05 17:27:24 -0700218 for (FlowEntry flow : frs.getFlowEntries(deviceId)) {
Simon Hunt36b658d2017-07-14 16:20:11 -0700219 populateRow(tm.addRow(), flow, lookup);
Simon Hunt3d1b0652015-05-05 17:27:24 -0700220 }
221 }
222 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700223
Simon Hunt36b658d2017-07-14 16:20:11 -0700224 private void populateRow(TableModel.Row row, FlowEntry flow,
225 Map<Short, ApplicationId> lookup) {
Simon Hunt3d1b0652015-05-05 17:27:24 -0700226 row.cell(ID, flow.id().value())
Simon Hunt9bff5952017-07-13 14:05:09 -0700227 .cell(STATE, flow.state())
228 .cell(BYTES, flow.bytes())
229 .cell(PACKETS, flow.packets())
230 .cell(DURATION, flow.life())
Simon Hunt8a0429a2017-01-06 16:52:47 -0800231 .cell(PRIORITY, flow.priority())
Simon Hunt9bff5952017-07-13 14:05:09 -0700232 .cell(TABLE_ID, flow.tableId())
233 .cell(APP_ID, flow.appId())
Simon Hunt36b658d2017-07-14 16:20:11 -0700234 .cell(APP_NAME, makeAppName(flow.appId(), lookup))
Simon Hunt9bff5952017-07-13 14:05:09 -0700235
236 .cell(GROUP_ID, flow.groupId().id())
Thomas Vachuskafbf08642017-10-04 13:49:12 -0700237 .cell(IDLE_TIMEOUT, flow.timeout())
Simon Hunt8a0429a2017-01-06 16:52:47 -0800238 .cell(PERMANENT, flow.isPermanent())
Simon Hunt9bff5952017-07-13 14:05:09 -0700239
240 .cell(SELECTOR_C, flow)
Simon Hunt8a0429a2017-01-06 16:52:47 -0800241 .cell(SELECTOR, flow)
Simon Hunt9bff5952017-07-13 14:05:09 -0700242 .cell(TREATMENT_C, flow)
Simon Hunt8a0429a2017-01-06 16:52:47 -0800243 .cell(TREATMENT, flow);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700244 }
245
Simon Hunt9bff5952017-07-13 14:05:09 -0700246 private class InternalSelectorFormatter implements CellFormatter {
247 private final boolean shortFormat;
248
249 InternalSelectorFormatter(boolean shortFormat) {
250 this.shortFormat = shortFormat;
251 }
252
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700253 @Override
254 public String format(Object value) {
255 FlowEntry flow = (FlowEntry) value;
256 Set<Criterion> criteria = flow.selector().criteria();
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700257
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700258 if (criteria.isEmpty()) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700259 return MSG_NO_SELECTOR;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700260 }
Simon Hunt9bff5952017-07-13 14:05:09 -0700261
262 StringBuilder sb = new StringBuilder();
263 if (!shortFormat) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700264 sb.append(CRITERIA).append(COLON).append(SPACE);
Simon Hunt9bff5952017-07-13 14:05:09 -0700265 }
266
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700267 for (Criterion c : criteria) {
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700268 sb.append(c).append(COMMA);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700269 }
270 removeTrailingComma(sb);
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700271
272 return sb.toString();
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700273 }
274 }
275
Simon Hunt9bff5952017-07-13 14:05:09 -0700276 private final class SelectorShortFormatter extends InternalSelectorFormatter {
277 SelectorShortFormatter() {
278 super(true);
279 }
280 }
281
282 private final class SelectorFormatter extends InternalSelectorFormatter {
283 SelectorFormatter() {
284 super(false);
285 }
286 }
287
288 private class InternalTreatmentFormatter implements CellFormatter {
289 private final boolean shortFormat;
290
291 InternalTreatmentFormatter(boolean shortFormat) {
292 this.shortFormat = shortFormat;
293 }
294
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700295 @Override
296 public String format(Object value) {
297 FlowEntry flow = (FlowEntry) value;
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530298 TrafficTreatment treatment = flow.treatment();
299 List<Instruction> imm = treatment.immediate();
300 List<Instruction> def = treatment.deferred();
301 if (imm.isEmpty() &&
302 def.isEmpty() &&
303 treatment.metered() == null &&
Simon Hunt8f057ee2017-07-19 14:05:26 -0700304 treatment.tableTransition() == null &&
305 treatment.writeMetadata() == null) {
306 return MSG_NO_TREATMENT;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700307 }
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530308
Simon Hunt9bff5952017-07-13 14:05:09 -0700309 StringBuilder sb = new StringBuilder();
310
311 if (!shortFormat) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700312 sb.append(TREATMENT_INSTRUCTIONS).append(COLON).append(SPACE);
Simon Hunt9bff5952017-07-13 14:05:09 -0700313 }
314
Simon Hunt8f057ee2017-07-19 14:05:26 -0700315 formatInstructs(sb, imm, IMM);
316 formatInstructs(sb, def, DEF);
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530317
Simon Hunt8f057ee2017-07-19 14:05:26 -0700318 addLabVal(sb, METERED, treatment.metered());
319 addLabVal(sb, TRANSITION, treatment.tableTransition());
320 addLabVal(sb, METADATA, treatment.writeMetadata());
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530321
Simon Hunt8f057ee2017-07-19 14:05:26 -0700322 sb.append(CLEARED).append(COLON)
323 .append(treatment.clearedDeferred());
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700324
325 return sb.toString();
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700326 }
Simon Hunt8f057ee2017-07-19 14:05:26 -0700327
328 private void addLabVal(StringBuilder sb, String label, Instruction value) {
329 if (value != null) {
330 sb.append(label).append(COLON).append(value).append(COMMA);
331 }
332 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700333 }
334
Simon Hunt9bff5952017-07-13 14:05:09 -0700335 private final class TreatmentShortFormatter extends InternalTreatmentFormatter {
336 TreatmentShortFormatter() {
337 super(true);
338 }
339 }
340
341 private final class TreatmentFormatter extends InternalTreatmentFormatter {
342 TreatmentFormatter() {
343 super(false);
344 }
345 }
346
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530347 private void formatInstructs(StringBuilder sb,
348 List<Instruction> instructs,
349 String type) {
350 if (!instructs.isEmpty()) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700351 sb.append(type).append(SQUARE_O);
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530352 for (Instruction i : instructs) {
353 sb.append(i).append(COMMA);
354 }
Simon Hunt8f057ee2017-07-19 14:05:26 -0700355 removeTrailingComma(sb);
356 sb.append(SQUARE_C).append(COMMA);
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530357 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700358 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700359 }
Viswanath KSPd1212c72016-10-21 01:03:35 +0530360
361 private final class DetailRequestHandler extends RequestHandler {
362 private DetailRequestHandler() {
363 super(FLOW_DETAILS_REQ);
364 }
365
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530366 private FlowEntry findFlowById(String appIdText, String flowId) {
Viswanath KSP70e0d142016-10-28 21:58:32 +0530367 String strippedFlowId = flowId.replaceAll(OX, EMPTY);
Viswanath KSPd1212c72016-10-21 01:03:35 +0530368 FlowRuleService fs = get(FlowRuleService.class);
369 int appIdInt = Integer.parseInt(appIdText);
Viswanath KSP70e0d142016-10-28 21:58:32 +0530370 ApplicationId appId = new DefaultApplicationId(appIdInt, DETAILS);
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530371 Iterable<FlowEntry> entries = fs.getFlowEntriesById(appId);
Viswanath KSPd1212c72016-10-21 01:03:35 +0530372
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530373 for (FlowEntry entry : entries) {
374 if (entry.id().toString().equals(strippedFlowId)) {
375 return entry;
Viswanath KSPd1212c72016-10-21 01:03:35 +0530376 }
377 }
378
379 return null;
380 }
381
Viswanath KSP70e0d142016-10-28 21:58:32 +0530382 private String decorateFlowId(FlowRule flow) {
383 return OX + flow.id();
384 }
385
Viswanath KSPed55ecf2016-12-22 18:15:45 +0530386 private String decorateGroupId(FlowRule flow) {
387 return OX + flow.groupId().id();
388 }
389
Viswanath KSPd1212c72016-10-21 01:03:35 +0530390 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800391 public void process(ObjectNode payload) {
Viswanath KSPd1212c72016-10-21 01:03:35 +0530392
393 String flowId = string(payload, FLOW_ID);
394 String appId = string(payload, APP_ID);
Simon Hunt9bff5952017-07-13 14:05:09 -0700395
396 FlowEntry flow = findFlowById(appId, flowId);
Viswanath KSP70e0d142016-10-28 21:58:32 +0530397 if (flow != null) {
398 ObjectNode data = objectNode();
Viswanath KSPd1212c72016-10-21 01:03:35 +0530399
Viswanath KSP70e0d142016-10-28 21:58:32 +0530400 data.put(FLOW_ID, decorateFlowId(flow));
Simon Hunt9bff5952017-07-13 14:05:09 -0700401
Simon Hunt8f057ee2017-07-19 14:05:26 -0700402 data.put(STATE, EnumFormatter.INSTANCE.format(flow.state()));
403 data.put(BYTES, NumberFormatter.INTEGER.format(flow.bytes()));
404 data.put(PACKETS, NumberFormatter.INTEGER.format(flow.packets()));
405 data.put(DURATION, NumberFormatter.INTEGER.format(flow.life()));
Simon Hunt9bff5952017-07-13 14:05:09 -0700406
Viswanath KSP70e0d142016-10-28 21:58:32 +0530407 data.put(FLOW_PRIORITY, flow.priority());
Viswanath KSPed55ecf2016-12-22 18:15:45 +0530408 data.put(TABLE_ID, flow.tableId());
Simon Hunt9bff5952017-07-13 14:05:09 -0700409 data.put(APP_ID, flow.appId());
Simon Hunt36b658d2017-07-14 16:20:11 -0700410 // NOTE: horribly inefficient... make a map and retrieve a single value...
411 data.put(APP_NAME, makeAppName(flow.appId(), appShortMap()));
Simon Hunt9bff5952017-07-13 14:05:09 -0700412
413 data.put(GROUP_ID, decorateGroupId(flow));
Thomas Vachuskafbf08642017-10-04 13:49:12 -0700414 data.put(IDLE_TIMEOUT, flow.timeout());
415 data.put(HARD_TIMEOUT, flow.hardTimeout());
Simon Hunt8f057ee2017-07-19 14:05:26 -0700416 data.put(PERMANENT, flow.isPermanent());
Simon Hunt9bff5952017-07-13 14:05:09 -0700417
Simon Hunt8f057ee2017-07-19 14:05:26 -0700418 data.set(SELECTOR, jsonCriteria(flow));
419 data.set(TREATMENT, jsonTreatment(flow));
Viswanath KSPed55ecf2016-12-22 18:15:45 +0530420
Viswanath KSP70e0d142016-10-28 21:58:32 +0530421 ObjectNode rootNode = objectNode();
422 rootNode.set(DETAILS, data);
423 sendMessage(FLOW_DETAILS_RESP, rootNode);
424 }
Viswanath KSPd1212c72016-10-21 01:03:35 +0530425 }
Simon Hunt8f057ee2017-07-19 14:05:26 -0700426
427 private ArrayNode jsonCriteria(FlowEntry flow) {
428 ArrayNode crits = arrayNode();
429 for (Criterion c : flow.selector().criteria()) {
430 crits.add(c.toString());
431 }
432 return crits;
433 }
434
435 private ObjectNode jsonTreatment(FlowEntry flow) {
436 ObjectNode treat = objectNode();
437 TrafficTreatment treatment = flow.treatment();
438 List<Instruction> imm = treatment.immediate();
439 List<Instruction> def = treatment.deferred();
440 Instructions.MeterInstruction meter = treatment.metered();
441 Instructions.TableTypeTransition table = treatment.tableTransition();
442 Instructions.MetadataInstruction meta = treatment.writeMetadata();
443
444 if (!imm.isEmpty()) {
445 treat.set(IMMED, jsonInstrList(imm));
446 }
447 if (!def.isEmpty()) {
448 treat.set(DEFER, jsonInstrList(def));
449 }
450 if (meter != null) {
451 treat.put(METER, meter.toString());
452 }
453 if (table != null) {
454 treat.put(TABLE, table.toString());
455 }
456 if (meta != null) {
457 treat.put(META, meta.toString());
458 }
459 treat.put(CLEARDEF, treatment.clearedDeferred());
460 return treat;
461 }
462
463 private ArrayNode jsonInstrList(List<Instruction> instructions) {
464 ArrayNode array = arrayNode();
465 for (Instruction i : instructions) {
466 array.add(i.toString());
467 }
468 return array;
469 }
Viswanath KSPd1212c72016-10-21 01:03:35 +0530470 }
Simon Hunt8f057ee2017-07-19 14:05:26 -0700471
472 // json structure keys
473 private static final String IMMED = "immed";
474 private static final String DEFER = "defer";
475 private static final String METER = "meter";
476 private static final String TABLE = "table";
477 private static final String META = "meta";
478 private static final String CLEARDEF = "clearDef";
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700479}