Daniele Moro | 94660a0 | 2019-12-02 12:02:07 -0800 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | |
| 17 | package org.opencord.bng.cli; |
| 18 | |
| 19 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 20 | import org.apache.karaf.shell.api.console.CommandLine; |
| 21 | import org.apache.karaf.shell.api.console.Completer; |
| 22 | import org.apache.karaf.shell.api.console.Session; |
| 23 | import org.apache.karaf.shell.support.completers.StringsCompleter; |
| 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.net.device.DeviceService; |
| 26 | |
| 27 | import java.util.List; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | /** |
| 31 | * ONU serial number completer. |
| 32 | */ |
| 33 | @Service |
| 34 | public class OnuSerialCompleter implements Completer { |
| 35 | |
| 36 | @Override |
| 37 | public int complete(Session session, CommandLine commandLine, List<String> candidates) { |
| 38 | // Delegate string completer |
| 39 | StringsCompleter delegate = new StringsCompleter(); |
| 40 | |
| 41 | DeviceService deviceService = AbstractShellCommand.get(DeviceService.class); |
| 42 | |
| 43 | Set<String> candidateOnuSerials = delegate.getStrings(); |
| 44 | deviceService.getDevices() |
| 45 | .forEach(device -> deviceService.getPorts( |
| 46 | device.id()).stream() |
| 47 | .map(port -> port.annotations().value("portName")) |
| 48 | .forEach(candidateOnuSerials::add) |
| 49 | ); |
| 50 | // Now let the completer do the work for figuring out what to offer. |
| 51 | return delegate.complete(session, commandLine, candidates); |
| 52 | } |
| 53 | } |