Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.ui.impl; |
| 18 | |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 20 | import com.google.common.base.Strings; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.core.DefaultApplicationId; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.flow.FlowEntry; |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 26 | import org.onosproject.net.flow.FlowRule; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 27 | import org.onosproject.net.flow.FlowRuleService; |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 28 | import org.onosproject.net.flow.TrafficTreatment; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 29 | import org.onosproject.net.flow.criteria.Criterion; |
| 30 | import org.onosproject.net.flow.instructions.Instruction; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.RequestHandler; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 32 | import org.onosproject.ui.UiMessageHandler; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 33 | import org.onosproject.ui.table.CellFormatter; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 34 | import org.onosproject.ui.table.TableModel; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 35 | import org.onosproject.ui.table.TableRequestHandler; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 36 | import org.onosproject.ui.table.cell.EnumFormatter; |
Simon Hunt | 3e4ccaf | 2016-01-12 19:59:54 -0800 | [diff] [blame] | 37 | import org.onosproject.ui.table.cell.HexLongFormatter; |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 38 | import org.onosproject.ui.table.cell.NumberFormatter; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 39 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 40 | import java.util.Collection; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 41 | import java.util.List; |
| 42 | import java.util.Set; |
| 43 | |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 44 | |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Message handler for flow view related messages. |
| 47 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 48 | public class FlowViewMessageHandler extends UiMessageHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 49 | |
| 50 | private static final String FLOW_DATA_REQ = "flowDataRequest"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 51 | private static final String FLOW_DATA_RESP = "flowDataResponse"; |
| 52 | private static final String FLOWS = "flows"; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 53 | |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 54 | private static final String FLOW_DETAILS_REQ = "flowDetailsRequest"; |
| 55 | private static final String FLOW_DETAILS_RESP = "flowDetailsResponse"; |
| 56 | private static final String DETAILS = "details"; |
| 57 | private static final String FLOW_PRIORITY = "priority"; |
| 58 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 59 | private static final String ID = "id"; |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 60 | private static final String FLOW_ID = "flowId"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 61 | private static final String APP_ID = "appId"; |
| 62 | private static final String GROUP_ID = "groupId"; |
| 63 | private static final String TABLE_ID = "tableId"; |
| 64 | private static final String PRIORITY = "priority"; |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame^] | 65 | private static final String SELECTOR_C = "selector_c"; // for table column |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 66 | private static final String SELECTOR = "selector"; |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame^] | 67 | private static final String TREATMENT_C = "treatment_c"; // for table column |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 68 | private static final String TREATMENT = "treatment"; |
| 69 | private static final String TIMEOUT = "timeout"; |
| 70 | private static final String PERMANENT = "permanent"; |
| 71 | private static final String STATE = "state"; |
Bri Prebilic Cole | 641b97b | 2015-05-05 14:47:40 -0700 | [diff] [blame] | 72 | private static final String PACKETS = "packets"; |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame^] | 73 | private static final String DURATION = "duration"; |
Bri Prebilic Cole | 641b97b | 2015-05-05 14:47:40 -0700 | [diff] [blame] | 74 | private static final String BYTES = "bytes"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 75 | |
| 76 | private static final String COMMA = ", "; |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 77 | private static final String OX = "0x"; |
| 78 | private static final String EMPTY = ""; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 79 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 80 | private static final String[] COL_IDS = { |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame^] | 81 | ID, |
| 82 | STATE, |
| 83 | BYTES, |
| 84 | PACKETS, |
| 85 | DURATION, |
| 86 | PRIORITY, |
| 87 | TABLE_ID, |
| 88 | APP_ID, |
| 89 | |
| 90 | GROUP_ID, |
| 91 | TIMEOUT, |
| 92 | PERMANENT, |
| 93 | |
| 94 | SELECTOR_C, |
| 95 | SELECTOR, |
| 96 | TREATMENT_C, |
| 97 | TREATMENT, |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 100 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 101 | protected Collection<RequestHandler> createRequestHandlers() { |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 102 | return ImmutableSet.of( |
| 103 | new FlowDataRequest(), |
| 104 | new DetailRequestHandler() |
| 105 | ); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 108 | private StringBuilder removeTrailingComma(StringBuilder sb) { |
| 109 | int pos = sb.lastIndexOf(COMMA); |
| 110 | sb.delete(pos, sb.length()); |
| 111 | return sb; |
| 112 | } |
| 113 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 114 | // handler for flow table requests |
| 115 | private final class FlowDataRequest extends TableRequestHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 116 | |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 117 | private static final String NO_ROWS_MESSAGE = "No flows found"; |
| 118 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 119 | private FlowDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 120 | super(FLOW_DATA_REQ, FLOW_DATA_RESP, FLOWS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 124 | protected String[] getColumnIds() { |
| 125 | return COL_IDS; |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 126 | } |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 127 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 128 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 129 | protected String noRowsMessage(ObjectNode payload) { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 130 | return NO_ROWS_MESSAGE; |
| 131 | } |
| 132 | |
| 133 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 134 | protected TableModel createTableModel() { |
| 135 | TableModel tm = super.createTableModel(); |
Simon Hunt | 3e4ccaf | 2016-01-12 19:59:54 -0800 | [diff] [blame] | 136 | tm.setFormatter(ID, HexLongFormatter.INSTANCE); |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 137 | tm.setFormatter(STATE, EnumFormatter.INSTANCE); |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 138 | tm.setFormatter(BYTES, NumberFormatter.INTEGER); |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame^] | 139 | tm.setFormatter(PACKETS, NumberFormatter.INTEGER); |
| 140 | tm.setFormatter(DURATION, NumberFormatter.INTEGER); |
| 141 | |
| 142 | tm.setFormatter(SELECTOR_C, new SelectorShortFormatter()); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 143 | tm.setFormatter(SELECTOR, new SelectorFormatter()); |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame^] | 144 | tm.setFormatter(TREATMENT_C, new TreatmentShortFormatter()); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 145 | tm.setFormatter(TREATMENT, new TreatmentFormatter()); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 146 | return tm; |
| 147 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 148 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 149 | @Override |
| 150 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 151 | String uri = string(payload, "devId"); |
| 152 | if (!Strings.isNullOrEmpty(uri)) { |
| 153 | DeviceId deviceId = DeviceId.deviceId(uri); |
| 154 | FlowRuleService frs = get(FlowRuleService.class); |
| 155 | for (FlowEntry flow : frs.getFlowEntries(deviceId)) { |
| 156 | populateRow(tm.addRow(), flow); |
| 157 | } |
| 158 | } |
| 159 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 160 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 161 | private void populateRow(TableModel.Row row, FlowEntry flow) { |
| 162 | row.cell(ID, flow.id().value()) |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame^] | 163 | .cell(STATE, flow.state()) |
| 164 | .cell(BYTES, flow.bytes()) |
| 165 | .cell(PACKETS, flow.packets()) |
| 166 | .cell(DURATION, flow.life()) |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 167 | .cell(PRIORITY, flow.priority()) |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame^] | 168 | .cell(TABLE_ID, flow.tableId()) |
| 169 | .cell(APP_ID, flow.appId()) |
| 170 | |
| 171 | .cell(GROUP_ID, flow.groupId().id()) |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 172 | .cell(TIMEOUT, flow.timeout()) |
| 173 | .cell(PERMANENT, flow.isPermanent()) |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame^] | 174 | |
| 175 | .cell(SELECTOR_C, flow) |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 176 | .cell(SELECTOR, flow) |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame^] | 177 | .cell(TREATMENT_C, flow) |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 178 | .cell(TREATMENT, flow); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame^] | 181 | |
| 182 | private class InternalSelectorFormatter implements CellFormatter { |
| 183 | private final boolean shortFormat; |
| 184 | |
| 185 | InternalSelectorFormatter(boolean shortFormat) { |
| 186 | this.shortFormat = shortFormat; |
| 187 | } |
| 188 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 189 | @Override |
| 190 | public String format(Object value) { |
| 191 | FlowEntry flow = (FlowEntry) value; |
| 192 | Set<Criterion> criteria = flow.selector().criteria(); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 193 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 194 | if (criteria.isEmpty()) { |
| 195 | return "(No traffic selector criteria for this flow)"; |
| 196 | } |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame^] | 197 | |
| 198 | StringBuilder sb = new StringBuilder(); |
| 199 | if (!shortFormat) { |
| 200 | sb.append("Criteria: "); |
| 201 | } |
| 202 | |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 203 | for (Criterion c : criteria) { |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 204 | sb.append(c).append(COMMA); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 205 | } |
| 206 | removeTrailingComma(sb); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 207 | |
| 208 | return sb.toString(); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame^] | 212 | private final class SelectorShortFormatter extends InternalSelectorFormatter { |
| 213 | SelectorShortFormatter() { |
| 214 | super(true); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | private final class SelectorFormatter extends InternalSelectorFormatter { |
| 219 | SelectorFormatter() { |
| 220 | super(false); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | private class InternalTreatmentFormatter implements CellFormatter { |
| 225 | private final boolean shortFormat; |
| 226 | |
| 227 | InternalTreatmentFormatter(boolean shortFormat) { |
| 228 | this.shortFormat = shortFormat; |
| 229 | } |
| 230 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 231 | @Override |
| 232 | public String format(Object value) { |
| 233 | FlowEntry flow = (FlowEntry) value; |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 234 | TrafficTreatment treatment = flow.treatment(); |
| 235 | List<Instruction> imm = treatment.immediate(); |
| 236 | List<Instruction> def = treatment.deferred(); |
| 237 | if (imm.isEmpty() && |
| 238 | def.isEmpty() && |
| 239 | treatment.metered() == null && |
| 240 | treatment.tableTransition() == null) { |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 241 | return "(No traffic treatment instructions for this flow)"; |
| 242 | } |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 243 | |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame^] | 244 | StringBuilder sb = new StringBuilder(); |
| 245 | |
| 246 | if (!shortFormat) { |
| 247 | sb.append("Treatment Instructions: "); |
| 248 | } |
| 249 | |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 250 | formatInstructs(sb, imm, "immediate:"); |
| 251 | formatInstructs(sb, def, "deferred:"); |
| 252 | |
| 253 | if (treatment.metered() != null) { |
| 254 | sb.append("metered:") |
| 255 | .append(treatment.metered()) |
| 256 | .append(COMMA); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 257 | } |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 258 | |
| 259 | if (treatment.tableTransition() != null) { |
| 260 | sb.append("transition:") |
| 261 | .append(treatment.tableTransition()) |
| 262 | .append(COMMA); |
Konstantinos Kanonakis | 845c328 | 2016-04-01 17:34:20 -0500 | [diff] [blame] | 263 | } |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 264 | |
| 265 | if (treatment.writeMetadata() != null) { |
| 266 | sb.append("metadata:") |
| 267 | .append(treatment.writeMetadata()) |
| 268 | .append(COMMA); |
Konstantinos Kanonakis | 845c328 | 2016-04-01 17:34:20 -0500 | [diff] [blame] | 269 | } |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 270 | |
| 271 | sb.append("cleared:").append(treatment.clearedDeferred()); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 272 | |
| 273 | return sb.toString(); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame^] | 277 | private final class TreatmentShortFormatter extends InternalTreatmentFormatter { |
| 278 | TreatmentShortFormatter() { |
| 279 | super(true); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | private final class TreatmentFormatter extends InternalTreatmentFormatter { |
| 284 | TreatmentFormatter() { |
| 285 | super(false); |
| 286 | } |
| 287 | } |
| 288 | |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 289 | private void formatInstructs(StringBuilder sb, |
| 290 | List<Instruction> instructs, |
| 291 | String type) { |
| 292 | if (!instructs.isEmpty()) { |
| 293 | sb.append(type); |
| 294 | for (Instruction i : instructs) { |
| 295 | sb.append(i).append(COMMA); |
| 296 | } |
| 297 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 298 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 299 | } |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 300 | |
| 301 | private final class DetailRequestHandler extends RequestHandler { |
| 302 | private DetailRequestHandler() { |
| 303 | super(FLOW_DETAILS_REQ); |
| 304 | } |
| 305 | |
Bharath Thiruveedula | 99849dc | 2016-11-17 22:04:38 +0530 | [diff] [blame] | 306 | private FlowEntry findFlowById(String appIdText, String flowId) { |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 307 | String strippedFlowId = flowId.replaceAll(OX, EMPTY); |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 308 | FlowRuleService fs = get(FlowRuleService.class); |
| 309 | int appIdInt = Integer.parseInt(appIdText); |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 310 | ApplicationId appId = new DefaultApplicationId(appIdInt, DETAILS); |
Bharath Thiruveedula | 99849dc | 2016-11-17 22:04:38 +0530 | [diff] [blame] | 311 | Iterable<FlowEntry> entries = fs.getFlowEntriesById(appId); |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 312 | |
Bharath Thiruveedula | 99849dc | 2016-11-17 22:04:38 +0530 | [diff] [blame] | 313 | for (FlowEntry entry : entries) { |
| 314 | if (entry.id().toString().equals(strippedFlowId)) { |
| 315 | return entry; |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
| 319 | return null; |
| 320 | } |
| 321 | |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 322 | private String decorateFlowId(FlowRule flow) { |
| 323 | return OX + flow.id(); |
| 324 | } |
| 325 | |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 326 | private String decorateGroupId(FlowRule flow) { |
| 327 | return OX + flow.groupId().id(); |
| 328 | } |
| 329 | |
| 330 | private String getCriteriaString(FlowRule flow) { |
| 331 | Set<Criterion> criteria = flow.selector().criteria(); |
| 332 | StringBuilder sb = new StringBuilder(); |
| 333 | for (Criterion c : criteria) { |
| 334 | sb.append(c).append(COMMA); |
| 335 | } |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 336 | removeTrailingComma(sb); |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 337 | return sb.toString(); |
| 338 | } |
| 339 | |
| 340 | private String getTreatmentString(FlowRule flow) { |
| 341 | List<Instruction> instructions = flow.treatment().allInstructions(); |
| 342 | StringBuilder sb = new StringBuilder(); |
| 343 | for (Instruction inst : instructions) { |
| 344 | sb.append(inst).append(COMMA); |
| 345 | } |
| 346 | if (flow.treatment().metered() != null) { |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 347 | sb.append(flow.treatment().metered()).append(COMMA); |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 348 | } |
| 349 | if (flow.treatment().tableTransition() != null) { |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 350 | sb.append(flow.treatment().tableTransition()).append(COMMA); |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 351 | } |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 352 | |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 353 | removeTrailingComma(sb); |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 354 | return sb.toString(); |
| 355 | } |
| 356 | |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 357 | @Override |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 358 | public void process(ObjectNode payload) { |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 359 | |
| 360 | String flowId = string(payload, FLOW_ID); |
| 361 | String appId = string(payload, APP_ID); |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame^] | 362 | |
| 363 | FlowEntry flow = findFlowById(appId, flowId); |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 364 | if (flow != null) { |
| 365 | ObjectNode data = objectNode(); |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 366 | |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 367 | data.put(FLOW_ID, decorateFlowId(flow)); |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame^] | 368 | |
| 369 | // TODO: use formatters for these values.. |
| 370 | data.put(STATE, flow.state().toString()); |
| 371 | data.put(BYTES, flow.bytes()); |
| 372 | data.put(PACKETS, flow.packets()); |
| 373 | data.put(DURATION, flow.life()); |
| 374 | |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 375 | data.put(FLOW_PRIORITY, flow.priority()); |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 376 | data.put(TABLE_ID, flow.tableId()); |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame^] | 377 | data.put(APP_ID, flow.appId()); |
| 378 | |
| 379 | data.put(GROUP_ID, decorateGroupId(flow)); |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 380 | data.put(TIMEOUT, flow.hardTimeout()); |
| 381 | data.put(PERMANENT, Boolean.toString(flow.isPermanent())); |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame^] | 382 | |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 383 | data.put(SELECTOR, getCriteriaString(flow)); |
| 384 | data.put(TREATMENT, getTreatmentString(flow)); |
| 385 | |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 386 | ObjectNode rootNode = objectNode(); |
| 387 | rootNode.set(DETAILS, data); |
| 388 | sendMessage(FLOW_DETAILS_RESP, rootNode); |
| 389 | } |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 390 | } |
| 391 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 392 | } |