blob: 222f6430a2090af5a43aec9008034043f4ee6066 [file] [log] [blame]
Yuta HIGUCHI5f3c0332017-01-09 17:41:07 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yuta HIGUCHI5f3c0332017-01-09 17:41:07 -08003 *
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.completer;
17
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.onosproject.cli.AbstractChoicesCompleter;
19import org.onosproject.net.ConnectPoint;
20import org.onosproject.net.device.DeviceService;
21import org.onosproject.net.link.LinkService;
Yuta HIGUCHI5f3c0332017-01-09 17:41:07 -080022
23import java.util.Arrays;
24import java.util.Collections;
25import java.util.List;
26import java.util.Objects;
27import java.util.Optional;
28import java.util.stream.Collectors;
29import java.util.stream.Stream;
30
Ray Milkeyd84f89b2018-08-17 14:54:17 -070031import static org.onlab.osgi.DefaultServiceDirectory.getService;
32import static org.onosproject.net.ConnectPoint.deviceConnectPoint;
Yuta HIGUCHI5f3c0332017-01-09 17:41:07 -080033
34/**
35 * Completer, which proposes remote end of existing Link in the system.
36 * <p>
37 * This completer will look for (device id)/(port number) in the
38 * existing argument and propose list of remote ports.
39 */
40public class PeerConnectPointCompleter extends AbstractChoicesCompleter {
41
42 @Override
43 protected List<String> choices() {
Yuta HIGUCHI5f3c0332017-01-09 17:41:07 -080044
45 DeviceService deviceService = getService(DeviceService.class);
46 LinkService linkService = getService(LinkService.class);
47
Ray Milkeyd84f89b2018-08-17 14:54:17 -070048 Optional<ConnectPoint> port = Arrays.asList(commandLine.getArguments()).stream()
Yuta HIGUCHI5f3c0332017-01-09 17:41:07 -080049 .filter(s -> s.contains(":") && s.contains("/"))
50 .map(s -> {
51 try {
52 return deviceConnectPoint(s);
53 } catch (IllegalArgumentException e) {
54 // silently ill-formed String
55 return null;
56 }
57 })
58 .filter(Objects::nonNull)
59 .filter(cp -> deviceService.getPort(cp) != null)
60 .findFirst();
61
62 if (!port.isPresent()) {
63 // no candidate
64 return Collections.emptyList();
65 }
66 final ConnectPoint cp = port.get();
67
68 return linkService.getLinks(cp).stream()
69 .flatMap(l -> Stream.of(l.src(), l.dst()))
70 .filter(peer -> !cp.equals(peer))
71 .distinct()
72 .map(ConnectPoint::toString)
73 .collect(Collectors.toList());
74 }
75
76}