Jian Li | fdbc0fc | 2016-02-12 12:58:48 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | fdbc0fc | 2016-02-12 12:58:48 -0800 | [diff] [blame] | 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 com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Sets; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 21 | import org.apache.karaf.shell.api.console.CommandLine; |
| 22 | import org.apache.karaf.shell.api.console.Session; |
| 23 | import org.apache.karaf.shell.support.completers.StringsCompleter; |
Jian Li | fdbc0fc | 2016-02-12 12:58:48 -0800 | [diff] [blame] | 24 | import org.onosproject.cli.AbstractCompleter; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
Jian Li | 89eeccd | 2016-05-06 02:10:33 -0700 | [diff] [blame] | 26 | import org.onosproject.cluster.NodeId; |
Jian Li | fdbc0fc | 2016-02-12 12:58:48 -0800 | [diff] [blame] | 27 | import org.onosproject.cpman.ControlPlaneMonitorService; |
| 28 | import org.onosproject.cpman.ControlResource; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import java.util.List; |
| 33 | import java.util.Set; |
| 34 | import java.util.SortedSet; |
| 35 | |
| 36 | /** |
| 37 | * Resource name completer. |
| 38 | */ |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 39 | @Service |
Jian Li | fdbc0fc | 2016-02-12 12:58:48 -0800 | [diff] [blame] | 40 | public class ResourceNameCompleter extends AbstractCompleter { |
| 41 | |
| 42 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 43 | |
| 44 | private static final String NETWORK = "network"; |
| 45 | private static final String DISK = "disk"; |
| 46 | private static final String CONTROL_MESSAGE = "control_message"; |
Jian Li | 89eeccd | 2016-05-06 02:10:33 -0700 | [diff] [blame] | 47 | private final Set<String> resourceTypes = ImmutableSet.of(NETWORK, DISK, CONTROL_MESSAGE); |
Jian Li | fdbc0fc | 2016-02-12 12:58:48 -0800 | [diff] [blame] | 48 | private static final String INVALID_MSG = "Invalid type name"; |
| 49 | |
| 50 | |
| 51 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 52 | public int complete(Session session, CommandLine commandLine, List<String> candidates) { |
Jian Li | fdbc0fc | 2016-02-12 12:58:48 -0800 | [diff] [blame] | 53 | // delegate string completer |
| 54 | StringsCompleter delegate = new StringsCompleter(); |
| 55 | |
| 56 | // Resource type is the second argument. |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 57 | String nodeId = commandLine.getArguments()[1]; |
| 58 | String type = commandLine.getArguments()[2]; |
Jian Li | fdbc0fc | 2016-02-12 12:58:48 -0800 | [diff] [blame] | 59 | |
| 60 | if (resourceTypes.contains(type)) { |
| 61 | ControlPlaneMonitorService monitorService = |
| 62 | AbstractShellCommand.get(ControlPlaneMonitorService.class); |
| 63 | |
| 64 | Set<String> set = Sets.newHashSet(); |
| 65 | switch (type) { |
| 66 | case NETWORK: |
Jian Li | 89eeccd | 2016-05-06 02:10:33 -0700 | [diff] [blame] | 67 | set = monitorService.availableResourcesSync(NodeId.nodeId(nodeId), |
| 68 | ControlResource.Type.NETWORK); |
Jian Li | fdbc0fc | 2016-02-12 12:58:48 -0800 | [diff] [blame] | 69 | break; |
| 70 | case DISK: |
Jian Li | 89eeccd | 2016-05-06 02:10:33 -0700 | [diff] [blame] | 71 | set = monitorService.availableResourcesSync(NodeId.nodeId(nodeId), |
| 72 | ControlResource.Type.DISK); |
Jian Li | fdbc0fc | 2016-02-12 12:58:48 -0800 | [diff] [blame] | 73 | break; |
| 74 | case CONTROL_MESSAGE: |
Jian Li | 89eeccd | 2016-05-06 02:10:33 -0700 | [diff] [blame] | 75 | set = monitorService.availableResourcesSync(NodeId.nodeId(nodeId), |
| 76 | ControlResource.Type.CONTROL_MESSAGE); |
Jian Li | fdbc0fc | 2016-02-12 12:58:48 -0800 | [diff] [blame] | 77 | break; |
| 78 | default: |
| 79 | log.warn(INVALID_MSG); |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | SortedSet<String> strings = delegate.getStrings(); |
| 84 | |
Jian Li | b1548ee | 2016-05-11 09:49:57 -0700 | [diff] [blame] | 85 | if (!set.isEmpty()) { |
Jian Li | 89eeccd | 2016-05-06 02:10:33 -0700 | [diff] [blame] | 86 | set.forEach(strings::add); |
Jian Li | fdbc0fc | 2016-02-12 12:58:48 -0800 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 90 | return delegate.complete(session, commandLine, candidates); |
Jian Li | fdbc0fc | 2016-02-12 12:58:48 -0800 | [diff] [blame] | 91 | } |
| 92 | } |