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; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 28 | import org.onosproject.net.flow.criteria.Criterion; |
| 29 | import org.onosproject.net.flow.instructions.Instruction; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.RequestHandler; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.UiMessageHandler; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 32 | import org.onosproject.ui.table.CellFormatter; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 33 | import org.onosproject.ui.table.TableModel; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 34 | import org.onosproject.ui.table.TableRequestHandler; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 35 | import org.onosproject.ui.table.cell.EnumFormatter; |
Simon Hunt | 3e4ccaf | 2016-01-12 19:59:54 -0800 | [diff] [blame] | 36 | import org.onosproject.ui.table.cell.HexFormatter; |
| 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 | |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Message handler for flow view related messages. |
| 46 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 47 | public class FlowViewMessageHandler extends UiMessageHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 48 | |
| 49 | private static final String FLOW_DATA_REQ = "flowDataRequest"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 50 | private static final String FLOW_DATA_RESP = "flowDataResponse"; |
| 51 | private static final String FLOWS = "flows"; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 52 | |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 53 | private static final String FLOW_DETAILS_REQ = "flowDetailsRequest"; |
| 54 | private static final String FLOW_DETAILS_RESP = "flowDetailsResponse"; |
| 55 | private static final String DETAILS = "details"; |
| 56 | private static final String FLOW_PRIORITY = "priority"; |
| 57 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 58 | private static final String ID = "id"; |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 59 | private static final String FLOW_ID = "flowId"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 60 | private static final String APP_ID = "appId"; |
| 61 | private static final String GROUP_ID = "groupId"; |
| 62 | private static final String TABLE_ID = "tableId"; |
| 63 | private static final String PRIORITY = "priority"; |
| 64 | private static final String SELECTOR = "selector"; |
| 65 | private static final String TREATMENT = "treatment"; |
| 66 | private static final String TIMEOUT = "timeout"; |
| 67 | private static final String PERMANENT = "permanent"; |
| 68 | private static final String STATE = "state"; |
Bri Prebilic Cole | 641b97b | 2015-05-05 14:47:40 -0700 | [diff] [blame] | 69 | private static final String PACKETS = "packets"; |
| 70 | private static final String BYTES = "bytes"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 71 | |
| 72 | private static final String COMMA = ", "; |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 73 | private static final String OX = "0x"; |
| 74 | private static final String EMPTY = ""; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 75 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 76 | private static final String[] COL_IDS = { |
| 77 | ID, APP_ID, GROUP_ID, TABLE_ID, PRIORITY, SELECTOR, |
| 78 | TREATMENT, TIMEOUT, PERMANENT, STATE, PACKETS, BYTES |
| 79 | }; |
| 80 | |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 81 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 82 | protected Collection<RequestHandler> createRequestHandlers() { |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 83 | return ImmutableSet.of( |
| 84 | new FlowDataRequest(), |
| 85 | new DetailRequestHandler() |
| 86 | ); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 89 | // handler for flow table requests |
| 90 | private final class FlowDataRequest extends TableRequestHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 91 | |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 92 | private static final String NO_ROWS_MESSAGE = "No flows found"; |
| 93 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 94 | private FlowDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 95 | super(FLOW_DATA_REQ, FLOW_DATA_RESP, FLOWS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 99 | protected String[] getColumnIds() { |
| 100 | return COL_IDS; |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 101 | } |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 102 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 103 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 104 | protected String noRowsMessage(ObjectNode payload) { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 105 | return NO_ROWS_MESSAGE; |
| 106 | } |
| 107 | |
| 108 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 109 | protected TableModel createTableModel() { |
| 110 | TableModel tm = super.createTableModel(); |
Simon Hunt | 3e4ccaf | 2016-01-12 19:59:54 -0800 | [diff] [blame] | 111 | tm.setFormatter(ID, HexLongFormatter.INSTANCE); |
| 112 | tm.setFormatter(GROUP_ID, HexFormatter.INSTANCE); |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 113 | tm.setFormatter(STATE, EnumFormatter.INSTANCE); |
| 114 | tm.setFormatter(PACKETS, NumberFormatter.INTEGER); |
| 115 | tm.setFormatter(BYTES, NumberFormatter.INTEGER); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 116 | tm.setFormatter(SELECTOR, new SelectorFormatter()); |
| 117 | tm.setFormatter(TREATMENT, new TreatmentFormatter()); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 118 | return tm; |
| 119 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 120 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 121 | @Override |
| 122 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 123 | String uri = string(payload, "devId"); |
| 124 | if (!Strings.isNullOrEmpty(uri)) { |
| 125 | DeviceId deviceId = DeviceId.deviceId(uri); |
| 126 | FlowRuleService frs = get(FlowRuleService.class); |
| 127 | for (FlowEntry flow : frs.getFlowEntries(deviceId)) { |
| 128 | populateRow(tm.addRow(), flow); |
| 129 | } |
| 130 | } |
| 131 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 132 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 133 | private void populateRow(TableModel.Row row, FlowEntry flow) { |
| 134 | row.cell(ID, flow.id().value()) |
| 135 | .cell(APP_ID, flow.appId()) |
| 136 | .cell(GROUP_ID, flow.groupId().id()) |
| 137 | .cell(TABLE_ID, flow.tableId()) |
| 138 | .cell(PRIORITY, flow.priority()) |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 139 | .cell(TIMEOUT, flow.timeout()) |
| 140 | .cell(PERMANENT, flow.isPermanent()) |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 141 | .cell(STATE, flow.state()) |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 142 | .cell(PACKETS, flow.packets()) |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 143 | .cell(BYTES, flow.bytes()) |
| 144 | .cell(SELECTOR, flow) |
| 145 | .cell(TREATMENT, flow); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 148 | private final class SelectorFormatter implements CellFormatter { |
| 149 | @Override |
| 150 | public String format(Object value) { |
| 151 | FlowEntry flow = (FlowEntry) value; |
| 152 | Set<Criterion> criteria = flow.selector().criteria(); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 153 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 154 | if (criteria.isEmpty()) { |
| 155 | return "(No traffic selector criteria for this flow)"; |
| 156 | } |
| 157 | StringBuilder sb = new StringBuilder("Criteria: "); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 158 | for (Criterion c : criteria) { |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 159 | sb.append(c).append(COMMA); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 160 | } |
| 161 | removeTrailingComma(sb); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 162 | |
| 163 | return sb.toString(); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 167 | private final class TreatmentFormatter implements CellFormatter { |
| 168 | @Override |
| 169 | public String format(Object value) { |
| 170 | FlowEntry flow = (FlowEntry) value; |
| 171 | List<Instruction> instructions = flow.treatment().allInstructions(); |
| 172 | |
Konstantinos Kanonakis | a7592bc | 2016-04-13 10:24:15 -0500 | [diff] [blame] | 173 | if (instructions.isEmpty() |
| 174 | && flow.treatment().metered() == null |
| 175 | && flow.treatment().tableTransition() == null) { |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 176 | return "(No traffic treatment instructions for this flow)"; |
| 177 | } |
| 178 | StringBuilder sb = new StringBuilder("Treatment Instructions: "); |
| 179 | for (Instruction i : instructions) { |
| 180 | sb.append(i).append(COMMA); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 181 | } |
Konstantinos Kanonakis | 845c328 | 2016-04-01 17:34:20 -0500 | [diff] [blame] | 182 | if (flow.treatment().metered() != null) { |
| 183 | sb.append(flow.treatment().metered().toString()).append(COMMA); |
| 184 | } |
| 185 | if (flow.treatment().tableTransition() != null) { |
| 186 | sb.append(flow.treatment().tableTransition().toString()).append(COMMA); |
| 187 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 188 | removeTrailingComma(sb); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 189 | |
| 190 | return sb.toString(); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | private StringBuilder removeTrailingComma(StringBuilder sb) { |
| 195 | int pos = sb.lastIndexOf(COMMA); |
| 196 | sb.delete(pos, sb.length()); |
| 197 | return sb; |
| 198 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 199 | } |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 200 | |
| 201 | private final class DetailRequestHandler extends RequestHandler { |
| 202 | private DetailRequestHandler() { |
| 203 | super(FLOW_DETAILS_REQ); |
| 204 | } |
| 205 | |
| 206 | private FlowRule findFlowById(String appIdText, String flowId) { |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 207 | String strippedFlowId = flowId.replaceAll(OX, EMPTY); |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 208 | FlowRuleService fs = get(FlowRuleService.class); |
| 209 | int appIdInt = Integer.parseInt(appIdText); |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 210 | ApplicationId appId = new DefaultApplicationId(appIdInt, DETAILS); |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 211 | Iterable<FlowRule> flows = fs.getFlowRulesById(appId); |
| 212 | |
| 213 | for (FlowRule flow : flows) { |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 214 | if (flow.id().toString().equals(strippedFlowId)) { |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 215 | return flow; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | return null; |
| 220 | } |
| 221 | |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 222 | private String decorateFlowId(FlowRule flow) { |
| 223 | return OX + flow.id(); |
| 224 | } |
| 225 | |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 226 | @Override |
| 227 | public void process(long sid, ObjectNode payload) { |
| 228 | |
| 229 | String flowId = string(payload, FLOW_ID); |
| 230 | String appId = string(payload, APP_ID); |
| 231 | FlowRule flow = findFlowById(appId, flowId); |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 232 | if (flow != null) { |
| 233 | ObjectNode data = objectNode(); |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 234 | |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 235 | data.put(FLOW_ID, decorateFlowId(flow)); |
| 236 | data.put(FLOW_PRIORITY, flow.priority()); |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 237 | |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 238 | //TODO put more detail info to data |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 239 | |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 240 | ObjectNode rootNode = objectNode(); |
| 241 | rootNode.set(DETAILS, data); |
| 242 | sendMessage(FLOW_DETAILS_RESP, rootNode); |
| 243 | } |
Viswanath KSP | d1212c7 | 2016-10-21 01:03:35 +0530 | [diff] [blame] | 244 | } |
| 245 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 246 | } |