blob: 8f2a0a5efb3c2f50da53b4f83b3a5c52ecc27b59 [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 Vachuska3da62122017-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;
Yi Tseng18177a52017-09-08 01:26:59 -070041import org.onosproject.ui.table.cell.DefaultCellFormatter;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070042import org.onosproject.ui.table.cell.EnumFormatter;
Simon Hunt3e4ccaf2016-01-12 19:59:54 -080043import org.onosproject.ui.table.cell.HexLongFormatter;
Simon Huntbe60dde2016-01-13 12:26:56 -080044import org.onosproject.ui.table.cell.NumberFormatter;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070045
Simon Huntd2747a02015-04-30 22:41:16 -070046import java.util.Collection;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070047import java.util.List;
Simon Hunt36b658d2017-07-14 16:20:11 -070048import java.util.Map;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070049import java.util.Set;
Simon Hunt36b658d2017-07-14 16:20:11 -070050import java.util.stream.Collectors;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070051
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +053052
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070053/**
54 * Message handler for flow view related messages.
55 */
Simon Hunta0ddb022015-05-01 09:53:01 -070056public class FlowViewMessageHandler extends UiMessageHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070057
58 private static final String FLOW_DATA_REQ = "flowDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070059 private static final String FLOW_DATA_RESP = "flowDataResponse";
60 private static final String FLOWS = "flows";
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070061
Viswanath KSPd1212c72016-10-21 01:03:35 +053062 private static final String FLOW_DETAILS_REQ = "flowDetailsRequest";
63 private static final String FLOW_DETAILS_RESP = "flowDetailsResponse";
64 private static final String DETAILS = "details";
65 private static final String FLOW_PRIORITY = "priority";
66
Simon Hunt8f057ee2017-07-19 14:05:26 -070067 private static final String DEV_ID = "devId";
68
Simon Huntabd16f62015-05-01 13:14:40 -070069 private static final String ID = "id";
Viswanath KSPd1212c72016-10-21 01:03:35 +053070 private static final String FLOW_ID = "flowId";
Simon Huntabd16f62015-05-01 13:14:40 -070071 private static final String APP_ID = "appId";
Simon Hunt36b658d2017-07-14 16:20:11 -070072 private static final String APP_NAME = "appName";
Simon Huntabd16f62015-05-01 13:14:40 -070073 private static final String GROUP_ID = "groupId";
Yi Tseng18177a52017-09-08 01:26:59 -070074 private static final String TABLE_NAME = "tableName";
Simon Huntabd16f62015-05-01 13:14:40 -070075 private static final String PRIORITY = "priority";
Simon Hunt9bff5952017-07-13 14:05:09 -070076 private static final String SELECTOR_C = "selector_c"; // for table column
Simon Huntabd16f62015-05-01 13:14:40 -070077 private static final String SELECTOR = "selector";
Simon Hunt9bff5952017-07-13 14:05:09 -070078 private static final String TREATMENT_C = "treatment_c"; // for table column
Simon Huntabd16f62015-05-01 13:14:40 -070079 private static final String TREATMENT = "treatment";
Thomas Vachuska3da62122017-10-04 13:49:12 -070080 private static final String IDLE_TIMEOUT = "idleTimeout";
81 private static final String HARD_TIMEOUT = "hardTimeout";
Simon Huntabd16f62015-05-01 13:14:40 -070082 private static final String PERMANENT = "permanent";
83 private static final String STATE = "state";
Bri Prebilic Cole641b97b2015-05-05 14:47:40 -070084 private static final String PACKETS = "packets";
Simon Hunt9bff5952017-07-13 14:05:09 -070085 private static final String DURATION = "duration";
Bri Prebilic Cole641b97b2015-05-05 14:47:40 -070086 private static final String BYTES = "bytes";
Simon Huntabd16f62015-05-01 13:14:40 -070087
88 private static final String COMMA = ", ";
Viswanath KSP70e0d142016-10-28 21:58:32 +053089 private static final String OX = "0x";
90 private static final String EMPTY = "";
Simon Hunt8f057ee2017-07-19 14:05:26 -070091 private static final String SPACE = " ";
92 private static final String COLON = ":";
93 private static final String ANGLE_O = "<";
94 private static final String ANGLE_C = ">";
95 private static final String SQUARE_O = "[";
96 private static final String SQUARE_C = "]";
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070097
Simon Hunt36b658d2017-07-14 16:20:11 -070098 private static final String ONOS_PREFIX = "org.onosproject.";
99 private static final String ONOS_MARKER = "*";
100
Simon Huntf257a962017-08-25 18:58:47 -0700101 // json structure keys
102 private static final String IMMED = "immed";
103 private static final String DEFER = "defer";
104 private static final String METER = "meter";
105 private static final String TABLE = "table";
106 private static final String META = "meta";
107 private static final String CLEARDEF = "clearDef";
108
Simon Hunt8f057ee2017-07-19 14:05:26 -0700109 // TODO: replace the use of the following constants with localized text
110 private static final String MSG_NO_SELECTOR =
111 "(No traffic selector criteria for this flow)";
112 private static final String MSG_NO_TREATMENT =
113 "(No traffic treatment instructions for this flow)";
114 private static final String NO_ROWS_NO_FLOWS = "No flows found";
115
116 private static final String CRITERIA = "Criteria";
117 private static final String TREATMENT_INSTRUCTIONS = "Treatment Instructions";
118 private static final String IMM = "imm";
119 private static final String DEF = "def";
120 private static final String METERED = "metered";
121 private static final String TRANSITION = "transition";
122 private static final String METADATA = "metadata";
123 private static final String CLEARED = "cleared";
124 private static final String UNKNOWN = "Unknown";
125
126
Simon Hunt3d1b0652015-05-05 17:27:24 -0700127 private static final String[] COL_IDS = {
Simon Hunt9bff5952017-07-13 14:05:09 -0700128 ID,
129 STATE,
130 BYTES,
131 PACKETS,
132 DURATION,
133 PRIORITY,
Yi Tseng18177a52017-09-08 01:26:59 -0700134 TABLE_NAME,
Simon Hunt9bff5952017-07-13 14:05:09 -0700135 APP_ID,
Simon Hunt36b658d2017-07-14 16:20:11 -0700136 APP_NAME,
Simon Hunt9bff5952017-07-13 14:05:09 -0700137
138 GROUP_ID,
Thomas Vachuska3da62122017-10-04 13:49:12 -0700139 IDLE_TIMEOUT,
Simon Hunt9bff5952017-07-13 14:05:09 -0700140 PERMANENT,
141
142 SELECTOR_C,
143 SELECTOR,
144 TREATMENT_C,
145 TREATMENT,
Simon Hunt3d1b0652015-05-05 17:27:24 -0700146 };
147
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700148 @Override
Simon Huntda580882015-05-12 20:58:18 -0700149 protected Collection<RequestHandler> createRequestHandlers() {
Viswanath KSPd1212c72016-10-21 01:03:35 +0530150 return ImmutableSet.of(
151 new FlowDataRequest(),
152 new DetailRequestHandler()
153 );
Simon Huntd2747a02015-04-30 22:41:16 -0700154 }
155
Simon Hunt8f057ee2017-07-19 14:05:26 -0700156 private void removeTrailingComma(StringBuilder sb) {
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530157 int pos = sb.lastIndexOf(COMMA);
158 sb.delete(pos, sb.length());
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530159 }
160
Simon Hunt36b658d2017-07-14 16:20:11 -0700161 // Generate a map of shorts->application IDs
162 // (working around deficiency(?) in Application Service API)
163 private Map<Short, ApplicationId> appShortMap() {
164 Set<Application> apps =
165 get(ApplicationService.class).getApplications();
166
167 return apps.stream()
168 .collect(Collectors.toMap(a -> a.id().id(), Application::id));
169 }
170
171 // Return an application name, based on a lookup of the internal short ID
172 private String makeAppName(short id, Map<Short, ApplicationId> lookup) {
173 ApplicationId appId = lookup.get(id);
174 if (appId == null) {
Thomas Vachuska3da62122017-10-04 13:49:12 -0700175 appId = get(CoreService.class).getAppId(id);
176 if (appId == null) {
177 return UNKNOWN + SPACE + ANGLE_O + id + ANGLE_C;
178 }
179 lookup.put(id, appId);
Simon Hunt36b658d2017-07-14 16:20:11 -0700180 }
181 String appName = appId.name();
182 return appName.startsWith(ONOS_PREFIX)
183 ? appName.replaceFirst(ONOS_PREFIX, ONOS_MARKER) : appName;
184 }
185
Simon Huntabd16f62015-05-01 13:14:40 -0700186 // handler for flow table requests
187 private final class FlowDataRequest extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -0700188
189 private FlowDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -0700190 super(FLOW_DATA_REQ, FLOW_DATA_RESP, FLOWS);
Simon Huntd2747a02015-04-30 22:41:16 -0700191 }
192
193 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700194 protected String[] getColumnIds() {
195 return COL_IDS;
Simon Hunt44aa2f82015-04-30 15:01:35 -0700196 }
Simon Hunt44aa2f82015-04-30 15:01:35 -0700197
Simon Hunt3d1b0652015-05-05 17:27:24 -0700198 @Override
Jian Li8baf4472016-01-15 15:08:09 -0800199 protected String noRowsMessage(ObjectNode payload) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700200 return NO_ROWS_NO_FLOWS;
Jian Li69f66632016-01-15 12:27:42 -0800201 }
202
203 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700204 protected TableModel createTableModel() {
205 TableModel tm = super.createTableModel();
Simon Hunt3e4ccaf2016-01-12 19:59:54 -0800206 tm.setFormatter(ID, HexLongFormatter.INSTANCE);
Simon Huntbe60dde2016-01-13 12:26:56 -0800207 tm.setFormatter(STATE, EnumFormatter.INSTANCE);
Simon Huntbe60dde2016-01-13 12:26:56 -0800208 tm.setFormatter(BYTES, NumberFormatter.INTEGER);
Simon Hunt9bff5952017-07-13 14:05:09 -0700209 tm.setFormatter(PACKETS, NumberFormatter.INTEGER);
210 tm.setFormatter(DURATION, NumberFormatter.INTEGER);
211
212 tm.setFormatter(SELECTOR_C, new SelectorShortFormatter());
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700213 tm.setFormatter(SELECTOR, new SelectorFormatter());
Simon Hunt9bff5952017-07-13 14:05:09 -0700214 tm.setFormatter(TREATMENT_C, new TreatmentShortFormatter());
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700215 tm.setFormatter(TREATMENT, new TreatmentFormatter());
Yi Tseng18177a52017-09-08 01:26:59 -0700216 tm.setFormatter(TABLE_NAME, DefaultCellFormatter.INSTANCE);
Simon Hunt3d1b0652015-05-05 17:27:24 -0700217 return tm;
218 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700219
Simon Hunt3d1b0652015-05-05 17:27:24 -0700220 @Override
221 protected void populateTable(TableModel tm, ObjectNode payload) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700222 String uri = string(payload, DEV_ID);
Simon Hunt3d1b0652015-05-05 17:27:24 -0700223 if (!Strings.isNullOrEmpty(uri)) {
224 DeviceId deviceId = DeviceId.deviceId(uri);
Simon Hunt36b658d2017-07-14 16:20:11 -0700225 Map<Short, ApplicationId> lookup = appShortMap();
Simon Hunt3d1b0652015-05-05 17:27:24 -0700226 FlowRuleService frs = get(FlowRuleService.class);
Simon Hunt36b658d2017-07-14 16:20:11 -0700227
Simon Hunt3d1b0652015-05-05 17:27:24 -0700228 for (FlowEntry flow : frs.getFlowEntries(deviceId)) {
Simon Hunt36b658d2017-07-14 16:20:11 -0700229 populateRow(tm.addRow(), flow, lookup);
Simon Hunt3d1b0652015-05-05 17:27:24 -0700230 }
231 }
232 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700233
Simon Hunt36b658d2017-07-14 16:20:11 -0700234 private void populateRow(TableModel.Row row, FlowEntry flow,
235 Map<Short, ApplicationId> lookup) {
Simon Hunt3d1b0652015-05-05 17:27:24 -0700236 row.cell(ID, flow.id().value())
Simon Hunt9bff5952017-07-13 14:05:09 -0700237 .cell(STATE, flow.state())
238 .cell(BYTES, flow.bytes())
239 .cell(PACKETS, flow.packets())
240 .cell(DURATION, flow.life())
Simon Hunt8a0429a2017-01-06 16:52:47 -0800241 .cell(PRIORITY, flow.priority())
Yi Tseng18177a52017-09-08 01:26:59 -0700242 .cell(TABLE_NAME, flow.table())
Simon Hunt9bff5952017-07-13 14:05:09 -0700243 .cell(APP_ID, flow.appId())
Simon Hunt36b658d2017-07-14 16:20:11 -0700244 .cell(APP_NAME, makeAppName(flow.appId(), lookup))
Simon Hunt9bff5952017-07-13 14:05:09 -0700245
246 .cell(GROUP_ID, flow.groupId().id())
Thomas Vachuska3da62122017-10-04 13:49:12 -0700247 .cell(IDLE_TIMEOUT, flow.timeout())
Simon Hunt8a0429a2017-01-06 16:52:47 -0800248 .cell(PERMANENT, flow.isPermanent())
Simon Hunt9bff5952017-07-13 14:05:09 -0700249
250 .cell(SELECTOR_C, flow)
Simon Hunt8a0429a2017-01-06 16:52:47 -0800251 .cell(SELECTOR, flow)
Simon Hunt9bff5952017-07-13 14:05:09 -0700252 .cell(TREATMENT_C, flow)
Simon Hunt8a0429a2017-01-06 16:52:47 -0800253 .cell(TREATMENT, flow);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700254 }
255
Simon Hunt9bff5952017-07-13 14:05:09 -0700256 private class InternalSelectorFormatter implements CellFormatter {
257 private final boolean shortFormat;
258
259 InternalSelectorFormatter(boolean shortFormat) {
260 this.shortFormat = shortFormat;
261 }
262
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700263 @Override
264 public String format(Object value) {
265 FlowEntry flow = (FlowEntry) value;
266 Set<Criterion> criteria = flow.selector().criteria();
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700267
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700268 if (criteria.isEmpty()) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700269 return MSG_NO_SELECTOR;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700270 }
Simon Hunt9bff5952017-07-13 14:05:09 -0700271
272 StringBuilder sb = new StringBuilder();
273 if (!shortFormat) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700274 sb.append(CRITERIA).append(COLON).append(SPACE);
Simon Hunt9bff5952017-07-13 14:05:09 -0700275 }
276
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700277 for (Criterion c : criteria) {
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700278 sb.append(c).append(COMMA);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700279 }
280 removeTrailingComma(sb);
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700281
282 return sb.toString();
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700283 }
284 }
285
Simon Hunt9bff5952017-07-13 14:05:09 -0700286 private final class SelectorShortFormatter extends InternalSelectorFormatter {
287 SelectorShortFormatter() {
288 super(true);
289 }
290 }
291
292 private final class SelectorFormatter extends InternalSelectorFormatter {
293 SelectorFormatter() {
294 super(false);
295 }
296 }
297
298 private class InternalTreatmentFormatter implements CellFormatter {
299 private final boolean shortFormat;
300
301 InternalTreatmentFormatter(boolean shortFormat) {
302 this.shortFormat = shortFormat;
303 }
304
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700305 @Override
306 public String format(Object value) {
307 FlowEntry flow = (FlowEntry) value;
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530308 TrafficTreatment treatment = flow.treatment();
309 List<Instruction> imm = treatment.immediate();
310 List<Instruction> def = treatment.deferred();
311 if (imm.isEmpty() &&
312 def.isEmpty() &&
313 treatment.metered() == null &&
Simon Hunt8f057ee2017-07-19 14:05:26 -0700314 treatment.tableTransition() == null &&
315 treatment.writeMetadata() == null) {
316 return MSG_NO_TREATMENT;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700317 }
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530318
Simon Hunt9bff5952017-07-13 14:05:09 -0700319 StringBuilder sb = new StringBuilder();
320
321 if (!shortFormat) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700322 sb.append(TREATMENT_INSTRUCTIONS).append(COLON).append(SPACE);
Simon Hunt9bff5952017-07-13 14:05:09 -0700323 }
324
Simon Hunt8f057ee2017-07-19 14:05:26 -0700325 formatInstructs(sb, imm, IMM);
326 formatInstructs(sb, def, DEF);
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530327
Simon Hunt8f057ee2017-07-19 14:05:26 -0700328 addLabVal(sb, METERED, treatment.metered());
329 addLabVal(sb, TRANSITION, treatment.tableTransition());
330 addLabVal(sb, METADATA, treatment.writeMetadata());
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530331
Simon Hunt8f057ee2017-07-19 14:05:26 -0700332 sb.append(CLEARED).append(COLON)
333 .append(treatment.clearedDeferred());
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700334
335 return sb.toString();
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700336 }
Simon Hunt8f057ee2017-07-19 14:05:26 -0700337
338 private void addLabVal(StringBuilder sb, String label, Instruction value) {
339 if (value != null) {
340 sb.append(label).append(COLON).append(value).append(COMMA);
341 }
342 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700343 }
344
Simon Hunt9bff5952017-07-13 14:05:09 -0700345 private final class TreatmentShortFormatter extends InternalTreatmentFormatter {
346 TreatmentShortFormatter() {
347 super(true);
348 }
349 }
350
351 private final class TreatmentFormatter extends InternalTreatmentFormatter {
352 TreatmentFormatter() {
353 super(false);
354 }
355 }
356
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530357 private void formatInstructs(StringBuilder sb,
358 List<Instruction> instructs,
359 String type) {
360 if (!instructs.isEmpty()) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700361 sb.append(type).append(SQUARE_O);
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530362 for (Instruction i : instructs) {
Simon Huntf257a962017-08-25 18:58:47 -0700363 sb.append(renderInstructionForDisplay(i)).append(COMMA);
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530364 }
Simon Hunt8f057ee2017-07-19 14:05:26 -0700365 removeTrailingComma(sb);
366 sb.append(SQUARE_C).append(COMMA);
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530367 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700368 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700369 }
Viswanath KSPd1212c72016-10-21 01:03:35 +0530370
371 private final class DetailRequestHandler extends RequestHandler {
372 private DetailRequestHandler() {
373 super(FLOW_DETAILS_REQ);
374 }
375
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530376 private FlowEntry findFlowById(String appIdText, String flowId) {
Viswanath KSP70e0d142016-10-28 21:58:32 +0530377 String strippedFlowId = flowId.replaceAll(OX, EMPTY);
Viswanath KSPd1212c72016-10-21 01:03:35 +0530378 FlowRuleService fs = get(FlowRuleService.class);
379 int appIdInt = Integer.parseInt(appIdText);
Viswanath KSP70e0d142016-10-28 21:58:32 +0530380 ApplicationId appId = new DefaultApplicationId(appIdInt, DETAILS);
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530381 Iterable<FlowEntry> entries = fs.getFlowEntriesById(appId);
Viswanath KSPd1212c72016-10-21 01:03:35 +0530382
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530383 for (FlowEntry entry : entries) {
384 if (entry.id().toString().equals(strippedFlowId)) {
385 return entry;
Viswanath KSPd1212c72016-10-21 01:03:35 +0530386 }
387 }
388
389 return null;
390 }
391
Viswanath KSP70e0d142016-10-28 21:58:32 +0530392 private String decorateFlowId(FlowRule flow) {
393 return OX + flow.id();
394 }
395
Viswanath KSPed55ecf2016-12-22 18:15:45 +0530396 private String decorateGroupId(FlowRule flow) {
397 return OX + flow.groupId().id();
398 }
399
Viswanath KSPd1212c72016-10-21 01:03:35 +0530400 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800401 public void process(ObjectNode payload) {
Viswanath KSPd1212c72016-10-21 01:03:35 +0530402
403 String flowId = string(payload, FLOW_ID);
404 String appId = string(payload, APP_ID);
Simon Hunt9bff5952017-07-13 14:05:09 -0700405
406 FlowEntry flow = findFlowById(appId, flowId);
Viswanath KSP70e0d142016-10-28 21:58:32 +0530407 if (flow != null) {
408 ObjectNode data = objectNode();
Viswanath KSPd1212c72016-10-21 01:03:35 +0530409
Viswanath KSP70e0d142016-10-28 21:58:32 +0530410 data.put(FLOW_ID, decorateFlowId(flow));
Simon Hunt9bff5952017-07-13 14:05:09 -0700411
Simon Hunt8f057ee2017-07-19 14:05:26 -0700412 data.put(STATE, EnumFormatter.INSTANCE.format(flow.state()));
413 data.put(BYTES, NumberFormatter.INTEGER.format(flow.bytes()));
414 data.put(PACKETS, NumberFormatter.INTEGER.format(flow.packets()));
415 data.put(DURATION, NumberFormatter.INTEGER.format(flow.life()));
Simon Hunt9bff5952017-07-13 14:05:09 -0700416
Viswanath KSP70e0d142016-10-28 21:58:32 +0530417 data.put(FLOW_PRIORITY, flow.priority());
Yi Tseng18177a52017-09-08 01:26:59 -0700418 data.put(TABLE_NAME, flow.table().toString());
Simon Hunt9bff5952017-07-13 14:05:09 -0700419 data.put(APP_ID, flow.appId());
Simon Hunt36b658d2017-07-14 16:20:11 -0700420 // NOTE: horribly inefficient... make a map and retrieve a single value...
421 data.put(APP_NAME, makeAppName(flow.appId(), appShortMap()));
Simon Hunt9bff5952017-07-13 14:05:09 -0700422
423 data.put(GROUP_ID, decorateGroupId(flow));
Thomas Vachuska3da62122017-10-04 13:49:12 -0700424 data.put(IDLE_TIMEOUT, flow.timeout());
425 data.put(HARD_TIMEOUT, flow.hardTimeout());
Simon Hunt8f057ee2017-07-19 14:05:26 -0700426 data.put(PERMANENT, flow.isPermanent());
Simon Hunt9bff5952017-07-13 14:05:09 -0700427
Simon Hunt8f057ee2017-07-19 14:05:26 -0700428 data.set(SELECTOR, jsonCriteria(flow));
429 data.set(TREATMENT, jsonTreatment(flow));
Viswanath KSPed55ecf2016-12-22 18:15:45 +0530430
Viswanath KSP70e0d142016-10-28 21:58:32 +0530431 ObjectNode rootNode = objectNode();
432 rootNode.set(DETAILS, data);
433 sendMessage(FLOW_DETAILS_RESP, rootNode);
434 }
Viswanath KSPd1212c72016-10-21 01:03:35 +0530435 }
Simon Hunt8f057ee2017-07-19 14:05:26 -0700436
437 private ArrayNode jsonCriteria(FlowEntry flow) {
438 ArrayNode crits = arrayNode();
439 for (Criterion c : flow.selector().criteria()) {
440 crits.add(c.toString());
441 }
442 return crits;
443 }
444
445 private ObjectNode jsonTreatment(FlowEntry flow) {
446 ObjectNode treat = objectNode();
447 TrafficTreatment treatment = flow.treatment();
448 List<Instruction> imm = treatment.immediate();
449 List<Instruction> def = treatment.deferred();
450 Instructions.MeterInstruction meter = treatment.metered();
451 Instructions.TableTypeTransition table = treatment.tableTransition();
452 Instructions.MetadataInstruction meta = treatment.writeMetadata();
453
454 if (!imm.isEmpty()) {
455 treat.set(IMMED, jsonInstrList(imm));
456 }
457 if (!def.isEmpty()) {
458 treat.set(DEFER, jsonInstrList(def));
459 }
460 if (meter != null) {
461 treat.put(METER, meter.toString());
462 }
463 if (table != null) {
464 treat.put(TABLE, table.toString());
465 }
466 if (meta != null) {
467 treat.put(META, meta.toString());
468 }
469 treat.put(CLEARDEF, treatment.clearedDeferred());
470 return treat;
471 }
472
473 private ArrayNode jsonInstrList(List<Instruction> instructions) {
474 ArrayNode array = arrayNode();
475 for (Instruction i : instructions) {
Simon Huntf257a962017-08-25 18:58:47 -0700476 array.add(renderInstructionForDisplay(i));
Simon Hunt8f057ee2017-07-19 14:05:26 -0700477 }
478 return array;
479 }
Viswanath KSPd1212c72016-10-21 01:03:35 +0530480 }
Simon Hunt8f057ee2017-07-19 14:05:26 -0700481
Simon Huntf257a962017-08-25 18:58:47 -0700482 // package private to allow unit test access...
483 String renderInstructionForDisplay(Instruction instr) {
484
485 // special handling for Extension Instruction Wrappers...
486 if (instr instanceof Instructions.ExtensionInstructionWrapper) {
487 Instructions.ExtensionInstructionWrapper wrap =
488 (Instructions.ExtensionInstructionWrapper) instr;
489 return wrap.type() + COLON + wrap.extensionInstruction();
490 }
491
492 // special handling of other instruction classes could be placed here
493
494 // default to the natural string representation otherwise
495 return instr.toString();
496 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700497}