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 |
Luca Prete | 99f6f28 | 2016-12-05 15:33:36 -0800 | [diff] [blame] | 14 | * limitations under the License. |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 15 | */ |
| 16 | package org.onosproject.vpls.cli; |
| 17 | |
Yi Tseng | 3069c61 | 2017-05-26 17:09:43 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 32ce0ed | 2018-10-10 15:39:24 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 24 | import org.onosproject.net.intf.Interface; |
| 25 | import org.onosproject.net.intf.InterfaceService; |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 26 | import org.onosproject.net.EncapsulationType; |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 27 | import org.onosproject.vpls.api.VplsData; |
| 28 | import org.onosproject.vpls.api.Vpls; |
Yi Tseng | 3069c61 | 2017-05-26 17:09:43 -0700 | [diff] [blame] | 29 | import org.onosproject.vpls.api.VplsData.VplsState; |
Ray Milkey | 32ce0ed | 2018-10-10 15:39:24 -0700 | [diff] [blame] | 30 | import org.onosproject.vpls.cli.completer.VplsCommandCompleter; |
| 31 | import org.onosproject.vpls.cli.completer.VplsNameCompleter; |
| 32 | import org.onosproject.vpls.cli.completer.VplsOptArgCompleter; |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 33 | |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 34 | import java.util.Collection; |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 35 | import java.util.Collections; |
| 36 | import java.util.List; |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 37 | import java.util.Set; |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 38 | import java.util.stream.Collectors; |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 39 | |
| 40 | import static com.google.common.base.Strings.isNullOrEmpty; |
Yi Tseng | 3069c61 | 2017-05-26 17:09:43 -0700 | [diff] [blame] | 41 | import static org.onosproject.vpls.api.VplsData.VplsState.*; |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 42 | |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 43 | |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 44 | /** |
| 45 | * CLI to interact with the VPLS application. |
| 46 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 47 | @Service |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 48 | @Command(scope = "onos", name = "vpls", |
| 49 | description = "Manages the VPLS application") |
| 50 | public class VplsCommand extends AbstractShellCommand { |
Yi Tseng | 3069c61 | 2017-05-26 17:09:43 -0700 | [diff] [blame] | 51 | private static final Set<VplsState> CHANGING_STATE = |
| 52 | ImmutableSet.of(ADDING, REMOVING, UPDATING); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 53 | |
| 54 | // Color codes and style |
| 55 | private static final String BOLD = "\u001B[1m"; |
| 56 | private static final String COLOR_ERROR = "\u001B[31m"; |
| 57 | private static final String RESET = "\u001B[0m"; |
| 58 | |
| 59 | // Messages and string formatter |
| 60 | private static final String ENCAP_NOT_FOUND = |
| 61 | COLOR_ERROR + "Encapsulation type " + BOLD + "%s" + RESET + |
| 62 | COLOR_ERROR + " not found" + RESET; |
| 63 | |
| 64 | private static final String IFACE_NOT_FOUND = |
| 65 | COLOR_ERROR + "Interface " + BOLD + "%s" + RESET + COLOR_ERROR + |
| 66 | " not found" + RESET; |
| 67 | |
| 68 | private static final String IFACE_ALREADY_ASSOCIATED = |
| 69 | COLOR_ERROR + "Interface " + BOLD + "%s" + RESET + COLOR_ERROR + |
| 70 | " already associated to VPLS " + BOLD + "%s" + RESET + |
| 71 | COLOR_ERROR + "" + RESET; |
| 72 | |
Luca Prete | 8dbbea8 | 2016-12-07 18:10:10 -0800 | [diff] [blame] | 73 | private static final String INSERT_VPLS_NAME = |
| 74 | COLOR_ERROR + "Missing the " + BOLD + "VPLS name." + RESET + |
| 75 | COLOR_ERROR + " Specifying a VPLS name is mandatory." + |
| 76 | RESET; |
| 77 | |
| 78 | private static final String INSERT_ENCAP_TYPE = |
| 79 | COLOR_ERROR + "Missing the " + BOLD + "encapsulation type." + |
| 80 | RESET + COLOR_ERROR + " Encapsulation type is mandatory." + |
| 81 | RESET; |
| 82 | |
| 83 | private static final String INSERT_INTERFACE = |
| 84 | COLOR_ERROR + "Missing the " + BOLD + "interface name." + |
| 85 | RESET + COLOR_ERROR + " Specifying an interface name is" + |
| 86 | " mandatory." + RESET; |
| 87 | |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 88 | private static final String SEPARATOR = "----------------"; |
| 89 | |
| 90 | private static final String VPLS_ALREADY_EXISTS = |
| 91 | COLOR_ERROR + "VPLS " + BOLD + "%s" + RESET + COLOR_ERROR + |
| 92 | " already exists" + RESET; |
| 93 | |
| 94 | private static final String VPLS_COMMAND_NOT_FOUND = |
| 95 | COLOR_ERROR + "VPLS command " + BOLD + "%s" + RESET + COLOR_ERROR + |
| 96 | " not found" + RESET; |
| 97 | |
| 98 | private static final String VPLS_DISPLAY = "VPLS name: " + BOLD + |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 99 | "%s" + RESET + "\nAssociated interfaces: %s\nEncapsulation: %s\n" + |
| 100 | "State: %s"; |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 101 | |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 102 | private static final String VPLS_NOT_FOUND = |
| 103 | COLOR_ERROR + "VPLS " + BOLD + "%s" + RESET + COLOR_ERROR + |
| 104 | " not found" + RESET; |
| 105 | |
| 106 | private static final String IFACE_NOT_ASSOCIATED = |
| 107 | COLOR_ERROR + "Interface " + BOLD + "%s" + RESET + COLOR_ERROR + |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 108 | " cannot be removed from VPLS " + BOLD + "%s" + RESET + "."; |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 109 | |
Ray Milkey | 06297ed | 2018-01-22 17:13:41 -0800 | [diff] [blame] | 110 | protected Vpls vpls; |
| 111 | protected InterfaceService interfaceService; |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 112 | |
Luca Prete | 8dbbea8 | 2016-12-07 18:10:10 -0800 | [diff] [blame] | 113 | @Argument(index = 0, name = "command", description = "Command name (add-if|" + |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 114 | "create|delete|list|rem-if|set-encap|show)", |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 115 | required = true, multiValued = false) |
Ray Milkey | 32ce0ed | 2018-10-10 15:39:24 -0700 | [diff] [blame] | 116 | @Completion(VplsCommandCompleter.class) |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 117 | String command = null; |
| 118 | |
| 119 | @Argument(index = 1, name = "vplsName", description = "The name of the VPLS", |
| 120 | required = false, multiValued = false) |
Ray Milkey | 32ce0ed | 2018-10-10 15:39:24 -0700 | [diff] [blame] | 121 | @Completion(VplsNameCompleter.class) |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 122 | String vplsName = null; |
| 123 | |
Luca Prete | 8dbbea8 | 2016-12-07 18:10:10 -0800 | [diff] [blame] | 124 | @Argument(index = 2, name = "optArg", description = "The interface name or" + |
| 125 | " the encapsulation type for set-encap", |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 126 | required = false, multiValued = false) |
Ray Milkey | 32ce0ed | 2018-10-10 15:39:24 -0700 | [diff] [blame] | 127 | @Completion(VplsOptArgCompleter.class) |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 128 | String optArg = null; |
| 129 | |
| 130 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 131 | protected void doExecute() { |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 132 | if (vpls == null) { |
| 133 | vpls = get(Vpls.class); |
| 134 | } |
| 135 | if (interfaceService == null) { |
| 136 | interfaceService = get(InterfaceService.class); |
| 137 | } |
| 138 | |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 139 | VplsCommandEnum enumCommand = VplsCommandEnum.enumFromString(command); |
| 140 | if (enumCommand != null) { |
| 141 | switch (enumCommand) { |
| 142 | case ADD_IFACE: |
| 143 | addIface(vplsName, optArg); |
| 144 | break; |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 145 | case CREATE: |
| 146 | create(vplsName); |
| 147 | break; |
| 148 | case DELETE: |
| 149 | delete(vplsName); |
| 150 | break; |
| 151 | case LIST: |
| 152 | list(); |
| 153 | break; |
| 154 | case REMOVE_IFACE: |
| 155 | removeIface(vplsName, optArg); |
| 156 | break; |
| 157 | case SET_ENCAP: |
| 158 | setEncap(vplsName, optArg); |
| 159 | break; |
| 160 | case SHOW: |
| 161 | show(vplsName); |
| 162 | break; |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 163 | case CLEAN: |
| 164 | cleanVpls(); |
| 165 | break; |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 166 | default: |
| 167 | print(VPLS_COMMAND_NOT_FOUND, command); |
| 168 | } |
Luca Prete | 8dbbea8 | 2016-12-07 18:10:10 -0800 | [diff] [blame] | 169 | } else { |
| 170 | print(VPLS_COMMAND_NOT_FOUND, command); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Adds an inteterface to a VPLS. |
| 176 | * |
| 177 | * @param vplsName the name of the VLPS |
| 178 | * @param ifaceName the name of the interface to add |
| 179 | */ |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 180 | protected void addIface(String vplsName, String ifaceName) { |
Luca Prete | 8dbbea8 | 2016-12-07 18:10:10 -0800 | [diff] [blame] | 181 | if (vplsName == null) { |
| 182 | print(INSERT_VPLS_NAME); |
| 183 | return; |
| 184 | } |
| 185 | if (ifaceName == null) { |
| 186 | print(INSERT_INTERFACE); |
| 187 | return; |
| 188 | } |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 189 | |
| 190 | Interface iface = getInterface(ifaceName); |
| 191 | VplsData vplsData = vpls.getVpls(vplsName); |
| 192 | |
| 193 | if (vplsData == null) { |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 194 | print(VPLS_NOT_FOUND, vplsName); |
| 195 | return; |
| 196 | } |
Yi Tseng | 3069c61 | 2017-05-26 17:09:43 -0700 | [diff] [blame] | 197 | if (CHANGING_STATE.contains(vplsData.state())) { |
| 198 | // when a VPLS is updating, we shouldn't try modify it. |
| 199 | print("VPLS %s still updating, please wait it finished", vplsData.name()); |
| 200 | return; |
| 201 | } |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 202 | if (iface == null) { |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 203 | print(IFACE_NOT_FOUND, ifaceName); |
| 204 | return; |
| 205 | } |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 206 | if (isIfaceAssociated(iface)) { |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 207 | print(IFACE_ALREADY_ASSOCIATED, |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 208 | ifaceName, getVplsByInterface(iface).name()); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 209 | return; |
| 210 | } |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 211 | vpls.addInterface(vplsData, iface); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Creates a new VPLS. |
| 216 | * |
| 217 | * @param vplsName the name of the VLPS |
| 218 | */ |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 219 | protected void create(String vplsName) { |
Luca Prete | 8dbbea8 | 2016-12-07 18:10:10 -0800 | [diff] [blame] | 220 | if (vplsName == null || vplsName.isEmpty()) { |
| 221 | print(INSERT_VPLS_NAME); |
| 222 | return; |
| 223 | } |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 224 | VplsData vplsData = vpls.getVpls(vplsName); |
| 225 | if (vplsData != null) { |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 226 | print(VPLS_ALREADY_EXISTS, vplsName); |
| 227 | return; |
| 228 | } |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 229 | vpls.createVpls(vplsName, EncapsulationType.NONE); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Deletes a VPLS. |
| 234 | * |
| 235 | * @param vplsName the name of the VLPS |
| 236 | */ |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 237 | protected void delete(String vplsName) { |
Luca Prete | 8dbbea8 | 2016-12-07 18:10:10 -0800 | [diff] [blame] | 238 | if (vplsName == null) { |
| 239 | print(INSERT_VPLS_NAME); |
| 240 | return; |
| 241 | } |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 242 | VplsData vplsData = vpls.getVpls(vplsName); |
| 243 | if (vplsData == null) { |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 244 | print(VPLS_NOT_FOUND, vplsName); |
| 245 | return; |
| 246 | } |
Yi Tseng | 3069c61 | 2017-05-26 17:09:43 -0700 | [diff] [blame] | 247 | if (CHANGING_STATE.contains(vplsData.state())) { |
| 248 | // when a VPLS is updating, we shouldn't try modify it. |
| 249 | print("VPLS %s still updating, please wait it finished", vplsData.name()); |
| 250 | return; |
| 251 | } |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 252 | vpls.removeVpls(vplsData); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Lists the configured VPLSs. |
| 257 | */ |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 258 | protected void list() { |
| 259 | List<String> vplsNames = vpls.getAllVpls().stream() |
| 260 | .map(VplsData::name) |
| 261 | .collect(Collectors.toList()); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 262 | Collections.sort(vplsNames); |
| 263 | |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 264 | vplsNames.forEach(vpls -> { |
| 265 | print(vpls); |
| 266 | }); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Removes an interface from a VPLS. |
| 271 | * |
| 272 | * @param vplsName the name of the VLPS |
| 273 | * @param ifaceName the name of the interface to remove |
| 274 | */ |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 275 | protected void removeIface(String vplsName, String ifaceName) { |
Luca Prete | 8dbbea8 | 2016-12-07 18:10:10 -0800 | [diff] [blame] | 276 | if (vplsName == null) { |
| 277 | print(INSERT_VPLS_NAME); |
| 278 | return; |
| 279 | } |
| 280 | if (ifaceName == null) { |
| 281 | print(INSERT_INTERFACE); |
| 282 | return; |
| 283 | } |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 284 | VplsData vplsData = vpls.getVpls(vplsName); |
| 285 | Interface iface = getInterface(ifaceName); |
| 286 | if (vplsData == null) { |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 287 | print(VPLS_NOT_FOUND, vplsName); |
| 288 | return; |
| 289 | } |
Yi Tseng | 3069c61 | 2017-05-26 17:09:43 -0700 | [diff] [blame] | 290 | if (CHANGING_STATE.contains(vplsData.state())) { |
| 291 | // when a VPLS is updating, we shouldn't try modify it. |
| 292 | print("VPLS %s still updating, please wait it finished", vplsData.name()); |
| 293 | return; |
| 294 | } |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 295 | if (iface == null) { |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 296 | print(IFACE_NOT_FOUND, ifaceName); |
| 297 | return; |
| 298 | } |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 299 | if (!vplsData.interfaces().contains(iface)) { |
| 300 | print(IFACE_NOT_ASSOCIATED, ifaceName, vplsName); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 301 | return; |
| 302 | } |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 303 | vpls.removeInterface(vplsData, iface); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Sets the encapsulation type for a VPLS. |
| 308 | * |
| 309 | * @param vplsName the name of the VPLS |
| 310 | * @param encap the encapsulation type |
| 311 | */ |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 312 | protected void setEncap(String vplsName, String encap) { |
Luca Prete | 8dbbea8 | 2016-12-07 18:10:10 -0800 | [diff] [blame] | 313 | if (vplsName == null) { |
| 314 | print(INSERT_VPLS_NAME); |
| 315 | return; |
| 316 | } |
| 317 | if (encap == null) { |
| 318 | print(INSERT_ENCAP_TYPE); |
| 319 | return; |
| 320 | } |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 321 | VplsData vplsData = vpls.getVpls(vplsName); |
| 322 | if (vplsData == null) { |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 323 | print(VPLS_NOT_FOUND, vplsName); |
| 324 | return; |
| 325 | } |
| 326 | EncapsulationType encapType = EncapsulationType.enumFromString(encap); |
| 327 | if (encapType.equals(EncapsulationType.NONE) && |
| 328 | !encapType.toString().equals(encap)) { |
| 329 | print(ENCAP_NOT_FOUND, encap); |
| 330 | return; |
| 331 | } |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 332 | vpls.setEncapsulationType(vplsData, encapType); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Shows the details of one or more VPLSs. |
| 337 | * |
| 338 | * @param vplsName the name of the VPLS |
| 339 | */ |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 340 | protected void show(String vplsName) { |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 341 | if (!isNullOrEmpty(vplsName)) { |
| 342 | // A VPLS name is provided. Check first if the VPLS exists |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 343 | VplsData vplsData = vpls.getVpls(vplsName); |
| 344 | if (vplsData != null) { |
| 345 | Set<String> ifaceNames = vplsData.interfaces().stream() |
| 346 | .map(Interface::name) |
| 347 | .collect(Collectors.toSet()); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 348 | print(VPLS_DISPLAY, |
| 349 | vplsName, |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 350 | ifaceNames, |
| 351 | vplsData.encapsulationType().toString(), |
| 352 | vplsData.state()); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 353 | } else { |
| 354 | print(VPLS_NOT_FOUND, vplsName); |
| 355 | } |
| 356 | } else { |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 357 | Collection<VplsData> vplses = vpls.getAllVpls(); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 358 | // No VPLS names are provided. Display all VPLSs configured |
Luca Prete | 8dbbea8 | 2016-12-07 18:10:10 -0800 | [diff] [blame] | 359 | print(SEPARATOR); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 360 | vplses.forEach(vplsData -> { |
| 361 | Set<String> ifaceNames = vplsData.interfaces().stream() |
| 362 | .map(Interface::name) |
| 363 | .collect(Collectors.toSet()); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 364 | print(VPLS_DISPLAY, |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 365 | vplsData.name(), |
| 366 | ifaceNames, |
| 367 | vplsData.encapsulationType().toString(), |
| 368 | vplsData.state()); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 369 | print(SEPARATOR); |
| 370 | }); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /** |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 375 | * Remove all VPLS. |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 376 | */ |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 377 | protected void cleanVpls() { |
| 378 | vpls.removeAllVpls(); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 381 | |
| 382 | /** |
| 383 | * States if an interface is already associated to a VPLS. |
| 384 | * |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 385 | * @param iface the interface |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 386 | * @return true if the interface is already associated to a VPLS; false |
| 387 | * otherwise |
| 388 | */ |
Ray Milkey | 06297ed | 2018-01-22 17:13:41 -0800 | [diff] [blame] | 389 | private boolean isIfaceAssociated(Interface iface) { |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 390 | return vpls.getAllVpls() |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 391 | .stream() |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 392 | .map(VplsData::interfaces) |
| 393 | .flatMap(Collection::stream) |
| 394 | .anyMatch(iface::equals); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /** |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 398 | * Gets a network interface by given interface name. |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 399 | * |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 400 | * @param interfaceName the interface name |
| 401 | * @return the network interface |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 402 | */ |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 403 | private Interface getInterface(String interfaceName) { |
| 404 | // FIXME: only returns first interface it found |
| 405 | // multiple interface with same name not support |
| 406 | return interfaceService.getInterfaces().stream() |
| 407 | .filter(iface -> iface.name().equals(interfaceName)) |
| 408 | .findFirst() |
| 409 | .orElse(null); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /** |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 413 | * Gets a VPLS related to the network interface. |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 414 | * |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 415 | * @param iface the network interface |
| 416 | * @return the VPLS related to the network interface |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 417 | */ |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 418 | private VplsData getVplsByInterface(Interface iface) { |
| 419 | return vpls.getAllVpls().stream() |
| 420 | .filter(vplsData -> vplsData.interfaces().contains(iface)) |
| 421 | .findFirst() |
| 422 | .orElse(null); |
Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 423 | } |
| 424 | } |