blob: 6e49dbabdb33ba41587b6ef2dea93f184bb7a76f [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;
26import org.onosproject.core.DefaultApplicationId;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070027import org.onosproject.net.DeviceId;
28import org.onosproject.net.flow.FlowEntry;
Viswanath KSPd1212c72016-10-21 01:03:35 +053029import org.onosproject.net.flow.FlowRule;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070030import org.onosproject.net.flow.FlowRuleService;
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +053031import org.onosproject.net.flow.TrafficTreatment;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070032import org.onosproject.net.flow.criteria.Criterion;
33import org.onosproject.net.flow.instructions.Instruction;
Simon Hunt8f057ee2017-07-19 14:05:26 -070034import org.onosproject.net.flow.instructions.Instructions;
Simon Huntd2747a02015-04-30 22:41:16 -070035import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070036import org.onosproject.ui.UiMessageHandler;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070037import org.onosproject.ui.table.CellFormatter;
Simon Hunt3d1b0652015-05-05 17:27:24 -070038import org.onosproject.ui.table.TableModel;
Simon Huntabd16f62015-05-01 13:14:40 -070039import org.onosproject.ui.table.TableRequestHandler;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070040import org.onosproject.ui.table.cell.EnumFormatter;
Simon Hunt3e4ccaf2016-01-12 19:59:54 -080041import org.onosproject.ui.table.cell.HexLongFormatter;
Simon Huntbe60dde2016-01-13 12:26:56 -080042import org.onosproject.ui.table.cell.NumberFormatter;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070043
Simon Huntd2747a02015-04-30 22:41:16 -070044import java.util.Collection;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070045import java.util.List;
Simon Hunt36b658d2017-07-14 16:20:11 -070046import java.util.Map;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070047import java.util.Set;
Simon Hunt36b658d2017-07-14 16:20:11 -070048import java.util.stream.Collectors;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070049
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +053050
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070051/**
52 * Message handler for flow view related messages.
53 */
Simon Hunta0ddb022015-05-01 09:53:01 -070054public class FlowViewMessageHandler extends UiMessageHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070055
56 private static final String FLOW_DATA_REQ = "flowDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070057 private static final String FLOW_DATA_RESP = "flowDataResponse";
58 private static final String FLOWS = "flows";
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070059
Viswanath KSPd1212c72016-10-21 01:03:35 +053060 private static final String FLOW_DETAILS_REQ = "flowDetailsRequest";
61 private static final String FLOW_DETAILS_RESP = "flowDetailsResponse";
62 private static final String DETAILS = "details";
63 private static final String FLOW_PRIORITY = "priority";
64
Simon Hunt8f057ee2017-07-19 14:05:26 -070065 private static final String DEV_ID = "devId";
66
Simon Huntabd16f62015-05-01 13:14:40 -070067 private static final String ID = "id";
Viswanath KSPd1212c72016-10-21 01:03:35 +053068 private static final String FLOW_ID = "flowId";
Simon Huntabd16f62015-05-01 13:14:40 -070069 private static final String APP_ID = "appId";
Simon Hunt36b658d2017-07-14 16:20:11 -070070 private static final String APP_NAME = "appName";
Simon Huntabd16f62015-05-01 13:14:40 -070071 private static final String GROUP_ID = "groupId";
72 private static final String TABLE_ID = "tableId";
73 private static final String PRIORITY = "priority";
Simon Hunt9bff5952017-07-13 14:05:09 -070074 private static final String SELECTOR_C = "selector_c"; // for table column
Simon Huntabd16f62015-05-01 13:14:40 -070075 private static final String SELECTOR = "selector";
Simon Hunt9bff5952017-07-13 14:05:09 -070076 private static final String TREATMENT_C = "treatment_c"; // for table column
Simon Huntabd16f62015-05-01 13:14:40 -070077 private static final String TREATMENT = "treatment";
78 private static final String TIMEOUT = "timeout";
79 private static final String PERMANENT = "permanent";
80 private static final String STATE = "state";
Bri Prebilic Cole641b97b2015-05-05 14:47:40 -070081 private static final String PACKETS = "packets";
Simon Hunt9bff5952017-07-13 14:05:09 -070082 private static final String DURATION = "duration";
Bri Prebilic Cole641b97b2015-05-05 14:47:40 -070083 private static final String BYTES = "bytes";
Simon Huntabd16f62015-05-01 13:14:40 -070084
85 private static final String COMMA = ", ";
Viswanath KSP70e0d142016-10-28 21:58:32 +053086 private static final String OX = "0x";
87 private static final String EMPTY = "";
Simon Hunt8f057ee2017-07-19 14:05:26 -070088 private static final String SPACE = " ";
89 private static final String COLON = ":";
90 private static final String ANGLE_O = "<";
91 private static final String ANGLE_C = ">";
92 private static final String SQUARE_O = "[";
93 private static final String SQUARE_C = "]";
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070094
Simon Hunt36b658d2017-07-14 16:20:11 -070095 private static final String ONOS_PREFIX = "org.onosproject.";
96 private static final String ONOS_MARKER = "*";
97
Simon Huntf257a962017-08-25 18:58:47 -070098 // json structure keys
99 private static final String IMMED = "immed";
100 private static final String DEFER = "defer";
101 private static final String METER = "meter";
102 private static final String TABLE = "table";
103 private static final String META = "meta";
104 private static final String CLEARDEF = "clearDef";
105
Simon Hunt8f057ee2017-07-19 14:05:26 -0700106 // TODO: replace the use of the following constants with localized text
107 private static final String MSG_NO_SELECTOR =
108 "(No traffic selector criteria for this flow)";
109 private static final String MSG_NO_TREATMENT =
110 "(No traffic treatment instructions for this flow)";
111 private static final String NO_ROWS_NO_FLOWS = "No flows found";
112
113 private static final String CRITERIA = "Criteria";
114 private static final String TREATMENT_INSTRUCTIONS = "Treatment Instructions";
115 private static final String IMM = "imm";
116 private static final String DEF = "def";
117 private static final String METERED = "metered";
118 private static final String TRANSITION = "transition";
119 private static final String METADATA = "metadata";
120 private static final String CLEARED = "cleared";
121 private static final String UNKNOWN = "Unknown";
122
123
Simon Hunt3d1b0652015-05-05 17:27:24 -0700124 private static final String[] COL_IDS = {
Simon Hunt9bff5952017-07-13 14:05:09 -0700125 ID,
126 STATE,
127 BYTES,
128 PACKETS,
129 DURATION,
130 PRIORITY,
131 TABLE_ID,
132 APP_ID,
Simon Hunt36b658d2017-07-14 16:20:11 -0700133 APP_NAME,
Simon Hunt9bff5952017-07-13 14:05:09 -0700134
135 GROUP_ID,
136 TIMEOUT,
137 PERMANENT,
138
139 SELECTOR_C,
140 SELECTOR,
141 TREATMENT_C,
142 TREATMENT,
Simon Hunt3d1b0652015-05-05 17:27:24 -0700143 };
144
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700145 @Override
Simon Huntda580882015-05-12 20:58:18 -0700146 protected Collection<RequestHandler> createRequestHandlers() {
Viswanath KSPd1212c72016-10-21 01:03:35 +0530147 return ImmutableSet.of(
148 new FlowDataRequest(),
149 new DetailRequestHandler()
150 );
Simon Huntd2747a02015-04-30 22:41:16 -0700151 }
152
Simon Hunt8f057ee2017-07-19 14:05:26 -0700153 private void removeTrailingComma(StringBuilder sb) {
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530154 int pos = sb.lastIndexOf(COMMA);
155 sb.delete(pos, sb.length());
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530156 }
157
Simon Hunt36b658d2017-07-14 16:20:11 -0700158 // Generate a map of shorts->application IDs
159 // (working around deficiency(?) in Application Service API)
160 private Map<Short, ApplicationId> appShortMap() {
161 Set<Application> apps =
162 get(ApplicationService.class).getApplications();
163
164 return apps.stream()
165 .collect(Collectors.toMap(a -> a.id().id(), Application::id));
166 }
167
168 // Return an application name, based on a lookup of the internal short ID
169 private String makeAppName(short id, Map<Short, ApplicationId> lookup) {
170 ApplicationId appId = lookup.get(id);
171 if (appId == null) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700172 return UNKNOWN + SPACE + ANGLE_O + id + ANGLE_C;
Simon Hunt36b658d2017-07-14 16:20:11 -0700173 }
174 String appName = appId.name();
175 return appName.startsWith(ONOS_PREFIX)
176 ? appName.replaceFirst(ONOS_PREFIX, ONOS_MARKER) : appName;
177 }
178
Simon Huntabd16f62015-05-01 13:14:40 -0700179 // handler for flow table requests
180 private final class FlowDataRequest extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -0700181
182 private FlowDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -0700183 super(FLOW_DATA_REQ, FLOW_DATA_RESP, FLOWS);
Simon Huntd2747a02015-04-30 22:41:16 -0700184 }
185
186 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700187 protected String[] getColumnIds() {
188 return COL_IDS;
Simon Hunt44aa2f82015-04-30 15:01:35 -0700189 }
Simon Hunt44aa2f82015-04-30 15:01:35 -0700190
Simon Hunt3d1b0652015-05-05 17:27:24 -0700191 @Override
Jian Li8baf4472016-01-15 15:08:09 -0800192 protected String noRowsMessage(ObjectNode payload) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700193 return NO_ROWS_NO_FLOWS;
Jian Li69f66632016-01-15 12:27:42 -0800194 }
195
196 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700197 protected TableModel createTableModel() {
198 TableModel tm = super.createTableModel();
Simon Hunt3e4ccaf2016-01-12 19:59:54 -0800199 tm.setFormatter(ID, HexLongFormatter.INSTANCE);
Simon Huntbe60dde2016-01-13 12:26:56 -0800200 tm.setFormatter(STATE, EnumFormatter.INSTANCE);
Simon Huntbe60dde2016-01-13 12:26:56 -0800201 tm.setFormatter(BYTES, NumberFormatter.INTEGER);
Simon Hunt9bff5952017-07-13 14:05:09 -0700202 tm.setFormatter(PACKETS, NumberFormatter.INTEGER);
203 tm.setFormatter(DURATION, NumberFormatter.INTEGER);
204
205 tm.setFormatter(SELECTOR_C, new SelectorShortFormatter());
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700206 tm.setFormatter(SELECTOR, new SelectorFormatter());
Simon Hunt9bff5952017-07-13 14:05:09 -0700207 tm.setFormatter(TREATMENT_C, new TreatmentShortFormatter());
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700208 tm.setFormatter(TREATMENT, new TreatmentFormatter());
Simon Hunt3d1b0652015-05-05 17:27:24 -0700209 return tm;
210 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700211
Simon Hunt3d1b0652015-05-05 17:27:24 -0700212 @Override
213 protected void populateTable(TableModel tm, ObjectNode payload) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700214 String uri = string(payload, DEV_ID);
Simon Hunt3d1b0652015-05-05 17:27:24 -0700215 if (!Strings.isNullOrEmpty(uri)) {
216 DeviceId deviceId = DeviceId.deviceId(uri);
Simon Hunt36b658d2017-07-14 16:20:11 -0700217 Map<Short, ApplicationId> lookup = appShortMap();
Simon Hunt3d1b0652015-05-05 17:27:24 -0700218 FlowRuleService frs = get(FlowRuleService.class);
Simon Hunt36b658d2017-07-14 16:20:11 -0700219
Simon Hunt3d1b0652015-05-05 17:27:24 -0700220 for (FlowEntry flow : frs.getFlowEntries(deviceId)) {
Simon Hunt36b658d2017-07-14 16:20:11 -0700221 populateRow(tm.addRow(), flow, lookup);
Simon Hunt3d1b0652015-05-05 17:27:24 -0700222 }
223 }
224 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700225
Simon Hunt36b658d2017-07-14 16:20:11 -0700226 private void populateRow(TableModel.Row row, FlowEntry flow,
227 Map<Short, ApplicationId> lookup) {
Simon Hunt3d1b0652015-05-05 17:27:24 -0700228 row.cell(ID, flow.id().value())
Simon Hunt9bff5952017-07-13 14:05:09 -0700229 .cell(STATE, flow.state())
230 .cell(BYTES, flow.bytes())
231 .cell(PACKETS, flow.packets())
232 .cell(DURATION, flow.life())
Simon Hunt8a0429a2017-01-06 16:52:47 -0800233 .cell(PRIORITY, flow.priority())
Simon Hunt9bff5952017-07-13 14:05:09 -0700234 .cell(TABLE_ID, flow.tableId())
235 .cell(APP_ID, flow.appId())
Simon Hunt36b658d2017-07-14 16:20:11 -0700236 .cell(APP_NAME, makeAppName(flow.appId(), lookup))
Simon Hunt9bff5952017-07-13 14:05:09 -0700237
238 .cell(GROUP_ID, flow.groupId().id())
Simon Hunt8a0429a2017-01-06 16:52:47 -0800239 .cell(TIMEOUT, flow.timeout())
240 .cell(PERMANENT, flow.isPermanent())
Simon Hunt9bff5952017-07-13 14:05:09 -0700241
242 .cell(SELECTOR_C, flow)
Simon Hunt8a0429a2017-01-06 16:52:47 -0800243 .cell(SELECTOR, flow)
Simon Hunt9bff5952017-07-13 14:05:09 -0700244 .cell(TREATMENT_C, flow)
Simon Hunt8a0429a2017-01-06 16:52:47 -0800245 .cell(TREATMENT, flow);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700246 }
247
Simon Hunt9bff5952017-07-13 14:05:09 -0700248 private class InternalSelectorFormatter implements CellFormatter {
249 private final boolean shortFormat;
250
251 InternalSelectorFormatter(boolean shortFormat) {
252 this.shortFormat = shortFormat;
253 }
254
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700255 @Override
256 public String format(Object value) {
257 FlowEntry flow = (FlowEntry) value;
258 Set<Criterion> criteria = flow.selector().criteria();
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700259
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700260 if (criteria.isEmpty()) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700261 return MSG_NO_SELECTOR;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700262 }
Simon Hunt9bff5952017-07-13 14:05:09 -0700263
264 StringBuilder sb = new StringBuilder();
265 if (!shortFormat) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700266 sb.append(CRITERIA).append(COLON).append(SPACE);
Simon Hunt9bff5952017-07-13 14:05:09 -0700267 }
268
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700269 for (Criterion c : criteria) {
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700270 sb.append(c).append(COMMA);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700271 }
272 removeTrailingComma(sb);
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700273
274 return sb.toString();
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700275 }
276 }
277
Simon Hunt9bff5952017-07-13 14:05:09 -0700278 private final class SelectorShortFormatter extends InternalSelectorFormatter {
279 SelectorShortFormatter() {
280 super(true);
281 }
282 }
283
284 private final class SelectorFormatter extends InternalSelectorFormatter {
285 SelectorFormatter() {
286 super(false);
287 }
288 }
289
290 private class InternalTreatmentFormatter implements CellFormatter {
291 private final boolean shortFormat;
292
293 InternalTreatmentFormatter(boolean shortFormat) {
294 this.shortFormat = shortFormat;
295 }
296
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700297 @Override
298 public String format(Object value) {
299 FlowEntry flow = (FlowEntry) value;
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530300 TrafficTreatment treatment = flow.treatment();
301 List<Instruction> imm = treatment.immediate();
302 List<Instruction> def = treatment.deferred();
303 if (imm.isEmpty() &&
304 def.isEmpty() &&
305 treatment.metered() == null &&
Simon Hunt8f057ee2017-07-19 14:05:26 -0700306 treatment.tableTransition() == null &&
307 treatment.writeMetadata() == null) {
308 return MSG_NO_TREATMENT;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700309 }
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530310
Simon Hunt9bff5952017-07-13 14:05:09 -0700311 StringBuilder sb = new StringBuilder();
312
313 if (!shortFormat) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700314 sb.append(TREATMENT_INSTRUCTIONS).append(COLON).append(SPACE);
Simon Hunt9bff5952017-07-13 14:05:09 -0700315 }
316
Simon Hunt8f057ee2017-07-19 14:05:26 -0700317 formatInstructs(sb, imm, IMM);
318 formatInstructs(sb, def, DEF);
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530319
Simon Hunt8f057ee2017-07-19 14:05:26 -0700320 addLabVal(sb, METERED, treatment.metered());
321 addLabVal(sb, TRANSITION, treatment.tableTransition());
322 addLabVal(sb, METADATA, treatment.writeMetadata());
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530323
Simon Hunt8f057ee2017-07-19 14:05:26 -0700324 sb.append(CLEARED).append(COLON)
325 .append(treatment.clearedDeferred());
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700326
327 return sb.toString();
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700328 }
Simon Hunt8f057ee2017-07-19 14:05:26 -0700329
330 private void addLabVal(StringBuilder sb, String label, Instruction value) {
331 if (value != null) {
332 sb.append(label).append(COLON).append(value).append(COMMA);
333 }
334 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700335 }
336
Simon Hunt9bff5952017-07-13 14:05:09 -0700337 private final class TreatmentShortFormatter extends InternalTreatmentFormatter {
338 TreatmentShortFormatter() {
339 super(true);
340 }
341 }
342
343 private final class TreatmentFormatter extends InternalTreatmentFormatter {
344 TreatmentFormatter() {
345 super(false);
346 }
347 }
348
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530349 private void formatInstructs(StringBuilder sb,
350 List<Instruction> instructs,
351 String type) {
352 if (!instructs.isEmpty()) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700353 sb.append(type).append(SQUARE_O);
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530354 for (Instruction i : instructs) {
Simon Huntf257a962017-08-25 18:58:47 -0700355 sb.append(renderInstructionForDisplay(i)).append(COMMA);
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530356 }
Simon Hunt8f057ee2017-07-19 14:05:26 -0700357 removeTrailingComma(sb);
358 sb.append(SQUARE_C).append(COMMA);
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530359 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700360 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700361 }
Viswanath KSPd1212c72016-10-21 01:03:35 +0530362
363 private final class DetailRequestHandler extends RequestHandler {
364 private DetailRequestHandler() {
365 super(FLOW_DETAILS_REQ);
366 }
367
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530368 private FlowEntry findFlowById(String appIdText, String flowId) {
Viswanath KSP70e0d142016-10-28 21:58:32 +0530369 String strippedFlowId = flowId.replaceAll(OX, EMPTY);
Viswanath KSPd1212c72016-10-21 01:03:35 +0530370 FlowRuleService fs = get(FlowRuleService.class);
371 int appIdInt = Integer.parseInt(appIdText);
Viswanath KSP70e0d142016-10-28 21:58:32 +0530372 ApplicationId appId = new DefaultApplicationId(appIdInt, DETAILS);
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530373 Iterable<FlowEntry> entries = fs.getFlowEntriesById(appId);
Viswanath KSPd1212c72016-10-21 01:03:35 +0530374
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530375 for (FlowEntry entry : entries) {
376 if (entry.id().toString().equals(strippedFlowId)) {
377 return entry;
Viswanath KSPd1212c72016-10-21 01:03:35 +0530378 }
379 }
380
381 return null;
382 }
383
Viswanath KSP70e0d142016-10-28 21:58:32 +0530384 private String decorateFlowId(FlowRule flow) {
385 return OX + flow.id();
386 }
387
Viswanath KSPed55ecf2016-12-22 18:15:45 +0530388 private String decorateGroupId(FlowRule flow) {
389 return OX + flow.groupId().id();
390 }
391
Viswanath KSPd1212c72016-10-21 01:03:35 +0530392 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800393 public void process(ObjectNode payload) {
Viswanath KSPd1212c72016-10-21 01:03:35 +0530394
395 String flowId = string(payload, FLOW_ID);
396 String appId = string(payload, APP_ID);
Simon Hunt9bff5952017-07-13 14:05:09 -0700397
398 FlowEntry flow = findFlowById(appId, flowId);
Viswanath KSP70e0d142016-10-28 21:58:32 +0530399 if (flow != null) {
400 ObjectNode data = objectNode();
Viswanath KSPd1212c72016-10-21 01:03:35 +0530401
Viswanath KSP70e0d142016-10-28 21:58:32 +0530402 data.put(FLOW_ID, decorateFlowId(flow));
Simon Hunt9bff5952017-07-13 14:05:09 -0700403
Simon Hunt8f057ee2017-07-19 14:05:26 -0700404 data.put(STATE, EnumFormatter.INSTANCE.format(flow.state()));
405 data.put(BYTES, NumberFormatter.INTEGER.format(flow.bytes()));
406 data.put(PACKETS, NumberFormatter.INTEGER.format(flow.packets()));
407 data.put(DURATION, NumberFormatter.INTEGER.format(flow.life()));
Simon Hunt9bff5952017-07-13 14:05:09 -0700408
Viswanath KSP70e0d142016-10-28 21:58:32 +0530409 data.put(FLOW_PRIORITY, flow.priority());
Viswanath KSPed55ecf2016-12-22 18:15:45 +0530410 data.put(TABLE_ID, flow.tableId());
Simon Hunt9bff5952017-07-13 14:05:09 -0700411 data.put(APP_ID, flow.appId());
Simon Hunt36b658d2017-07-14 16:20:11 -0700412 // NOTE: horribly inefficient... make a map and retrieve a single value...
413 data.put(APP_NAME, makeAppName(flow.appId(), appShortMap()));
Simon Hunt9bff5952017-07-13 14:05:09 -0700414
415 data.put(GROUP_ID, decorateGroupId(flow));
Viswanath KSPed55ecf2016-12-22 18:15:45 +0530416 data.put(TIMEOUT, flow.hardTimeout());
Simon Hunt8f057ee2017-07-19 14:05:26 -0700417 data.put(PERMANENT, flow.isPermanent());
Simon Hunt9bff5952017-07-13 14:05:09 -0700418
Simon Hunt8f057ee2017-07-19 14:05:26 -0700419 data.set(SELECTOR, jsonCriteria(flow));
420 data.set(TREATMENT, jsonTreatment(flow));
Viswanath KSPed55ecf2016-12-22 18:15:45 +0530421
Viswanath KSP70e0d142016-10-28 21:58:32 +0530422 ObjectNode rootNode = objectNode();
423 rootNode.set(DETAILS, data);
424 sendMessage(FLOW_DETAILS_RESP, rootNode);
425 }
Viswanath KSPd1212c72016-10-21 01:03:35 +0530426 }
Simon Hunt8f057ee2017-07-19 14:05:26 -0700427
428 private ArrayNode jsonCriteria(FlowEntry flow) {
429 ArrayNode crits = arrayNode();
430 for (Criterion c : flow.selector().criteria()) {
431 crits.add(c.toString());
432 }
433 return crits;
434 }
435
436 private ObjectNode jsonTreatment(FlowEntry flow) {
437 ObjectNode treat = objectNode();
438 TrafficTreatment treatment = flow.treatment();
439 List<Instruction> imm = treatment.immediate();
440 List<Instruction> def = treatment.deferred();
441 Instructions.MeterInstruction meter = treatment.metered();
442 Instructions.TableTypeTransition table = treatment.tableTransition();
443 Instructions.MetadataInstruction meta = treatment.writeMetadata();
444
445 if (!imm.isEmpty()) {
446 treat.set(IMMED, jsonInstrList(imm));
447 }
448 if (!def.isEmpty()) {
449 treat.set(DEFER, jsonInstrList(def));
450 }
451 if (meter != null) {
452 treat.put(METER, meter.toString());
453 }
454 if (table != null) {
455 treat.put(TABLE, table.toString());
456 }
457 if (meta != null) {
458 treat.put(META, meta.toString());
459 }
460 treat.put(CLEARDEF, treatment.clearedDeferred());
461 return treat;
462 }
463
464 private ArrayNode jsonInstrList(List<Instruction> instructions) {
465 ArrayNode array = arrayNode();
466 for (Instruction i : instructions) {
Simon Huntf257a962017-08-25 18:58:47 -0700467 array.add(renderInstructionForDisplay(i));
Simon Hunt8f057ee2017-07-19 14:05:26 -0700468 }
469 return array;
470 }
Viswanath KSPd1212c72016-10-21 01:03:35 +0530471 }
Simon Hunt8f057ee2017-07-19 14:05:26 -0700472
Simon Huntf257a962017-08-25 18:58:47 -0700473 // package private to allow unit test access...
474 String renderInstructionForDisplay(Instruction instr) {
475
476 // special handling for Extension Instruction Wrappers...
477 if (instr instanceof Instructions.ExtensionInstructionWrapper) {
478 Instructions.ExtensionInstructionWrapper wrap =
479 (Instructions.ExtensionInstructionWrapper) instr;
480 return wrap.type() + COLON + wrap.extensionInstruction();
481 }
482
483 // special handling of other instruction classes could be placed here
484
485 // default to the natural string representation otherwise
486 return instr.toString();
487 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700488}