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