blob: fbf8100049cdea9de736f3801aa155a84743d791 [file] [log] [blame]
Daniel Park95f73312018-07-31 15:48:34 +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 Park95f73312018-07-31 15:48:34 +090023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
25import org.openstack4j.model.network.Port;
26
27import java.util.Iterator;
28import java.util.List;
29import java.util.Set;
30import java.util.SortedSet;
31import java.util.stream.Collectors;
32
33import static org.onosproject.openstacknetworking.api.Constants.DIRECT;
34
35/**
36 * Direct port completer.
37 */
Ray Milkey86ad7bb2018-09-27 12:32:28 -070038@Service
Daniel Park95f73312018-07-31 15:48:34 +090039public class DirectPortListCompleter implements Completer {
40
41 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070042 public int complete(Session session, CommandLine commandLine, List<String> candidates) {
Daniel Park95f73312018-07-31 15:48:34 +090043 StringsCompleter delegate = new StringsCompleter();
44 OpenstackNetworkService osNetService = AbstractShellCommand.get(OpenstackNetworkService.class);
45 Set<String> set = osNetService.ports().stream()
46 .filter(port -> port.getvNicType().equals(DIRECT))
47 .map(Port::getId)
48 .collect(Collectors.toSet());
49
50 SortedSet<String> strings = delegate.getStrings();
51 Iterator<String> it = set.iterator();
52
53 while (it.hasNext()) {
Daniel Parka3ffbdb2018-11-28 13:51:39 +090054 strings.add(it.next());
Daniel Park95f73312018-07-31 15:48:34 +090055 }
Ray Milkey86ad7bb2018-09-27 12:32:28 -070056 return delegate.complete(session, commandLine, candidates);
Daniel Park95f73312018-07-31 15:48:34 +090057 }
58}