Yuta HIGUCHI | 89111d9 | 2017-05-04 11:29:17 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2017-present Open Networking Foundation |
Yuta HIGUCHI | 89111d9 | 2017-05-04 11:29:17 -0700 | [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.netconf.cli.impl.completers; |
| 17 | |
| 18 | import java.lang.reflect.Modifier; |
| 19 | import java.util.Arrays; |
| 20 | import java.util.List; |
| 21 | import java.util.Objects; |
| 22 | import java.util.stream.Collectors; |
| 23 | |
| 24 | import org.onosproject.cli.AbstractChoicesCompleter; |
| 25 | import org.onosproject.netconf.DatastoreId; |
| 26 | |
| 27 | /** |
| 28 | * Completer for predefined {@link DatastoreId}. |
| 29 | * |
| 30 | */ |
| 31 | public class DatastoreIdCompleter extends AbstractChoicesCompleter { |
| 32 | |
| 33 | @Override |
| 34 | protected List<String> choices() { |
| 35 | // TODO: Ideally candidates should be chosen based on Device capability |
| 36 | return Arrays.asList(DatastoreId.class.getFields()) |
| 37 | .stream() |
| 38 | .filter(f -> f.getType() == DatastoreId.class) |
| 39 | .filter(f -> Modifier.isStatic(f.getModifiers())) |
| 40 | .map(f -> { |
| 41 | try { |
| 42 | return (DatastoreId) f.get(null); |
| 43 | } catch (IllegalArgumentException | IllegalAccessException e) { |
| 44 | return null; |
| 45 | } |
| 46 | }) |
| 47 | .filter(Objects::nonNull) |
| 48 | .map(DatastoreId::id) |
| 49 | .collect(Collectors.toList()); |
| 50 | } |
| 51 | |
| 52 | } |