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