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; |
| 24 | import org.onosproject.net.packet.PacketRequest; |
| 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 | /** |
| 30 | * Lists packet requests. |
| 31 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 32 | @Service |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 33 | @Command(scope = "onos", name = "packet-requests", |
| 34 | description = "Lists packet requests") |
| 35 | public class PacketRequestsListCommand extends AbstractShellCommand { |
| 36 | |
Charles Chan | bb11581 | 2018-04-06 15:25:48 -0700 | [diff] [blame] | 37 | private static final String FMT = "nodeId=%s appId=%s, priority=%s, criteria=%s, deviceId=%s"; |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 38 | |
| 39 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 40 | protected void doExecute() { |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 41 | PacketService service = get(PacketService.class); |
| 42 | if (outputJson()) { |
sangyun-han | 3ccd373 | 2017-02-02 19:17:17 +0900 | [diff] [blame] | 43 | print("%s", json(service.getRequests())); |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 44 | } else { |
| 45 | service.getRequests().forEach(this::print); |
| 46 | } |
| 47 | } |
| 48 | |
sangyun-han | 3ccd373 | 2017-02-02 19:17:17 +0900 | [diff] [blame] | 49 | private JsonNode json(List<PacketRequest> requests) { |
| 50 | ObjectMapper mapper = new ObjectMapper(); |
| 51 | ArrayNode result = mapper.createArrayNode(); |
| 52 | |
| 53 | for (PacketRequest r : requests) { |
| 54 | result.add(mapper.createObjectNode() |
| 55 | .put("nodeId", r.nodeId().toString()) |
| 56 | .put("appId", r.appId().name()) |
| 57 | .put("priority", r.priority().toString()) |
Charles Chan | bb11581 | 2018-04-06 15:25:48 -0700 | [diff] [blame] | 58 | .put("criteria", r.selector().criteria().toString()) |
| 59 | .put("deviceId", r.deviceId().isPresent() ? r.deviceId().get().toString() : "ALL")); |
sangyun-han | 3ccd373 | 2017-02-02 19:17:17 +0900 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | return result; |
| 63 | } |
| 64 | |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 65 | private void print(PacketRequest request) { |
Charles Chan | bb11581 | 2018-04-06 15:25:48 -0700 | [diff] [blame] | 66 | print(FMT, request.nodeId(), request.appId().name(), request.priority(), request.selector().criteria(), |
| 67 | request.deviceId().isPresent() ? request.deviceId().get().toString() : "ALL"); |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | } |