GetFlowStatistics.java
13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.cli.net;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.apache.karaf.shell.commands.Option;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.Device;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Port;
import org.onosproject.net.PortNumber;
import org.onosproject.net.device.DeviceService;
import org.onosproject.net.flow.TypedStoredFlowEntry;
import org.onosproject.net.flow.instructions.Instruction;
import org.onosproject.net.statistic.FlowStatisticService;
import org.onosproject.net.statistic.SummaryFlowEntryWithLoad;
import org.onosproject.net.statistic.TypedFlowEntryWithLoad;
import java.util.List;
import java.util.Map;
import static org.onosproject.net.DeviceId.deviceId;
import static org.onosproject.net.PortNumber.portNumber;
/**
* Fetches flow statistics with a flow type and instruction type.
*/
@Command(scope = "onos", name = "get-flow-stats",
description = "Fetches flow stats for a connection point with given flow type and instruction type")
public class GetFlowStatistics extends AbstractShellCommand {
@Argument(index = 0, name = "devicePort",
description = "Device[/Port] connectPoint Description",
required = true, multiValued = false)
String devicePort = null;
@Option(name = "-s", aliases = "--summary",
description = "Show flow stats summary",
required = false, multiValued = false)
boolean showSummary = true; // default summary
@Option(name = "-a", aliases = "--all",
description = "Show flow stats all",
required = false, multiValued = false)
boolean showAll = false;
@Option(name = "-t", aliases = "--topn",
description = "Show flow stats topn",
required = false, multiValued = false)
String showTopn = null;
@Option(name = "-f", aliases = "--flowType",
description = "Flow live type, It includes IMMEDIATE, SHORT, MID, LONG, UNKNOWN"
+ ", and is valid with -a or -t option only",
required = false, multiValued = false)
String flowLiveType = null;
@Option(name = "-i", aliases = "--instructionType",
description = "Flow instruction type, It includes DROP, OUTPUT, GROUP, L0MODIFICATION, L2MODIFICATION,"
+ " TABLE, L3MODIFICATION, METADATA"
+ ", and is valid with -a or -t option only",
required = false, multiValued = false)
String instructionType = null;
@Override
protected void execute() {
DeviceService deviceService = get(DeviceService.class);
FlowStatisticService flowStatsService = get(FlowStatisticService.class);
String deviceURI = getDeviceId(devicePort);
String portURI = getPortNumber(devicePort);
DeviceId ingressDeviceId = deviceId(deviceURI);
PortNumber ingressPortNumber;
if (portURI.length() == 0) {
ingressPortNumber = null;
} else {
ingressPortNumber = portNumber(portURI);
}
Device device = deviceService.getDevice(ingressDeviceId);
if (device == null) {
error("No such device %s", ingressDeviceId.uri());
return;
}
if (ingressPortNumber != null) {
Port port = deviceService.getPort(ingressDeviceId, ingressPortNumber);
if (port == null) {
error("No such port %s on device %s", portURI, ingressDeviceId.uri());
return;
}
}
if (flowLiveType != null) {
flowLiveType = flowLiveType.toUpperCase();
}
if (instructionType != null) {
instructionType = instructionType.toUpperCase();
}
// convert String to FlowLiveType and check validity
TypedStoredFlowEntry.FlowLiveType inLiveType;
if (flowLiveType == null) {
inLiveType = null;
} else {
inLiveType = getFlowLiveType(flowLiveType);
if (inLiveType == null) {
error("Invalid flow live type [%s] error", flowLiveType);
return;
}
}
// convert String to InstructionType and check validity
Instruction.Type inInstructionType;
if (instructionType == null) {
inInstructionType = null;
} else {
inInstructionType = getInstructionType(instructionType);
if (inInstructionType == null) {
error("Invalid instruction type [%s] error", instructionType);
return;
}
}
if (showTopn != null) {
int topn = Integer.parseInt(showTopn);
if (topn <= 0) {
topn = 100; //default value
} else if (topn > 1000) {
topn = 1000; //max value
}
// print show topn head line with type
print("deviceId=%s, show TOPN=%s flows, live type=%s, instruction type=%s",
deviceURI,
Integer.toString(topn),
flowLiveType == null ? "ALL" : flowLiveType,
instructionType == null ? "ALL" : instructionType);
if (ingressPortNumber == null) {
Map<ConnectPoint, List<TypedFlowEntryWithLoad>> typedFlowLoadMap =
flowStatsService.loadTopnByType(device, inLiveType, inInstructionType, topn);
// print all ports topn flows load for a given device
for (ConnectPoint cp : typedFlowLoadMap.keySet()) {
printPortFlowsLoad(cp, typedFlowLoadMap.get(cp));
}
} else {
List<TypedFlowEntryWithLoad> typedFlowLoad =
flowStatsService.loadTopnByType(device, ingressPortNumber, inLiveType, inInstructionType, topn);
// print device/port topn flows load
ConnectPoint cp = new ConnectPoint(ingressDeviceId, ingressPortNumber);
printPortFlowsLoad(cp, typedFlowLoad);
}
} else if (showAll) { // is true?
// print show all head line with type
print("deviceId=%s, show ALL flows, live type=%s, instruction type=%s",
deviceURI,
flowLiveType == null ? "ALL" : flowLiveType,
instructionType == null ? "ALL" : instructionType);
if (ingressPortNumber == null) {
Map<ConnectPoint, List<TypedFlowEntryWithLoad>> typedFlowLoadMap =
flowStatsService.loadAllByType(device, inLiveType, inInstructionType);
// print all ports all flows load for a given device
for (ConnectPoint cp : typedFlowLoadMap.keySet()) {
printPortFlowsLoad(cp, typedFlowLoadMap.get(cp));
}
} else {
List<TypedFlowEntryWithLoad> typedFlowLoad =
flowStatsService.loadAllByType(device, ingressPortNumber, inLiveType, inInstructionType);
// print device/port all flows load
ConnectPoint cp = new ConnectPoint(ingressDeviceId, ingressPortNumber);
printPortFlowsLoad(cp, typedFlowLoad);
}
} else { // if (showSummary == true) //always is true
// print show summary head line
print("deviceId=%s, show SUMMARY flows", deviceURI);
if (ingressPortNumber == null) {
Map<ConnectPoint, SummaryFlowEntryWithLoad> summaryFlowLoadMap =
flowStatsService.loadSummary(device);
// print all ports flow load summary for a given device
for (ConnectPoint cp : summaryFlowLoadMap.keySet()) {
printPortSummaryLoad(cp, summaryFlowLoadMap.get(cp));
}
} else {
SummaryFlowEntryWithLoad summaryFlowLoad =
flowStatsService.loadSummary(device, ingressPortNumber);
// print device/port flow load summary
ConnectPoint cp = new ConnectPoint(ingressDeviceId, ingressPortNumber);
printPortSummaryLoad(cp, summaryFlowLoad);
}
}
}
/**
* Extracts the port number portion of the ConnectPoint.
*
* @param deviceString string representing the device/port
* @return port number as a string, empty string if the port is not found
*/
private String getPortNumber(String deviceString) {
if (deviceString == null) {
return "";
}
int slash = deviceString.indexOf('/');
if (slash <= 0) {
return ""; // return when no port number
}
return deviceString.substring(slash + 1, deviceString.length());
}
/**
* Extracts the device ID portion of the ConnectPoint.
*
* @param deviceString string representing the device/port
* @return device ID string
*/
private String getDeviceId(String deviceString) {
if (deviceString == null) {
return "";
}
int slash = deviceString.indexOf('/');
if (slash <= 0) {
return deviceString; // return only included device ID
}
return deviceString.substring(0, slash);
}
/**
* converts string of flow live type to FloeLiveType enum.
*
* @param liveType string representing the flow live type
* @return TypedStoredFlowEntry.FlowLiveType
*/
private TypedStoredFlowEntry.FlowLiveType getFlowLiveType(String liveType) {
String liveTypeUC = liveType.toUpperCase();
if (liveTypeUC.equals("IMMEDIATE")) {
return TypedStoredFlowEntry.FlowLiveType.IMMEDIATE_FLOW;
} else if (liveTypeUC.equals("SHORT")) {
return TypedStoredFlowEntry.FlowLiveType.SHORT_FLOW;
} else if (liveTypeUC.equals("MID")) {
return TypedStoredFlowEntry.FlowLiveType.MID_FLOW;
} else if (liveTypeUC.equals("LONG")) {
return TypedStoredFlowEntry.FlowLiveType.LONG_FLOW;
} else if (liveTypeUC.equals("UNKNOWN")) {
return TypedStoredFlowEntry.FlowLiveType.UNKNOWN_FLOW;
} else {
return null; // flow live type error
}
}
/**
* converts string of instruction type to Instruction type enum.
*
* @param instType string representing the instruction type
* @return Instruction.Type
*/
private Instruction.Type getInstructionType(String instType) {
String instTypeUC = instType.toUpperCase();
if (instTypeUC.equals("DROP")) {
return Instruction.Type.DROP;
} else if (instTypeUC.equals("OUTPUT")) {
return Instruction.Type.OUTPUT;
} else if (instTypeUC.equals("GROUP")) {
return Instruction.Type.GROUP;
} else if (instTypeUC.equals("L0MODIFICATION")) {
return Instruction.Type.L0MODIFICATION;
} else if (instTypeUC.equals("L2MODIFICATION")) {
return Instruction.Type.L2MODIFICATION;
} else if (instTypeUC.equals("TABLE")) {
return Instruction.Type.TABLE;
} else if (instTypeUC.equals("L3MODIFICATION")) {
return Instruction.Type.L3MODIFICATION;
} else if (instTypeUC.equals("METADATA")) {
return Instruction.Type.METADATA;
} else {
return null; // instruction type error
}
}
private void printPortFlowsLoad(ConnectPoint cp, List<TypedFlowEntryWithLoad> typedFlowLoad) {
print(" deviceId/Port=%s/%s, %s flows", cp.elementId(), cp.port(), typedFlowLoad.size());
for (TypedFlowEntryWithLoad tfel: typedFlowLoad) {
TypedStoredFlowEntry tfe = tfel.typedStoredFlowEntry();
print(" flowId=%s, state=%s, liveType=%s, life=%s -> %s",
Long.toHexString(tfe.id().value()),
tfe.state(),
tfe.flowLiveType(),
tfe.life(),
tfel.load().isValid() ? tfel.load() : "Load{rate=0, NOT VALID}");
}
}
private void printPortSummaryLoad(ConnectPoint cp, SummaryFlowEntryWithLoad summaryFlowLoad) {
print(" deviceId/Port=%s/%s, Total=%s, Immediate=%s, Short=%s, Mid=%s, Long=%s, Unknown=%s",
cp.elementId(),
cp.port(),
summaryFlowLoad.totalLoad().isValid() ? summaryFlowLoad.totalLoad() : "Load{rate=0, NOT VALID}",
summaryFlowLoad.immediateLoad().isValid() ? summaryFlowLoad.immediateLoad() : "Load{rate=0, NOT VALID}",
summaryFlowLoad.shortLoad().isValid() ? summaryFlowLoad.shortLoad() : "Load{rate=0, NOT VALID}",
summaryFlowLoad.midLoad().isValid() ? summaryFlowLoad.midLoad() : "Load{rate=0, NOT VALID}",
summaryFlowLoad.longLoad().isValid() ? summaryFlowLoad.longLoad() : "Load{rate=0, NOT VALID}",
summaryFlowLoad.unknownLoad().isValid() ? summaryFlowLoad.unknownLoad() : "Load{rate=0, NOT VALID}");
}
}