blob: 6f7ae26cfb04bde0716fa62aed5f9e62790052a9 [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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
Ray Milkey0068fd02018-10-11 15:45:39 -070020import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070022import org.onosproject.cli.AbstractShellCommand;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070023import org.onosproject.net.ConnectPoint;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070024import org.onosproject.net.edge.EdgePortService;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070025import org.onosproject.utils.Comparators;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070026
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070027import java.util.Collections;
28import java.util.List;
29
30import static com.google.common.collect.Lists.newArrayList;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070031import static org.onosproject.net.DeviceId.deviceId;
32
33/**
34 * Lists all edge ports.
35 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070036@Service
Aaron Kruglikova2b59152015-06-24 14:01:41 -070037@Command(scope = "onos", name = "edge-ports",
38 description = "Lists all edge ports.")
39public class EdgePortsListCommand extends AbstractShellCommand {
40
41 private static final String FMT = "%s/%s";
42
43 @Argument(index = 0, name = "uri", description = "Device ID",
44 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070045 @Completion(DeviceIdCompleter.class)
Aaron Kruglikova2b59152015-06-24 14:01:41 -070046 String uri = null;
47
Aaron Kruglikova2b59152015-06-24 14:01:41 -070048 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070049 protected void doExecute() {
Aaron Kruglikova2b59152015-06-24 14:01:41 -070050 EdgePortService service = get(EdgePortService.class);
51 if (uri == null) {
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070052 printEdgePoints(service.getEdgePoints());
Aaron Kruglikova2b59152015-06-24 14:01:41 -070053 } else {
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070054 printEdgePoints(service.getEdgePoints(deviceId(uri)));
Aaron Kruglikova2b59152015-06-24 14:01:41 -070055 }
56 }
57
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070058 private void printEdgePoints(Iterable<ConnectPoint> edgePoints) {
59 sort(edgePoints).forEach(e -> print(FMT, e.deviceId(), e.port()));
60 }
61
62 private static List<ConnectPoint> sort(Iterable<ConnectPoint> connectPoints) {
63 List<ConnectPoint> edgePoints = newArrayList(connectPoints);
64 Collections.sort(edgePoints, Comparators.CONNECT_POINT_COMPARATOR);
65 return edgePoints;
66 }
67
Aaron Kruglikova2b59152015-06-24 14:01:41 -070068}