blob: ecc2dc173c0dfedeec7a8f36123ce15b467b36e4 [file] [log] [blame]
Thomas Vachuska7f171b22015-08-21 12:49:08 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.apache.karaf.shell.api.action.Command;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
Thomas Vachuska7f171b22015-08-21 12:49:08 -070023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.net.packet.PacketRequest;
25import org.onosproject.net.packet.PacketService;
26
sangyun-han3ccd3732017-02-02 19:17:17 +090027import java.util.List;
28
Thomas Vachuska7f171b22015-08-21 12:49:08 -070029/**
30 * Lists packet requests.
31 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070032@Service
Thomas Vachuska7f171b22015-08-21 12:49:08 -070033@Command(scope = "onos", name = "packet-requests",
34 description = "Lists packet requests")
35public class PacketRequestsListCommand extends AbstractShellCommand {
36
Charles Chanbb115812018-04-06 15:25:48 -070037 private static final String FMT = "nodeId=%s appId=%s, priority=%s, criteria=%s, deviceId=%s";
Thomas Vachuska7f171b22015-08-21 12:49:08 -070038
39 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040 protected void doExecute() {
Thomas Vachuska7f171b22015-08-21 12:49:08 -070041 PacketService service = get(PacketService.class);
42 if (outputJson()) {
sangyun-han3ccd3732017-02-02 19:17:17 +090043 print("%s", json(service.getRequests()));
Thomas Vachuska7f171b22015-08-21 12:49:08 -070044 } else {
45 service.getRequests().forEach(this::print);
46 }
47 }
48
sangyun-han3ccd3732017-02-02 19:17:17 +090049 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 Chanbb115812018-04-06 15:25:48 -070058 .put("criteria", r.selector().criteria().toString())
59 .put("deviceId", r.deviceId().isPresent() ? r.deviceId().get().toString() : "ALL"));
sangyun-han3ccd3732017-02-02 19:17:17 +090060 }
61
62 return result;
63 }
64
Thomas Vachuska7f171b22015-08-21 12:49:08 -070065 private void print(PacketRequest request) {
Charles Chanbb115812018-04-06 15:25:48 -070066 print(FMT, request.nodeId(), request.appId().name(), request.priority(), request.selector().criteria(),
67 request.deviceId().isPresent() ? request.deviceId().get().toString() : "ALL");
Thomas Vachuska7f171b22015-08-21 12:49:08 -070068 }
69
70}