blob: 28a01f79c50ced154dffcdbbe4406d8552871f54 [file] [log] [blame]
Thomas Vachuska7f171b22015-08-21 12:49:08 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.apache.karaf.shell.api.action.Command;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
Thomas Vachuska7f171b22015-08-21 12:49:08 -070023import org.onosproject.cli.AbstractShellCommand;
Thomas Vachuska924cda42015-09-22 12:11:27 -070024import org.onosproject.net.packet.PacketProcessorEntry;
Thomas Vachuska7f171b22015-08-21 12:49:08 -070025import org.onosproject.net.packet.PacketService;
26
sangyun-han3ccd3732017-02-02 19:17:17 +090027import java.util.List;
28
Thomas Vachuska7f171b22015-08-21 12:49:08 -070029import static org.onosproject.net.packet.PacketProcessor.ADVISOR_MAX;
30import static org.onosproject.net.packet.PacketProcessor.DIRECTOR_MAX;
31
32/**
33 * Lists packet processors.
34 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070035@Service
Thomas Vachuska7f171b22015-08-21 12:49:08 -070036@Command(scope = "onos", name = "packet-processors",
37 description = "Lists packet processors")
38public class PacketProcessorsListCommand extends AbstractShellCommand {
39
Thomas Vachuska924cda42015-09-22 12:11:27 -070040 private static final String FMT = "priority=%s, class=%s, packets=%d, avgNanos=%d";
Thomas Vachuska7f171b22015-08-21 12:49:08 -070041
42 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070043 protected void doExecute() {
Thomas Vachuska7f171b22015-08-21 12:49:08 -070044 PacketService service = get(PacketService.class);
45 if (outputJson()) {
sangyun-han3ccd3732017-02-02 19:17:17 +090046 print("%s", json(service.getProcessors()));
Thomas Vachuska7f171b22015-08-21 12:49:08 -070047 } else {
48 service.getProcessors().forEach(this::print);
49 }
50 }
51
sangyun-han3ccd3732017-02-02 19:17:17 +090052 private JsonNode json(List<PacketProcessorEntry> processors) {
53 ObjectMapper mapper = new ObjectMapper();
54 ArrayNode result = mapper.createArrayNode();
55
56 for (PacketProcessorEntry p : processors) {
57 result.add(mapper.createObjectNode()
58 .put("priority", priorityFormat(p.priority()))
59 .put("class", p.processor().getClass().getName())
60 .put("packets", p.invocations())
61 .put("avgNanos", p.averageNanos()));
62 }
63
64 return result;
65 }
66
Thomas Vachuska924cda42015-09-22 12:11:27 -070067 private void print(PacketProcessorEntry entry) {
68 print(FMT, priorityFormat(entry.priority()),
69 entry.processor().getClass().getName(),
70 entry.invocations(), entry.averageNanos());
Thomas Vachuska7f171b22015-08-21 12:49:08 -070071 }
72
73 private String priorityFormat(int priority) {
74 if (priority > DIRECTOR_MAX) {
75 return "observer(" + (priority - DIRECTOR_MAX - 1) + ")";
76 } else if (priority > ADVISOR_MAX) {
77 return "director(" + (priority - ADVISOR_MAX - 1) + ")";
78 }
79 return "advisor(" + (priority - 1) + ")";
80 }
81
82}