blob: 6f87a3d69115a77063e684f157f5a53de4cb2428 [file] [log] [blame]
Daniele Moro94660a02019-12-02 12:02:07 -08001/*
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
17package org.opencord.bng.cli;
18
19import org.apache.karaf.shell.api.action.lifecycle.Service;
20import org.apache.karaf.shell.api.console.CommandLine;
21import org.apache.karaf.shell.api.console.Completer;
22import org.apache.karaf.shell.api.console.Session;
23import org.apache.karaf.shell.support.completers.StringsCompleter;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.net.device.DeviceService;
26
27import java.util.List;
28import java.util.Set;
29
30/**
31 * ONU serial number completer.
32 */
33@Service
34public 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}