Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | |
| 18 | import org.apache.karaf.shell.commands.Command; |
| 19 | import org.onosproject.cli.AbstractShellCommand; |
Thomas Vachuska | 924cda4 | 2015-09-22 12:11:27 -0700 | [diff] [blame] | 20 | import org.onosproject.net.packet.PacketProcessorEntry; |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 21 | import org.onosproject.net.packet.PacketService; |
| 22 | |
| 23 | import static org.onosproject.net.packet.PacketProcessor.ADVISOR_MAX; |
| 24 | import 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") |
| 31 | public class PacketProcessorsListCommand extends AbstractShellCommand { |
| 32 | |
Thomas Vachuska | 924cda4 | 2015-09-22 12:11:27 -0700 | [diff] [blame] | 33 | 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] | 34 | |
| 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 Vachuska | 924cda4 | 2015-09-22 12:11:27 -0700 | [diff] [blame] | 46 | private void print(PacketProcessorEntry entry) { |
| 47 | print(FMT, priorityFormat(entry.priority()), |
| 48 | entry.processor().getClass().getName(), |
| 49 | entry.invocations(), entry.averageNanos()); |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 50 | } |
| 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 | } |