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