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; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import org.apache.commons.lang.WordUtils; |
| 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 | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame^] | 29 | import org.onosproject.ui.UiMessageHandler; |
| 30 | import org.onosproject.ui.table.AbstractTableRow; |
| 31 | import org.onosproject.ui.table.RowComparator; |
| 32 | import org.onosproject.ui.table.TableRow; |
| 33 | import org.onosproject.ui.table.TableUtils; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 34 | |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.Arrays; |
| 37 | import java.util.List; |
| 38 | import java.util.Set; |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * Message handler for flow view related messages. |
| 43 | */ |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame^] | 44 | public class FlowViewMessageHandler extends UiMessageHandler { |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 45 | |
| 46 | private static final String NO_DEV = "none"; |
| 47 | |
| 48 | /** |
| 49 | * Creates a new message handler for the flow messages. |
| 50 | */ |
| 51 | protected FlowViewMessageHandler() { |
| 52 | super(ImmutableSet.of("flowDataRequest")); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void process(ObjectNode message) { |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame^] | 57 | String type = eventType(message); |
| 58 | if (type.equals("flowDataRequest")) { |
| 59 | sendFlowList(message); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | private void sendFlowList(ObjectNode message) { |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 64 | ObjectNode payload = payload(message); |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame^] | 65 | RowComparator rc = TableUtils.createRowComparator(payload); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 66 | String uri = string(payload, "devId", NO_DEV); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 67 | |
| 68 | ObjectNode rootNode; |
| 69 | if (uri.equals(NO_DEV)) { |
| 70 | rootNode = mapper.createObjectNode(); |
| 71 | rootNode.set("flows", mapper.createArrayNode()); |
| 72 | } else { |
| 73 | DeviceId deviceId = DeviceId.deviceId(uri); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 74 | FlowRuleService service = get(FlowRuleService.class); |
| 75 | TableRow[] rows = generateTableRows(service, deviceId); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 76 | Arrays.sort(rows, rc); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 77 | rootNode = mapper.createObjectNode(); |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame^] | 78 | rootNode.set("flows", TableUtils.generateArrayNode(rows)); |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | connection().sendMessage("flowDataResponse", 0, rootNode); |
| 82 | } |
| 83 | |
| 84 | private TableRow[] generateTableRows(FlowRuleService service, |
| 85 | DeviceId deviceId) { |
| 86 | List<TableRow> list = new ArrayList<>(); |
| 87 | for (FlowEntry flow : service.getFlowEntries(deviceId)) { |
| 88 | list.add(new FlowTableRow(flow)); |
| 89 | } |
| 90 | return list.toArray(new TableRow[list.size()]); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * TableRow implementation for {@link org.onosproject.net.flow.FlowRule flows}. |
| 95 | */ |
| 96 | private static class FlowTableRow extends AbstractTableRow { |
| 97 | |
| 98 | private static final String ID = "id"; |
| 99 | private static final String APP_ID = "appId"; |
| 100 | private static final String GROUP_ID = "groupId"; |
| 101 | private static final String TABLE_ID = "tableId"; |
| 102 | private static final String PRIORITY = "priority"; |
| 103 | private static final String SELECTOR = "selector"; |
| 104 | private static final String TREATMENT = "treatment"; |
| 105 | private static final String TIMEOUT = "timeout"; |
| 106 | private static final String PERMANENT = "permanent"; |
| 107 | private static final String STATE = "state"; |
| 108 | |
| 109 | private static final String COMMA = ", "; |
| 110 | |
| 111 | private static final String[] COL_IDS = { |
| 112 | ID, APP_ID, GROUP_ID, TABLE_ID, PRIORITY, SELECTOR, |
| 113 | TREATMENT, TIMEOUT, PERMANENT, STATE |
| 114 | }; |
| 115 | |
| 116 | public FlowTableRow(FlowEntry f) { |
| 117 | add(ID, Long.toString(f.id().value())); |
| 118 | add(APP_ID, Short.toString(f.appId())); |
| 119 | add(GROUP_ID, Integer.toString(f.groupId().id())); |
| 120 | add(TABLE_ID, Integer.toString(f.tableId())); |
| 121 | add(PRIORITY, Integer.toString(f.priority())); |
| 122 | add(SELECTOR, getSelectorString(f)); |
| 123 | add(TREATMENT, getTreatmentString(f)); |
| 124 | add(TIMEOUT, Integer.toString(f.timeout())); |
| 125 | add(PERMANENT, Boolean.toString(f.isPermanent())); |
| 126 | add(STATE, WordUtils.capitalizeFully(f.state().toString())); |
| 127 | } |
| 128 | |
| 129 | private String getSelectorString(FlowEntry f) { |
| 130 | String result; |
| 131 | TrafficSelector selector = f.selector(); |
| 132 | Set<Criterion> criteria = selector.criteria(); |
| 133 | |
| 134 | if (criteria.isEmpty()) { |
| 135 | result = "(No traffic selectors for this flow)"; |
| 136 | } else { |
| 137 | StringBuilder sb = new StringBuilder("Criteria = "); |
| 138 | for (Criterion c : criteria) { |
| 139 | sb.append(WordUtils.capitalizeFully(c.type().toString())) |
| 140 | .append(COMMA); |
| 141 | } |
| 142 | result = removeTrailingComma(sb).toString(); |
| 143 | } |
| 144 | return result; |
| 145 | } |
| 146 | |
| 147 | private String getTreatmentString(FlowEntry f) { |
| 148 | TrafficTreatment treatment = f.treatment(); |
| 149 | List<Instruction> deferred = treatment.deferred(); |
| 150 | List<Instruction> immediate = treatment.immediate(); |
| 151 | boolean haveDef = !deferred.isEmpty(); |
| 152 | boolean haveImm = !immediate.isEmpty(); |
| 153 | boolean both = haveDef && haveImm; |
| 154 | boolean neither = !haveDef && !haveImm; |
| 155 | String result; |
| 156 | |
| 157 | if (neither) { |
| 158 | result = "(No traffic treatment instructions for this flow)"; |
| 159 | } else { |
| 160 | StringBuilder sb = new StringBuilder(); |
| 161 | addDeferred(sb, deferred); |
| 162 | if (both) { |
| 163 | sb.append(COMMA); |
| 164 | } |
| 165 | addImmediate(sb, immediate); |
| 166 | result = sb.toString(); |
| 167 | } |
| 168 | return result; |
| 169 | } |
| 170 | |
| 171 | private void addDeferred(StringBuilder sb, List<Instruction> deferred) { |
| 172 | if (!deferred.isEmpty()) { |
| 173 | sb.append("Deferred instructions = "); |
| 174 | for (Instruction i : deferred) { |
| 175 | sb.append(WordUtils.capitalizeFully(i.type().toString())) |
| 176 | .append(COMMA); |
| 177 | } |
| 178 | removeTrailingComma(sb); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | private void addImmediate(StringBuilder sb, List<Instruction> immediate) { |
| 183 | if (!immediate.isEmpty()) { |
| 184 | sb.append("Immediate instructions = "); |
| 185 | for (Instruction i : immediate) { |
| 186 | sb.append(WordUtils.capitalizeFully(i.type().toString())) |
| 187 | .append(COMMA); |
| 188 | } |
| 189 | removeTrailingComma(sb); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | private StringBuilder removeTrailingComma(StringBuilder sb) { |
| 194 | int pos = sb.lastIndexOf(COMMA); |
| 195 | sb.delete(pos, sb.length()); |
| 196 | return sb; |
| 197 | } |
| 198 | |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 199 | @Override |
| 200 | protected String[] columnIds() { |
| 201 | return COL_IDS; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | } |