Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 3 | * |
| 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 | package org.onosproject.cli.net; |
| 17 | |
sangyun-han | 3ccd373 | 2017-02-02 19:17:17 +0900 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Command; |
| 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
Thomas Vachuska | 924cda4 | 2015-09-22 12:11:27 -0700 | [diff] [blame] | 24 | import org.onosproject.net.packet.PacketProcessorEntry; |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 25 | import org.onosproject.net.packet.PacketService; |
| 26 | |
sangyun-han | 3ccd373 | 2017-02-02 19:17:17 +0900 | [diff] [blame] | 27 | import java.util.List; |
| 28 | |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 29 | import static org.onosproject.net.packet.PacketProcessor.ADVISOR_MAX; |
| 30 | import static org.onosproject.net.packet.PacketProcessor.DIRECTOR_MAX; |
| 31 | |
| 32 | /** |
| 33 | * Lists packet processors. |
| 34 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 35 | @Service |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 36 | @Command(scope = "onos", name = "packet-processors", |
| 37 | description = "Lists packet processors") |
| 38 | public class PacketProcessorsListCommand extends AbstractShellCommand { |
| 39 | |
Thomas Vachuska | 924cda4 | 2015-09-22 12:11:27 -0700 | [diff] [blame] | 40 | private static final String FMT = "priority=%s, class=%s, packets=%d, avgNanos=%d"; |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 41 | |
| 42 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 43 | protected void doExecute() { |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 44 | PacketService service = get(PacketService.class); |
| 45 | if (outputJson()) { |
sangyun-han | 3ccd373 | 2017-02-02 19:17:17 +0900 | [diff] [blame] | 46 | print("%s", json(service.getProcessors())); |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 47 | } else { |
| 48 | service.getProcessors().forEach(this::print); |
| 49 | } |
| 50 | } |
| 51 | |
sangyun-han | 3ccd373 | 2017-02-02 19:17:17 +0900 | [diff] [blame] | 52 | 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 Vachuska | 924cda4 | 2015-09-22 12:11:27 -0700 | [diff] [blame] | 67 | private void print(PacketProcessorEntry entry) { |
| 68 | print(FMT, priorityFormat(entry.priority()), |
| 69 | entry.processor().getClass().getName(), |
| 70 | entry.invocations(), entry.averageNanos()); |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 71 | } |
| 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 | } |