Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 21 | import com.google.common.base.Strings; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableSet; |
Simon Hunt | 36b658d | 2017-07-14 16:20:11 -0700 | [diff] [blame] | 23 | import org.onosproject.app.ApplicationService; |
| 24 | import org.onosproject.core.Application; |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.DefaultApplicationId; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.flow.FlowEntry; |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 29 | import org.onosproject.net.flow.FlowRule; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 30 | import org.onosproject.net.flow.FlowRuleService; |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 31 | import org.onosproject.net.flow.TrafficTreatment; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 32 | import org.onosproject.net.flow.criteria.Criterion; |
| 33 | import org.onosproject.net.flow.instructions.Instruction; |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 34 | import org.onosproject.net.flow.instructions.Instructions; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 35 | import org.onosproject.ui.RequestHandler; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 36 | import org.onosproject.ui.UiMessageHandler; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 37 | import org.onosproject.ui.table.CellFormatter; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 38 | import org.onosproject.ui.table.TableModel; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 39 | import org.onosproject.ui.table.TableRequestHandler; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 40 | import org.onosproject.ui.table.cell.EnumFormatter; |
Simon Hunt | 3e4ccaf | 2016-01-12 19:59:54 -0800 | [diff] [blame] | 41 | import org.onosproject.ui.table.cell.HexLongFormatter; |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 42 | import org.onosproject.ui.table.cell.NumberFormatter; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 43 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 44 | import java.util.Collection; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 45 | import java.util.List; |
Simon Hunt | 36b658d | 2017-07-14 16:20:11 -0700 | [diff] [blame] | 46 | import java.util.Map; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 47 | import java.util.Set; |
Simon Hunt | 36b658d | 2017-07-14 16:20:11 -0700 | [diff] [blame] | 48 | import java.util.stream.Collectors; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 49 | |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 50 | |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Message handler for flow view related messages. |
| 53 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 54 | public class FlowViewMessageHandler extends UiMessageHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 55 | |
| 56 | private static final String FLOW_DATA_REQ = "flowDataRequest"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 57 | private static final String FLOW_DATA_RESP = "flowDataResponse"; |
| 58 | private static final String FLOWS = "flows"; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 59 | |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 60 | 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 Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 65 | private static final String DEV_ID = "devId"; |
| 66 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 67 | private static final String ID = "id"; |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 68 | private static final String FLOW_ID = "flowId"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 69 | private static final String APP_ID = "appId"; |
Simon Hunt | 36b658d | 2017-07-14 16:20:11 -0700 | [diff] [blame] | 70 | private static final String APP_NAME = "appName"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 71 | private static final String GROUP_ID = "groupId"; |
| 72 | private static final String TABLE_ID = "tableId"; |
| 73 | private static final String PRIORITY = "priority"; |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 74 | private static final String SELECTOR_C = "selector_c"; // for table column |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 75 | private static final String SELECTOR = "selector"; |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 76 | private static final String TREATMENT_C = "treatment_c"; // for table column |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 77 | 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 Cole | 641b97b | 2015-05-05 14:47:40 -0700 | [diff] [blame] | 81 | private static final String PACKETS = "packets"; |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 82 | private static final String DURATION = "duration"; |
Bri Prebilic Cole | 641b97b | 2015-05-05 14:47:40 -0700 | [diff] [blame] | 83 | private static final String BYTES = "bytes"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 84 | |
| 85 | private static final String COMMA = ", "; |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 86 | private static final String OX = "0x"; |
| 87 | private static final String EMPTY = ""; |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 88 | 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 Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 94 | |
Simon Hunt | 36b658d | 2017-07-14 16:20:11 -0700 | [diff] [blame] | 95 | private static final String ONOS_PREFIX = "org.onosproject."; |
| 96 | private static final String ONOS_MARKER = "*"; |
| 97 | |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 98 | // TODO: replace the use of the following constants with localized text |
| 99 | private static final String MSG_NO_SELECTOR = |
| 100 | "(No traffic selector criteria for this flow)"; |
| 101 | private static final String MSG_NO_TREATMENT = |
| 102 | "(No traffic treatment instructions for this flow)"; |
| 103 | private static final String NO_ROWS_NO_FLOWS = "No flows found"; |
| 104 | |
| 105 | private static final String CRITERIA = "Criteria"; |
| 106 | private static final String TREATMENT_INSTRUCTIONS = "Treatment Instructions"; |
| 107 | private static final String IMM = "imm"; |
| 108 | private static final String DEF = "def"; |
| 109 | private static final String METERED = "metered"; |
| 110 | private static final String TRANSITION = "transition"; |
| 111 | private static final String METADATA = "metadata"; |
| 112 | private static final String CLEARED = "cleared"; |
| 113 | private static final String UNKNOWN = "Unknown"; |
| 114 | |
| 115 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 116 | private static final String[] COL_IDS = { |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 117 | ID, |
| 118 | STATE, |
| 119 | BYTES, |
| 120 | PACKETS, |
| 121 | DURATION, |
| 122 | PRIORITY, |
| 123 | TABLE_ID, |
| 124 | APP_ID, |
Simon Hunt | 36b658d | 2017-07-14 16:20:11 -0700 | [diff] [blame] | 125 | APP_NAME, |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 126 | |
| 127 | GROUP_ID, |
| 128 | TIMEOUT, |
| 129 | PERMANENT, |
| 130 | |
| 131 | SELECTOR_C, |
| 132 | SELECTOR, |
| 133 | TREATMENT_C, |
| 134 | TREATMENT, |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 137 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 138 | protected Collection<RequestHandler> createRequestHandlers() { |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 139 | return ImmutableSet.of( |
| 140 | new FlowDataRequest(), |
| 141 | new DetailRequestHandler() |
| 142 | ); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 145 | private void removeTrailingComma(StringBuilder sb) { |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 146 | int pos = sb.lastIndexOf(COMMA); |
| 147 | sb.delete(pos, sb.length()); |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 148 | } |
| 149 | |
Simon Hunt | 36b658d | 2017-07-14 16:20:11 -0700 | [diff] [blame] | 150 | // Generate a map of shorts->application IDs |
| 151 | // (working around deficiency(?) in Application Service API) |
| 152 | private Map<Short, ApplicationId> appShortMap() { |
| 153 | Set<Application> apps = |
| 154 | get(ApplicationService.class).getApplications(); |
| 155 | |
| 156 | return apps.stream() |
| 157 | .collect(Collectors.toMap(a -> a.id().id(), Application::id)); |
| 158 | } |
| 159 | |
| 160 | // Return an application name, based on a lookup of the internal short ID |
| 161 | private String makeAppName(short id, Map<Short, ApplicationId> lookup) { |
| 162 | ApplicationId appId = lookup.get(id); |
| 163 | if (appId == null) { |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 164 | return UNKNOWN + SPACE + ANGLE_O + id + ANGLE_C; |
Simon Hunt | 36b658d | 2017-07-14 16:20:11 -0700 | [diff] [blame] | 165 | } |
| 166 | String appName = appId.name(); |
| 167 | return appName.startsWith(ONOS_PREFIX) |
| 168 | ? appName.replaceFirst(ONOS_PREFIX, ONOS_MARKER) : appName; |
| 169 | } |
| 170 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 171 | // handler for flow table requests |
| 172 | private final class FlowDataRequest extends TableRequestHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 173 | |
| 174 | private FlowDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 175 | super(FLOW_DATA_REQ, FLOW_DATA_RESP, FLOWS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 179 | protected String[] getColumnIds() { |
| 180 | return COL_IDS; |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 181 | } |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 182 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 183 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 184 | protected String noRowsMessage(ObjectNode payload) { |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 185 | return NO_ROWS_NO_FLOWS; |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 189 | protected TableModel createTableModel() { |
| 190 | TableModel tm = super.createTableModel(); |
Simon Hunt | 3e4ccaf | 2016-01-12 19:59:54 -0800 | [diff] [blame] | 191 | tm.setFormatter(ID, HexLongFormatter.INSTANCE); |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 192 | tm.setFormatter(STATE, EnumFormatter.INSTANCE); |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 193 | tm.setFormatter(BYTES, NumberFormatter.INTEGER); |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 194 | tm.setFormatter(PACKETS, NumberFormatter.INTEGER); |
| 195 | tm.setFormatter(DURATION, NumberFormatter.INTEGER); |
| 196 | |
| 197 | tm.setFormatter(SELECTOR_C, new SelectorShortFormatter()); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 198 | tm.setFormatter(SELECTOR, new SelectorFormatter()); |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 199 | tm.setFormatter(TREATMENT_C, new TreatmentShortFormatter()); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 200 | tm.setFormatter(TREATMENT, new TreatmentFormatter()); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 201 | return tm; |
| 202 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 203 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 204 | @Override |
| 205 | protected void populateTable(TableModel tm, ObjectNode payload) { |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 206 | String uri = string(payload, DEV_ID); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 207 | if (!Strings.isNullOrEmpty(uri)) { |
| 208 | DeviceId deviceId = DeviceId.deviceId(uri); |
Simon Hunt | 36b658d | 2017-07-14 16:20:11 -0700 | [diff] [blame] | 209 | Map<Short, ApplicationId> lookup = appShortMap(); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 210 | FlowRuleService frs = get(FlowRuleService.class); |
Simon Hunt | 36b658d | 2017-07-14 16:20:11 -0700 | [diff] [blame] | 211 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 212 | for (FlowEntry flow : frs.getFlowEntries(deviceId)) { |
Simon Hunt | 36b658d | 2017-07-14 16:20:11 -0700 | [diff] [blame] | 213 | populateRow(tm.addRow(), flow, lookup); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 217 | |
Simon Hunt | 36b658d | 2017-07-14 16:20:11 -0700 | [diff] [blame] | 218 | private void populateRow(TableModel.Row row, FlowEntry flow, |
| 219 | Map<Short, ApplicationId> lookup) { |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 220 | row.cell(ID, flow.id().value()) |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 221 | .cell(STATE, flow.state()) |
| 222 | .cell(BYTES, flow.bytes()) |
| 223 | .cell(PACKETS, flow.packets()) |
| 224 | .cell(DURATION, flow.life()) |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 225 | .cell(PRIORITY, flow.priority()) |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 226 | .cell(TABLE_ID, flow.tableId()) |
| 227 | .cell(APP_ID, flow.appId()) |
Simon Hunt | 36b658d | 2017-07-14 16:20:11 -0700 | [diff] [blame] | 228 | .cell(APP_NAME, makeAppName(flow.appId(), lookup)) |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 229 | |
| 230 | .cell(GROUP_ID, flow.groupId().id()) |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 231 | .cell(TIMEOUT, flow.timeout()) |
| 232 | .cell(PERMANENT, flow.isPermanent()) |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 233 | |
| 234 | .cell(SELECTOR_C, flow) |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 235 | .cell(SELECTOR, flow) |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 236 | .cell(TREATMENT_C, flow) |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 237 | .cell(TREATMENT, flow); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 240 | private class InternalSelectorFormatter implements CellFormatter { |
| 241 | private final boolean shortFormat; |
| 242 | |
| 243 | InternalSelectorFormatter(boolean shortFormat) { |
| 244 | this.shortFormat = shortFormat; |
| 245 | } |
| 246 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 247 | @Override |
| 248 | public String format(Object value) { |
| 249 | FlowEntry flow = (FlowEntry) value; |
| 250 | Set<Criterion> criteria = flow.selector().criteria(); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 251 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 252 | if (criteria.isEmpty()) { |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 253 | return MSG_NO_SELECTOR; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 254 | } |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 255 | |
| 256 | StringBuilder sb = new StringBuilder(); |
| 257 | if (!shortFormat) { |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 258 | sb.append(CRITERIA).append(COLON).append(SPACE); |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 261 | for (Criterion c : criteria) { |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 262 | sb.append(c).append(COMMA); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 263 | } |
| 264 | removeTrailingComma(sb); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 265 | |
| 266 | return sb.toString(); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 270 | private final class SelectorShortFormatter extends InternalSelectorFormatter { |
| 271 | SelectorShortFormatter() { |
| 272 | super(true); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | private final class SelectorFormatter extends InternalSelectorFormatter { |
| 277 | SelectorFormatter() { |
| 278 | super(false); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | private class InternalTreatmentFormatter implements CellFormatter { |
| 283 | private final boolean shortFormat; |
| 284 | |
| 285 | InternalTreatmentFormatter(boolean shortFormat) { |
| 286 | this.shortFormat = shortFormat; |
| 287 | } |
| 288 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 289 | @Override |
| 290 | public String format(Object value) { |
| 291 | FlowEntry flow = (FlowEntry) value; |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 292 | TrafficTreatment treatment = flow.treatment(); |
| 293 | List<Instruction> imm = treatment.immediate(); |
| 294 | List<Instruction> def = treatment.deferred(); |
| 295 | if (imm.isEmpty() && |
| 296 | def.isEmpty() && |
| 297 | treatment.metered() == null && |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 298 | treatment.tableTransition() == null && |
| 299 | treatment.writeMetadata() == null) { |
| 300 | return MSG_NO_TREATMENT; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 301 | } |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 302 | |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 303 | StringBuilder sb = new StringBuilder(); |
| 304 | |
| 305 | if (!shortFormat) { |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 306 | sb.append(TREATMENT_INSTRUCTIONS).append(COLON).append(SPACE); |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 309 | formatInstructs(sb, imm, IMM); |
| 310 | formatInstructs(sb, def, DEF); |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 311 | |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 312 | addLabVal(sb, METERED, treatment.metered()); |
| 313 | addLabVal(sb, TRANSITION, treatment.tableTransition()); |
| 314 | addLabVal(sb, METADATA, treatment.writeMetadata()); |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 315 | |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 316 | sb.append(CLEARED).append(COLON) |
| 317 | .append(treatment.clearedDeferred()); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 318 | |
| 319 | return sb.toString(); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 320 | } |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 321 | |
| 322 | private void addLabVal(StringBuilder sb, String label, Instruction value) { |
| 323 | if (value != null) { |
| 324 | sb.append(label).append(COLON).append(value).append(COMMA); |
| 325 | } |
| 326 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 329 | private final class TreatmentShortFormatter extends InternalTreatmentFormatter { |
| 330 | TreatmentShortFormatter() { |
| 331 | super(true); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | private final class TreatmentFormatter extends InternalTreatmentFormatter { |
| 336 | TreatmentFormatter() { |
| 337 | super(false); |
| 338 | } |
| 339 | } |
| 340 | |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 341 | private void formatInstructs(StringBuilder sb, |
| 342 | List<Instruction> instructs, |
| 343 | String type) { |
| 344 | if (!instructs.isEmpty()) { |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 345 | sb.append(type).append(SQUARE_O); |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 346 | for (Instruction i : instructs) { |
| 347 | sb.append(i).append(COMMA); |
| 348 | } |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 349 | removeTrailingComma(sb); |
| 350 | sb.append(SQUARE_C).append(COMMA); |
Deepa Vaddireddy | f3932c0 | 2017-02-09 17:32:28 +0530 | [diff] [blame] | 351 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 352 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 353 | } |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 354 | |
| 355 | private final class DetailRequestHandler extends RequestHandler { |
| 356 | private DetailRequestHandler() { |
| 357 | super(FLOW_DETAILS_REQ); |
| 358 | } |
| 359 | |
Bharath Thiruveedula | 99849dc | 2016-11-17 22:04:38 +0530 | [diff] [blame] | 360 | private FlowEntry findFlowById(String appIdText, String flowId) { |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 361 | String strippedFlowId = flowId.replaceAll(OX, EMPTY); |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 362 | FlowRuleService fs = get(FlowRuleService.class); |
| 363 | int appIdInt = Integer.parseInt(appIdText); |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 364 | ApplicationId appId = new DefaultApplicationId(appIdInt, DETAILS); |
Bharath Thiruveedula | 99849dc | 2016-11-17 22:04:38 +0530 | [diff] [blame] | 365 | Iterable<FlowEntry> entries = fs.getFlowEntriesById(appId); |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 366 | |
Bharath Thiruveedula | 99849dc | 2016-11-17 22:04:38 +0530 | [diff] [blame] | 367 | for (FlowEntry entry : entries) { |
| 368 | if (entry.id().toString().equals(strippedFlowId)) { |
| 369 | return entry; |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
| 373 | return null; |
| 374 | } |
| 375 | |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 376 | private String decorateFlowId(FlowRule flow) { |
| 377 | return OX + flow.id(); |
| 378 | } |
| 379 | |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 380 | private String decorateGroupId(FlowRule flow) { |
| 381 | return OX + flow.groupId().id(); |
| 382 | } |
| 383 | |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 384 | @Override |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 385 | public void process(ObjectNode payload) { |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 386 | |
| 387 | String flowId = string(payload, FLOW_ID); |
| 388 | String appId = string(payload, APP_ID); |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 389 | |
| 390 | FlowEntry flow = findFlowById(appId, flowId); |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 391 | if (flow != null) { |
| 392 | ObjectNode data = objectNode(); |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 393 | |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 394 | data.put(FLOW_ID, decorateFlowId(flow)); |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 395 | |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 396 | data.put(STATE, EnumFormatter.INSTANCE.format(flow.state())); |
| 397 | data.put(BYTES, NumberFormatter.INTEGER.format(flow.bytes())); |
| 398 | data.put(PACKETS, NumberFormatter.INTEGER.format(flow.packets())); |
| 399 | data.put(DURATION, NumberFormatter.INTEGER.format(flow.life())); |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 400 | |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 401 | data.put(FLOW_PRIORITY, flow.priority()); |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 402 | data.put(TABLE_ID, flow.tableId()); |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 403 | data.put(APP_ID, flow.appId()); |
Simon Hunt | 36b658d | 2017-07-14 16:20:11 -0700 | [diff] [blame] | 404 | // NOTE: horribly inefficient... make a map and retrieve a single value... |
| 405 | data.put(APP_NAME, makeAppName(flow.appId(), appShortMap())); |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 406 | |
| 407 | data.put(GROUP_ID, decorateGroupId(flow)); |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 408 | data.put(TIMEOUT, flow.hardTimeout()); |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 409 | data.put(PERMANENT, flow.isPermanent()); |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 410 | |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 411 | data.set(SELECTOR, jsonCriteria(flow)); |
| 412 | data.set(TREATMENT, jsonTreatment(flow)); |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 413 | |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 414 | ObjectNode rootNode = objectNode(); |
| 415 | rootNode.set(DETAILS, data); |
| 416 | sendMessage(FLOW_DETAILS_RESP, rootNode); |
| 417 | } |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 418 | } |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 419 | |
| 420 | private ArrayNode jsonCriteria(FlowEntry flow) { |
| 421 | ArrayNode crits = arrayNode(); |
| 422 | for (Criterion c : flow.selector().criteria()) { |
| 423 | crits.add(c.toString()); |
| 424 | } |
| 425 | return crits; |
| 426 | } |
| 427 | |
| 428 | private ObjectNode jsonTreatment(FlowEntry flow) { |
| 429 | ObjectNode treat = objectNode(); |
| 430 | TrafficTreatment treatment = flow.treatment(); |
| 431 | List<Instruction> imm = treatment.immediate(); |
| 432 | List<Instruction> def = treatment.deferred(); |
| 433 | Instructions.MeterInstruction meter = treatment.metered(); |
| 434 | Instructions.TableTypeTransition table = treatment.tableTransition(); |
| 435 | Instructions.MetadataInstruction meta = treatment.writeMetadata(); |
| 436 | |
| 437 | if (!imm.isEmpty()) { |
| 438 | treat.set(IMMED, jsonInstrList(imm)); |
| 439 | } |
| 440 | if (!def.isEmpty()) { |
| 441 | treat.set(DEFER, jsonInstrList(def)); |
| 442 | } |
| 443 | if (meter != null) { |
| 444 | treat.put(METER, meter.toString()); |
| 445 | } |
| 446 | if (table != null) { |
| 447 | treat.put(TABLE, table.toString()); |
| 448 | } |
| 449 | if (meta != null) { |
| 450 | treat.put(META, meta.toString()); |
| 451 | } |
| 452 | treat.put(CLEARDEF, treatment.clearedDeferred()); |
| 453 | return treat; |
| 454 | } |
| 455 | |
| 456 | private ArrayNode jsonInstrList(List<Instruction> instructions) { |
| 457 | ArrayNode array = arrayNode(); |
| 458 | for (Instruction i : instructions) { |
| 459 | array.add(i.toString()); |
| 460 | } |
| 461 | return array; |
| 462 | } |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 463 | } |
Simon Hunt | 8f057ee | 2017-07-19 14:05:26 -0700 | [diff] [blame] | 464 | |
| 465 | // json structure keys |
| 466 | private static final String IMMED = "immed"; |
| 467 | private static final String DEFER = "defer"; |
| 468 | private static final String METER = "meter"; |
| 469 | private static final String TABLE = "table"; |
| 470 | private static final String META = "meta"; |
| 471 | private static final String CLEARDEF = "clearDef"; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 472 | } |