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 | |
Seyeon Jeong | 357bcec | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 25 | import org.onosproject.net.edge.EdgePortService; |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 26 | import org.onosproject.utils.Comparators; |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 27 | |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 28 | import java.util.Collections; |
| 29 | import java.util.List; |
| 30 | |
| 31 | import static com.google.common.collect.Lists.newArrayList; |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 32 | import static org.onosproject.net.DeviceId.deviceId; |
| 33 | |
| 34 | /** |
| 35 | * Lists all edge ports. |
| 36 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 37 | @Service |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 38 | @Command(scope = "onos", name = "edge-ports", |
| 39 | description = "Lists all edge ports.") |
| 40 | public 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 Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 46 | @Completion(DeviceIdCompleter.class) |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 47 | String uri = null; |
| 48 | |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 49 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 50 | protected void doExecute() { |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 51 | EdgePortService service = get(EdgePortService.class); |
| 52 | if (uri == null) { |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 53 | printEdgePoints(service.getEdgePoints()); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 54 | } else { |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 55 | printEdgePoints(service.getEdgePoints(deviceId(uri))); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 59 | private void printEdgePoints(Iterable<ConnectPoint> edgePoints) { |
Seyeon Jeong | 357bcec | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 60 | 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 Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 71 | } |
| 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 Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 79 | } |