Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 1 | /* |
Luca Prete | 99f6f28 | 2016-12-05 15:33:36 -0800 | [diff] [blame] | 2 | * Copyright 2015-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 |
Luca Prete | 99f6f28 | 2016-12-05 15:33:36 -0800 | [diff] [blame] | 14 | * limitations under the License. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 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; |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 53 | import org.onosproject.vpls.config.VplsConfigService; |
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 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 62 | import static org.onosproject.vpls.IntentInstaller.PREFIX_BROADCAST; |
| 63 | import static org.onosproject.vpls.IntentInstaller.PREFIX_UNICAST; |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 64 | import static org.slf4j.LoggerFactory.getLogger; |
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) |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 105 | protected VplsConfigService vplsConfigService; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 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, |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 179 | vplsAffectedByApi.contains(vplsName)); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 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 | * |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 193 | * @param vplsName the VPLS name |
| 194 | * @param interfaces the interfaces that belong to the VPLS |
| 195 | * @param hosts the hosts that belong to the VPLS |
| 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) { |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 205 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 206 | List<Intent> intents = Lists.newArrayList(); |
| 207 | List<Key> keys = Lists.newArrayList(); |
| 208 | Set<FilteredConnectPoint> fcPoints = buildFCPoints(interfaces); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 209 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 210 | intents.addAll(buildBroadcastIntents( |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 211 | vplsName, fcPoints, encap, affectedByApi)); |
| 212 | intents.addAll(buildUnicastIntents( |
| 213 | vplsName, hosts, fcPoints, encap, affectedByApi)); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 214 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 215 | if (affectedByApi) { |
| 216 | intents.forEach(intent -> keys.add(intent.key())); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 217 | withdrawIntents(vplsName, keys); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | intentInstaller.submitIntents(intents); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 224 | * Withdraws intents belonging to a VPLS, given a VPLS name. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 225 | * |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 226 | * @param vplsName the VPLS name |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 227 | * @param keys the keys of the intents to be installed |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 228 | */ |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 229 | private void withdrawIntents(String vplsName, List<Key> keys) { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 230 | List<Intent> intents = Lists.newArrayList(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 231 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 232 | intentInstaller.getIntentsFromVpls(vplsName) |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 233 | .forEach(intent -> { |
| 234 | if (!keys.contains(intent.key())) { |
| 235 | intents.add(intent); |
| 236 | } |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 237 | }); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 238 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 239 | intentInstaller.withdrawIntents(intents); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 240 | } |
| 241 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 242 | /** |
| 243 | * Sets up broadcast intents between any given filtered connect point. |
| 244 | * |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 245 | * @param vplsName the VPLS name |
| 246 | * @param fcPoints the set of filtered connect points |
| 247 | * @param encap the encapsulation type |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 248 | * @param affectedByApi true if the function triggered from APIs; |
| 249 | * false otherwise |
| 250 | * @return the set of broadcast intents |
| 251 | */ |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 252 | private Set<Intent> buildBroadcastIntents(String vplsName, |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 253 | Set<FilteredConnectPoint> fcPoints, |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 254 | EncapsulationType encap, |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 255 | boolean affectedByApi) { |
| 256 | Set<Intent> intents = Sets.newHashSet(); |
| 257 | fcPoints.forEach(point -> { |
| 258 | Set<FilteredConnectPoint> otherPoints = |
| 259 | fcPoints.stream() |
| 260 | .filter(fcp -> !fcp.equals(point)) |
| 261 | .collect(Collectors.toSet()); |
| 262 | |
| 263 | Key brcKey = intentInstaller.buildKey(PREFIX_BROADCAST, |
| 264 | point.connectPoint(), |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 265 | vplsName, |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 266 | MacAddress.BROADCAST); |
| 267 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 268 | if ((!intentInstaller.intentExists(brcKey) || affectedByApi) |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 269 | && !otherPoints.isEmpty()) { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 270 | intents.add(intentInstaller.buildBrcIntent(brcKey, |
| 271 | point, |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 272 | otherPoints, |
| 273 | encap)); |
Yi Tseng | bf8b67e | 2016-09-01 15:02:11 +0800 | [diff] [blame] | 274 | } |
| 275 | }); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 276 | |
| 277 | return ImmutableSet.copyOf(intents); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Sets up unicast intents between any given filtered connect point. |
| 282 | * |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 283 | * @param vplsName the VPLS name |
| 284 | * @param hosts the set of destination hosts |
| 285 | * @param fcPoints the set of filtered connect points |
| 286 | * @param encap the encapsulation type |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 287 | * @param affectedByApi true if the function triggered from APIs; |
| 288 | * false otherwise |
| 289 | * @return the set of unicast intents |
| 290 | */ |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 291 | private Set<Intent> buildUnicastIntents(String vplsName, |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 292 | Set<Host> hosts, |
| 293 | Set<FilteredConnectPoint> fcPoints, |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 294 | EncapsulationType encap, |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 295 | boolean affectedByApi) { |
| 296 | Set<Intent> intents = Sets.newHashSet(); |
| 297 | hosts.forEach(host -> { |
| 298 | FilteredConnectPoint hostPoint = getHostPoint(host, fcPoints); |
| 299 | |
| 300 | if (hostPoint == null) { |
| 301 | log.warn(HOST_FCP_NOT_FOUND, host); |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | Set<FilteredConnectPoint> otherPoints = |
| 306 | fcPoints.stream() |
| 307 | .filter(fcp -> !fcp.equals(hostPoint)) |
| 308 | .collect(Collectors.toSet()); |
| 309 | |
| 310 | Key uniKey = intentInstaller.buildKey(PREFIX_UNICAST, |
| 311 | host.location(), |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 312 | vplsName, |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 313 | host.mac()); |
| 314 | |
| 315 | if ((!intentInstaller.intentExists(uniKey) || affectedByApi) && |
| 316 | !otherPoints.isEmpty()) { |
| 317 | intents.add(intentInstaller.buildUniIntent(uniKey, |
| 318 | otherPoints, |
| 319 | hostPoint, |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 320 | host, |
| 321 | encap)); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 322 | } |
| 323 | }); |
| 324 | |
| 325 | return ImmutableSet.copyOf(intents); |
| 326 | } |
| 327 | |
| 328 | /** |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 329 | * Returns the filtered connect point associated to a given host. |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 330 | * |
| 331 | * @param host the target host |
| 332 | * @param fcps the filtered connected points |
| 333 | * @return null if not found; the filtered connect point otherwise |
| 334 | */ |
| 335 | private FilteredConnectPoint getHostPoint(Host host, |
| 336 | Set<FilteredConnectPoint> fcps) { |
| 337 | return fcps.stream() |
| 338 | .filter(fcp -> fcp.connectPoint().equals(host.location())) |
| 339 | .filter(fcp -> { |
| 340 | VlanIdCriterion vlanCriterion = |
| 341 | (VlanIdCriterion) fcp.trafficSelector(). |
| 342 | getCriterion(Criterion.Type.VLAN_VID); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 343 | return vlanCriterion == null || |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 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(); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 363 | if (!intf.vlan().equals(VlanId.NONE)) { |
| 364 | selectorBuilder.matchVlanId(intf.vlan()); |
| 365 | } |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 366 | return new FilteredConnectPoint(intf.connectPoint(), |
| 367 | selectorBuilder.build()); |
| 368 | }) |
| 369 | .collect(Collectors.toSet()); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Listener for host events. |
| 374 | */ |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 375 | private class InternalHostListener implements HostListener { |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 376 | @Override |
| 377 | public void event(HostEvent event) { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 378 | log.debug(HOST_EVENT, event); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 379 | switch (event.type()) { |
| 380 | case HOST_ADDED: |
| 381 | case HOST_UPDATED: |
| 382 | case HOST_REMOVED: |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 383 | setupConnectivity(false); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 384 | break; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 385 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 386 | default: |
| 387 | break; |
| 388 | } |
| 389 | } |
| 390 | } |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 391 | |
| 392 | /** |
| 393 | * Listener for interface configuration events. |
| 394 | */ |
| 395 | private class InternalInterfaceListener implements InterfaceListener { |
| 396 | @Override |
| 397 | public void event(InterfaceEvent event) { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 398 | log.debug(INTF_CONF_EVENT, event); |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 399 | switch (event.type()) { |
| 400 | case INTERFACE_ADDED: |
| 401 | case INTERFACE_UPDATED: |
| 402 | case INTERFACE_REMOVED: |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 403 | setupConnectivity(false); |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 404 | break; |
| 405 | default: |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | } |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 410 | |
| 411 | /** |
| 412 | * Listener for VPLS configuration events. |
| 413 | */ |
| 414 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 415 | @Override |
| 416 | public void event(NetworkConfigEvent event) { |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 417 | if (event.configClass() == VplsConfigService.CONFIG_CLASS) { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 418 | log.debug(NET_CONF_EVENT, event.configClass()); |
| 419 | switch (event.type()) { |
| 420 | case CONFIG_ADDED: |
| 421 | case CONFIG_UPDATED: |
| 422 | case CONFIG_REMOVED: |
| 423 | setupConnectivity(true); |
| 424 | break; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 425 | default: |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | } |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 431 | } |