blob: ced2f5ce387beabc0825d021365ea4e5a7fa6a7d [file] [log] [blame]
Thomas Vachuska7f171b22015-08-21 12:49:08 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska7f171b22015-08-21 12:49:08 -07003 *
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 */
16package org.onosproject.cli.net;
17
18import org.apache.karaf.shell.commands.Command;
19import org.onosproject.cli.AbstractShellCommand;
Thomas Vachuska924cda42015-09-22 12:11:27 -070020import org.onosproject.net.packet.PacketProcessorEntry;
Thomas Vachuska7f171b22015-08-21 12:49:08 -070021import org.onosproject.net.packet.PacketService;
22
23import static org.onosproject.net.packet.PacketProcessor.ADVISOR_MAX;
24import static org.onosproject.net.packet.PacketProcessor.DIRECTOR_MAX;
25
26/**
27 * Lists packet processors.
28 */
29@Command(scope = "onos", name = "packet-processors",
30 description = "Lists packet processors")
31public class PacketProcessorsListCommand extends AbstractShellCommand {
32
Thomas Vachuska924cda42015-09-22 12:11:27 -070033 private static final String FMT = "priority=%s, class=%s, packets=%d, avgNanos=%d";
Thomas Vachuska7f171b22015-08-21 12:49:08 -070034
35 @Override
36 protected void execute() {
37 PacketService service = get(PacketService.class);
38 if (outputJson()) {
39 // TODO: implement this
40 print("Not implemented.");
41 } else {
42 service.getProcessors().forEach(this::print);
43 }
44 }
45
Thomas Vachuska924cda42015-09-22 12:11:27 -070046 private void print(PacketProcessorEntry entry) {
47 print(FMT, priorityFormat(entry.priority()),
48 entry.processor().getClass().getName(),
49 entry.invocations(), entry.averageNanos());
Thomas Vachuska7f171b22015-08-21 12:49:08 -070050 }
51
52 private String priorityFormat(int priority) {
53 if (priority > DIRECTOR_MAX) {
54 return "observer(" + (priority - DIRECTOR_MAX - 1) + ")";
55 } else if (priority > ADVISOR_MAX) {
56 return "director(" + (priority - ADVISOR_MAX - 1) + ")";
57 }
58 return "advisor(" + (priority - 1) + ")";
59 }
60
61}