blob: 1095f6cfe7304fc0c3a3d828c37eb1730bb0d093 [file] [log] [blame]
Yuta HIGUCHI5f3c0332017-01-09 17:41:07 -08001/*
2 * Copyright 2017-present Open Networking Laboratory
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 */
16package org.onosproject.cli.net.completer;
17
18import static org.onlab.osgi.DefaultServiceDirectory.getService;
19import static org.onosproject.net.ConnectPoint.deviceConnectPoint;
20
21import java.util.Arrays;
22import java.util.Collections;
23import java.util.List;
24import java.util.Objects;
25import java.util.Optional;
26import java.util.stream.Collectors;
27import java.util.stream.Stream;
28
29import org.apache.karaf.shell.console.completer.ArgumentCompleter.ArgumentList;
30import org.onosproject.cli.AbstractChoicesCompleter;
31import org.onosproject.net.ConnectPoint;
32import org.onosproject.net.device.DeviceService;
33import org.onosproject.net.link.LinkService;
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 */
41public class PeerConnectPointCompleter extends AbstractChoicesCompleter {
42
43 @Override
44 protected List<String> choices() {
45 ArgumentList args = getArgumentList();
46
47 DeviceService deviceService = getService(DeviceService.class);
48 LinkService linkService = getService(LinkService.class);
49
50 Optional<ConnectPoint> port = Arrays.asList(args.getArguments()).stream()
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}