blob: 71c7d5ae481781cd7f74a1c497f9ca1c7f68a745 [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;
Yi Tseng18177a52017-09-08 01:26:59 -070040import org.onosproject.ui.table.cell.DefaultCellFormatter;
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";
Yi Tseng18177a52017-09-08 01:26:59 -070073 private static final String TABLE_NAME = "tableName";
Simon Huntabd16f62015-05-01 13:14:40 -070074 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";
79 private static final String TIMEOUT = "timeout";
80 private static final String PERMANENT = "permanent";
81 private static final String STATE = "state";
Bri Prebilic Cole641b97b2015-05-05 14:47:40 -070082 private static final String PACKETS = "packets";
Simon Hunt9bff5952017-07-13 14:05:09 -070083 private static final String DURATION = "duration";
Bri Prebilic Cole641b97b2015-05-05 14:47:40 -070084 private static final String BYTES = "bytes";
Simon Huntabd16f62015-05-01 13:14:40 -070085
86 private static final String COMMA = ", ";
Viswanath KSP70e0d142016-10-28 21:58:32 +053087 private static final String OX = "0x";
88 private static final String EMPTY = "";
Simon Hunt8f057ee2017-07-19 14:05:26 -070089 private static final String SPACE = " ";
90 private static final String COLON = ":";
91 private static final String ANGLE_O = "<";
92 private static final String ANGLE_C = ">";
93 private static final String SQUARE_O = "[";
94 private static final String SQUARE_C = "]";
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070095
Simon Hunt36b658d2017-07-14 16:20:11 -070096 private static final String ONOS_PREFIX = "org.onosproject.";
97 private static final String ONOS_MARKER = "*";
98
Simon Huntf257a962017-08-25 18:58:47 -070099 // json structure keys
100 private static final String IMMED = "immed";
101 private static final String DEFER = "defer";
102 private static final String METER = "meter";
103 private static final String TABLE = "table";
104 private static final String META = "meta";
105 private static final String CLEARDEF = "clearDef";
106
Simon Hunt8f057ee2017-07-19 14:05:26 -0700107 // TODO: replace the use of the following constants with localized text
108 private static final String MSG_NO_SELECTOR =
109 "(No traffic selector criteria for this flow)";
110 private static final String MSG_NO_TREATMENT =
111 "(No traffic treatment instructions for this flow)";
112 private static final String NO_ROWS_NO_FLOWS = "No flows found";
113
114 private static final String CRITERIA = "Criteria";
115 private static final String TREATMENT_INSTRUCTIONS = "Treatment Instructions";
116 private static final String IMM = "imm";
117 private static final String DEF = "def";
118 private static final String METERED = "metered";
119 private static final String TRANSITION = "transition";
120 private static final String METADATA = "metadata";
121 private static final String CLEARED = "cleared";
122 private static final String UNKNOWN = "Unknown";
123
124
Simon Hunt3d1b0652015-05-05 17:27:24 -0700125 private static final String[] COL_IDS = {
Simon Hunt9bff5952017-07-13 14:05:09 -0700126 ID,
127 STATE,
128 BYTES,
129 PACKETS,
130 DURATION,
131 PRIORITY,
Yi Tseng18177a52017-09-08 01:26:59 -0700132 TABLE_NAME,
Simon Hunt9bff5952017-07-13 14:05:09 -0700133 APP_ID,
Simon Hunt36b658d2017-07-14 16:20:11 -0700134 APP_NAME,
Simon Hunt9bff5952017-07-13 14:05:09 -0700135
136 GROUP_ID,
137 TIMEOUT,
138 PERMANENT,
139
140 SELECTOR_C,
141 SELECTOR,
142 TREATMENT_C,
143 TREATMENT,
Simon Hunt3d1b0652015-05-05 17:27:24 -0700144 };
145
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700146 @Override
Simon Huntda580882015-05-12 20:58:18 -0700147 protected Collection<RequestHandler> createRequestHandlers() {
Viswanath KSPd1212c72016-10-21 01:03:35 +0530148 return ImmutableSet.of(
149 new FlowDataRequest(),
150 new DetailRequestHandler()
151 );
Simon Huntd2747a02015-04-30 22:41:16 -0700152 }
153
Simon Hunt8f057ee2017-07-19 14:05:26 -0700154 private void removeTrailingComma(StringBuilder sb) {
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530155 int pos = sb.lastIndexOf(COMMA);
156 sb.delete(pos, sb.length());
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530157 }
158
Simon Hunt36b658d2017-07-14 16:20:11 -0700159 // Generate a map of shorts->application IDs
160 // (working around deficiency(?) in Application Service API)
161 private Map<Short, ApplicationId> appShortMap() {
162 Set<Application> apps =
163 get(ApplicationService.class).getApplications();
164
165 return apps.stream()
166 .collect(Collectors.toMap(a -> a.id().id(), Application::id));
167 }
168
169 // Return an application name, based on a lookup of the internal short ID
170 private String makeAppName(short id, Map<Short, ApplicationId> lookup) {
171 ApplicationId appId = lookup.get(id);
172 if (appId == null) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700173 return UNKNOWN + SPACE + ANGLE_O + id + ANGLE_C;
Simon Hunt36b658d2017-07-14 16:20:11 -0700174 }
175 String appName = appId.name();
176 return appName.startsWith(ONOS_PREFIX)
177 ? appName.replaceFirst(ONOS_PREFIX, ONOS_MARKER) : appName;
178 }
179
Simon Huntabd16f62015-05-01 13:14:40 -0700180 // handler for flow table requests
181 private final class FlowDataRequest extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -0700182
183 private FlowDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -0700184 super(FLOW_DATA_REQ, FLOW_DATA_RESP, FLOWS);
Simon Huntd2747a02015-04-30 22:41:16 -0700185 }
186
187 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700188 protected String[] getColumnIds() {
189 return COL_IDS;
Simon Hunt44aa2f82015-04-30 15:01:35 -0700190 }
Simon Hunt44aa2f82015-04-30 15:01:35 -0700191
Simon Hunt3d1b0652015-05-05 17:27:24 -0700192 @Override
Jian Li8baf4472016-01-15 15:08:09 -0800193 protected String noRowsMessage(ObjectNode payload) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700194 return NO_ROWS_NO_FLOWS;
Jian Li69f66632016-01-15 12:27:42 -0800195 }
196
197 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700198 protected TableModel createTableModel() {
199 TableModel tm = super.createTableModel();
Simon Hunt3e4ccaf2016-01-12 19:59:54 -0800200 tm.setFormatter(ID, HexLongFormatter.INSTANCE);
Simon Huntbe60dde2016-01-13 12:26:56 -0800201 tm.setFormatter(STATE, EnumFormatter.INSTANCE);
Simon Huntbe60dde2016-01-13 12:26:56 -0800202 tm.setFormatter(BYTES, NumberFormatter.INTEGER);
Simon Hunt9bff5952017-07-13 14:05:09 -0700203 tm.setFormatter(PACKETS, NumberFormatter.INTEGER);
204 tm.setFormatter(DURATION, NumberFormatter.INTEGER);
205
206 tm.setFormatter(SELECTOR_C, new SelectorShortFormatter());
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700207 tm.setFormatter(SELECTOR, new SelectorFormatter());
Simon Hunt9bff5952017-07-13 14:05:09 -0700208 tm.setFormatter(TREATMENT_C, new TreatmentShortFormatter());
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700209 tm.setFormatter(TREATMENT, new TreatmentFormatter());
Yi Tseng18177a52017-09-08 01:26:59 -0700210 tm.setFormatter(TABLE_NAME, DefaultCellFormatter.INSTANCE);
Simon Hunt3d1b0652015-05-05 17:27:24 -0700211 return tm;
212 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700213
Simon Hunt3d1b0652015-05-05 17:27:24 -0700214 @Override
215 protected void populateTable(TableModel tm, ObjectNode payload) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700216 String uri = string(payload, DEV_ID);
Simon Hunt3d1b0652015-05-05 17:27:24 -0700217 if (!Strings.isNullOrEmpty(uri)) {
218 DeviceId deviceId = DeviceId.deviceId(uri);
Simon Hunt36b658d2017-07-14 16:20:11 -0700219 Map<Short, ApplicationId> lookup = appShortMap();
Simon Hunt3d1b0652015-05-05 17:27:24 -0700220 FlowRuleService frs = get(FlowRuleService.class);
Simon Hunt36b658d2017-07-14 16:20:11 -0700221
Simon Hunt3d1b0652015-05-05 17:27:24 -0700222 for (FlowEntry flow : frs.getFlowEntries(deviceId)) {
Simon Hunt36b658d2017-07-14 16:20:11 -0700223 populateRow(tm.addRow(), flow, lookup);
Simon Hunt3d1b0652015-05-05 17:27:24 -0700224 }
225 }
226 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700227
Simon Hunt36b658d2017-07-14 16:20:11 -0700228 private void populateRow(TableModel.Row row, FlowEntry flow,
229 Map<Short, ApplicationId> lookup) {
Simon Hunt3d1b0652015-05-05 17:27:24 -0700230 row.cell(ID, flow.id().value())
Simon Hunt9bff5952017-07-13 14:05:09 -0700231 .cell(STATE, flow.state())
232 .cell(BYTES, flow.bytes())
233 .cell(PACKETS, flow.packets())
234 .cell(DURATION, flow.life())
Simon Hunt8a0429a2017-01-06 16:52:47 -0800235 .cell(PRIORITY, flow.priority())
Yi Tseng18177a52017-09-08 01:26:59 -0700236 .cell(TABLE_NAME, flow.table())
Simon Hunt9bff5952017-07-13 14:05:09 -0700237 .cell(APP_ID, flow.appId())
Simon Hunt36b658d2017-07-14 16:20:11 -0700238 .cell(APP_NAME, makeAppName(flow.appId(), lookup))
Simon Hunt9bff5952017-07-13 14:05:09 -0700239
240 .cell(GROUP_ID, flow.groupId().id())
Simon Hunt8a0429a2017-01-06 16:52:47 -0800241 .cell(TIMEOUT, flow.timeout())
242 .cell(PERMANENT, flow.isPermanent())
Simon Hunt9bff5952017-07-13 14:05:09 -0700243
244 .cell(SELECTOR_C, flow)
Simon Hunt8a0429a2017-01-06 16:52:47 -0800245 .cell(SELECTOR, flow)
Simon Hunt9bff5952017-07-13 14:05:09 -0700246 .cell(TREATMENT_C, flow)
Simon Hunt8a0429a2017-01-06 16:52:47 -0800247 .cell(TREATMENT, flow);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700248 }
249
Simon Hunt9bff5952017-07-13 14:05:09 -0700250 private class InternalSelectorFormatter implements CellFormatter {
251 private final boolean shortFormat;
252
253 InternalSelectorFormatter(boolean shortFormat) {
254 this.shortFormat = shortFormat;
255 }
256
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700257 @Override
258 public String format(Object value) {
259 FlowEntry flow = (FlowEntry) value;
260 Set<Criterion> criteria = flow.selector().criteria();
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700261
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700262 if (criteria.isEmpty()) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700263 return MSG_NO_SELECTOR;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700264 }
Simon Hunt9bff5952017-07-13 14:05:09 -0700265
266 StringBuilder sb = new StringBuilder();
267 if (!shortFormat) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700268 sb.append(CRITERIA).append(COLON).append(SPACE);
Simon Hunt9bff5952017-07-13 14:05:09 -0700269 }
270
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700271 for (Criterion c : criteria) {
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700272 sb.append(c).append(COMMA);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700273 }
274 removeTrailingComma(sb);
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700275
276 return sb.toString();
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700277 }
278 }
279
Simon Hunt9bff5952017-07-13 14:05:09 -0700280 private final class SelectorShortFormatter extends InternalSelectorFormatter {
281 SelectorShortFormatter() {
282 super(true);
283 }
284 }
285
286 private final class SelectorFormatter extends InternalSelectorFormatter {
287 SelectorFormatter() {
288 super(false);
289 }
290 }
291
292 private class InternalTreatmentFormatter implements CellFormatter {
293 private final boolean shortFormat;
294
295 InternalTreatmentFormatter(boolean shortFormat) {
296 this.shortFormat = shortFormat;
297 }
298
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700299 @Override
300 public String format(Object value) {
301 FlowEntry flow = (FlowEntry) value;
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530302 TrafficTreatment treatment = flow.treatment();
303 List<Instruction> imm = treatment.immediate();
304 List<Instruction> def = treatment.deferred();
305 if (imm.isEmpty() &&
306 def.isEmpty() &&
307 treatment.metered() == null &&
Simon Hunt8f057ee2017-07-19 14:05:26 -0700308 treatment.tableTransition() == null &&
309 treatment.writeMetadata() == null) {
310 return MSG_NO_TREATMENT;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700311 }
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530312
Simon Hunt9bff5952017-07-13 14:05:09 -0700313 StringBuilder sb = new StringBuilder();
314
315 if (!shortFormat) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700316 sb.append(TREATMENT_INSTRUCTIONS).append(COLON).append(SPACE);
Simon Hunt9bff5952017-07-13 14:05:09 -0700317 }
318
Simon Hunt8f057ee2017-07-19 14:05:26 -0700319 formatInstructs(sb, imm, IMM);
320 formatInstructs(sb, def, DEF);
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530321
Simon Hunt8f057ee2017-07-19 14:05:26 -0700322 addLabVal(sb, METERED, treatment.metered());
323 addLabVal(sb, TRANSITION, treatment.tableTransition());
324 addLabVal(sb, METADATA, treatment.writeMetadata());
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530325
Simon Hunt8f057ee2017-07-19 14:05:26 -0700326 sb.append(CLEARED).append(COLON)
327 .append(treatment.clearedDeferred());
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700328
329 return sb.toString();
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700330 }
Simon Hunt8f057ee2017-07-19 14:05:26 -0700331
332 private void addLabVal(StringBuilder sb, String label, Instruction value) {
333 if (value != null) {
334 sb.append(label).append(COLON).append(value).append(COMMA);
335 }
336 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700337 }
338
Simon Hunt9bff5952017-07-13 14:05:09 -0700339 private final class TreatmentShortFormatter extends InternalTreatmentFormatter {
340 TreatmentShortFormatter() {
341 super(true);
342 }
343 }
344
345 private final class TreatmentFormatter extends InternalTreatmentFormatter {
346 TreatmentFormatter() {
347 super(false);
348 }
349 }
350
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530351 private void formatInstructs(StringBuilder sb,
352 List<Instruction> instructs,
353 String type) {
354 if (!instructs.isEmpty()) {
Simon Hunt8f057ee2017-07-19 14:05:26 -0700355 sb.append(type).append(SQUARE_O);
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530356 for (Instruction i : instructs) {
Simon Huntf257a962017-08-25 18:58:47 -0700357 sb.append(renderInstructionForDisplay(i)).append(COMMA);
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530358 }
Simon Hunt8f057ee2017-07-19 14:05:26 -0700359 removeTrailingComma(sb);
360 sb.append(SQUARE_C).append(COMMA);
Deepa Vaddireddyf3932c02017-02-09 17:32:28 +0530361 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700362 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700363 }
Viswanath KSPd1212c72016-10-21 01:03:35 +0530364
365 private final class DetailRequestHandler extends RequestHandler {
366 private DetailRequestHandler() {
367 super(FLOW_DETAILS_REQ);
368 }
369
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530370 private FlowEntry findFlowById(String appIdText, String flowId) {
Viswanath KSP70e0d142016-10-28 21:58:32 +0530371 String strippedFlowId = flowId.replaceAll(OX, EMPTY);
Viswanath KSPd1212c72016-10-21 01:03:35 +0530372 FlowRuleService fs = get(FlowRuleService.class);
373 int appIdInt = Integer.parseInt(appIdText);
Viswanath KSP70e0d142016-10-28 21:58:32 +0530374 ApplicationId appId = new DefaultApplicationId(appIdInt, DETAILS);
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530375 Iterable<FlowEntry> entries = fs.getFlowEntriesById(appId);
Viswanath KSPd1212c72016-10-21 01:03:35 +0530376
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530377 for (FlowEntry entry : entries) {
378 if (entry.id().toString().equals(strippedFlowId)) {
379 return entry;
Viswanath KSPd1212c72016-10-21 01:03:35 +0530380 }
381 }
382
383 return null;
384 }
385
Viswanath KSP70e0d142016-10-28 21:58:32 +0530386 private String decorateFlowId(FlowRule flow) {
387 return OX + flow.id();
388 }
389
Viswanath KSPed55ecf2016-12-22 18:15:45 +0530390 private String decorateGroupId(FlowRule flow) {
391 return OX + flow.groupId().id();
392 }
393
Viswanath KSPd1212c72016-10-21 01:03:35 +0530394 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800395 public void process(ObjectNode payload) {
Viswanath KSPd1212c72016-10-21 01:03:35 +0530396
397 String flowId = string(payload, FLOW_ID);
398 String appId = string(payload, APP_ID);
Simon Hunt9bff5952017-07-13 14:05:09 -0700399
400 FlowEntry flow = findFlowById(appId, flowId);
Viswanath KSP70e0d142016-10-28 21:58:32 +0530401 if (flow != null) {
402 ObjectNode data = objectNode();
Viswanath KSPd1212c72016-10-21 01:03:35 +0530403
Viswanath KSP70e0d142016-10-28 21:58:32 +0530404 data.put(FLOW_ID, decorateFlowId(flow));
Simon Hunt9bff5952017-07-13 14:05:09 -0700405
Simon Hunt8f057ee2017-07-19 14:05:26 -0700406 data.put(STATE, EnumFormatter.INSTANCE.format(flow.state()));
407 data.put(BYTES, NumberFormatter.INTEGER.format(flow.bytes()));
408 data.put(PACKETS, NumberFormatter.INTEGER.format(flow.packets()));
409 data.put(DURATION, NumberFormatter.INTEGER.format(flow.life()));
Simon Hunt9bff5952017-07-13 14:05:09 -0700410
Viswanath KSP70e0d142016-10-28 21:58:32 +0530411 data.put(FLOW_PRIORITY, flow.priority());
Yi Tseng18177a52017-09-08 01:26:59 -0700412 data.put(TABLE_NAME, flow.table().toString());
Simon Hunt9bff5952017-07-13 14:05:09 -0700413 data.put(APP_ID, flow.appId());
Simon Hunt36b658d2017-07-14 16:20:11 -0700414 // NOTE: horribly inefficient... make a map and retrieve a single value...
415 data.put(APP_NAME, makeAppName(flow.appId(), appShortMap()));
Simon Hunt9bff5952017-07-13 14:05:09 -0700416
417 data.put(GROUP_ID, decorateGroupId(flow));
Viswanath KSPed55ecf2016-12-22 18:15:45 +0530418 data.put(TIMEOUT, flow.hardTimeout());
Simon Hunt8f057ee2017-07-19 14:05:26 -0700419 data.put(PERMANENT, flow.isPermanent());
Simon Hunt9bff5952017-07-13 14:05:09 -0700420
Simon Hunt8f057ee2017-07-19 14:05:26 -0700421 data.set(SELECTOR, jsonCriteria(flow));
422 data.set(TREATMENT, jsonTreatment(flow));
Viswanath KSPed55ecf2016-12-22 18:15:45 +0530423
Viswanath KSP70e0d142016-10-28 21:58:32 +0530424 ObjectNode rootNode = objectNode();
425 rootNode.set(DETAILS, data);
426 sendMessage(FLOW_DETAILS_RESP, rootNode);
427 }
Viswanath KSPd1212c72016-10-21 01:03:35 +0530428 }
Simon Hunt8f057ee2017-07-19 14:05:26 -0700429
430 private ArrayNode jsonCriteria(FlowEntry flow) {
431 ArrayNode crits = arrayNode();
432 for (Criterion c : flow.selector().criteria()) {
433 crits.add(c.toString());
434 }
435 return crits;
436 }
437
438 private ObjectNode jsonTreatment(FlowEntry flow) {
439 ObjectNode treat = objectNode();
440 TrafficTreatment treatment = flow.treatment();
441 List<Instruction> imm = treatment.immediate();
442 List<Instruction> def = treatment.deferred();
443 Instructions.MeterInstruction meter = treatment.metered();
444 Instructions.TableTypeTransition table = treatment.tableTransition();
445 Instructions.MetadataInstruction meta = treatment.writeMetadata();
446
447 if (!imm.isEmpty()) {
448 treat.set(IMMED, jsonInstrList(imm));
449 }
450 if (!def.isEmpty()) {
451 treat.set(DEFER, jsonInstrList(def));
452 }
453 if (meter != null) {
454 treat.put(METER, meter.toString());
455 }
456 if (table != null) {
457 treat.put(TABLE, table.toString());
458 }
459 if (meta != null) {
460 treat.put(META, meta.toString());
461 }
462 treat.put(CLEARDEF, treatment.clearedDeferred());
463 return treat;
464 }
465
466 private ArrayNode jsonInstrList(List<Instruction> instructions) {
467 ArrayNode array = arrayNode();
468 for (Instruction i : instructions) {
Simon Huntf257a962017-08-25 18:58:47 -0700469 array.add(renderInstructionForDisplay(i));
Simon Hunt8f057ee2017-07-19 14:05:26 -0700470 }
471 return array;
472 }
Viswanath KSPd1212c72016-10-21 01:03:35 +0530473 }
Simon Hunt8f057ee2017-07-19 14:05:26 -0700474
Simon Huntf257a962017-08-25 18:58:47 -0700475 // package private to allow unit test access...
476 String renderInstructionForDisplay(Instruction instr) {
477
478 // special handling for Extension Instruction Wrappers...
479 if (instr instanceof Instructions.ExtensionInstructionWrapper) {
480 Instructions.ExtensionInstructionWrapper wrap =
481 (Instructions.ExtensionInstructionWrapper) instr;
482 return wrap.type() + COLON + wrap.extensionInstruction();
483 }
484
485 // special handling of other instruction classes could be placed here
486
487 // default to the natural string representation otherwise
488 return instr.toString();
489 }
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700490}