blob: 184197080f7065b9f0ddbd22199ee3397c3549b0 [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";
62
63 private static final String COMMA = ", ";
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070064
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070065 @Override
Simon Huntd2747a02015-04-30 22:41:16 -070066 protected Collection<RequestHandler> getHandlers() {
67 return ImmutableSet.of(new FlowDataRequest());
68 }
69
Simon Huntabd16f62015-05-01 13:14:40 -070070 // handler for flow table requests
71 private final class FlowDataRequest extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070072
73 private FlowDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070074 super(FLOW_DATA_REQ, FLOW_DATA_RESP, FLOWS);
Simon Huntd2747a02015-04-30 22:41:16 -070075 }
76
77 @Override
Simon Huntabd16f62015-05-01 13:14:40 -070078 protected TableRow[] generateTableRows(ObjectNode payload) {
79 String uri = string(payload, "devId");
80 if (Strings.isNullOrEmpty(uri)) {
81 return new TableRow[0];
Simon Huntd2747a02015-04-30 22:41:16 -070082 }
Simon Huntabd16f62015-05-01 13:14:40 -070083 DeviceId deviceId = DeviceId.deviceId(uri);
84 FlowRuleService service = get(FlowRuleService.class);
Simon Huntd2747a02015-04-30 22:41:16 -070085 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 Hunt44aa2f82015-04-30 15:01:35 -070090 }
91 }
92
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070093 /**
Simon Huntabd16f62015-05-01 13:14:40 -070094 * TableRow implementation for
95 * {@link org.onosproject.net.flow.FlowRule flows}.
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070096 */
97 private static class FlowTableRow extends AbstractTableRow {
98
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070099 private static final String[] COL_IDS = {
Simon Huntabd16f62015-05-01 13:14:40 -0700100 ID, APP_ID, GROUP_ID, TABLE_ID, PRIORITY, SELECTOR,
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700101 TREATMENT, TIMEOUT, PERMANENT, STATE
102 };
103
104 public FlowTableRow(FlowEntry f) {
Simon Huntabd16f62015-05-01 13:14:40 -0700105 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 Colecdc188d2015-04-24 16:40:11 -0700110 add(SELECTOR, getSelectorString(f));
111 add(TREATMENT, getTreatmentString(f));
Simon Huntabd16f62015-05-01 13:14:40 -0700112 add(TIMEOUT, f.timeout());
113 add(PERMANENT, f.isPermanent());
114 add(STATE, capitalizeFully(f.state().toString()));
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700115 }
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 Huntabd16f62015-05-01 13:14:40 -0700127 sb.append(capitalizeFully(c.type().toString())).append(COMMA);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700128 }
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 Huntabd16f62015-05-01 13:14:40 -0700162 sb.append(capitalizeFully(i.type().toString())).append(COMMA);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700163 }
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 Huntabd16f62015-05-01 13:14:40 -0700172 sb.append(capitalizeFully(i.type().toString())).append(COMMA);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700173 }
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 Colecdc188d2015-04-24 16:40:11 -0700184 @Override
185 protected String[] columnIds() {
186 return COL_IDS;
187 }
188 }
189
190}