blob: 81965dad2bc223c70e2efa5c7873b84be3635623 [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;
20import com.google.common.collect.ImmutableSet;
21import org.apache.commons.lang.WordUtils;
22import 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 Hunt44aa2f82015-04-30 15:01:35 -070029import org.onosproject.ui.UiMessageHandler;
30import org.onosproject.ui.table.AbstractTableRow;
31import org.onosproject.ui.table.RowComparator;
32import org.onosproject.ui.table.TableRow;
33import org.onosproject.ui.table.TableUtils;
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070034
35import java.util.ArrayList;
36import java.util.Arrays;
37import java.util.List;
38import java.util.Set;
39
40
41/**
42 * Message handler for flow view related messages.
43 */
Simon Hunt44aa2f82015-04-30 15:01:35 -070044public class FlowViewMessageHandler extends UiMessageHandler {
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070045
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 Hunt44aa2f82015-04-30 15:01:35 -070057 String type = eventType(message);
58 if (type.equals("flowDataRequest")) {
59 sendFlowList(message);
60 }
61 }
62
63 private void sendFlowList(ObjectNode message) {
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070064 ObjectNode payload = payload(message);
Simon Hunt44aa2f82015-04-30 15:01:35 -070065 RowComparator rc = TableUtils.createRowComparator(payload);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070066 String uri = string(payload, "devId", NO_DEV);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070067
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 Colecdc188d2015-04-24 16:40:11 -070074 FlowRuleService service = get(FlowRuleService.class);
75 TableRow[] rows = generateTableRows(service, deviceId);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070076 Arrays.sort(rows, rc);
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070077 rootNode = mapper.createObjectNode();
Simon Hunt44aa2f82015-04-30 15:01:35 -070078 rootNode.set("flows", TableUtils.generateArrayNode(rows));
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070079 }
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 Colecdc188d2015-04-24 16:40:11 -0700199 @Override
200 protected String[] columnIds() {
201 return COL_IDS;
202 }
203 }
204
205}