blob: a161c2433a2e3d74643a09ff704945d3272ba7f8 [file] [log] [blame]
Tunahan Sezen03e55272020-04-18 09:18:53 +00001/*
Joey Armstrong53fcac22023-01-11 13:25:01 -05002 * Copyright 2017-2023 Open Networking Foundation (ONF) and the ONF Contributors
Tunahan Sezen03e55272020-04-18 09:18:53 +00003 *
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.opencord.maclearner.app.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.opencord.maclearner.api.MacLearnerService;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.net.PortNumber;
26
27import java.util.Iterator;
28import java.util.List;
29import java.util.SortedSet;
30
31/**
32 * CLI completer for mapped port numbers.
33 */
34@Service
35public class MappedPortNumberCompleter implements Completer {
36
37 @Override
38 public int complete(Session session, CommandLine commandLine, List<String> candidates) {
39 // Delegate string completer
40 StringsCompleter delegate = new StringsCompleter();
41 MacLearnerService macLearnerService = AbstractShellCommand.get(MacLearnerService.class);
42 Iterator<PortNumber> it = macLearnerService.getMappedPorts().iterator();
43 SortedSet<String> strings = delegate.getStrings();
44
45 while (it.hasNext()) {
46 strings.add(it.next().toString());
47 }
48
49 // Now let the completer do the work for figuring out what to offer.
50 return delegate.complete(session, commandLine, candidates);
51 }
52
53}