blob: 2b036378346d86f07e3c40214bced65e92db6038 [file] [log] [blame]
Aaron Kruglikova2b59152015-06-24 14:01:41 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onosproject.cli.AbstractShellCommand;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070021import org.onosproject.net.ConnectPoint;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070022import org.onosproject.net.edge.EdgePortService;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070023import org.onosproject.utils.Comparators;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070024
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070025import java.util.Collections;
26import java.util.List;
27
28import static com.google.common.collect.Lists.newArrayList;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070029import static org.onosproject.net.DeviceId.deviceId;
30
31/**
32 * Lists all edge ports.
33 */
34@Command(scope = "onos", name = "edge-ports",
35 description = "Lists all edge ports.")
36public class EdgePortsListCommand extends AbstractShellCommand {
37
38 private static final String FMT = "%s/%s";
39
40 @Argument(index = 0, name = "uri", description = "Device ID",
41 required = false, multiValued = false)
42 String uri = null;
43
Aaron Kruglikova2b59152015-06-24 14:01:41 -070044 @Override
45 protected void execute() {
46 EdgePortService service = get(EdgePortService.class);
47 if (uri == null) {
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070048 printEdgePoints(service.getEdgePoints());
Aaron Kruglikova2b59152015-06-24 14:01:41 -070049 } else {
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070050 printEdgePoints(service.getEdgePoints(deviceId(uri)));
Aaron Kruglikova2b59152015-06-24 14:01:41 -070051 }
52 }
53
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070054 private void printEdgePoints(Iterable<ConnectPoint> edgePoints) {
55 sort(edgePoints).forEach(e -> print(FMT, e.deviceId(), e.port()));
56 }
57
58 private static List<ConnectPoint> sort(Iterable<ConnectPoint> connectPoints) {
59 List<ConnectPoint> edgePoints = newArrayList(connectPoints);
60 Collections.sort(edgePoints, Comparators.CONNECT_POINT_COMPARATOR);
61 return edgePoints;
62 }
63
Aaron Kruglikova2b59152015-06-24 14:01:41 -070064}