Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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.cpman.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.console.completer.ArgumentCompleter; |
| 19 | import org.apache.karaf.shell.console.completer.StringsCompleter; |
| 20 | import org.onosproject.cli.AbstractCompleter; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.cpman.ControlPlaneMonitorService; |
| 23 | |
| 24 | import java.util.List; |
| 25 | import java.util.Set; |
| 26 | import java.util.SortedSet; |
| 27 | |
| 28 | import static org.onosproject.cpman.ControlResource.Type; |
| 29 | |
| 30 | /** |
| 31 | * Device identification completer for control plane manager. |
| 32 | */ |
| 33 | public class ControlMessageDeviceIdCompleter extends AbstractCompleter { |
| 34 | @Override |
| 35 | public int complete(String buffer, int cursor, List<String> candidates) { |
| 36 | // delegate string completer |
| 37 | StringsCompleter delegate = new StringsCompleter(); |
| 38 | |
| 39 | // Resource type is the second argument. |
| 40 | ArgumentCompleter.ArgumentList list = getArgumentList(); |
| 41 | String type = list.getArguments()[1]; |
| 42 | |
| 43 | if (Type.CONTROL_MESSAGE.toString().toLowerCase().equals(type)) { |
| 44 | ControlPlaneMonitorService monitorService = |
| 45 | AbstractShellCommand.get(ControlPlaneMonitorService.class); |
| 46 | |
| 47 | Set<String> set = monitorService.availableResources(Type.CONTROL_MESSAGE); |
| 48 | |
| 49 | SortedSet<String> strings = delegate.getStrings(); |
| 50 | if (set != null) { |
| 51 | set.forEach(s -> strings.add(s)); |
| 52 | } |
| 53 | } |
| 54 | return delegate.complete(buffer, cursor, candidates); |
| 55 | } |
| 56 | } |