Yuta HIGUCHI | 5f3c033 | 2017-01-09 17:41:07 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yuta HIGUCHI | 5f3c033 | 2017-01-09 17:41:07 -0800 | [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.completer; |
| 17 | |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame^] | 18 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.onosproject.cli.AbstractChoicesCompleter; |
| 20 | import org.onosproject.net.ConnectPoint; |
| 21 | import org.onosproject.net.device.DeviceService; |
| 22 | import org.onosproject.net.link.LinkService; |
Yuta HIGUCHI | 5f3c033 | 2017-01-09 17:41:07 -0800 | [diff] [blame] | 23 | |
| 24 | import java.util.Arrays; |
| 25 | import java.util.Collections; |
| 26 | import java.util.List; |
| 27 | import java.util.Objects; |
| 28 | import java.util.Optional; |
| 29 | import java.util.stream.Collectors; |
| 30 | import java.util.stream.Stream; |
| 31 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 32 | import static org.onlab.osgi.DefaultServiceDirectory.getService; |
| 33 | import static org.onosproject.net.ConnectPoint.deviceConnectPoint; |
Yuta HIGUCHI | 5f3c033 | 2017-01-09 17:41:07 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Completer, which proposes remote end of existing Link in the system. |
| 37 | * <p> |
| 38 | * This completer will look for (device id)/(port number) in the |
| 39 | * existing argument and propose list of remote ports. |
| 40 | */ |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame^] | 41 | @Service |
Yuta HIGUCHI | 5f3c033 | 2017-01-09 17:41:07 -0800 | [diff] [blame] | 42 | public class PeerConnectPointCompleter extends AbstractChoicesCompleter { |
| 43 | |
| 44 | @Override |
| 45 | protected List<String> choices() { |
Yuta HIGUCHI | 5f3c033 | 2017-01-09 17:41:07 -0800 | [diff] [blame] | 46 | |
| 47 | DeviceService deviceService = getService(DeviceService.class); |
| 48 | LinkService linkService = getService(LinkService.class); |
| 49 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 50 | Optional<ConnectPoint> port = Arrays.asList(commandLine.getArguments()).stream() |
Yuta HIGUCHI | 5f3c033 | 2017-01-09 17:41:07 -0800 | [diff] [blame] | 51 | .filter(s -> s.contains(":") && s.contains("/")) |
| 52 | .map(s -> { |
| 53 | try { |
| 54 | return deviceConnectPoint(s); |
| 55 | } catch (IllegalArgumentException e) { |
| 56 | // silently ill-formed String |
| 57 | return null; |
| 58 | } |
| 59 | }) |
| 60 | .filter(Objects::nonNull) |
| 61 | .filter(cp -> deviceService.getPort(cp) != null) |
| 62 | .findFirst(); |
| 63 | |
| 64 | if (!port.isPresent()) { |
| 65 | // no candidate |
| 66 | return Collections.emptyList(); |
| 67 | } |
| 68 | final ConnectPoint cp = port.get(); |
| 69 | |
| 70 | return linkService.getLinks(cp).stream() |
| 71 | .flatMap(l -> Stream.of(l.src(), l.dst())) |
| 72 | .filter(peer -> !cp.equals(peer)) |
| 73 | .distinct() |
| 74 | .map(ConnectPoint::toString) |
| 75 | .collect(Collectors.toList()); |
| 76 | } |
| 77 | |
| 78 | } |