blob: 2634592ff17163629f173ec7188d3191a364d607 [file] [log] [blame]
Thomas Vachuska7f171b22015-08-21 12:49:08 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
Thomas Vachuska7f171b22015-08-21 12:49:08 -070021import org.apache.karaf.shell.commands.Command;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.net.packet.PacketRequest;
24import org.onosproject.net.packet.PacketService;
25
sangyun-han3ccd3732017-02-02 19:17:17 +090026import java.util.List;
27
Thomas Vachuska7f171b22015-08-21 12:49:08 -070028/**
29 * Lists packet requests.
30 */
31@Command(scope = "onos", name = "packet-requests",
32 description = "Lists packet requests")
33public class PacketRequestsListCommand extends AbstractShellCommand {
34
Pier Luigi Ventre6fd1ca92017-01-13 03:15:50 +000035 private static final String FMT = "nodeId=%s appId=%s, priority=%s, criteria=%s";
Thomas Vachuska7f171b22015-08-21 12:49:08 -070036
37 @Override
38 protected void execute() {
39 PacketService service = get(PacketService.class);
40 if (outputJson()) {
sangyun-han3ccd3732017-02-02 19:17:17 +090041 print("%s", json(service.getRequests()));
Thomas Vachuska7f171b22015-08-21 12:49:08 -070042 } else {
43 service.getRequests().forEach(this::print);
44 }
45 }
46
sangyun-han3ccd3732017-02-02 19:17:17 +090047 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 Vachuska7f171b22015-08-21 12:49:08 -070062 private void print(PacketRequest request) {
Pier Luigi Ventre6fd1ca92017-01-13 03:15:50 +000063 print(FMT, request.nodeId(), request.appId().name(), request.priority(), request.selector().criteria());
Thomas Vachuska7f171b22015-08-21 12:49:08 -070064 }
65
66}