Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.flow.FlowEntry; |
| 24 | import org.onosproject.net.flow.FlowRuleService; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 25 | import org.onosproject.net.flow.criteria.Criterion; |
| 26 | import org.onosproject.net.flow.instructions.Instruction; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 27 | import org.onosproject.ui.RequestHandler; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.UiMessageHandler; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.table.CellFormatter; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.table.TableModel; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.table.TableRequestHandler; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 32 | import org.onosproject.ui.table.cell.EnumFormatter; |
Simon Hunt | 3e4ccaf | 2016-01-12 19:59:54 -0800 | [diff] [blame] | 33 | import org.onosproject.ui.table.cell.HexFormatter; |
| 34 | import org.onosproject.ui.table.cell.HexLongFormatter; |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 35 | import org.onosproject.ui.table.cell.NumberFormatter; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 36 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 37 | import java.util.Collection; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 38 | import java.util.List; |
| 39 | import java.util.Set; |
| 40 | |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Message handler for flow view related messages. |
| 43 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 44 | public class FlowViewMessageHandler extends UiMessageHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 45 | |
| 46 | private static final String FLOW_DATA_REQ = "flowDataRequest"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 47 | private static final String FLOW_DATA_RESP = "flowDataResponse"; |
| 48 | private static final String FLOWS = "flows"; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 49 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 50 | private static final String ID = "id"; |
| 51 | private static final String APP_ID = "appId"; |
| 52 | private static final String GROUP_ID = "groupId"; |
| 53 | private static final String TABLE_ID = "tableId"; |
| 54 | private static final String PRIORITY = "priority"; |
| 55 | private static final String SELECTOR = "selector"; |
| 56 | private static final String TREATMENT = "treatment"; |
| 57 | private static final String TIMEOUT = "timeout"; |
| 58 | private static final String PERMANENT = "permanent"; |
| 59 | private static final String STATE = "state"; |
Bri Prebilic Cole | 641b97b | 2015-05-05 14:47:40 -0700 | [diff] [blame] | 60 | private static final String PACKETS = "packets"; |
| 61 | private static final String BYTES = "bytes"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 62 | |
| 63 | private static final String COMMA = ", "; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 64 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 65 | private static final String[] COL_IDS = { |
| 66 | ID, APP_ID, GROUP_ID, TABLE_ID, PRIORITY, SELECTOR, |
| 67 | TREATMENT, TIMEOUT, PERMANENT, STATE, PACKETS, BYTES |
| 68 | }; |
| 69 | |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 70 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 71 | protected Collection<RequestHandler> createRequestHandlers() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 72 | return ImmutableSet.of(new FlowDataRequest()); |
| 73 | } |
| 74 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 75 | // handler for flow table requests |
| 76 | private final class FlowDataRequest extends TableRequestHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 77 | |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 78 | private static final String NO_ROWS_MESSAGE = "No flows found"; |
| 79 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 80 | private FlowDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 81 | super(FLOW_DATA_REQ, FLOW_DATA_RESP, FLOWS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 85 | protected String[] getColumnIds() { |
| 86 | return COL_IDS; |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 87 | } |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 88 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 89 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 90 | protected String noRowsMessage(ObjectNode payload) { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 91 | return NO_ROWS_MESSAGE; |
| 92 | } |
| 93 | |
| 94 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 95 | protected TableModel createTableModel() { |
| 96 | TableModel tm = super.createTableModel(); |
Simon Hunt | 3e4ccaf | 2016-01-12 19:59:54 -0800 | [diff] [blame] | 97 | tm.setFormatter(ID, HexLongFormatter.INSTANCE); |
| 98 | tm.setFormatter(GROUP_ID, HexFormatter.INSTANCE); |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 99 | tm.setFormatter(STATE, EnumFormatter.INSTANCE); |
| 100 | tm.setFormatter(PACKETS, NumberFormatter.INTEGER); |
| 101 | tm.setFormatter(BYTES, NumberFormatter.INTEGER); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 102 | tm.setFormatter(SELECTOR, new SelectorFormatter()); |
| 103 | tm.setFormatter(TREATMENT, new TreatmentFormatter()); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 104 | return tm; |
| 105 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 106 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 107 | @Override |
| 108 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 109 | String uri = string(payload, "devId"); |
| 110 | if (!Strings.isNullOrEmpty(uri)) { |
| 111 | DeviceId deviceId = DeviceId.deviceId(uri); |
| 112 | FlowRuleService frs = get(FlowRuleService.class); |
| 113 | for (FlowEntry flow : frs.getFlowEntries(deviceId)) { |
| 114 | populateRow(tm.addRow(), flow); |
| 115 | } |
| 116 | } |
| 117 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 118 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 119 | private void populateRow(TableModel.Row row, FlowEntry flow) { |
| 120 | row.cell(ID, flow.id().value()) |
| 121 | .cell(APP_ID, flow.appId()) |
| 122 | .cell(GROUP_ID, flow.groupId().id()) |
| 123 | .cell(TABLE_ID, flow.tableId()) |
| 124 | .cell(PRIORITY, flow.priority()) |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 125 | .cell(TIMEOUT, flow.timeout()) |
| 126 | .cell(PERMANENT, flow.isPermanent()) |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 127 | .cell(STATE, flow.state()) |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 128 | .cell(PACKETS, flow.packets()) |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 129 | .cell(BYTES, flow.bytes()) |
| 130 | .cell(SELECTOR, flow) |
| 131 | .cell(TREATMENT, flow); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 134 | private final class SelectorFormatter implements CellFormatter { |
| 135 | @Override |
| 136 | public String format(Object value) { |
| 137 | FlowEntry flow = (FlowEntry) value; |
| 138 | Set<Criterion> criteria = flow.selector().criteria(); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 139 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 140 | if (criteria.isEmpty()) { |
| 141 | return "(No traffic selector criteria for this flow)"; |
| 142 | } |
| 143 | StringBuilder sb = new StringBuilder("Criteria: "); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 144 | for (Criterion c : criteria) { |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 145 | sb.append(c).append(COMMA); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 146 | } |
| 147 | removeTrailingComma(sb); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 148 | |
| 149 | return sb.toString(); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 153 | private final class TreatmentFormatter implements CellFormatter { |
| 154 | @Override |
| 155 | public String format(Object value) { |
| 156 | FlowEntry flow = (FlowEntry) value; |
| 157 | List<Instruction> instructions = flow.treatment().allInstructions(); |
| 158 | |
| 159 | if (instructions.isEmpty()) { |
| 160 | return "(No traffic treatment instructions for this flow)"; |
| 161 | } |
| 162 | StringBuilder sb = new StringBuilder("Treatment Instructions: "); |
| 163 | for (Instruction i : instructions) { |
| 164 | sb.append(i).append(COMMA); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 165 | } |
| 166 | removeTrailingComma(sb); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 167 | |
| 168 | return sb.toString(); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | private StringBuilder removeTrailingComma(StringBuilder sb) { |
| 173 | int pos = sb.lastIndexOf(COMMA); |
| 174 | sb.delete(pos, sb.length()); |
| 175 | return sb; |
| 176 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 177 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 178 | } |