blob: 2f24cebac4b7dc7cee2dbbeddbb45cd634798f74 [file] [log] [blame]
Aaron Kruglikova2b59152015-06-24 14:01:41 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Aaron Kruglikova2b59152015-06-24 14:01:41 -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
Seyeon Jeong357bcec2020-02-28 01:17:34 -080018import com.fasterxml.jackson.databind.node.ArrayNode;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
Ray Milkey0068fd02018-10-11 15:45:39 -070021import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070023import org.onosproject.cli.AbstractShellCommand;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070024import org.onosproject.net.ConnectPoint;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070025import org.onosproject.net.edge.EdgePortService;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070026import org.onosproject.utils.Comparators;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070027
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070028import java.util.Collections;
29import java.util.List;
30
31import static com.google.common.collect.Lists.newArrayList;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070032import static org.onosproject.net.DeviceId.deviceId;
33
34/**
35 * Lists all edge ports.
36 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070037@Service
Aaron Kruglikova2b59152015-06-24 14:01:41 -070038@Command(scope = "onos", name = "edge-ports",
39 description = "Lists all edge ports.")
40public class EdgePortsListCommand extends AbstractShellCommand {
41
42 private static final String FMT = "%s/%s";
43
44 @Argument(index = 0, name = "uri", description = "Device ID",
45 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070046 @Completion(DeviceIdCompleter.class)
Aaron Kruglikova2b59152015-06-24 14:01:41 -070047 String uri = null;
48
Aaron Kruglikova2b59152015-06-24 14:01:41 -070049 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070050 protected void doExecute() {
Aaron Kruglikova2b59152015-06-24 14:01:41 -070051 EdgePortService service = get(EdgePortService.class);
52 if (uri == null) {
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070053 printEdgePoints(service.getEdgePoints());
Aaron Kruglikova2b59152015-06-24 14:01:41 -070054 } else {
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070055 printEdgePoints(service.getEdgePoints(deviceId(uri)));
Aaron Kruglikova2b59152015-06-24 14:01:41 -070056 }
57 }
58
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070059 private void printEdgePoints(Iterable<ConnectPoint> edgePoints) {
Seyeon Jeong357bcec2020-02-28 01:17:34 -080060 List<ConnectPoint> sorted = sort(edgePoints);
61 if (outputJson()) {
62 ArrayNode result = mapper().createObjectNode().putArray(null);
63 sorted.forEach(e -> {
64 result.add(mapper().createObjectNode()
65 .put(e.deviceId().toString(), e.port().toString()));
66 });
67 print("%s", result.toString());
68 } else {
69 sorted.forEach(e -> print(FMT, e.deviceId(), e.port()));
70 }
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070071 }
72
73 private static List<ConnectPoint> sort(Iterable<ConnectPoint> connectPoints) {
74 List<ConnectPoint> edgePoints = newArrayList(connectPoints);
75 Collections.sort(edgePoints, Comparators.CONNECT_POINT_COMPARATOR);
76 return edgePoints;
77 }
78
Aaron Kruglikova2b59152015-06-24 14:01:41 -070079}