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