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 |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 14 | */ |
| 15 | package org.onosproject.vpls; |
| 16 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 17 | import com.google.common.collect.ImmutableSet; |
| 18 | import com.google.common.collect.Lists; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 19 | import com.google.common.collect.SetMultimap; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onlab.packet.VlanId; |
| 28 | import org.onosproject.app.ApplicationService; |
| 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.CoreService; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 31 | import org.onosproject.incubator.net.intf.Interface; |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 32 | import org.onosproject.incubator.net.intf.InterfaceEvent; |
| 33 | import org.onosproject.incubator.net.intf.InterfaceListener; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 34 | import org.onosproject.incubator.net.intf.InterfaceService; |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 35 | import org.onosproject.net.EncapsulationType; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 36 | import org.onosproject.net.FilteredConnectPoint; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 37 | import org.onosproject.net.Host; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 38 | import org.onosproject.net.config.NetworkConfigEvent; |
| 39 | import org.onosproject.net.config.NetworkConfigListener; |
| 40 | import org.onosproject.net.config.NetworkConfigService; |
| 41 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 42 | import org.onosproject.net.flow.TrafficSelector; |
| 43 | import org.onosproject.net.flow.criteria.Criterion; |
| 44 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 45 | import org.onosproject.net.host.HostEvent; |
| 46 | import org.onosproject.net.host.HostListener; |
| 47 | import org.onosproject.net.host.HostService; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 48 | import org.onosproject.net.intent.Intent; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 49 | import org.onosproject.net.intent.IntentService; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 50 | import org.onosproject.net.intent.Key; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 51 | import org.onosproject.routing.IntentSynchronizationService; |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 52 | import org.onosproject.vpls.config.VplsConfigService; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 53 | import org.slf4j.Logger; |
| 54 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 55 | import java.util.Collection; |
| 56 | import java.util.HashSet; |
| 57 | import java.util.List; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 58 | import java.util.Set; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 59 | import java.util.stream.Collectors; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 60 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 61 | import static org.onosproject.vpls.IntentInstaller.PREFIX_BROADCAST; |
| 62 | import static org.onosproject.vpls.IntentInstaller.PREFIX_UNICAST; |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 63 | import static org.slf4j.LoggerFactory.getLogger; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 66 | * Application to create L2 broadcast overlay networks using VLANs. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 67 | */ |
| 68 | @Component(immediate = true) |
| 69 | public class Vpls { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 70 | static final String VPLS_APP = "org.onosproject.vpls"; |
| 71 | |
| 72 | private static final String HOST_FCP_NOT_FOUND = |
| 73 | "Filtered connected point for host {} not found"; |
| 74 | private static final String HOST_EVENT = "Received HostEvent {}"; |
| 75 | private static final String INTF_CONF_EVENT = |
| 76 | "Received InterfaceConfigEvent {}"; |
| 77 | private static final String NET_CONF_EVENT = |
| 78 | "Received NetworkConfigEvent {}"; |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 79 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 80 | private final Logger log = getLogger(getClass()); |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected ApplicationService applicationService; |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected CoreService coreService; |
| 87 | |
| 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 89 | protected HostService hostService; |
| 90 | |
| 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 92 | protected IntentService intentService; |
| 93 | |
| 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 95 | protected InterfaceService interfaceService; |
| 96 | |
| 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 98 | protected IntentSynchronizationService intentSynchronizer; |
| 99 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 101 | protected NetworkConfigService configService; |
| 102 | |
| 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 104 | protected VplsConfigService vplsConfigService; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 105 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 106 | private final HostListener hostListener = new InternalHostListener(); |
| 107 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 108 | private final InternalInterfaceListener interfaceListener = |
| 109 | new InternalInterfaceListener(); |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 110 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 111 | private final InternalNetworkConfigListener configListener = |
| 112 | new InternalNetworkConfigListener(); |
| 113 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 114 | private IntentInstaller intentInstaller; |
| 115 | |
| 116 | private ApplicationId appId; |
| 117 | |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 118 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 119 | @Activate |
| 120 | public void activate() { |
| 121 | appId = coreService.registerApplication(VPLS_APP); |
| 122 | |
| 123 | intentInstaller = new IntentInstaller(appId, |
| 124 | intentService, |
| 125 | intentSynchronizer); |
| 126 | |
Luca Prete | 2705d66 | 2016-04-29 15:30:23 -0700 | [diff] [blame] | 127 | applicationService.registerDeactivateHook(appId, () -> { |
| 128 | intentSynchronizer.removeIntentsByAppId(appId); |
| 129 | }); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 130 | |
| 131 | hostService.addListener(hostListener); |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 132 | interfaceService.addListener(interfaceListener); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 133 | configService.addListener(configListener); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 134 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 135 | setupConnectivity(false); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 136 | |
Luca Prete | c21c6e6 | 2016-09-22 14:52:21 -0700 | [diff] [blame] | 137 | log.info("Activated"); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Deactivate |
| 141 | public void deactivate() { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 142 | configService.removeListener(configListener); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 143 | intentSynchronizer.removeIntentsByAppId(appId); |
Luca Prete | c21c6e6 | 2016-09-22 14:52:21 -0700 | [diff] [blame] | 144 | log.info("Deactivated"); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 145 | } |
| 146 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 147 | /** |
| 148 | * Sets up connectivity for all VPLSs. |
| 149 | * |
| 150 | * @param isNetworkConfigEvent true if this function is triggered |
| 151 | * by NetworkConfigEvent; false otherwise |
| 152 | */ |
| 153 | private void setupConnectivity(boolean isNetworkConfigEvent) { |
| 154 | SetMultimap<String, Interface> networkInterfaces = |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 155 | vplsConfigService.ifacesByVplsName(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 156 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 157 | Set<String> vplsAffectedByApi = |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 158 | new HashSet<>(vplsConfigService.vplsAffectedByApi()); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 159 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 160 | if (isNetworkConfigEvent && vplsAffectedByApi.isEmpty()) { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 161 | vplsAffectedByApi.addAll(vplsConfigService.vplsNamesOld()); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 162 | } |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 163 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 164 | networkInterfaces.asMap().forEach((vplsName, interfaces) -> { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 165 | Set<Host> hosts = Sets.newHashSet(); |
| 166 | interfaces.forEach(intf -> { |
| 167 | // Add hosts that belongs to the specific VPLS |
| 168 | hostService.getConnectedHosts(intf.connectPoint()) |
| 169 | .stream() |
| 170 | .filter(host -> host.vlan().equals(intf.vlan())) |
| 171 | .forEach(hosts::add); |
| 172 | }); |
| 173 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 174 | EncapsulationType encap = |
| 175 | vplsConfigService.encap(vplsName); |
| 176 | |
| 177 | setupConnectivity(vplsName, interfaces, hosts, encap, |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 178 | vplsAffectedByApi.contains(vplsName)); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 179 | vplsAffectedByApi.remove(vplsName); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 180 | }); |
| 181 | |
| 182 | if (!vplsAffectedByApi.isEmpty()) { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 183 | for (String vplsName : vplsAffectedByApi) { |
| 184 | withdrawIntents(vplsName, Lists.newArrayList()); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 190 | * Sets up connectivity for specific VPLS. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 191 | * |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 192 | * @param vplsName the VPLS name |
| 193 | * @param interfaces the interfaces that belong to the VPLS |
| 194 | * @param hosts the hosts that belong to the VPLS |
| 195 | * @param encap the encapsulation type |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 196 | * @param affectedByApi true if this function is triggered from the APIs; |
| 197 | * false otherwise |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 198 | */ |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 199 | private void setupConnectivity(String vplsName, |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 200 | Collection<Interface> interfaces, |
| 201 | Set<Host> hosts, |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 202 | EncapsulationType encap, |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 203 | boolean affectedByApi) { |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 204 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 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 |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [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 | } |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 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 | * |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 244 | * @param vplsName the VPLS name |
| 245 | * @param fcPoints the set of filtered connect points |
| 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) |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 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 | * |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 282 | * @param vplsName the VPLS name |
| 283 | * @param hosts the set of destination hosts |
| 284 | * @param fcPoints the set of filtered connect points |
| 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 | /** |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 328 | * Returns the filtered connect point associated to a given host. |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 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); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 342 | return vlanCriterion == null || |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 343 | vlanCriterion.vlanId().equals(host.vlan()); |
| 344 | }) |
| 345 | .findFirst() |
| 346 | .orElse(null); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Computes a set of filtered connect points from a list of given interfaces. |
| 351 | * |
| 352 | * @param interfaces the interfaces to compute |
| 353 | * @return the set of filtered connect points |
| 354 | */ |
| 355 | private Set<FilteredConnectPoint> buildFCPoints(Collection<Interface> interfaces) { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 356 | // Build all filtered connected points in the VPLS |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 357 | return interfaces |
| 358 | .stream() |
| 359 | .map(intf -> { |
| 360 | TrafficSelector.Builder selectorBuilder = |
| 361 | DefaultTrafficSelector.builder(); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 362 | if (!intf.vlan().equals(VlanId.NONE)) { |
| 363 | selectorBuilder.matchVlanId(intf.vlan()); |
| 364 | } |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 365 | return new FilteredConnectPoint(intf.connectPoint(), |
| 366 | selectorBuilder.build()); |
| 367 | }) |
| 368 | .collect(Collectors.toSet()); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Listener for host events. |
| 373 | */ |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 374 | private class InternalHostListener implements HostListener { |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 375 | @Override |
| 376 | public void event(HostEvent event) { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 377 | log.debug(HOST_EVENT, event); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 378 | switch (event.type()) { |
| 379 | case HOST_ADDED: |
| 380 | case HOST_UPDATED: |
| 381 | case HOST_REMOVED: |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 382 | setupConnectivity(false); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 383 | break; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 384 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 385 | default: |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | } |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 390 | |
| 391 | /** |
| 392 | * Listener for interface configuration events. |
| 393 | */ |
| 394 | private class InternalInterfaceListener implements InterfaceListener { |
| 395 | @Override |
| 396 | public void event(InterfaceEvent event) { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 397 | log.debug(INTF_CONF_EVENT, event); |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 398 | switch (event.type()) { |
| 399 | case INTERFACE_ADDED: |
| 400 | case INTERFACE_UPDATED: |
| 401 | case INTERFACE_REMOVED: |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 402 | setupConnectivity(false); |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 403 | break; |
| 404 | default: |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | } |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 409 | |
| 410 | /** |
| 411 | * Listener for VPLS configuration events. |
| 412 | */ |
| 413 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 414 | @Override |
| 415 | public void event(NetworkConfigEvent event) { |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 416 | if (event.configClass() == VplsConfigService.CONFIG_CLASS) { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 417 | log.debug(NET_CONF_EVENT, event.configClass()); |
| 418 | switch (event.type()) { |
| 419 | case CONFIG_ADDED: |
| 420 | case CONFIG_UPDATED: |
| 421 | case CONFIG_REMOVED: |
| 422 | setupConnectivity(true); |
| 423 | break; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 424 | default: |
| 425 | break; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | } |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 430 | } |