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