blob: 9eec5b2dd42c0b3838e3303ff85b9ceeaf950e94 [file] [log] [blame]
Daniel Parkc7102f32019-05-17 16:35:04 +09001/*
2 * Copyright 2019-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
18import 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;
23import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
24import org.openstack4j.model.network.Port;
25
26import java.util.Iterator;
27import java.util.List;
28import java.util.Set;
29import java.util.SortedSet;
30import java.util.stream.Collectors;
31
32import static org.onosproject.cli.AbstractShellCommand.get;
33
34/**
35 * VM Device ID Completer.
36 */
37@Service
38public class VmDeviceIdCompleter implements Completer {
39
40 @Override
41 public int complete(Session session, CommandLine commandLine, List<String> candidates) {
42 StringsCompleter delegate = new StringsCompleter();
43 OpenstackNetworkService osNetService = get(OpenstackNetworkService.class);
44
45 Set<String> set = osNetService.ports().stream().map(Port::getDeviceId)
46 .collect(Collectors.toSet());
47
48 SortedSet<String> strings = delegate.getStrings();
49
50 Iterator<String> it = set.iterator();
51
52 while (it.hasNext()) {
53 strings.add(it.next());
54 }
55
56 return delegate.complete(session, commandLine, candidates);
57 }
58}