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; |
| 25 | import org.onosproject.net.flow.TrafficSelector; |
| 26 | import org.onosproject.net.flow.TrafficTreatment; |
| 27 | import org.onosproject.net.flow.criteria.Criterion; |
| 28 | import org.onosproject.net.flow.instructions.Instruction; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.RequestHandler; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame^] | 31 | import org.onosproject.ui.table.TableModel; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 32 | import org.onosproject.ui.table.TableRequestHandler; |
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 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 40 | import static org.apache.commons.lang.WordUtils.capitalizeFully; |
| 41 | |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Message handler for flow view related messages. |
| 45 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 46 | public class FlowViewMessageHandler extends UiMessageHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 47 | |
| 48 | private static final String FLOW_DATA_REQ = "flowDataRequest"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 49 | private static final String FLOW_DATA_RESP = "flowDataResponse"; |
| 50 | private static final String FLOWS = "flows"; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 51 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 52 | private static final String ID = "id"; |
| 53 | private static final String APP_ID = "appId"; |
| 54 | private static final String GROUP_ID = "groupId"; |
| 55 | private static final String TABLE_ID = "tableId"; |
| 56 | private static final String PRIORITY = "priority"; |
| 57 | private static final String SELECTOR = "selector"; |
| 58 | private static final String TREATMENT = "treatment"; |
| 59 | private static final String TIMEOUT = "timeout"; |
| 60 | private static final String PERMANENT = "permanent"; |
| 61 | private static final String STATE = "state"; |
Bri Prebilic Cole | 641b97b | 2015-05-05 14:47:40 -0700 | [diff] [blame] | 62 | private static final String PACKETS = "packets"; |
| 63 | private static final String BYTES = "bytes"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 64 | |
| 65 | private static final String COMMA = ", "; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 66 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame^] | 67 | private static final String[] COL_IDS = { |
| 68 | ID, APP_ID, GROUP_ID, TABLE_ID, PRIORITY, SELECTOR, |
| 69 | TREATMENT, TIMEOUT, PERMANENT, STATE, PACKETS, BYTES |
| 70 | }; |
| 71 | |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 72 | @Override |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 73 | protected Collection<RequestHandler> getHandlers() { |
| 74 | return ImmutableSet.of(new FlowDataRequest()); |
| 75 | } |
| 76 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 77 | // handler for flow table requests |
| 78 | private final class FlowDataRequest extends TableRequestHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 79 | |
| 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 |
| 90 | protected TableModel createTableModel() { |
| 91 | TableModel tm = super.createTableModel(); |
| 92 | tm.setComparator(GROUP_ID, IntComparator.INSTANCE); |
| 93 | tm.setComparator(TABLE_ID, IntComparator.INSTANCE); |
| 94 | tm.setComparator(PRIORITY, IntComparator.INSTANCE); |
| 95 | tm.setComparator(TIMEOUT, IntComparator.INSTANCE); |
| 96 | tm.setComparator(PACKETS, LongComparator.INSTANCE); |
| 97 | tm.setComparator(BYTES, LongComparator.INSTANCE); |
| 98 | return tm; |
| 99 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 100 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame^] | 101 | @Override |
| 102 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 103 | String uri = string(payload, "devId"); |
| 104 | if (!Strings.isNullOrEmpty(uri)) { |
| 105 | DeviceId deviceId = DeviceId.deviceId(uri); |
| 106 | FlowRuleService frs = get(FlowRuleService.class); |
| 107 | for (FlowEntry flow : frs.getFlowEntries(deviceId)) { |
| 108 | populateRow(tm.addRow(), flow); |
| 109 | } |
| 110 | } |
| 111 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 112 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame^] | 113 | private void populateRow(TableModel.Row row, FlowEntry flow) { |
| 114 | row.cell(ID, flow.id().value()) |
| 115 | .cell(APP_ID, flow.appId()) |
| 116 | .cell(GROUP_ID, flow.groupId().id()) |
| 117 | .cell(TABLE_ID, flow.tableId()) |
| 118 | .cell(PRIORITY, flow.priority()) |
| 119 | .cell(SELECTOR, getSelectorString(flow)) |
| 120 | .cell(TREATMENT, getTreatmentString(flow)) |
| 121 | .cell(TIMEOUT, flow.timeout()) |
| 122 | .cell(PERMANENT, flow.isPermanent()) |
| 123 | .cell(STATE, capitalizeFully(flow.state().toString())) |
| 124 | .cell(PACKETS, flow.packets()) |
| 125 | .cell(BYTES, flow.bytes()); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | private String getSelectorString(FlowEntry f) { |
| 129 | String result; |
| 130 | TrafficSelector selector = f.selector(); |
| 131 | Set<Criterion> criteria = selector.criteria(); |
| 132 | |
| 133 | if (criteria.isEmpty()) { |
| 134 | result = "(No traffic selectors for this flow)"; |
| 135 | } else { |
| 136 | StringBuilder sb = new StringBuilder("Criteria = "); |
| 137 | for (Criterion c : criteria) { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 138 | sb.append(capitalizeFully(c.type().toString())).append(COMMA); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 139 | } |
| 140 | result = removeTrailingComma(sb).toString(); |
| 141 | } |
| 142 | return result; |
| 143 | } |
| 144 | |
| 145 | private String getTreatmentString(FlowEntry f) { |
| 146 | TrafficTreatment treatment = f.treatment(); |
| 147 | List<Instruction> deferred = treatment.deferred(); |
| 148 | List<Instruction> immediate = treatment.immediate(); |
| 149 | boolean haveDef = !deferred.isEmpty(); |
| 150 | boolean haveImm = !immediate.isEmpty(); |
| 151 | boolean both = haveDef && haveImm; |
| 152 | boolean neither = !haveDef && !haveImm; |
| 153 | String result; |
| 154 | |
| 155 | if (neither) { |
| 156 | result = "(No traffic treatment instructions for this flow)"; |
| 157 | } else { |
| 158 | StringBuilder sb = new StringBuilder(); |
| 159 | addDeferred(sb, deferred); |
| 160 | if (both) { |
| 161 | sb.append(COMMA); |
| 162 | } |
| 163 | addImmediate(sb, immediate); |
| 164 | result = sb.toString(); |
| 165 | } |
| 166 | return result; |
| 167 | } |
| 168 | |
| 169 | private void addDeferred(StringBuilder sb, List<Instruction> deferred) { |
| 170 | if (!deferred.isEmpty()) { |
| 171 | sb.append("Deferred instructions = "); |
| 172 | for (Instruction i : deferred) { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 173 | sb.append(capitalizeFully(i.type().toString())).append(COMMA); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 174 | } |
| 175 | removeTrailingComma(sb); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | private void addImmediate(StringBuilder sb, List<Instruction> immediate) { |
| 180 | if (!immediate.isEmpty()) { |
| 181 | sb.append("Immediate instructions = "); |
| 182 | for (Instruction i : immediate) { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 183 | sb.append(capitalizeFully(i.type().toString())).append(COMMA); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 184 | } |
| 185 | removeTrailingComma(sb); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | private StringBuilder removeTrailingComma(StringBuilder sb) { |
| 190 | int pos = sb.lastIndexOf(COMMA); |
| 191 | sb.delete(pos, sb.length()); |
| 192 | return sb; |
| 193 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 194 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 195 | } |