Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-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 | */ |
| 16 | package org.opencord.maclearner.app.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 19 | import org.apache.karaf.shell.api.console.CommandLine; |
| 20 | import org.apache.karaf.shell.api.console.Completer; |
| 21 | import org.apache.karaf.shell.api.console.Session; |
| 22 | import org.apache.karaf.shell.support.completers.StringsCompleter; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.opencord.maclearner.api.MacLearnerService; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | |
| 27 | import java.util.Iterator; |
| 28 | import java.util.List; |
| 29 | import java.util.SortedSet; |
| 30 | |
| 31 | /** |
| 32 | * CLI completer for mapped device ids. |
| 33 | */ |
| 34 | @Service |
| 35 | public class MappedDeviceIdCompleter 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<DeviceId> it = macLearnerService.getMappedDevices().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 | } |