blob: ce55d01bf4ff2067a0f6861ce280a1c997847ebb [file] [log] [blame]
Jian Li26ef1302018-07-04 14:37:06 +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.openstackvtap.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;
Jian Li26ef1302018-07-04 14:37:06 +090023import org.onlab.packet.IpAddress;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.net.Host;
26import org.onosproject.net.host.HostService;
27
28import java.util.Iterator;
29import java.util.List;
30import java.util.SortedSet;
31
32/**
33 * VM IP completer.
34 */
Ray Milkey86ad7bb2018-09-27 12:32:28 -070035@Service
Jian Li26ef1302018-07-04 14:37:06 +090036public class VmIpCompleter implements Completer {
37
38 private static final String CIDR = "/32";
39
40 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070041 public int complete(Session session, CommandLine commandLine, List<String> candidates) {
Jian Li26ef1302018-07-04 14:37:06 +090042 // Delegate string completer
43 StringsCompleter delegate = new StringsCompleter();
44
45 HostService service = AbstractShellCommand.get(HostService.class);
46 Iterator<Host> it = service.getHosts().iterator();
47 SortedSet<String> strings = delegate.getStrings();
48 while (it.hasNext()) {
49 for (IpAddress ip : it.next().ipAddresses()) {
50 strings.add(ip.toString() + CIDR);
51 }
52 }
53
Ray Milkey86ad7bb2018-09-27 12:32:28 -070054 return delegate.complete(session, commandLine, candidates);
Jian Li26ef1302018-07-04 14:37:06 +090055 }
56}