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 | |
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; |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Command; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.net.packet.PacketRequest; |
| 24 | import org.onosproject.net.packet.PacketService; |
| 25 | |
sangyun-han | 3ccd373 | 2017-02-02 19:17:17 +0900 | [diff] [blame] | 26 | import java.util.List; |
| 27 | |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Lists packet requests. |
| 30 | */ |
| 31 | @Command(scope = "onos", name = "packet-requests", |
| 32 | description = "Lists packet requests") |
| 33 | public class PacketRequestsListCommand extends AbstractShellCommand { |
| 34 | |
Pier Luigi Ventre | 6fd1ca9 | 2017-01-13 03:15:50 +0000 | [diff] [blame] | 35 | private static final String FMT = "nodeId=%s appId=%s, priority=%s, criteria=%s"; |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 36 | |
| 37 | @Override |
| 38 | protected void execute() { |
| 39 | PacketService service = get(PacketService.class); |
| 40 | if (outputJson()) { |
sangyun-han | 3ccd373 | 2017-02-02 19:17:17 +0900 | [diff] [blame] | 41 | print("%s", json(service.getRequests())); |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 42 | } else { |
| 43 | service.getRequests().forEach(this::print); |
| 44 | } |
| 45 | } |
| 46 | |
sangyun-han | 3ccd373 | 2017-02-02 19:17:17 +0900 | [diff] [blame] | 47 | private JsonNode json(List<PacketRequest> requests) { |
| 48 | ObjectMapper mapper = new ObjectMapper(); |
| 49 | ArrayNode result = mapper.createArrayNode(); |
| 50 | |
| 51 | for (PacketRequest r : requests) { |
| 52 | result.add(mapper.createObjectNode() |
| 53 | .put("nodeId", r.nodeId().toString()) |
| 54 | .put("appId", r.appId().name()) |
| 55 | .put("priority", r.priority().toString()) |
| 56 | .put("criteria", r.selector().criteria().toString())); |
| 57 | } |
| 58 | |
| 59 | return result; |
| 60 | } |
| 61 | |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 62 | private void print(PacketRequest request) { |
Pier Luigi Ventre | 6fd1ca9 | 2017-01-13 03:15:50 +0000 | [diff] [blame] | 63 | print(FMT, request.nodeId(), request.appId().name(), request.priority(), request.selector().criteria()); |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | } |