Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.cli.net; |
| 17 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 23 | import org.onosproject.net.ConnectPoint; |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 24 | import org.onosproject.net.edge.EdgePortService; |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 25 | import org.onosproject.utils.Comparators; |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 26 | |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 27 | import java.util.Collections; |
| 28 | import java.util.List; |
| 29 | |
| 30 | import static com.google.common.collect.Lists.newArrayList; |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 31 | import static org.onosproject.net.DeviceId.deviceId; |
| 32 | |
| 33 | /** |
| 34 | * Lists all edge ports. |
| 35 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 36 | @Service |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 37 | @Command(scope = "onos", name = "edge-ports", |
| 38 | description = "Lists all edge ports.") |
| 39 | public 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 Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 45 | @Completion(DeviceIdCompleter.class) |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 46 | String uri = null; |
| 47 | |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 48 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 49 | protected void doExecute() { |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 50 | EdgePortService service = get(EdgePortService.class); |
| 51 | if (uri == null) { |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 52 | printEdgePoints(service.getEdgePoints()); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 53 | } else { |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 54 | printEdgePoints(service.getEdgePoints(deviceId(uri))); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 58 | 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 Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 68 | } |