blob: 869d2c96fdc743e3db25208f48ebaa050e70db1c [file] [log] [blame]
Boyuan Yan53c2c682019-07-17 11:22:31 -07001/*
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 */
16package org.onosproject.cli.net;
17
18import org.apache.karaf.shell.api.console.CommandLine;
19import org.apache.karaf.shell.api.console.Completer;
20import org.apache.karaf.shell.api.action.lifecycle.Service;
21import org.apache.karaf.shell.api.console.Session;
22import org.apache.karaf.shell.support.completers.StringsCompleter;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.net.Device;
25import org.onosproject.net.Port;
26import org.onosproject.net.device.DeviceService;
27
28import java.util.List;
29import java.util.SortedSet;
30
31/**
32 * Optical ConnectPoint completer.
33 */
34@Service
35public class OpticalConnectPointCompleter implements Completer {
36 @Override
37 public int complete(Session session, CommandLine commandLine, List<String> candidates) {
38 // Delegate string completer
39 StringsCompleter delegate = new StringsCompleter();
40
41 // Fetch our service and feed it's offerings to the string completer
42 DeviceService service = AbstractShellCommand.get(DeviceService.class);
43
44 // Generate the device ID/port number identifiers
45 for (Device device : service.getDevices()) {
46 SortedSet<String> strings = delegate.getStrings();
47 for (Port port : service.getPorts(device.id())) {
48 if (!port.number().isLogical() && (port.type().equals(Port.Type.OCH) ||
49 port.type().equals(Port.Type.OMS) || port.type().equals(Port.Type.OTU))) {
50 strings.add(device.id().toString() + "/" + port.number());
51 }
52 }
53 }
54
55 // Now let the completer do the work for figuring out what to offer.
56 return delegate.complete(session, commandLine, candidates);
57 }
58}