Boyuan Yan | 53c2c68 | 2019-07-17 11:22:31 -0700 | [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 | package org.onosproject.cli.net; |
| 17 | |
| 18 | import org.apache.karaf.shell.api.console.CommandLine; |
| 19 | import org.apache.karaf.shell.api.console.Completer; |
| 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 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.onosproject.net.Device; |
| 25 | import org.onosproject.net.Port; |
| 26 | import org.onosproject.net.device.DeviceService; |
| 27 | |
| 28 | import java.util.List; |
| 29 | import java.util.SortedSet; |
| 30 | |
| 31 | /** |
| 32 | * Optical ConnectPoint completer. |
| 33 | */ |
| 34 | @Service |
| 35 | public 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 | } |