blob: 5a795ed8edc6a62138182d5d8086d2e1089b65f9 [file] [log] [blame]
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -07001/*
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
17package org.onosproject.ui.impl;
18
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070019import com.fasterxml.jackson.databind.node.ObjectNode;
Simon Huntabd16f62015-05-01 13:14:40 -070020import com.google.common.base.Strings;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070021import com.google.common.collect.ImmutableSet;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070022import org.onosproject.net.DeviceId;
23import org.onosproject.net.flow.FlowEntry;
24import org.onosproject.net.flow.FlowRuleService;
25import org.onosproject.net.flow.TrafficSelector;
26import org.onosproject.net.flow.TrafficTreatment;
27import org.onosproject.net.flow.criteria.Criterion;
28import org.onosproject.net.flow.instructions.Instruction;
Simon Huntd2747a02015-04-30 22:41:16 -070029import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070030import org.onosproject.ui.UiMessageHandler;
Simon Hunt44aa2f82015-04-30 15:01:35 -070031import org.onosproject.ui.table.AbstractTableRow;
Simon Huntabd16f62015-05-01 13:14:40 -070032import org.onosproject.ui.table.TableRequestHandler;
Simon Hunt44aa2f82015-04-30 15:01:35 -070033import org.onosproject.ui.table.TableRow;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070034
35import java.util.ArrayList;
Simon Huntd2747a02015-04-30 22:41:16 -070036import java.util.Collection;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070037import java.util.List;
38import java.util.Set;
39
Simon Huntabd16f62015-05-01 13:14:40 -070040import static org.apache.commons.lang.WordUtils.capitalizeFully;
41
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070042
43/**
44 * Message handler for flow view related messages.
45 */
Simon Hunta0ddb022015-05-01 09:53:01 -070046public class FlowViewMessageHandler extends UiMessageHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070047
48 private static final String FLOW_DATA_REQ = "flowDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070049 private static final String FLOW_DATA_RESP = "flowDataResponse";
50 private static final String FLOWS = "flows";
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070051
Simon Huntabd16f62015-05-01 13:14:40 -070052 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 Cole641b97b2015-05-05 14:47:40 -070062 private static final String PACKETS = "packets";
63 private static final String BYTES = "bytes";
Simon Huntabd16f62015-05-01 13:14:40 -070064
65 private static final String COMMA = ", ";
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070066
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070067 @Override
Simon Huntd2747a02015-04-30 22:41:16 -070068 protected Collection<RequestHandler> getHandlers() {
69 return ImmutableSet.of(new FlowDataRequest());
70 }
71
Simon Huntabd16f62015-05-01 13:14:40 -070072 // handler for flow table requests
73 private final class FlowDataRequest extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070074
75 private FlowDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070076 super(FLOW_DATA_REQ, FLOW_DATA_RESP, FLOWS);
Simon Huntd2747a02015-04-30 22:41:16 -070077 }
78
79 @Override
Simon Huntabd16f62015-05-01 13:14:40 -070080 protected TableRow[] generateTableRows(ObjectNode payload) {
81 String uri = string(payload, "devId");
82 if (Strings.isNullOrEmpty(uri)) {
83 return new TableRow[0];
Simon Huntd2747a02015-04-30 22:41:16 -070084 }
Simon Huntabd16f62015-05-01 13:14:40 -070085 DeviceId deviceId = DeviceId.deviceId(uri);
86 FlowRuleService service = get(FlowRuleService.class);
Simon Huntd2747a02015-04-30 22:41:16 -070087 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 Hunt44aa2f82015-04-30 15:01:35 -070092 }
93 }
94
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070095 /**
Simon Huntabd16f62015-05-01 13:14:40 -070096 * TableRow implementation for
97 * {@link org.onosproject.net.flow.FlowRule flows}.
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070098 */
99 private static class FlowTableRow extends AbstractTableRow {
100
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700101 private static final String[] COL_IDS = {
Simon Huntabd16f62015-05-01 13:14:40 -0700102 ID, APP_ID, GROUP_ID, TABLE_ID, PRIORITY, SELECTOR,
Bri Prebilic Cole641b97b2015-05-05 14:47:40 -0700103 TREATMENT, TIMEOUT, PERMANENT, STATE, PACKETS, BYTES
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700104 };
105
106 public FlowTableRow(FlowEntry f) {
Simon Huntabd16f62015-05-01 13:14:40 -0700107 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 Colecdc188d2015-04-24 16:40:11 -0700112 add(SELECTOR, getSelectorString(f));
113 add(TREATMENT, getTreatmentString(f));
Simon Huntabd16f62015-05-01 13:14:40 -0700114 add(TIMEOUT, f.timeout());
115 add(PERMANENT, f.isPermanent());
116 add(STATE, capitalizeFully(f.state().toString()));
Bri Prebilic Cole641b97b2015-05-05 14:47:40 -0700117 add(PACKETS, f.packets());
118 add(BYTES, f.packets());
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700119 }
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 Huntabd16f62015-05-01 13:14:40 -0700131 sb.append(capitalizeFully(c.type().toString())).append(COMMA);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700132 }
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 Huntabd16f62015-05-01 13:14:40 -0700166 sb.append(capitalizeFully(i.type().toString())).append(COMMA);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700167 }
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 Huntabd16f62015-05-01 13:14:40 -0700176 sb.append(capitalizeFully(i.type().toString())).append(COMMA);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700177 }
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 Colecdc188d2015-04-24 16:40:11 -0700188 @Override
189 protected String[] columnIds() {
190 return COL_IDS;
191 }
192 }
193
194}