blob: 2844b5e1d0613d98b6f3fb57044a243f6fdc9798 [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
sangyun-han3ccd3732017-02-02 19:17:17 +090018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
Thomas Vachuska7f171b22015-08-21 12:49:08 -070021import org.apache.karaf.shell.commands.Command;
22import org.onosproject.cli.AbstractShellCommand;
Thomas Vachuska924cda42015-09-22 12:11:27 -070023import org.onosproject.net.packet.PacketProcessorEntry;
Thomas Vachuska7f171b22015-08-21 12:49:08 -070024import org.onosproject.net.packet.PacketService;
25
sangyun-han3ccd3732017-02-02 19:17:17 +090026import java.util.List;
27
Thomas Vachuska7f171b22015-08-21 12:49:08 -070028import static org.onosproject.net.packet.PacketProcessor.ADVISOR_MAX;
29import static org.onosproject.net.packet.PacketProcessor.DIRECTOR_MAX;
30
31/**
32 * Lists packet processors.
33 */
34@Command(scope = "onos", name = "packet-processors",
35 description = "Lists packet processors")
36public class PacketProcessorsListCommand extends AbstractShellCommand {
37
Thomas Vachuska924cda42015-09-22 12:11:27 -070038 private static final String FMT = "priority=%s, class=%s, packets=%d, avgNanos=%d";
Thomas Vachuska7f171b22015-08-21 12:49:08 -070039
40 @Override
41 protected void execute() {
42 PacketService service = get(PacketService.class);
43 if (outputJson()) {
sangyun-han3ccd3732017-02-02 19:17:17 +090044 print("%s", json(service.getProcessors()));
Thomas Vachuska7f171b22015-08-21 12:49:08 -070045 } else {
46 service.getProcessors().forEach(this::print);
47 }
48 }
49
sangyun-han3ccd3732017-02-02 19:17:17 +090050 private JsonNode json(List<PacketProcessorEntry> processors) {
51 ObjectMapper mapper = new ObjectMapper();
52 ArrayNode result = mapper.createArrayNode();
53
54 for (PacketProcessorEntry p : processors) {
55 result.add(mapper.createObjectNode()
56 .put("priority", priorityFormat(p.priority()))
57 .put("class", p.processor().getClass().getName())
58 .put("packets", p.invocations())
59 .put("avgNanos", p.averageNanos()));
60 }
61
62 return result;
63 }
64
Thomas Vachuska924cda42015-09-22 12:11:27 -070065 private void print(PacketProcessorEntry entry) {
66 print(FMT, priorityFormat(entry.priority()),
67 entry.processor().getClass().getName(),
68 entry.invocations(), entry.averageNanos());
Thomas Vachuska7f171b22015-08-21 12:49:08 -070069 }
70
71 private String priorityFormat(int priority) {
72 if (priority > DIRECTOR_MAX) {
73 return "observer(" + (priority - DIRECTOR_MAX - 1) + ")";
74 } else if (priority > ADVISOR_MAX) {
75 return "director(" + (priority - ADVISOR_MAX - 1) + ")";
76 }
77 return "advisor(" + (priority - 1) + ")";
78 }
79
80}