Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 1 | package org.onosproject.vpls.cli; |
| 2 | |
| 3 | import com.google.common.collect.SetMultimap; |
| 4 | import com.google.common.collect.Sets; |
| 5 | import org.onosproject.incubator.net.intf.Interface; |
| 6 | import org.onosproject.incubator.net.intf.InterfaceService; |
| 7 | import org.onosproject.vpls.config.VplsConfigurationService; |
| 8 | |
| 9 | import java.util.Map; |
| 10 | import java.util.Optional; |
| 11 | import java.util.Set; |
| 12 | |
| 13 | import static org.onlab.osgi.DefaultServiceDirectory.getService; |
| 14 | |
| 15 | /** |
| 16 | * Util class for VPLS Commands. |
| 17 | */ |
| 18 | public final class VplsCommandUtils { |
| 19 | |
| 20 | protected static final String VPLS_NAME = "VPLS name %s: "; |
| 21 | protected static final String VPLS_DISPLAY = "VPLS name: %s, associated " + |
| 22 | "interfaces: %s, encapsulation: %s"; |
| 23 | protected static final String VPLS_NOT_FOUND = "VPLS %s not found."; |
| 24 | protected static final String IFACE_NOT_FOUND = "Interface %s not found."; |
| 25 | protected static final String VPLS_ALREADY_EXISTS = "VPLS %s already exists."; |
| 26 | protected static final String IFACE_ALREADY_ASSOCIATED = |
| 27 | "Interface %s already associated to VPLS %s."; |
| 28 | protected static final String IFACE_NOT_ASSOCIATED = |
| 29 | "Interface %s is associated to VPLS %s."; |
| 30 | |
| 31 | private static VplsConfigurationService vplsConfigService = |
| 32 | getService(VplsConfigurationService.class); |
| 33 | private static InterfaceService interfaceService = |
| 34 | getService(InterfaceService.class); |
| 35 | |
| 36 | private VplsCommandUtils() {} |
| 37 | |
| 38 | /** |
| 39 | * States if a VPLS exists or not. |
| 40 | * |
| 41 | * @param vplsName the name of the VPLS |
| 42 | * @return true if the VPLS exists; false otherwise |
| 43 | */ |
| 44 | protected static boolean vplsExists(String vplsName) { |
| 45 | return vplsConfigService.vplsNames().contains(vplsName); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * States if an interface is defined or not in the system. |
| 50 | * |
| 51 | * @param ifaceName the name of the interface |
| 52 | * @return true if the interface is defined; false otherwise |
| 53 | */ |
| 54 | protected static boolean ifaceExists(String ifaceName) { |
| 55 | return interfaceService.getInterfaces() |
| 56 | .stream() |
| 57 | .anyMatch(iface -> iface.name().equals(ifaceName)); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * States if an interface is already associated or not to a VPLS. |
| 62 | * |
| 63 | * @param ifaceName the name of the interface |
| 64 | * @return true if the interface is already associated to a VPLS; false |
| 65 | * otherwise |
| 66 | */ |
| 67 | protected static boolean ifaceAlreadyAssociated(String ifaceName) { |
| 68 | return vplsConfigService.allIfaces() |
| 69 | .stream() |
| 70 | .anyMatch(iface -> iface.name().equals(ifaceName)); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns the name of a VPLS, given the name of an interface associated to |
| 75 | * it. |
| 76 | * |
| 77 | * @param ifaceName the name of the interface |
| 78 | * @return the name of the VPLS that has the interface configured; null if |
| 79 | * the interface does not exist or is not associated to any VPLS |
| 80 | */ |
| 81 | protected static String vplsNameFromIfaceName(String ifaceName) { |
| 82 | String vplsName = null; |
| 83 | |
| 84 | Optional<String> optVplsName = vplsConfigService.ifacesByVplsName() |
| 85 | .entries() |
| 86 | .stream() |
| 87 | .filter(iface -> iface.getValue().name().equals(ifaceName)) |
| 88 | .map(Map.Entry::getKey) |
| 89 | .findFirst(); |
| 90 | |
| 91 | if (optVplsName.isPresent()) { |
| 92 | vplsName = optVplsName.get(); |
| 93 | } |
| 94 | |
| 95 | return vplsName; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Returns a list of interfaces associated to a VPLS, given a VPLS name. |
| 100 | * |
| 101 | * @param vplsName the name of the VPLS |
| 102 | * @return the set of interfaces associated to the given VPLS; null if the |
| 103 | * VPLS is not found |
| 104 | */ |
| 105 | protected static Set<String> ifacesFromVplsName(String vplsName) { |
| 106 | if (!vplsExists(vplsName)) { |
| 107 | return null; |
| 108 | } |
| 109 | |
| 110 | SetMultimap<String, Interface> ifacesByVplsName = |
| 111 | vplsConfigService.ifacesByVplsName(); |
| 112 | Set<String> ifaceNames = Sets.newHashSet(); |
| 113 | |
| 114 | ifacesByVplsName.get(vplsName).forEach(iface -> ifaceNames.add(iface.name())); |
| 115 | |
| 116 | return ifaceNames; |
| 117 | } |
| 118 | } |