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