Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -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.vpls.cli.completer; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 19 | import org.apache.karaf.shell.console.completer.ArgumentCompleter; |
| 20 | import org.onosproject.cli.AbstractChoicesCompleter; |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame^] | 21 | import org.onosproject.net.intf.Interface; |
| 22 | import org.onosproject.net.intf.InterfaceService; |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 23 | import org.onosproject.net.EncapsulationType; |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 24 | import org.onosproject.vpls.api.Vpls; |
| 25 | import org.onosproject.vpls.api.VplsData; |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 26 | import org.onosproject.vpls.cli.VplsCommandEnum; |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 27 | |
| 28 | import java.util.Arrays; |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 29 | import java.util.Collection; |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 30 | import java.util.Collections; |
| 31 | import java.util.List; |
| 32 | import java.util.Set; |
| 33 | import java.util.stream.Collectors; |
| 34 | |
| 35 | import static org.onosproject.cli.AbstractShellCommand.get; |
| 36 | |
| 37 | /** |
| 38 | * VPLS optional argument completer. |
| 39 | */ |
| 40 | public class VplsOptArgCompleter extends AbstractChoicesCompleter { |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 41 | protected static Vpls vpls; |
| 42 | protected static InterfaceService interfaceService; |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 43 | |
| 44 | @Override |
| 45 | public List<String> choices() { |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 46 | if (vpls == null) { |
| 47 | vpls = get(Vpls.class); |
| 48 | } |
| 49 | ArgumentCompleter.ArgumentList argumentList = getArgumentList(); |
| 50 | if (argumentList == null) { |
| 51 | return Collections.emptyList(); |
| 52 | } |
| 53 | List<String> argList = Lists.newArrayList(argumentList.getArguments()); |
| 54 | String argOne = argList.get(1); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 55 | VplsCommandEnum vplsCommandEnum = VplsCommandEnum.enumFromString(argOne); |
| 56 | if (vplsCommandEnum != null) { |
| 57 | switch (vplsCommandEnum) { |
| 58 | case ADD_IFACE: |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 59 | return availableIfaces(); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 60 | case SET_ENCAP: |
| 61 | return encap(); |
| 62 | case REMOVE_IFACE: |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 63 | return vplsIfaces(); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 64 | default: |
| 65 | return Collections.emptyList(); |
| 66 | } |
| 67 | } |
| 68 | return Collections.emptyList(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns the list of interfaces not yet assigned to any VPLS. |
| 73 | * |
| 74 | * @return the list of interfaces not yet assigned to any VPLS |
| 75 | */ |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 76 | private List<String> availableIfaces() { |
| 77 | if (interfaceService == null) { |
| 78 | interfaceService = get(InterfaceService.class); |
| 79 | } |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 80 | |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 81 | Set<Interface> allIfaces = interfaceService.getInterfaces(); |
| 82 | Set<Interface> usedIfaces = vpls.getAllVpls().stream() |
| 83 | .map(VplsData::interfaces) |
| 84 | .flatMap(Collection::stream) |
| 85 | .collect(Collectors.toSet()); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 86 | |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 87 | return allIfaces.stream() |
| 88 | .filter(iface -> !usedIfaces.contains(iface)) |
| 89 | .map(Interface::name) |
| 90 | .collect(Collectors.toList()); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns the list of supported encapsulation types. |
| 95 | * |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 96 | * @return the list of supported encapsulation types |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 97 | */ |
| 98 | private List<String> encap() { |
| 99 | return Arrays.stream(EncapsulationType.values()) |
| 100 | .map(Enum::toString) |
| 101 | .collect(Collectors.toList()); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns the list of interfaces associated to a VPLS. |
| 106 | * |
| 107 | * @return the list of interfaces associated to a VPLS |
| 108 | */ |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 109 | private List<String> vplsIfaces() { |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 110 | ArgumentCompleter.ArgumentList list = getArgumentList(); |
| 111 | String vplsName = list.getArguments()[2]; |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 112 | VplsData vplsData = vpls.getVpls(vplsName); |
| 113 | return vplsData.interfaces().stream() |
| 114 | .map(Interface::name) |
| 115 | .collect(Collectors.toList()); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 116 | } |
| 117 | } |