blob: fe0aae20e9f351484a7c7795a5f50e41b6b71e9b [file] [log] [blame]
Jian Lifdbc0fc2016-02-12 12:58:48 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jian Lifdbc0fc2016-02-12 12:58:48 -08003 *
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.cpman.cli;
17
18import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Sets;
20import org.apache.karaf.shell.console.completer.ArgumentCompleter;
21import org.apache.karaf.shell.console.completer.StringsCompleter;
22import org.onosproject.cli.AbstractCompleter;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.cpman.ControlPlaneMonitorService;
25import org.onosproject.cpman.ControlResource;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import java.util.List;
30import java.util.Set;
31import java.util.SortedSet;
32
33/**
34 * Resource name completer.
35 */
36public class ResourceNameCompleter extends AbstractCompleter {
37
38 private final Logger log = LoggerFactory.getLogger(getClass());
39
40 private static final String NETWORK = "network";
41 private static final String DISK = "disk";
42 private static final String CONTROL_MESSAGE = "control_message";
43 Set<String> resourceTypes = ImmutableSet.of(NETWORK, DISK, CONTROL_MESSAGE);
44 private static final String INVALID_MSG = "Invalid type name";
45
46
47 @Override
48 public int complete(String buffer, int cursor, List<String> candidates) {
49 // delegate string completer
50 StringsCompleter delegate = new StringsCompleter();
51
52 // Resource type is the second argument.
53 ArgumentCompleter.ArgumentList list = getArgumentList();
54 String type = list.getArguments()[1];
55
56 if (resourceTypes.contains(type)) {
57 ControlPlaneMonitorService monitorService =
58 AbstractShellCommand.get(ControlPlaneMonitorService.class);
59
60 Set<String> set = Sets.newHashSet();
61 switch (type) {
62 case NETWORK:
63 set = monitorService.availableResources(ControlResource.Type.NETWORK);
64 break;
65 case DISK:
66 set = monitorService.availableResources(ControlResource.Type.DISK);
67 break;
68 case CONTROL_MESSAGE:
69 set = monitorService.availableResources(ControlResource.Type.CONTROL_MESSAGE);
70 break;
71 default:
72 log.warn(INVALID_MSG);
73 break;
74 }
75
76 SortedSet<String> strings = delegate.getStrings();
77
78 if (set.size() != 0) {
79 set.forEach(s -> strings.add(s));
80 }
81 }
82
83 return delegate.complete(buffer, cursor, candidates);
84 }
85}