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 | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.table.AbstractTableRow; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 32 | import org.onosproject.ui.table.TableRequestHandler; |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 33 | import org.onosproject.ui.table.TableRow; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 34 | |
| 35 | import java.util.ArrayList; |
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 | |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 67 | @Override |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 68 | protected Collection<RequestHandler> getHandlers() { |
| 69 | return ImmutableSet.of(new FlowDataRequest()); |
| 70 | } |
| 71 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 72 | // handler for flow table requests |
| 73 | private final class FlowDataRequest extends TableRequestHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 74 | |
| 75 | private FlowDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 76 | super(FLOW_DATA_REQ, FLOW_DATA_RESP, FLOWS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 80 | protected TableRow[] generateTableRows(ObjectNode payload) { |
| 81 | String uri = string(payload, "devId"); |
| 82 | if (Strings.isNullOrEmpty(uri)) { |
| 83 | return new TableRow[0]; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 84 | } |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 85 | DeviceId deviceId = DeviceId.deviceId(uri); |
| 86 | FlowRuleService service = get(FlowRuleService.class); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 87 | List<TableRow> list = new ArrayList<>(); |
| 88 | for (FlowEntry flow : service.getFlowEntries(deviceId)) { |
| 89 | list.add(new FlowTableRow(flow)); |
| 90 | } |
| 91 | return list.toArray(new TableRow[list.size()]); |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 95 | /** |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 96 | * TableRow implementation for |
| 97 | * {@link org.onosproject.net.flow.FlowRule flows}. |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 98 | */ |
| 99 | private static class FlowTableRow extends AbstractTableRow { |
| 100 | |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 101 | private static final String[] COL_IDS = { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 102 | ID, APP_ID, GROUP_ID, TABLE_ID, PRIORITY, SELECTOR, |
Bri Prebilic Cole | 641b97b | 2015-05-05 14:47:40 -0700 | [diff] [blame^] | 103 | TREATMENT, TIMEOUT, PERMANENT, STATE, PACKETS, BYTES |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | public FlowTableRow(FlowEntry f) { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 107 | add(ID, f.id().value()); |
| 108 | add(APP_ID, f.appId()); |
| 109 | add(GROUP_ID, f.groupId().id()); |
| 110 | add(TABLE_ID, f.tableId()); |
| 111 | add(PRIORITY, f.priority()); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 112 | add(SELECTOR, getSelectorString(f)); |
| 113 | add(TREATMENT, getTreatmentString(f)); |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 114 | add(TIMEOUT, f.timeout()); |
| 115 | add(PERMANENT, f.isPermanent()); |
| 116 | add(STATE, capitalizeFully(f.state().toString())); |
Bri Prebilic Cole | 641b97b | 2015-05-05 14:47:40 -0700 | [diff] [blame^] | 117 | add(PACKETS, f.packets()); |
| 118 | add(BYTES, f.packets()); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | private String getSelectorString(FlowEntry f) { |
| 122 | String result; |
| 123 | TrafficSelector selector = f.selector(); |
| 124 | Set<Criterion> criteria = selector.criteria(); |
| 125 | |
| 126 | if (criteria.isEmpty()) { |
| 127 | result = "(No traffic selectors for this flow)"; |
| 128 | } else { |
| 129 | StringBuilder sb = new StringBuilder("Criteria = "); |
| 130 | for (Criterion c : criteria) { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 131 | sb.append(capitalizeFully(c.type().toString())).append(COMMA); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 132 | } |
| 133 | result = removeTrailingComma(sb).toString(); |
| 134 | } |
| 135 | return result; |
| 136 | } |
| 137 | |
| 138 | private String getTreatmentString(FlowEntry f) { |
| 139 | TrafficTreatment treatment = f.treatment(); |
| 140 | List<Instruction> deferred = treatment.deferred(); |
| 141 | List<Instruction> immediate = treatment.immediate(); |
| 142 | boolean haveDef = !deferred.isEmpty(); |
| 143 | boolean haveImm = !immediate.isEmpty(); |
| 144 | boolean both = haveDef && haveImm; |
| 145 | boolean neither = !haveDef && !haveImm; |
| 146 | String result; |
| 147 | |
| 148 | if (neither) { |
| 149 | result = "(No traffic treatment instructions for this flow)"; |
| 150 | } else { |
| 151 | StringBuilder sb = new StringBuilder(); |
| 152 | addDeferred(sb, deferred); |
| 153 | if (both) { |
| 154 | sb.append(COMMA); |
| 155 | } |
| 156 | addImmediate(sb, immediate); |
| 157 | result = sb.toString(); |
| 158 | } |
| 159 | return result; |
| 160 | } |
| 161 | |
| 162 | private void addDeferred(StringBuilder sb, List<Instruction> deferred) { |
| 163 | if (!deferred.isEmpty()) { |
| 164 | sb.append("Deferred instructions = "); |
| 165 | for (Instruction i : deferred) { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 166 | sb.append(capitalizeFully(i.type().toString())).append(COMMA); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 167 | } |
| 168 | removeTrailingComma(sb); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | private void addImmediate(StringBuilder sb, List<Instruction> immediate) { |
| 173 | if (!immediate.isEmpty()) { |
| 174 | sb.append("Immediate instructions = "); |
| 175 | for (Instruction i : immediate) { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 176 | sb.append(capitalizeFully(i.type().toString())).append(COMMA); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 177 | } |
| 178 | removeTrailingComma(sb); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | private StringBuilder removeTrailingComma(StringBuilder sb) { |
| 183 | int pos = sb.lastIndexOf(COMMA); |
| 184 | sb.delete(pos, sb.length()); |
| 185 | return sb; |
| 186 | } |
| 187 | |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 188 | @Override |
| 189 | protected String[] columnIds() { |
| 190 | return COL_IDS; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | } |