Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -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; |
| 17 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Lists; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 20 | import com.google.common.collect.SetMultimap; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onlab.packet.VlanId; |
| 29 | import org.onosproject.app.ApplicationService; |
| 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.core.CoreService; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 32 | import org.onosproject.incubator.net.intf.Interface; |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 33 | import org.onosproject.incubator.net.intf.InterfaceEvent; |
| 34 | import org.onosproject.incubator.net.intf.InterfaceListener; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 35 | import org.onosproject.incubator.net.intf.InterfaceService; |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 36 | import org.onosproject.net.EncapsulationType; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 37 | import org.onosproject.net.FilteredConnectPoint; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 38 | import org.onosproject.net.Host; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 39 | import org.onosproject.net.config.NetworkConfigEvent; |
| 40 | import org.onosproject.net.config.NetworkConfigListener; |
| 41 | import org.onosproject.net.config.NetworkConfigService; |
| 42 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 43 | import org.onosproject.net.flow.TrafficSelector; |
| 44 | import org.onosproject.net.flow.criteria.Criterion; |
| 45 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 46 | import org.onosproject.net.host.HostEvent; |
| 47 | import org.onosproject.net.host.HostListener; |
| 48 | import org.onosproject.net.host.HostService; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 49 | import org.onosproject.net.intent.Intent; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 50 | import org.onosproject.net.intent.IntentService; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 51 | import org.onosproject.net.intent.Key; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 52 | import org.onosproject.routing.IntentSynchronizationService; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 53 | import org.onosproject.vpls.config.VplsConfigurationService; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 54 | import org.slf4j.Logger; |
| 55 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 56 | import java.util.Collection; |
| 57 | import java.util.HashSet; |
| 58 | import java.util.List; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 59 | import java.util.Set; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 60 | import java.util.stream.Collectors; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 61 | |
| 62 | import static org.slf4j.LoggerFactory.getLogger; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 63 | import static org.onosproject.vpls.IntentInstaller.PREFIX_BROADCAST; |
| 64 | import static org.onosproject.vpls.IntentInstaller.PREFIX_UNICAST; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 65 | |
| 66 | /** |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 67 | * Application to create L2 broadcast overlay networks using VLANs. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 68 | */ |
| 69 | @Component(immediate = true) |
| 70 | public class Vpls { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 71 | static final String VPLS_APP = "org.onosproject.vpls"; |
| 72 | |
| 73 | private static final String HOST_FCP_NOT_FOUND = |
| 74 | "Filtered connected point for host {} not found"; |
| 75 | private static final String HOST_EVENT = "Received HostEvent {}"; |
| 76 | private static final String INTF_CONF_EVENT = |
| 77 | "Received InterfaceConfigEvent {}"; |
| 78 | private static final String NET_CONF_EVENT = |
| 79 | "Received NetworkConfigEvent {}"; |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 80 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 81 | private final Logger log = getLogger(getClass()); |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected ApplicationService applicationService; |
| 85 | |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 87 | protected CoreService coreService; |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected HostService hostService; |
| 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 93 | protected IntentService intentService; |
| 94 | |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | protected InterfaceService interfaceService; |
| 97 | |
| 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 99 | protected IntentSynchronizationService intentSynchronizer; |
| 100 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 102 | protected NetworkConfigService configService; |
| 103 | |
| 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 105 | protected VplsConfigurationService vplsConfigService; |
| 106 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 107 | private final HostListener hostListener = new InternalHostListener(); |
| 108 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 109 | private final InternalInterfaceListener interfaceListener = |
| 110 | new InternalInterfaceListener(); |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 111 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 112 | private final InternalNetworkConfigListener configListener = |
| 113 | new InternalNetworkConfigListener(); |
| 114 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 115 | private IntentInstaller intentInstaller; |
| 116 | |
| 117 | private ApplicationId appId; |
| 118 | |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 119 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 120 | @Activate |
| 121 | public void activate() { |
| 122 | appId = coreService.registerApplication(VPLS_APP); |
| 123 | |
| 124 | intentInstaller = new IntentInstaller(appId, |
| 125 | intentService, |
| 126 | intentSynchronizer); |
| 127 | |
Luca Prete | 2705d66 | 2016-04-29 15:30:23 -0700 | [diff] [blame] | 128 | applicationService.registerDeactivateHook(appId, () -> { |
| 129 | intentSynchronizer.removeIntentsByAppId(appId); |
| 130 | }); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 131 | |
| 132 | hostService.addListener(hostListener); |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 133 | interfaceService.addListener(interfaceListener); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 134 | configService.addListener(configListener); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 135 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 136 | setupConnectivity(false); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 137 | |
Luca Prete | c21c6e6 | 2016-09-22 14:52:21 -0700 | [diff] [blame] | 138 | log.info("Activated"); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Deactivate |
| 142 | public void deactivate() { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 143 | configService.removeListener(configListener); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 144 | intentSynchronizer.removeIntentsByAppId(appId); |
Luca Prete | c21c6e6 | 2016-09-22 14:52:21 -0700 | [diff] [blame] | 145 | log.info("Deactivated"); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 146 | } |
| 147 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 148 | /** |
| 149 | * Sets up connectivity for all VPLSs. |
| 150 | * |
| 151 | * @param isNetworkConfigEvent true if this function is triggered |
| 152 | * by NetworkConfigEvent; false otherwise |
| 153 | */ |
| 154 | private void setupConnectivity(boolean isNetworkConfigEvent) { |
| 155 | SetMultimap<String, Interface> networkInterfaces = |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 156 | vplsConfigService.ifacesByVplsName(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 157 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 158 | Set<String> vplsAffectedByApi = |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 159 | new HashSet<>(vplsConfigService.vplsAffectedByApi()); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 160 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 161 | if (isNetworkConfigEvent && vplsAffectedByApi.isEmpty()) { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 162 | vplsAffectedByApi.addAll(vplsConfigService.vplsNamesOld()); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 163 | } |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 164 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 165 | networkInterfaces.asMap().forEach((vplsName, interfaces) -> { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 166 | Set<Host> hosts = Sets.newHashSet(); |
| 167 | interfaces.forEach(intf -> { |
| 168 | // Add hosts that belongs to the specific VPLS |
| 169 | hostService.getConnectedHosts(intf.connectPoint()) |
| 170 | .stream() |
| 171 | .filter(host -> host.vlan().equals(intf.vlan())) |
| 172 | .forEach(hosts::add); |
| 173 | }); |
| 174 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 175 | EncapsulationType encap = |
| 176 | vplsConfigService.encap(vplsName); |
| 177 | |
| 178 | setupConnectivity(vplsName, interfaces, hosts, encap, |
| 179 | vplsAffectedByApi.contains(vplsName)); |
| 180 | vplsAffectedByApi.remove(vplsName); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 181 | }); |
| 182 | |
| 183 | if (!vplsAffectedByApi.isEmpty()) { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 184 | for (String vplsName : vplsAffectedByApi) { |
| 185 | withdrawIntents(vplsName, Lists.newArrayList()); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 191 | * Sets up connectivity for specific VPLS. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 192 | * |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 193 | * @param vplsName the VPLS name |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 194 | * @param interfaces the interfaces that belong to the VPLS |
| 195 | * @param hosts the hosts that belong to the VPLS |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 196 | * @param encap the encapsulation type |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 197 | * @param affectedByApi true if this function is triggered from the APIs; |
| 198 | * false otherwise |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 199 | */ |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 200 | private void setupConnectivity(String vplsName, |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 201 | Collection<Interface> interfaces, |
| 202 | Set<Host> hosts, |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 203 | EncapsulationType encap, |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 204 | boolean affectedByApi) { |
| 205 | List<Intent> intents = Lists.newArrayList(); |
| 206 | List<Key> keys = Lists.newArrayList(); |
| 207 | Set<FilteredConnectPoint> fcPoints = buildFCPoints(interfaces); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 208 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 209 | intents.addAll(buildBroadcastIntents( |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 210 | vplsName, fcPoints, encap, affectedByApi)); |
| 211 | intents.addAll(buildUnicastIntents( |
| 212 | vplsName, hosts, fcPoints, encap, affectedByApi)); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 213 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 214 | if (affectedByApi) { |
| 215 | intents.forEach(intent -> keys.add(intent.key())); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 216 | withdrawIntents(vplsName, keys); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | intentInstaller.submitIntents(intents); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 223 | * Withdraws intents belonging to a VPLS, given a VPLS name. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 224 | * |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 225 | * @param vplsName the VPLS name |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 226 | * @param keys the keys of the intents to be installed |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 227 | */ |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 228 | private void withdrawIntents(String vplsName, List<Key> keys) { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 229 | List<Intent> intents = Lists.newArrayList(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 230 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 231 | intentInstaller.getIntentsFromVpls(vplsName) |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 232 | .forEach(intent -> { |
| 233 | if (!keys.contains(intent.key())) { |
| 234 | intents.add(intent); |
| 235 | } |
| 236 | }); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 237 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 238 | intentInstaller.withdrawIntents(intents); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 239 | } |
| 240 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 241 | /** |
| 242 | * Sets up broadcast intents between any given filtered connect point. |
| 243 | * |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 244 | * @param vplsName the VPLS name |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 245 | * @param fcPoints the set of filtered connect points |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 246 | * @param encap the encapsulation type |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 247 | * @param affectedByApi true if the function triggered from APIs; |
| 248 | * false otherwise |
| 249 | * @return the set of broadcast intents |
| 250 | */ |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 251 | private Set<Intent> buildBroadcastIntents(String vplsName, |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 252 | Set<FilteredConnectPoint> fcPoints, |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 253 | EncapsulationType encap, |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 254 | boolean affectedByApi) { |
| 255 | Set<Intent> intents = Sets.newHashSet(); |
| 256 | fcPoints.forEach(point -> { |
| 257 | Set<FilteredConnectPoint> otherPoints = |
| 258 | fcPoints.stream() |
| 259 | .filter(fcp -> !fcp.equals(point)) |
| 260 | .collect(Collectors.toSet()); |
| 261 | |
| 262 | Key brcKey = intentInstaller.buildKey(PREFIX_BROADCAST, |
| 263 | point.connectPoint(), |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 264 | vplsName, |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 265 | MacAddress.BROADCAST); |
| 266 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 267 | if ((!intentInstaller.intentExists(brcKey) || affectedByApi) |
| 268 | && !otherPoints.isEmpty()) { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 269 | intents.add(intentInstaller.buildBrcIntent(brcKey, |
| 270 | point, |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 271 | otherPoints, |
| 272 | encap)); |
Yi Tseng | bf8b67e | 2016-09-01 15:02:11 +0800 | [diff] [blame] | 273 | } |
| 274 | }); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 275 | |
| 276 | return ImmutableSet.copyOf(intents); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Sets up unicast intents between any given filtered connect point. |
| 281 | * |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 282 | * @param vplsName the VPLS name |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 283 | * @param hosts the set of destination hosts |
| 284 | * @param fcPoints the set of filtered connect points |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 285 | * @param encap the encapsulation type |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 286 | * @param affectedByApi true if the function triggered from APIs; |
| 287 | * false otherwise |
| 288 | * @return the set of unicast intents |
| 289 | */ |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 290 | private Set<Intent> buildUnicastIntents(String vplsName, |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 291 | Set<Host> hosts, |
| 292 | Set<FilteredConnectPoint> fcPoints, |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 293 | EncapsulationType encap, |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 294 | boolean affectedByApi) { |
| 295 | Set<Intent> intents = Sets.newHashSet(); |
| 296 | hosts.forEach(host -> { |
| 297 | FilteredConnectPoint hostPoint = getHostPoint(host, fcPoints); |
| 298 | |
| 299 | if (hostPoint == null) { |
| 300 | log.warn(HOST_FCP_NOT_FOUND, host); |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | Set<FilteredConnectPoint> otherPoints = |
| 305 | fcPoints.stream() |
| 306 | .filter(fcp -> !fcp.equals(hostPoint)) |
| 307 | .collect(Collectors.toSet()); |
| 308 | |
| 309 | Key uniKey = intentInstaller.buildKey(PREFIX_UNICAST, |
| 310 | host.location(), |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 311 | vplsName, |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 312 | host.mac()); |
| 313 | |
| 314 | if ((!intentInstaller.intentExists(uniKey) || affectedByApi) && |
| 315 | !otherPoints.isEmpty()) { |
| 316 | intents.add(intentInstaller.buildUniIntent(uniKey, |
| 317 | otherPoints, |
| 318 | hostPoint, |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 319 | host, |
| 320 | encap)); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 321 | } |
| 322 | }); |
| 323 | |
| 324 | return ImmutableSet.copyOf(intents); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Finds the filtered connect point a host is attached to. |
| 329 | * |
| 330 | * @param host the target host |
| 331 | * @param fcps the filtered connected points |
| 332 | * @return null if not found; the filtered connect point otherwise |
| 333 | */ |
| 334 | private FilteredConnectPoint getHostPoint(Host host, |
| 335 | Set<FilteredConnectPoint> fcps) { |
| 336 | return fcps.stream() |
| 337 | .filter(fcp -> fcp.connectPoint().equals(host.location())) |
| 338 | .filter(fcp -> { |
| 339 | VlanIdCriterion vlanCriterion = |
| 340 | (VlanIdCriterion) fcp.trafficSelector(). |
| 341 | getCriterion(Criterion.Type.VLAN_VID); |
| 342 | |
| 343 | return vlanCriterion != null && |
| 344 | vlanCriterion.vlanId().equals(host.vlan()); |
| 345 | }) |
| 346 | .findFirst() |
| 347 | .orElse(null); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Computes a set of filtered connect points from a list of given interfaces. |
| 352 | * |
| 353 | * @param interfaces the interfaces to compute |
| 354 | * @return the set of filtered connect points |
| 355 | */ |
| 356 | private Set<FilteredConnectPoint> buildFCPoints(Collection<Interface> interfaces) { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame^] | 357 | // Build all filtered connected points in the VPLS |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 358 | return interfaces |
| 359 | .stream() |
| 360 | .map(intf -> { |
| 361 | TrafficSelector.Builder selectorBuilder = |
| 362 | DefaultTrafficSelector.builder(); |
| 363 | |
| 364 | if (!intf.vlan().equals(VlanId.NONE)) { |
| 365 | selectorBuilder.matchVlanId(intf.vlan()); |
| 366 | } |
| 367 | |
| 368 | return new FilteredConnectPoint(intf.connectPoint(), |
| 369 | selectorBuilder.build()); |
| 370 | }) |
| 371 | .collect(Collectors.toSet()); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Listener for host events. |
| 376 | */ |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 377 | private class InternalHostListener implements HostListener { |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 378 | @Override |
| 379 | public void event(HostEvent event) { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 380 | log.debug(HOST_EVENT, event); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 381 | switch (event.type()) { |
| 382 | case HOST_ADDED: |
| 383 | case HOST_UPDATED: |
| 384 | case HOST_REMOVED: |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 385 | setupConnectivity(false); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 386 | break; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 387 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 388 | default: |
| 389 | break; |
| 390 | } |
| 391 | } |
| 392 | } |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 393 | |
| 394 | /** |
| 395 | * Listener for interface configuration events. |
| 396 | */ |
| 397 | private class InternalInterfaceListener implements InterfaceListener { |
| 398 | @Override |
| 399 | public void event(InterfaceEvent event) { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 400 | log.debug(INTF_CONF_EVENT, event); |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 401 | switch (event.type()) { |
| 402 | case INTERFACE_ADDED: |
| 403 | case INTERFACE_UPDATED: |
| 404 | case INTERFACE_REMOVED: |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 405 | setupConnectivity(false); |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 406 | break; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 407 | |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 408 | default: |
| 409 | break; |
| 410 | } |
| 411 | } |
| 412 | } |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 413 | |
| 414 | /** |
| 415 | * Listener for VPLS configuration events. |
| 416 | */ |
| 417 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 418 | @Override |
| 419 | public void event(NetworkConfigEvent event) { |
| 420 | if (event.configClass() == VplsConfigurationService.CONFIG_CLASS) { |
| 421 | log.debug(NET_CONF_EVENT, event.configClass()); |
| 422 | switch (event.type()) { |
| 423 | case CONFIG_ADDED: |
| 424 | case CONFIG_UPDATED: |
| 425 | case CONFIG_REMOVED: |
| 426 | setupConnectivity(true); |
| 427 | break; |
| 428 | |
| 429 | default: |
| 430 | break; |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | } |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 435 | } |