blob: 708d4f683c01753d5cfdf030338ac94ecdb32aba [file] [log] [blame]
Daniel Parka73c2362018-09-17 17:43:25 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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.openstacknetworking.cli;
17
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.lifecycle.Service;
19import org.apache.karaf.shell.api.console.CommandLine;
20import org.apache.karaf.shell.api.console.Completer;
21import org.apache.karaf.shell.api.console.Session;
22import org.apache.karaf.shell.support.completers.StringsCompleter;
Daniel Parka73c2362018-09-17 17:43:25 +090023import org.onlab.packet.IpAddress;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.openstacknetworking.api.InstancePort;
26import org.onosproject.openstacknetworking.api.InstancePortService;
27
28import java.util.Iterator;
29import java.util.List;
30import java.util.Set;
31import java.util.SortedSet;
32import java.util.stream.Collectors;
33
34/**
35 * Instance port ip address completer.
36 */
Ray Milkey86ad7bb2018-09-27 12:32:28 -070037@Service
Daniel Parka73c2362018-09-17 17:43:25 +090038public class InstanceIpAddressCompleter implements Completer {
39 private static final String EXTERNAL_IP = "8.8.8.8";
40
41 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070042 public int complete(Session session, CommandLine commandLine, List<String> candidates) {
Daniel Parka73c2362018-09-17 17:43:25 +090043 StringsCompleter delegate = new StringsCompleter();
44 InstancePortService instancePortService =
45 AbstractShellCommand.get(InstancePortService.class);
46
47 Set<IpAddress> set = instancePortService.instancePorts().stream()
48 .map(InstancePort::ipAddress)
49 .collect(Collectors.toSet());
50 set.add(IpAddress.valueOf(EXTERNAL_IP));
51
52 SortedSet<String> strings = delegate.getStrings();
53
54 Iterator<IpAddress> it = set.iterator();
55
56 while (it.hasNext()) {
57 strings.add(it.next().toString());
58 }
59
Ray Milkey86ad7bb2018-09-27 12:32:28 -070060 return delegate.complete(session, commandLine, candidates);
Daniel Parka73c2362018-09-17 17:43:25 +090061
62
63 }
64}