blob: aa86d6e6a7e0c9840b3ef909e78ca440729b9ca9 [file] [log] [blame]
Madan Jampanic27b6b22016-02-05 11:36:31 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampanic27b6b22016-02-05 11:36:31 -08003 *
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.cli.net;
18
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
22import org.apache.karaf.shell.api.action.Option;
Madan Jampanic27b6b22016-02-05 11:36:31 -080023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.Device;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.Port;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.device.DeviceService;
Sangsik Yoonb1b823f2016-05-16 18:55:39 +090030import org.onosproject.net.flow.FlowEntry;
31import org.onosproject.net.flow.StoredFlowEntry;
Madan Jampanic27b6b22016-02-05 11:36:31 -080032import org.onosproject.net.flow.instructions.Instruction;
Sangsik Yoonb1b823f2016-05-16 18:55:39 +090033import org.onosproject.net.statistic.FlowEntryWithLoad;
Madan Jampanic27b6b22016-02-05 11:36:31 -080034import org.onosproject.net.statistic.FlowStatisticService;
35import org.onosproject.net.statistic.SummaryFlowEntryWithLoad;
Madan Jampanic27b6b22016-02-05 11:36:31 -080036
37import java.util.List;
38import java.util.Map;
39
40import static org.onosproject.net.DeviceId.deviceId;
41import static org.onosproject.net.PortNumber.portNumber;
42
43/**
44 * Fetches flow statistics with a flow type and instruction type.
45 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070046@Service
Madan Jampanic27b6b22016-02-05 11:36:31 -080047@Command(scope = "onos", name = "get-flow-stats",
48 description = "Fetches flow stats for a connection point with given flow type and instruction type")
sangyun-han483731c2016-06-06 12:03:16 +090049public class GetFlowStatisticsCommand extends AbstractShellCommand {
Madan Jampanic27b6b22016-02-05 11:36:31 -080050 @Argument(index = 0, name = "devicePort",
51 description = "Device[/Port] connectPoint Description",
52 required = true, multiValued = false)
53 String devicePort = null;
54
55 @Option(name = "-s", aliases = "--summary",
56 description = "Show flow stats summary",
57 required = false, multiValued = false)
58 boolean showSummary = true; // default summary
59
60 @Option(name = "-a", aliases = "--all",
61 description = "Show flow stats all",
62 required = false, multiValued = false)
63 boolean showAll = false;
64
65 @Option(name = "-t", aliases = "--topn",
Sangsik Yoonb1b823f2016-05-16 18:55:39 +090066 description = "Show flow stats topn entry",
Madan Jampanic27b6b22016-02-05 11:36:31 -080067 required = false, multiValued = false)
68 String showTopn = null;
69
70 @Option(name = "-f", aliases = "--flowType",
71 description = "Flow live type, It includes IMMEDIATE, SHORT, MID, LONG, UNKNOWN"
72 + ", and is valid with -a or -t option only",
73 required = false, multiValued = false)
74 String flowLiveType = null;
75
76 @Option(name = "-i", aliases = "--instructionType",
77 description = "Flow instruction type, It includes DROP, OUTPUT, GROUP, L0MODIFICATION, L2MODIFICATION,"
78 + " TABLE, L3MODIFICATION, METADATA"
79 + ", and is valid with -a or -t option only",
80 required = false, multiValued = false)
81 String instructionType = null;
82
83 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070084 protected void doExecute() {
Madan Jampanic27b6b22016-02-05 11:36:31 -080085 DeviceService deviceService = get(DeviceService.class);
86 FlowStatisticService flowStatsService = get(FlowStatisticService.class);
87
88 String deviceUri = getDeviceId(devicePort);
89 String portUri = getPortNumber(devicePort);
90
91 DeviceId ingressDeviceId = deviceId(deviceUri);
92 PortNumber ingressPortNumber;
93 if (portUri.length() == 0) {
94 ingressPortNumber = null;
95 } else {
96 ingressPortNumber = portNumber(portUri);
97 }
98
99 Device device = deviceService.getDevice(ingressDeviceId);
100 if (device == null) {
101 error("No such device %s", ingressDeviceId.uri());
102 return;
103 }
104
105 if (ingressPortNumber != null) {
106 Port port = deviceService.getPort(ingressDeviceId, ingressPortNumber);
107 if (port == null) {
108 error("No such port %s on device %s", portUri, ingressDeviceId.uri());
109 return;
110 }
111 }
112
113 if (flowLiveType != null) {
114 flowLiveType = flowLiveType.toUpperCase();
115 }
116 if (instructionType != null) {
117 instructionType = instructionType.toUpperCase();
118 }
119
120 // convert String to FlowLiveType and check validity
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900121 FlowEntry.FlowLiveType inLiveType;
Madan Jampanic27b6b22016-02-05 11:36:31 -0800122 if (flowLiveType == null) {
123 inLiveType = null;
124 } else {
125 inLiveType = getFlowLiveType(flowLiveType);
126 if (inLiveType == null) {
127 error("Invalid flow live type [%s] error", flowLiveType);
128 return;
129 }
130 }
131 // convert String to InstructionType and check validity
132 Instruction.Type inInstructionType;
133 if (instructionType == null) {
134 inInstructionType = null;
135 } else {
136 inInstructionType = getInstructionType(instructionType);
137 if (inInstructionType == null) {
138 error("Invalid instruction type [%s] error", instructionType);
139 return;
140 }
141 }
142
143 if (showTopn != null) {
144 int topn = Integer.parseInt(showTopn);
145
146 if (topn <= 0) {
147 topn = 100; //default value
148 } else if (topn > 1000) {
149 topn = 1000; //max value
150 }
151
152 // print show topn head line with type
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900153 print("deviceId=%s, show TOPN=%s flows, liveType=%s, instruction type=%s",
Madan Jampanic27b6b22016-02-05 11:36:31 -0800154 deviceUri,
155 Integer.toString(topn),
156 flowLiveType == null ? "ALL" : flowLiveType,
157 instructionType == null ? "ALL" : instructionType);
158 if (ingressPortNumber == null) {
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900159 Map<ConnectPoint, List<FlowEntryWithLoad>> typedFlowLoadMap =
Madan Jampanic27b6b22016-02-05 11:36:31 -0800160 flowStatsService.loadTopnByType(device, inLiveType, inInstructionType, topn);
161 // print all ports topn flows load for a given device
162 for (ConnectPoint cp : typedFlowLoadMap.keySet()) {
163 printPortFlowsLoad(cp, typedFlowLoadMap.get(cp));
164 }
165 } else {
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900166 List<FlowEntryWithLoad> typedFlowLoad =
Madan Jampanic27b6b22016-02-05 11:36:31 -0800167 flowStatsService.loadTopnByType(device, ingressPortNumber, inLiveType, inInstructionType, topn);
168 // print device/port topn flows load
169 ConnectPoint cp = new ConnectPoint(ingressDeviceId, ingressPortNumber);
170 printPortFlowsLoad(cp, typedFlowLoad);
171 }
172 } else if (showAll) { // is true?
173 // print show all head line with type
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900174 print("deviceId=%s, show ALL flows, liveType=%s, instruction type=%s",
Madan Jampanic27b6b22016-02-05 11:36:31 -0800175 deviceUri,
176 flowLiveType == null ? "ALL" : flowLiveType,
177 instructionType == null ? "ALL" : instructionType);
178 if (ingressPortNumber == null) {
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900179 Map<ConnectPoint, List<FlowEntryWithLoad>> typedFlowLoadMap =
Madan Jampanic27b6b22016-02-05 11:36:31 -0800180 flowStatsService.loadAllByType(device, inLiveType, inInstructionType);
181 // print all ports all flows load for a given device
182 for (ConnectPoint cp : typedFlowLoadMap.keySet()) {
183 printPortFlowsLoad(cp, typedFlowLoadMap.get(cp));
184 }
185 } else {
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900186 List<FlowEntryWithLoad> typedFlowLoad =
Madan Jampanic27b6b22016-02-05 11:36:31 -0800187 flowStatsService.loadAllByType(device, ingressPortNumber, inLiveType, inInstructionType);
188 // print device/port all flows load
189 ConnectPoint cp = new ConnectPoint(ingressDeviceId, ingressPortNumber);
190 printPortFlowsLoad(cp, typedFlowLoad);
191 }
192 } else { // if (showSummary == true) //always is true
193 // print show summary head line
194 print("deviceId=%s, show SUMMARY flows", deviceUri);
195 if (ingressPortNumber == null) {
196 Map<ConnectPoint, SummaryFlowEntryWithLoad> summaryFlowLoadMap =
197 flowStatsService.loadSummary(device);
198 // print all ports flow load summary for a given device
199 for (ConnectPoint cp : summaryFlowLoadMap.keySet()) {
200 printPortSummaryLoad(cp, summaryFlowLoadMap.get(cp));
201 }
202 } else {
203 SummaryFlowEntryWithLoad summaryFlowLoad =
204 flowStatsService.loadSummary(device, ingressPortNumber);
205 // print device/port flow load summary
206 ConnectPoint cp = new ConnectPoint(ingressDeviceId, ingressPortNumber);
207 printPortSummaryLoad(cp, summaryFlowLoad);
208 }
209 }
210 }
211
212 /**
213 * Extracts the port number portion of the ConnectPoint.
214 *
215 * @param deviceString string representing the device/port
216 * @return port number as a string, empty string if the port is not found
217 */
218 private String getPortNumber(String deviceString) {
219 if (deviceString == null) {
220 return "";
221 }
222
223 int slash = deviceString.indexOf('/');
224 if (slash <= 0) {
225 return ""; // return when no port number
226 }
227 return deviceString.substring(slash + 1, deviceString.length());
228 }
229
230 /**
231 * Extracts the device ID portion of the ConnectPoint.
232 *
233 * @param deviceString string representing the device/port
234 * @return device ID string
235 */
236 private String getDeviceId(String deviceString) {
237 if (deviceString == null) {
238 return "";
239 }
240
241 int slash = deviceString.indexOf('/');
242 if (slash <= 0) {
243 return deviceString; // return only included device ID
244 }
245 return deviceString.substring(0, slash);
246 }
247
248 /**
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900249 * converts string of flow live type to FlowLiveType enum.
Madan Jampanic27b6b22016-02-05 11:36:31 -0800250 *
251 * @param liveType string representing the flow live type
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900252 * @return FlowEntry.FlowLiveType
Madan Jampanic27b6b22016-02-05 11:36:31 -0800253 */
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900254 private FlowEntry.FlowLiveType getFlowLiveType(String liveType) {
Madan Jampanic27b6b22016-02-05 11:36:31 -0800255 String liveTypeUC = liveType.toUpperCase();
256
Jon Halla3fcf672017-03-28 16:53:22 -0700257 if ("IMMEDIATE".equals(liveTypeUC)) {
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900258 return FlowEntry.FlowLiveType.IMMEDIATE;
Jon Halla3fcf672017-03-28 16:53:22 -0700259 } else if ("SHORT".equals(liveTypeUC)) {
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900260 return FlowEntry.FlowLiveType.SHORT;
Jon Halla3fcf672017-03-28 16:53:22 -0700261 } else if ("MID".equals(liveTypeUC)) {
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900262 return FlowEntry.FlowLiveType.MID;
Jon Halla3fcf672017-03-28 16:53:22 -0700263 } else if ("LONG".equals(liveTypeUC)) {
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900264 return FlowEntry.FlowLiveType.LONG;
Jon Halla3fcf672017-03-28 16:53:22 -0700265 } else if ("UNKNOWN".equals(liveTypeUC)) {
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900266 return FlowEntry.FlowLiveType.UNKNOWN;
Madan Jampanic27b6b22016-02-05 11:36:31 -0800267 } else {
268 return null; // flow live type error
269 }
270 }
271
272 /**
273 * converts string of instruction type to Instruction type enum.
274 *
275 * @param instType string representing the instruction type
276 * @return Instruction.Type
277 */
278 private Instruction.Type getInstructionType(String instType) {
279 String instTypeUC = instType.toUpperCase();
280
Jon Halla3fcf672017-03-28 16:53:22 -0700281 if ("OUTPUT".equals(instTypeUC)) {
Madan Jampanic27b6b22016-02-05 11:36:31 -0800282 return Instruction.Type.OUTPUT;
Jon Halla3fcf672017-03-28 16:53:22 -0700283 } else if ("GROUP".equals(instTypeUC)) {
Madan Jampanic27b6b22016-02-05 11:36:31 -0800284 return Instruction.Type.GROUP;
Jon Halla3fcf672017-03-28 16:53:22 -0700285 } else if ("L0MODIFICATION".equals(instTypeUC)) {
Madan Jampanic27b6b22016-02-05 11:36:31 -0800286 return Instruction.Type.L0MODIFICATION;
Jon Halla3fcf672017-03-28 16:53:22 -0700287 } else if ("L2MODIFICATION".equals(instTypeUC)) {
Madan Jampanic27b6b22016-02-05 11:36:31 -0800288 return Instruction.Type.L2MODIFICATION;
Jon Halla3fcf672017-03-28 16:53:22 -0700289 } else if ("TABLE".equals(instTypeUC)) {
Madan Jampanic27b6b22016-02-05 11:36:31 -0800290 return Instruction.Type.TABLE;
Jon Halla3fcf672017-03-28 16:53:22 -0700291 } else if ("L3MODIFICATION".equals(instTypeUC)) {
Madan Jampanic27b6b22016-02-05 11:36:31 -0800292 return Instruction.Type.L3MODIFICATION;
Jon Halla3fcf672017-03-28 16:53:22 -0700293 } else if ("METADATA".equals(instTypeUC)) {
Madan Jampanic27b6b22016-02-05 11:36:31 -0800294 return Instruction.Type.METADATA;
295 } else {
296 return null; // instruction type error
297 }
298 }
299
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900300 private void printPortFlowsLoad(ConnectPoint cp, List<FlowEntryWithLoad> typedFlowLoad) {
Madan Jampanic27b6b22016-02-05 11:36:31 -0800301 print(" deviceId/Port=%s/%s, %s flows", cp.elementId(), cp.port(), typedFlowLoad.size());
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900302 for (FlowEntryWithLoad fel: typedFlowLoad) {
303 StoredFlowEntry sfe = fel.storedFlowEntry();
Madan Jampanic27b6b22016-02-05 11:36:31 -0800304 print(" flowId=%s, state=%s, liveType=%s, life=%s -> %s",
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900305 Long.toHexString(sfe.id().value()),
306 sfe.state(),
307 sfe.liveType(),
308 sfe.life(),
309 fel.load().isValid() ? fel.load() : "Load{rate=0, NOT VALID}");
Madan Jampanic27b6b22016-02-05 11:36:31 -0800310 }
311 }
312
313 private void printPortSummaryLoad(ConnectPoint cp, SummaryFlowEntryWithLoad summaryFlowLoad) {
314 print(" deviceId/Port=%s/%s, Total=%s, Immediate=%s, Short=%s, Mid=%s, Long=%s, Unknown=%s",
315 cp.elementId(),
316 cp.port(),
317 summaryFlowLoad.totalLoad().isValid() ? summaryFlowLoad.totalLoad() : "Load{rate=0, NOT VALID}",
318 summaryFlowLoad.immediateLoad().isValid() ? summaryFlowLoad.immediateLoad() : "Load{rate=0, NOT VALID}",
319 summaryFlowLoad.shortLoad().isValid() ? summaryFlowLoad.shortLoad() : "Load{rate=0, NOT VALID}",
320 summaryFlowLoad.midLoad().isValid() ? summaryFlowLoad.midLoad() : "Load{rate=0, NOT VALID}",
321 summaryFlowLoad.longLoad().isValid() ? summaryFlowLoad.longLoad() : "Load{rate=0, NOT VALID}",
322 summaryFlowLoad.unknownLoad().isValid() ? summaryFlowLoad.unknownLoad() : "Load{rate=0, NOT VALID}");
323 }
324}