Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.segmentrouting.xconnect.impl; |
| 17 | |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 19 | import com.google.common.cache.Cache; |
| 20 | import com.google.common.cache.CacheBuilder; |
| 21 | import com.google.common.cache.RemovalNotification; |
Charles Chan | 0b1dd7e | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableMap; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableSet; |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 24 | import com.google.common.collect.Lists; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 25 | import com.google.common.collect.Sets; |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 26 | import org.onlab.packet.Ethernet; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onlab.packet.VlanId; |
| 29 | import org.onlab.util.KryoNamespace; |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 30 | import org.onosproject.cluster.ClusterService; |
| 31 | import org.onosproject.cluster.LeadershipService; |
| 32 | import org.onosproject.cluster.NodeId; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 33 | import org.onosproject.codec.CodecService; |
| 34 | import org.onosproject.core.ApplicationId; |
| 35 | import org.onosproject.core.CoreService; |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 36 | import org.onosproject.portloadbalancer.api.PortLoadBalancerEvent; |
| 37 | import org.onosproject.portloadbalancer.api.PortLoadBalancerId; |
| 38 | import org.onosproject.portloadbalancer.api.PortLoadBalancerListener; |
| 39 | import org.onosproject.portloadbalancer.api.PortLoadBalancerService; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 40 | import org.onosproject.mastership.MastershipService; |
| 41 | import org.onosproject.net.ConnectPoint; |
| 42 | import org.onosproject.net.DeviceId; |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 43 | import org.onosproject.net.Host; |
| 44 | import org.onosproject.net.HostLocation; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 45 | import org.onosproject.net.PortNumber; |
| 46 | import org.onosproject.net.config.NetworkConfigService; |
| 47 | import org.onosproject.net.device.DeviceEvent; |
| 48 | import org.onosproject.net.device.DeviceListener; |
| 49 | import org.onosproject.net.device.DeviceService; |
| 50 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 51 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 52 | import org.onosproject.net.flow.TrafficSelector; |
| 53 | import org.onosproject.net.flow.TrafficTreatment; |
| 54 | import org.onosproject.net.flow.criteria.Criteria; |
| 55 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 56 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 57 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 58 | import org.onosproject.net.flowobjective.DefaultNextTreatment; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 59 | import org.onosproject.net.flowobjective.DefaultObjectiveContext; |
| 60 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 61 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 62 | import org.onosproject.net.flowobjective.ForwardingObjective; |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 63 | import org.onosproject.net.flowobjective.IdNextTreatment; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 64 | import org.onosproject.net.flowobjective.NextObjective; |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 65 | import org.onosproject.net.flowobjective.NextTreatment; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 66 | import org.onosproject.net.flowobjective.Objective; |
| 67 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 68 | import org.onosproject.net.flowobjective.ObjectiveError; |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 69 | import org.onosproject.net.host.HostEvent; |
| 70 | import org.onosproject.net.host.HostListener; |
| 71 | import org.onosproject.net.host.HostService; |
| 72 | import org.onosproject.net.intf.InterfaceService; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 73 | import org.onosproject.segmentrouting.SegmentRoutingService; |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 74 | import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 75 | import org.onosproject.segmentrouting.xconnect.api.XconnectCodec; |
| 76 | import org.onosproject.segmentrouting.xconnect.api.XconnectDesc; |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 77 | import org.onosproject.segmentrouting.xconnect.api.XconnectEndpoint; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 78 | import org.onosproject.segmentrouting.xconnect.api.XconnectKey; |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 79 | import org.onosproject.segmentrouting.xconnect.api.XconnectLoadBalancerEndpoint; |
| 80 | import org.onosproject.segmentrouting.xconnect.api.XconnectPortEndpoint; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 81 | import org.onosproject.segmentrouting.xconnect.api.XconnectService; |
| 82 | import org.onosproject.store.serializers.KryoNamespaces; |
| 83 | import org.onosproject.store.service.ConsistentMap; |
| 84 | import org.onosproject.store.service.MapEvent; |
| 85 | import org.onosproject.store.service.MapEventListener; |
| 86 | import org.onosproject.store.service.Serializer; |
| 87 | import org.onosproject.store.service.StorageService; |
| 88 | import org.onosproject.store.service.Versioned; |
Ray Milkey | 2bd24a9 | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 89 | import org.osgi.service.component.annotations.Activate; |
| 90 | import org.osgi.service.component.annotations.Component; |
| 91 | import org.osgi.service.component.annotations.Deactivate; |
| 92 | import org.osgi.service.component.annotations.Reference; |
| 93 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 94 | import org.slf4j.Logger; |
| 95 | import org.slf4j.LoggerFactory; |
| 96 | |
| 97 | import java.io.Serializable; |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 98 | import java.util.Collections; |
| 99 | import java.util.List; |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 100 | import java.util.Optional; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 101 | import java.util.Set; |
| 102 | import java.util.concurrent.CompletableFuture; |
Charles Chan | 56542b6 | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 103 | import java.util.concurrent.ExecutorService; |
| 104 | import java.util.concurrent.Executors; |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 105 | import java.util.concurrent.ScheduledExecutorService; |
| 106 | import java.util.concurrent.TimeUnit; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 107 | import java.util.function.BiConsumer; |
| 108 | import java.util.function.Consumer; |
| 109 | import java.util.stream.Collectors; |
| 110 | |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 111 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
Charles Chan | 56542b6 | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 112 | import static org.onlab.util.Tools.groupedThreads; |
| 113 | |
Ray Milkey | 2bd24a9 | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 114 | @Component(immediate = true, service = XconnectService.class) |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 115 | public class XconnectManager implements XconnectService { |
Ray Milkey | 2bd24a9 | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 117 | private CoreService coreService; |
| 118 | |
Ray Milkey | 2bd24a9 | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 120 | private CodecService codecService; |
| 121 | |
Ray Milkey | 2bd24a9 | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 122 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 123 | private StorageService storageService; |
| 124 | |
Ray Milkey | 2bd24a9 | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 125 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 126 | public NetworkConfigService netCfgService; |
| 127 | |
Ray Milkey | 2bd24a9 | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 129 | public DeviceService deviceService; |
| 130 | |
Ray Milkey | 2bd24a9 | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 131 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 132 | public FlowObjectiveService flowObjectiveService; |
| 133 | |
Ray Milkey | 2bd24a9 | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 134 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 135 | private LeadershipService leadershipService; |
| 136 | |
| 137 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 138 | private ClusterService clusterService; |
| 139 | |
| 140 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 141 | public MastershipService mastershipService; |
| 142 | |
Ray Milkey | 2bd24a9 | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 143 | @Reference(cardinality = ReferenceCardinality.OPTIONAL) |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 144 | public SegmentRoutingService srService; |
| 145 | |
Ray Milkey | 3cad4db | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 146 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 147 | public InterfaceService interfaceService; |
| 148 | |
Ray Milkey | 3cad4db | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 149 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 150 | HostService hostService; |
| 151 | |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 152 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 153 | private PortLoadBalancerService portLoadBalancerService; |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 154 | |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 155 | private static final String APP_NAME = "org.onosproject.xconnect"; |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 156 | private static final String ERROR_NOT_LEADER = "Not leader controller"; |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 157 | private static final String ERROR_NEXT_OBJ_BUILDER = "Unable to construct next objective builder"; |
| 158 | private static final String ERROR_NEXT_ID = "Unable to get next id"; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 159 | |
| 160 | private static Logger log = LoggerFactory.getLogger(XconnectManager.class); |
| 161 | |
| 162 | private ApplicationId appId; |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 163 | private ConsistentMap<XconnectKey, Set<XconnectEndpoint>> xconnectStore; |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 164 | private ConsistentMap<XconnectKey, Integer> xconnectNextObjStore; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 165 | |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 166 | private ConsistentMap<VlanNextObjectiveStoreKey, Integer> xconnectMulticastNextStore; |
| 167 | private ConsistentMap<VlanNextObjectiveStoreKey, List<PortNumber>> xconnectMulticastPortsStore; |
| 168 | |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 169 | private final MapEventListener<XconnectKey, Set<XconnectEndpoint>> xconnectListener = new XconnectMapListener(); |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 170 | private ExecutorService xConnectExecutor; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 171 | |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 172 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
Charles Chan | 56542b6 | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 173 | private ExecutorService deviceEventExecutor; |
| 174 | |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 175 | private final HostListener hostListener = new InternalHostListener(); |
| 176 | private ExecutorService hostEventExecutor; |
| 177 | |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 178 | // Wait time for the cache |
| 179 | private static final int WAIT_TIME_MS = 15000; |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 180 | //The cache is implemented as buffer for waiting the installation of PortLoadBalancer when present |
| 181 | private Cache<PortLoadBalancerId, XconnectKey> portLoadBalancerCache; |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 182 | // Executor for the cache |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 183 | private ScheduledExecutorService portLoadBalancerExecutor; |
| 184 | // We need to listen for some events to properly installed the xconnect with portloadbalancer |
| 185 | private final PortLoadBalancerListener portLoadBalancerListener = new InternalPortLoadBalancerListener(); |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 186 | |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 187 | @Activate |
| 188 | void activate() { |
| 189 | appId = coreService.registerApplication(APP_NAME); |
| 190 | codecService.registerCodec(XconnectDesc.class, new XconnectCodec()); |
| 191 | |
| 192 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 193 | .register(KryoNamespaces.API) |
Charles Chan | fbaad96 | 2018-07-23 12:53:16 -0700 | [diff] [blame] | 194 | .register(XconnectManager.class) |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 195 | .register(XconnectKey.class) |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 196 | .register(XconnectEndpoint.class) |
| 197 | .register(XconnectPortEndpoint.class) |
| 198 | .register(XconnectLoadBalancerEndpoint.class) |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 199 | .register(VlanNextObjectiveStoreKey.class); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 200 | |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 201 | xconnectStore = storageService.<XconnectKey, Set<XconnectEndpoint>>consistentMapBuilder() |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 202 | .withName("onos-sr-xconnect") |
| 203 | .withRelaxedReadConsistency() |
| 204 | .withSerializer(Serializer.using(serializer.build())) |
| 205 | .build(); |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 206 | xConnectExecutor = Executors.newSingleThreadScheduledExecutor( |
| 207 | groupedThreads("sr-xconnect-event", "%d", log)); |
| 208 | xconnectStore.addListener(xconnectListener, xConnectExecutor); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 209 | |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 210 | xconnectNextObjStore = storageService.<XconnectKey, Integer>consistentMapBuilder() |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 211 | .withName("onos-sr-xconnect-next") |
| 212 | .withRelaxedReadConsistency() |
| 213 | .withSerializer(Serializer.using(serializer.build())) |
| 214 | .build(); |
| 215 | |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 216 | xconnectMulticastNextStore = storageService.<VlanNextObjectiveStoreKey, Integer>consistentMapBuilder() |
| 217 | .withName("onos-sr-xconnect-l2multicast-next") |
| 218 | .withSerializer(Serializer.using(serializer.build())) |
| 219 | .build(); |
| 220 | xconnectMulticastPortsStore = storageService.<VlanNextObjectiveStoreKey, List<PortNumber>>consistentMapBuilder() |
| 221 | .withName("onos-sr-xconnect-l2multicast-ports") |
| 222 | .withSerializer(Serializer.using(serializer.build())) |
| 223 | .build(); |
| 224 | |
Charles Chan | 56542b6 | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 225 | deviceEventExecutor = Executors.newSingleThreadScheduledExecutor( |
| 226 | groupedThreads("sr-xconnect-device-event", "%d", log)); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 227 | deviceService.addListener(deviceListener); |
| 228 | |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 229 | hostEventExecutor = Executors.newSingleThreadExecutor( |
| 230 | groupedThreads("sr-xconnect-host-event", "%d", log)); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 231 | hostService.addListener(hostListener); |
| 232 | |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 233 | portLoadBalancerCache = CacheBuilder.newBuilder() |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 234 | .expireAfterWrite(WAIT_TIME_MS, TimeUnit.MILLISECONDS) |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 235 | .removalListener((RemovalNotification<PortLoadBalancerId, XconnectKey> notification) -> |
| 236 | log.debug("PortLoadBalancer cache removal event. portLoadBalancerId={}, xConnectKey={}", |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 237 | notification.getKey(), notification.getValue())).build(); |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 238 | portLoadBalancerExecutor = newScheduledThreadPool(1, |
| 239 | groupedThreads("portLoadBalancerCacheWorker", "-%d", log)); |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 240 | // Let's schedule the cleanup of the cache |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 241 | portLoadBalancerExecutor.scheduleAtFixedRate(portLoadBalancerCache::cleanUp, 0, |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 242 | WAIT_TIME_MS, TimeUnit.MILLISECONDS); |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 243 | portLoadBalancerService.addListener(portLoadBalancerListener); |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 244 | |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 245 | log.info("Started"); |
| 246 | } |
| 247 | |
| 248 | @Deactivate |
| 249 | void deactivate() { |
| 250 | xconnectStore.removeListener(xconnectListener); |
| 251 | deviceService.removeListener(deviceListener); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 252 | hostService.removeListener(hostListener); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 253 | codecService.unregisterCodec(XconnectDesc.class); |
| 254 | |
Charles Chan | 56542b6 | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 255 | deviceEventExecutor.shutdown(); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 256 | hostEventExecutor.shutdown(); |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 257 | xConnectExecutor.shutdown(); |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 258 | portLoadBalancerExecutor.shutdown(); |
Charles Chan | 56542b6 | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 259 | |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 260 | log.info("Stopped"); |
| 261 | } |
| 262 | |
| 263 | @Override |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 264 | public void addOrUpdateXconnect(DeviceId deviceId, VlanId vlanId, Set<XconnectEndpoint> endpoints) { |
| 265 | log.info("Adding or updating xconnect. deviceId={}, vlanId={}, endpoints={}", |
| 266 | deviceId, vlanId, endpoints); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 267 | final XconnectKey key = new XconnectKey(deviceId, vlanId); |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 268 | xconnectStore.put(key, endpoints); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public void removeXonnect(DeviceId deviceId, VlanId vlanId) { |
| 273 | log.info("Removing xconnect. deviceId={}, vlanId={}", |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 274 | deviceId, vlanId); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 275 | final XconnectKey key = new XconnectKey(deviceId, vlanId); |
| 276 | xconnectStore.remove(key); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 277 | |
| 278 | // Cleanup multicasting support, if any. |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 279 | srService.getPairDeviceId(deviceId).ifPresent(pairDeviceId -> |
| 280 | cleanupL2MulticastRule(pairDeviceId, srService.getPairLocalPort(pairDeviceId).get(), vlanId, true) |
| 281 | ); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 282 | |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public Set<XconnectDesc> getXconnects() { |
| 287 | return xconnectStore.asJavaMap().entrySet().stream() |
| 288 | .map(e -> new XconnectDesc(e.getKey(), e.getValue())) |
| 289 | .collect(Collectors.toSet()); |
| 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public boolean hasXconnect(ConnectPoint cp) { |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 294 | return getXconnects().stream().anyMatch(desc -> |
| 295 | desc.key().deviceId().equals(cp.deviceId()) && desc.endpoints().stream().anyMatch(ep -> |
| 296 | ep.type() == XconnectEndpoint.Type.PORT && ((XconnectPortEndpoint) ep).port().equals(cp.port()) |
| 297 | ) |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 298 | ); |
| 299 | } |
| 300 | |
Charles Chan | 0b1dd7e | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 301 | @Override |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 302 | public List<VlanId> getXconnectVlans(DeviceId deviceId, PortNumber port) { |
| 303 | return getXconnects().stream() |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 304 | .filter(desc -> desc.key().deviceId().equals(deviceId) && desc.endpoints().stream().anyMatch(ep -> |
| 305 | ep.type() == XconnectEndpoint.Type.PORT && ((XconnectPortEndpoint) ep).port().equals(port))) |
| 306 | .map(XconnectDesc::key) |
| 307 | .map(XconnectKey::vlanId) |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 308 | .collect(Collectors.toList()); |
| 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public boolean isXconnectVlan(DeviceId deviceId, VlanId vlanId) { |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 313 | XconnectKey key = new XconnectKey(deviceId, vlanId); |
| 314 | return Versioned.valueOrNull(xconnectStore.get(key)) != null; |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | @Override |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 318 | public ImmutableMap<XconnectKey, Integer> getNext() { |
Charles Chan | 0b1dd7e | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 319 | if (xconnectNextObjStore != null) { |
| 320 | return ImmutableMap.copyOf(xconnectNextObjStore.asJavaMap()); |
| 321 | } else { |
| 322 | return ImmutableMap.of(); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | @Override |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 327 | public int getNextId(DeviceId deviceId, VlanId vlanId) { |
| 328 | return Versioned.valueOrElse(xconnectNextObjStore.get(new XconnectKey(deviceId, vlanId)), -1); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | @Override |
Charles Chan | 0b1dd7e | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 332 | public void removeNextId(int nextId) { |
| 333 | xconnectNextObjStore.entrySet().forEach(e -> { |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 334 | if (e.getValue().value() == nextId) { |
Charles Chan | 0b1dd7e | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 335 | xconnectNextObjStore.remove(e.getKey()); |
| 336 | } |
| 337 | }); |
| 338 | } |
| 339 | |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 340 | private class XconnectMapListener implements MapEventListener<XconnectKey, Set<XconnectEndpoint>> { |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 341 | @Override |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 342 | public void event(MapEvent<XconnectKey, Set<XconnectEndpoint>> event) { |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 343 | XconnectKey key = event.key(); |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 344 | Set<XconnectEndpoint> ports = Versioned.valueOrNull(event.newValue()); |
| 345 | Set<XconnectEndpoint> oldPorts = Versioned.valueOrNull(event.oldValue()); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 346 | |
| 347 | switch (event.type()) { |
| 348 | case INSERT: |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 349 | populateXConnect(key, ports); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 350 | break; |
| 351 | case UPDATE: |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 352 | updateXConnect(key, oldPorts, ports); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 353 | break; |
| 354 | case REMOVE: |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 355 | revokeXConnect(key, oldPorts); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 356 | break; |
| 357 | default: |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | private class InternalDeviceListener implements DeviceListener { |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 364 | // Offload the execution to an executor and then process the event |
| 365 | // if this instance is the leader of the device |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 366 | @Override |
| 367 | public void event(DeviceEvent event) { |
Charles Chan | 56542b6 | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 368 | deviceEventExecutor.execute(() -> { |
| 369 | DeviceId deviceId = event.subject().id(); |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 370 | // Just skip if we are not the leader |
| 371 | if (!isLocalLeader(deviceId)) { |
| 372 | log.debug("Not the leader of {}. Skip event {}", deviceId, event); |
Charles Chan | 56542b6 | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 373 | return; |
| 374 | } |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 375 | // Populate or revoke according to the device availability |
| 376 | if (deviceService.isAvailable(deviceId)) { |
| 377 | init(deviceId); |
| 378 | } else { |
| 379 | cleanup(deviceId); |
Charles Chan | 56542b6 | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 380 | } |
| 381 | }); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 382 | } |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 383 | // We want to manage only a subset of events and if we are the leader |
| 384 | @Override |
| 385 | public boolean isRelevant(DeviceEvent event) { |
| 386 | return event.type() == DeviceEvent.Type.DEVICE_ADDED || |
| 387 | event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED || |
| 388 | event.type() == DeviceEvent.Type.DEVICE_UPDATED; |
| 389 | } |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 390 | } |
| 391 | |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 392 | private class InternalHostListener implements HostListener { |
| 393 | @Override |
| 394 | public void event(HostEvent event) { |
| 395 | hostEventExecutor.execute(() -> { |
| 396 | |
| 397 | switch (event.type()) { |
| 398 | case HOST_MOVED: |
| 399 | log.trace("Processing host event {}", event); |
| 400 | |
| 401 | Host host = event.subject(); |
| 402 | Set<HostLocation> prevLocations = event.prevSubject().locations(); |
| 403 | Set<HostLocation> newLocations = host.locations(); |
| 404 | |
| 405 | // Dual-home host port failure |
| 406 | // For each old location, in failed and paired devices update L2 vlan groups |
| 407 | Sets.difference(prevLocations, newLocations).forEach(prevLocation -> { |
| 408 | |
| 409 | Optional<DeviceId> pairDeviceId = srService.getPairDeviceId(prevLocation.deviceId()); |
| 410 | Optional<PortNumber> pairLocalPort = srService.getPairLocalPort(prevLocation.deviceId()); |
| 411 | |
| 412 | if (pairDeviceId.isPresent() && pairLocalPort.isPresent() && newLocations.stream() |
| 413 | .anyMatch(location -> location.deviceId().equals(pairDeviceId.get())) && |
| 414 | hasXconnect(new ConnectPoint(prevLocation.deviceId(), prevLocation.port()))) { |
| 415 | |
| 416 | List<VlanId> xconnectVlans = getXconnectVlans(prevLocation.deviceId(), |
| 417 | prevLocation.port()); |
| 418 | xconnectVlans.forEach(xconnectVlan -> { |
| 419 | // Add single-home host into L2 multicast group at paired device side. |
| 420 | // Also append ACL rule to forward traffic from paired port to L2 multicast group. |
| 421 | newLocations.stream() |
| 422 | .filter(location -> location.deviceId().equals(pairDeviceId.get())) |
| 423 | .forEach(location -> populateL2Multicast(location.deviceId(), |
| 424 | srService.getPairLocalPort( |
| 425 | location.deviceId()).get(), |
| 426 | xconnectVlan, |
| 427 | Collections.singletonList( |
| 428 | location.port()))); |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 429 | // Ensure pair-port attached to xconnect vlan flooding group |
| 430 | // at dual home failed device. |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 431 | updateL2Flooding(prevLocation.deviceId(), pairLocalPort.get(), xconnectVlan, true); |
| 432 | }); |
| 433 | } |
| 434 | }); |
| 435 | |
| 436 | // Dual-home host port restoration |
| 437 | // For each new location, reverse xconnect loop prevention groups. |
| 438 | Sets.difference(newLocations, prevLocations).forEach(newLocation -> { |
| 439 | final Optional<DeviceId> pairDeviceId = srService.getPairDeviceId(newLocation.deviceId()); |
| 440 | Optional<PortNumber> pairLocalPort = srService.getPairLocalPort(newLocation.deviceId()); |
| 441 | if (pairDeviceId.isPresent() && pairLocalPort.isPresent() && |
| 442 | hasXconnect((new ConnectPoint(newLocation.deviceId(), newLocation.port())))) { |
| 443 | |
| 444 | List<VlanId> xconnectVlans = getXconnectVlans(newLocation.deviceId(), |
| 445 | newLocation.port()); |
| 446 | xconnectVlans.forEach(xconnectVlan -> { |
| 447 | // Remove recovered dual homed port from vlan L2 multicast group |
| 448 | prevLocations.stream() |
| 449 | .filter(prevLocation -> prevLocation.deviceId().equals(pairDeviceId.get())) |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 450 | .forEach(prevLocation -> revokeL2Multicast( |
| 451 | prevLocation.deviceId(), |
| 452 | xconnectVlan, |
| 453 | Collections.singletonList(newLocation.port())) |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 454 | ); |
| 455 | |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 456 | // Remove pair-port from vlan's flooding group at dual home |
| 457 | // restored device, if needed. |
| 458 | if (!hasAccessPortInMulticastGroup(new VlanNextObjectiveStoreKey( |
| 459 | newLocation.deviceId(), xconnectVlan), pairLocalPort.get())) { |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 460 | updateL2Flooding(newLocation.deviceId(), |
| 461 | pairLocalPort.get(), |
| 462 | xconnectVlan, |
| 463 | false); |
| 464 | |
| 465 | // Clean L2 multicast group at pair-device; also update store. |
| 466 | cleanupL2MulticastRule(pairDeviceId.get(), |
| 467 | srService.getPairLocalPort(pairDeviceId.get()).get(), |
| 468 | xconnectVlan, |
| 469 | false); |
| 470 | } |
| 471 | }); |
| 472 | } |
| 473 | }); |
| 474 | break; |
| 475 | |
| 476 | default: |
| 477 | log.warn("Unsupported host event type: {} received. Ignoring.", event.type()); |
| 478 | break; |
| 479 | } |
| 480 | }); |
| 481 | } |
| 482 | } |
| 483 | |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 484 | private void init(DeviceId deviceId) { |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 485 | getXconnects().stream() |
| 486 | .filter(desc -> desc.key().deviceId().equals(deviceId)) |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 487 | .forEach(desc -> populateXConnect(desc.key(), desc.endpoints())); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 490 | private void cleanup(DeviceId deviceId) { |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 491 | xconnectNextObjStore.entrySet().stream() |
| 492 | .filter(entry -> entry.getKey().deviceId().equals(deviceId)) |
| 493 | .forEach(entry -> xconnectNextObjStore.remove(entry.getKey())); |
| 494 | log.debug("{} is removed from xConnectNextObjStore", deviceId); |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Populates XConnect groups and flows for given key. |
| 499 | * |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 500 | * @param key XConnect key |
| 501 | * @param endpoints a set of endpoints to be cross-connected |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 502 | */ |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 503 | private void populateXConnect(XconnectKey key, Set<XconnectEndpoint> endpoints) { |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 504 | if (!isLocalLeader(key.deviceId())) { |
| 505 | log.debug("Abort populating XConnect {}: {}", key, ERROR_NOT_LEADER); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 506 | return; |
| 507 | } |
| 508 | |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 509 | int nextId = populateNext(key, endpoints); |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 510 | if (nextId == -1) { |
| 511 | log.warn("Fail to populateXConnect {}: {}", key, ERROR_NEXT_ID); |
| 512 | return; |
| 513 | } |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 514 | populateFilter(key, endpoints); |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 515 | populateFwd(key, nextId); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 516 | populateAcl(key); |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Populates filtering objectives for given XConnect. |
| 521 | * |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 522 | * @param key XConnect store key |
| 523 | * @param endpoints XConnect endpoints |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 524 | */ |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 525 | private void populateFilter(XconnectKey key, Set<XconnectEndpoint> endpoints) { |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 526 | // FIXME Improve the logic |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 527 | // If port load balancer is not involved, use filtered port. Otherwise, use unfiltered port. |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 528 | // The purpose is to make sure existing XConnect logic can still work on a configured port. |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 529 | boolean filtered = endpoints.stream() |
| 530 | .map(ep -> getNextTreatment(key.deviceId(), ep, false)) |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 531 | .allMatch(t -> t.type().equals(NextTreatment.Type.TREATMENT)); |
| 532 | |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 533 | endpoints.stream() |
| 534 | .map(ep -> getPhysicalPorts(key.deviceId(), ep)) |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 535 | .flatMap(Set::stream).forEach(port -> { |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 536 | FilteringObjective.Builder filtObjBuilder = filterObjBuilder(key, port, filtered); |
| 537 | ObjectiveContext context = new DefaultObjectiveContext( |
| 538 | (objective) -> log.debug("XConnect FilterObj for {} on port {} populated", |
| 539 | key, port), |
| 540 | (objective, error) -> |
| 541 | log.warn("Failed to populate XConnect FilterObj for {} on port {}: {}", |
| 542 | key, port, error)); |
| 543 | flowObjectiveService.filter(key.deviceId(), filtObjBuilder.add(context)); |
| 544 | }); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Populates next objectives for given XConnect. |
| 549 | * |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 550 | * @param key XConnect store key |
| 551 | * @param endpoints XConnect endpoints |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 552 | * @return next id |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 553 | */ |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 554 | private int populateNext(XconnectKey key, Set<XconnectEndpoint> endpoints) { |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 555 | int nextId = Versioned.valueOrElse(xconnectNextObjStore.get(key), -1); |
| 556 | if (nextId != -1) { |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 557 | log.debug("NextObj for {} found, id={}", key, nextId); |
| 558 | return nextId; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 559 | } else { |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 560 | NextObjective.Builder nextObjBuilder = nextObjBuilder(key, endpoints); |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 561 | if (nextObjBuilder == null) { |
| 562 | log.warn("Fail to populate {}: {}", key, ERROR_NEXT_OBJ_BUILDER); |
| 563 | return -1; |
| 564 | } |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 565 | ObjectiveContext nextContext = new DefaultObjectiveContext( |
| 566 | // To serialize this with kryo |
| 567 | (Serializable & Consumer<Objective>) (objective) -> |
| 568 | log.debug("XConnect NextObj for {} added", key), |
Charles Chan | facfbef | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 569 | (Serializable & BiConsumer<Objective, ObjectiveError>) (objective, error) -> { |
| 570 | log.warn("Failed to add XConnect NextObj for {}: {}", key, error); |
| 571 | srService.invalidateNextObj(objective.id()); |
| 572 | }); |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 573 | NextObjective nextObj = nextObjBuilder.add(nextContext); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 574 | flowObjectiveService.next(key.deviceId(), nextObj); |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 575 | xconnectNextObjStore.put(key, nextObj.id()); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 576 | log.debug("NextObj for {} not found. Creating new NextObj with id={}", key, nextObj.id()); |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 577 | return nextObj.id(); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 578 | } |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /** |
| 582 | * Populates bridging forwarding objectives for given XConnect. |
| 583 | * |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 584 | * @param key XConnect store key |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 585 | * @param nextId next objective id |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 586 | */ |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 587 | private void populateFwd(XconnectKey key, int nextId) { |
| 588 | ForwardingObjective.Builder fwdObjBuilder = fwdObjBuilder(key, nextId); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 589 | ObjectiveContext fwdContext = new DefaultObjectiveContext( |
| 590 | (objective) -> log.debug("XConnect FwdObj for {} populated", key), |
| 591 | (objective, error) -> |
| 592 | log.warn("Failed to populate XConnect FwdObj for {}: {}", key, error)); |
| 593 | flowObjectiveService.forward(key.deviceId(), fwdObjBuilder.add(fwdContext)); |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * Populates ACL forwarding objectives for given XConnect. |
| 598 | * |
| 599 | * @param key XConnect store key |
| 600 | */ |
| 601 | private void populateAcl(XconnectKey key) { |
| 602 | ForwardingObjective.Builder aclObjBuilder = aclObjBuilder(key.vlanId()); |
| 603 | ObjectiveContext aclContext = new DefaultObjectiveContext( |
| 604 | (objective) -> log.debug("XConnect AclObj for {} populated", key), |
| 605 | (objective, error) -> |
| 606 | log.warn("Failed to populate XConnect AclObj for {}: {}", key, error)); |
| 607 | flowObjectiveService.forward(key.deviceId(), aclObjBuilder.add(aclContext)); |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * Revokes XConnect groups and flows for given key. |
| 612 | * |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 613 | * @param key XConnect key |
| 614 | * @param endpoints XConnect endpoints |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 615 | */ |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 616 | private void revokeXConnect(XconnectKey key, Set<XconnectEndpoint> endpoints) { |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 617 | if (!isLocalLeader(key.deviceId())) { |
| 618 | log.debug("Abort revoking XConnect {}: {}", key, ERROR_NOT_LEADER); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 619 | return; |
| 620 | } |
| 621 | |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 622 | revokeFilter(key, endpoints); |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 623 | int nextId = Versioned.valueOrElse(xconnectNextObjStore.get(key), -1); |
| 624 | if (nextId != -1) { |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 625 | revokeFwd(key, nextId, null); |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 626 | revokeNext(key, endpoints, nextId, null); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 627 | } else { |
| 628 | log.warn("NextObj for {} does not exist in the store.", key); |
| 629 | } |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 630 | revokeFilter(key, endpoints); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 631 | revokeAcl(key); |
| 632 | } |
| 633 | |
| 634 | /** |
| 635 | * Revokes filtering objectives for given XConnect. |
| 636 | * |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 637 | * @param key XConnect store key |
| 638 | * @param endpoints XConnect endpoints |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 639 | */ |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 640 | private void revokeFilter(XconnectKey key, Set<XconnectEndpoint> endpoints) { |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 641 | // FIXME Improve the logic |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 642 | // If port load balancer is not involved, use filtered port. Otherwise, use unfiltered port. |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 643 | // The purpose is to make sure existing XConnect logic can still work on a configured port. |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 644 | boolean filtered = endpoints.stream() |
| 645 | .map(ep -> getNextTreatment(key.deviceId(), ep, false)) |
| 646 | .allMatch(t -> t.type().equals(NextTreatment.Type.TREATMENT)); |
| 647 | |
| 648 | endpoints.stream() |
| 649 | .map(ep -> getPhysicalPorts(key.deviceId(), ep)). |
| 650 | flatMap(Set::stream).forEach(port -> { |
| 651 | FilteringObjective.Builder filtObjBuilder = filterObjBuilder(key, port, filtered); |
| 652 | ObjectiveContext context = new DefaultObjectiveContext( |
| 653 | (objective) -> log.debug("XConnect FilterObj for {} on port {} revoked", |
| 654 | key, port), |
| 655 | (objective, error) -> |
| 656 | log.warn("Failed to revoke XConnect FilterObj for {} on port {}: {}", |
| 657 | key, port, error)); |
| 658 | flowObjectiveService.filter(key.deviceId(), filtObjBuilder.remove(context)); |
| 659 | }); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | /** |
| 663 | * Revokes next objectives for given XConnect. |
| 664 | * |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 665 | * @param key XConnect store key |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 666 | * @param endpoints XConnect endpoints |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 667 | * @param nextId next objective id |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 668 | * @param nextFuture completable future for this next objective operation |
| 669 | */ |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 670 | private void revokeNext(XconnectKey key, Set<XconnectEndpoint> endpoints, int nextId, |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 671 | CompletableFuture<ObjectiveError> nextFuture) { |
| 672 | ObjectiveContext context = new ObjectiveContext() { |
| 673 | @Override |
| 674 | public void onSuccess(Objective objective) { |
| 675 | log.debug("Previous NextObj for {} removed", key); |
| 676 | if (nextFuture != null) { |
| 677 | nextFuture.complete(null); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | @Override |
| 682 | public void onError(Objective objective, ObjectiveError error) { |
| 683 | log.warn("Failed to remove previous NextObj for {}: {}", key, error); |
| 684 | if (nextFuture != null) { |
| 685 | nextFuture.complete(error); |
| 686 | } |
Charles Chan | facfbef | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 687 | srService.invalidateNextObj(objective.id()); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 688 | } |
| 689 | }; |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 690 | |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 691 | NextObjective.Builder nextObjBuilder = nextObjBuilder(key, endpoints, nextId); |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 692 | if (nextObjBuilder == null) { |
| 693 | log.warn("Fail to revokeNext {}: {}", key, ERROR_NEXT_OBJ_BUILDER); |
| 694 | return; |
| 695 | } |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 696 | // Release the port load balancer if present |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 697 | endpoints.stream() |
| 698 | .filter(endpoint -> endpoint.type() == XconnectEndpoint.Type.LOAD_BALANCER) |
| 699 | .forEach(endpoint -> { |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 700 | String portLoadBalancerKey = String.valueOf(((XconnectLoadBalancerEndpoint) endpoint).key()); |
| 701 | portLoadBalancerService.release(new PortLoadBalancerId(key.deviceId(), |
| 702 | Integer.parseInt(portLoadBalancerKey)), appId); |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 703 | }); |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 704 | flowObjectiveService.next(key.deviceId(), nextObjBuilder.remove(context)); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 705 | xconnectNextObjStore.remove(key); |
| 706 | } |
| 707 | |
| 708 | /** |
| 709 | * Revokes bridging forwarding objectives for given XConnect. |
| 710 | * |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 711 | * @param key XConnect store key |
| 712 | * @param nextId next objective id |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 713 | * @param fwdFuture completable future for this forwarding objective operation |
| 714 | */ |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 715 | private void revokeFwd(XconnectKey key, int nextId, CompletableFuture<ObjectiveError> fwdFuture) { |
| 716 | ForwardingObjective.Builder fwdObjBuilder = fwdObjBuilder(key, nextId); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 717 | ObjectiveContext context = new ObjectiveContext() { |
| 718 | @Override |
| 719 | public void onSuccess(Objective objective) { |
| 720 | log.debug("Previous FwdObj for {} removed", key); |
| 721 | if (fwdFuture != null) { |
| 722 | fwdFuture.complete(null); |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | @Override |
| 727 | public void onError(Objective objective, ObjectiveError error) { |
| 728 | log.warn("Failed to remove previous FwdObj for {}: {}", key, error); |
| 729 | if (fwdFuture != null) { |
| 730 | fwdFuture.complete(error); |
| 731 | } |
| 732 | } |
| 733 | }; |
| 734 | flowObjectiveService.forward(key.deviceId(), fwdObjBuilder.remove(context)); |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * Revokes ACL forwarding objectives for given XConnect. |
| 739 | * |
| 740 | * @param key XConnect store key |
| 741 | */ |
| 742 | private void revokeAcl(XconnectKey key) { |
| 743 | ForwardingObjective.Builder aclObjBuilder = aclObjBuilder(key.vlanId()); |
| 744 | ObjectiveContext aclContext = new DefaultObjectiveContext( |
| 745 | (objective) -> log.debug("XConnect AclObj for {} populated", key), |
| 746 | (objective, error) -> |
| 747 | log.warn("Failed to populate XConnect AclObj for {}: {}", key, error)); |
| 748 | flowObjectiveService.forward(key.deviceId(), aclObjBuilder.remove(aclContext)); |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * Updates XConnect groups and flows for given key. |
| 753 | * |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 754 | * @param key XConnect key |
| 755 | * @param prevEndpoints previous XConnect endpoints |
| 756 | * @param endpoints new XConnect endpoints |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 757 | */ |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 758 | private void updateXConnect(XconnectKey key, Set<XconnectEndpoint> prevEndpoints, |
| 759 | Set<XconnectEndpoint> endpoints) { |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 760 | if (!isLocalLeader(key.deviceId())) { |
| 761 | log.debug("Abort updating XConnect {}: {}", key, ERROR_NOT_LEADER); |
| 762 | return; |
| 763 | } |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 764 | // NOTE: ACL flow doesn't include port information. No need to update it. |
| 765 | // Pair port is built-in and thus not going to change. No need to update it. |
| 766 | |
| 767 | // remove old filter |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 768 | prevEndpoints.stream().filter(prevEndpoint -> !endpoints.contains(prevEndpoint)).forEach(prevEndpoint -> |
| 769 | revokeFilter(key, ImmutableSet.of(prevEndpoint))); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 770 | // install new filter |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 771 | endpoints.stream().filter(endpoint -> !prevEndpoints.contains(endpoint)).forEach(endpoint -> |
| 772 | populateFilter(key, ImmutableSet.of(endpoint))); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 773 | |
| 774 | CompletableFuture<ObjectiveError> fwdFuture = new CompletableFuture<>(); |
| 775 | CompletableFuture<ObjectiveError> nextFuture = new CompletableFuture<>(); |
| 776 | |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 777 | int nextId = Versioned.valueOrElse(xconnectNextObjStore.get(key), -1); |
| 778 | if (nextId != -1) { |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 779 | revokeFwd(key, nextId, fwdFuture); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 780 | |
| 781 | fwdFuture.thenAcceptAsync(fwdStatus -> { |
| 782 | if (fwdStatus == null) { |
| 783 | log.debug("Fwd removed. Now remove group {}", key); |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 784 | revokeNext(key, prevEndpoints, nextId, nextFuture); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 785 | } |
| 786 | }); |
| 787 | |
| 788 | nextFuture.thenAcceptAsync(nextStatus -> { |
| 789 | if (nextStatus == null) { |
| 790 | log.debug("Installing new group and flow for {}", key); |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 791 | int newNextId = populateNext(key, endpoints); |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 792 | if (newNextId == -1) { |
| 793 | log.warn("Fail to updateXConnect {}: {}", key, ERROR_NEXT_ID); |
| 794 | return; |
| 795 | } |
| 796 | populateFwd(key, newNextId); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 797 | } |
| 798 | }); |
| 799 | } else { |
| 800 | log.warn("NextObj for {} does not exist in the store.", key); |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | /** |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 805 | * Creates a next objective builder for XConnect with given nextId. |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 806 | * |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 807 | * @param key XConnect key |
| 808 | * @param endpoints XConnect endpoints |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 809 | * @param nextId next objective id |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 810 | * @return next objective builder |
| 811 | */ |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 812 | private NextObjective.Builder nextObjBuilder(XconnectKey key, Set<XconnectEndpoint> endpoints, int nextId) { |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 813 | TrafficSelector metadata = |
| 814 | DefaultTrafficSelector.builder().matchVlanId(key.vlanId()).build(); |
| 815 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 816 | .builder().withId(nextId) |
| 817 | .withType(NextObjective.Type.BROADCAST).fromApp(appId) |
| 818 | .withMeta(metadata); |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 819 | |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 820 | for (XconnectEndpoint endpoint : endpoints) { |
| 821 | NextTreatment nextTreatment = getNextTreatment(key.deviceId(), endpoint, true); |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 822 | if (nextTreatment == null) { |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 823 | // If a PortLoadBalancer is used in the XConnect - putting on hold |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 824 | if (endpoint.type() == XconnectEndpoint.Type.LOAD_BALANCER) { |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 825 | log.warn("Unable to create nextObj. PortLoadBalancer not ready"); |
| 826 | String portLoadBalancerKey = String.valueOf(((XconnectLoadBalancerEndpoint) endpoint).key()); |
| 827 | portLoadBalancerCache.asMap().putIfAbsent(new PortLoadBalancerId(key.deviceId(), |
| 828 | Integer.parseInt(portLoadBalancerKey)), key); |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 829 | } else { |
| 830 | log.warn("Unable to create nextObj. Null NextTreatment"); |
| 831 | } |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 832 | return null; |
| 833 | } |
| 834 | nextObjBuilder.addTreatment(nextTreatment); |
| 835 | } |
| 836 | |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 837 | return nextObjBuilder; |
| 838 | } |
| 839 | |
| 840 | /** |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 841 | * Creates a next objective builder for XConnect. |
| 842 | * |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 843 | * @param key XConnect key |
| 844 | * @param endpoints Xconnect endpoints |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 845 | * @return next objective builder |
| 846 | */ |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 847 | private NextObjective.Builder nextObjBuilder(XconnectKey key, Set<XconnectEndpoint> endpoints) { |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 848 | int nextId = flowObjectiveService.allocateNextId(); |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 849 | return nextObjBuilder(key, endpoints, nextId); |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | |
| 853 | /** |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 854 | * Creates a bridging forwarding objective builder for XConnect. |
| 855 | * |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 856 | * @param key XConnect key |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 857 | * @param nextId next ID of the broadcast group for this XConnect key |
| 858 | * @return forwarding objective builder |
| 859 | */ |
| 860 | private ForwardingObjective.Builder fwdObjBuilder(XconnectKey key, int nextId) { |
| 861 | /* |
| 862 | * Driver should treat objectives with MacAddress.NONE and !VlanId.NONE |
| 863 | * as the VLAN cross-connect broadcast rules |
| 864 | */ |
| 865 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 866 | sbuilder.matchVlanId(key.vlanId()); |
| 867 | sbuilder.matchEthDst(MacAddress.NONE); |
| 868 | |
| 869 | ForwardingObjective.Builder fob = DefaultForwardingObjective.builder(); |
| 870 | fob.withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 871 | .withSelector(sbuilder.build()) |
| 872 | .nextStep(nextId) |
| 873 | .withPriority(XCONNECT_PRIORITY) |
| 874 | .fromApp(appId) |
| 875 | .makePermanent(); |
| 876 | return fob; |
| 877 | } |
| 878 | |
| 879 | /** |
| 880 | * Creates an ACL forwarding objective builder for XConnect. |
| 881 | * |
| 882 | * @param vlanId cross connect VLAN id |
| 883 | * @return forwarding objective builder |
| 884 | */ |
| 885 | private ForwardingObjective.Builder aclObjBuilder(VlanId vlanId) { |
| 886 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 887 | sbuilder.matchVlanId(vlanId); |
| 888 | |
| 889 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 890 | |
| 891 | ForwardingObjective.Builder fob = DefaultForwardingObjective.builder(); |
| 892 | fob.withFlag(ForwardingObjective.Flag.VERSATILE) |
| 893 | .withSelector(sbuilder.build()) |
| 894 | .withTreatment(tbuilder.build()) |
| 895 | .withPriority(XCONNECT_ACL_PRIORITY) |
| 896 | .fromApp(appId) |
| 897 | .makePermanent(); |
| 898 | return fob; |
| 899 | } |
| 900 | |
| 901 | /** |
| 902 | * Creates a filtering objective builder for XConnect. |
| 903 | * |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 904 | * @param key XConnect key |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 905 | * @param port XConnect ports |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 906 | * @param filtered true if this is a filtered port |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 907 | * @return next objective builder |
| 908 | */ |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 909 | private FilteringObjective.Builder filterObjBuilder(XconnectKey key, PortNumber port, boolean filtered) { |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 910 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 911 | fob.withKey(Criteria.matchInPort(port)) |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 912 | .addCondition(Criteria.matchEthDst(MacAddress.NONE)) |
| 913 | .withPriority(XCONNECT_PRIORITY); |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 914 | if (filtered) { |
| 915 | fob.addCondition(Criteria.matchVlanId(key.vlanId())); |
| 916 | } else { |
| 917 | fob.addCondition(Criteria.matchVlanId(VlanId.ANY)); |
| 918 | } |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 919 | return fob.permit().fromApp(appId); |
| 920 | } |
| 921 | |
| 922 | /** |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 923 | * Updates L2 flooding groups; add pair link into L2 flooding group of given xconnect vlan. |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 924 | * |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 925 | * @param deviceId Device ID |
| 926 | * @param port Port details |
| 927 | * @param vlanId VLAN ID |
| 928 | * @param install Whether to add or revoke pair link addition to flooding group |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 929 | */ |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 930 | private void updateL2Flooding(DeviceId deviceId, PortNumber port, VlanId vlanId, boolean install) { |
| 931 | XconnectKey key = new XconnectKey(deviceId, vlanId); |
| 932 | // Ensure leadership on device |
| 933 | if (!isLocalLeader(deviceId)) { |
| 934 | log.debug("Abort updating L2Flood {}: {}", key, ERROR_NOT_LEADER); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 935 | return; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 936 | } |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 937 | |
| 938 | // Locate L2 flooding group details for given xconnect vlan |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 939 | int nextId = Versioned.valueOrElse(xconnectNextObjStore.get(key), -1); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 940 | if (nextId == -1) { |
| 941 | log.debug("XConnect vlan {} broadcast group for device {} doesn't exists. " + |
| 942 | "Aborting pair group linking.", vlanId, deviceId); |
| 943 | return; |
| 944 | } |
| 945 | |
| 946 | // Add pairing-port group to flooding group |
| 947 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 948 | // treatment.popVlan(); |
| 949 | treatment.setOutput(port); |
| 950 | ObjectiveContext context = new DefaultObjectiveContext( |
| 951 | (objective) -> |
| 952 | log.debug("Pair port added/removed to vlan {} next objective {} on {}", |
| 953 | vlanId, nextId, deviceId), |
| 954 | (objective, error) -> |
| 955 | log.warn("Failed adding/removing pair port to vlan {} next objective {} on {}." + |
| 956 | "Error : {}", vlanId, nextId, deviceId, error) |
| 957 | ); |
| 958 | NextObjective.Builder vlanNextObjectiveBuilder = DefaultNextObjective.builder() |
| 959 | .withId(nextId) |
| 960 | .withType(NextObjective.Type.BROADCAST) |
| 961 | .fromApp(srService.appId()) |
| 962 | .withMeta(DefaultTrafficSelector.builder().matchVlanId(vlanId).build()) |
| 963 | .addTreatment(treatment.build()); |
| 964 | if (install) { |
| 965 | flowObjectiveService.next(deviceId, vlanNextObjectiveBuilder.addToExisting(context)); |
| 966 | } else { |
| 967 | flowObjectiveService.next(deviceId, vlanNextObjectiveBuilder.removeFromExisting(context)); |
| 968 | } |
| 969 | log.debug("Submitted next objective {} for vlan: {} in device {}", |
| 970 | nextId, vlanId, deviceId); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 971 | } |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 972 | |
| 973 | /** |
| 974 | * Populate L2 multicast rule on given deviceId that matches given mac, given vlan and |
| 975 | * output to given port's L2 mulitcast group. |
| 976 | * |
| 977 | * @param deviceId Device ID |
| 978 | * @param pairPort Pair port number |
| 979 | * @param vlanId VLAN ID |
| 980 | * @param accessPorts List of access ports to be added into L2 multicast group |
| 981 | */ |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 982 | private void populateL2Multicast(DeviceId deviceId, PortNumber pairPort, |
| 983 | VlanId vlanId, List<PortNumber> accessPorts) { |
| 984 | // Ensure enough rights to program pair device |
| 985 | if (!srService.shouldProgram(deviceId)) { |
| 986 | log.debug("Abort populate L2Multicast {}-{}: {}", deviceId, vlanId, ERROR_NOT_LEADER); |
| 987 | return; |
| 988 | } |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 989 | |
| 990 | boolean multicastGroupExists = true; |
| 991 | int vlanMulticastNextId; |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 992 | VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 993 | |
| 994 | // Step 1 : Populate single homed access ports into vlan's L2 multicast group |
| 995 | NextObjective.Builder vlanMulticastNextObjBuilder = DefaultNextObjective |
| 996 | .builder() |
| 997 | .withType(NextObjective.Type.BROADCAST) |
| 998 | .fromApp(srService.appId()) |
| 999 | .withMeta(DefaultTrafficSelector.builder().matchVlanId(vlanId) |
| 1000 | .matchEthDst(MacAddress.IPV4_MULTICAST).build()); |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1001 | vlanMulticastNextId = getMulticastGroupNextObjectiveId(key); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1002 | if (vlanMulticastNextId == -1) { |
| 1003 | // Vlan's L2 multicast group doesn't exist; create it, update store and add pair port as sub-group |
| 1004 | multicastGroupExists = false; |
| 1005 | vlanMulticastNextId = flowObjectiveService.allocateNextId(); |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1006 | addMulticastGroupNextObjectiveId(key, vlanMulticastNextId); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1007 | vlanMulticastNextObjBuilder.addTreatment( |
| 1008 | DefaultTrafficTreatment.builder().popVlan().setOutput(pairPort).build() |
| 1009 | ); |
| 1010 | } |
| 1011 | vlanMulticastNextObjBuilder.withId(vlanMulticastNextId); |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1012 | int nextId = vlanMulticastNextId; |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1013 | accessPorts.forEach(p -> { |
| 1014 | TrafficTreatment.Builder egressAction = DefaultTrafficTreatment.builder(); |
| 1015 | // Do vlan popup action based on interface configuration |
| 1016 | if (interfaceService.getInterfacesByPort(new ConnectPoint(deviceId, p)) |
| 1017 | .stream().noneMatch(i -> i.vlanTagged().contains(vlanId))) { |
| 1018 | egressAction.popVlan(); |
| 1019 | } |
| 1020 | egressAction.setOutput(p); |
| 1021 | vlanMulticastNextObjBuilder.addTreatment(egressAction.build()); |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1022 | addMulticastGroupPort(key, p); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1023 | }); |
| 1024 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1025 | (objective) -> |
| 1026 | log.debug("L2 multicast group installed/updated. " |
| 1027 | + "NextObject Id {} on {} for subnet {} ", |
| 1028 | nextId, deviceId, vlanId), |
| 1029 | (objective, error) -> |
| 1030 | log.warn("L2 multicast group failed to install/update. " |
| 1031 | + " NextObject Id {} on {} for subnet {} : {}", |
| 1032 | nextId, deviceId, vlanId, error) |
| 1033 | ); |
| 1034 | if (!multicastGroupExists) { |
| 1035 | flowObjectiveService.next(deviceId, vlanMulticastNextObjBuilder.add(context)); |
| 1036 | |
| 1037 | // Step 2 : Populate ACL rule; selector = vlan + pair-port, output = vlan L2 multicast group |
| 1038 | TrafficSelector.Builder multicastSelector = DefaultTrafficSelector.builder(); |
| 1039 | multicastSelector.matchEthType(Ethernet.TYPE_VLAN); |
| 1040 | multicastSelector.matchInPort(pairPort); |
| 1041 | multicastSelector.matchVlanId(vlanId); |
| 1042 | ForwardingObjective.Builder vlanMulticastForwardingObj = DefaultForwardingObjective.builder() |
| 1043 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 1044 | .nextStep(vlanMulticastNextId) |
| 1045 | .withSelector(multicastSelector.build()) |
| 1046 | .withPriority(100) |
| 1047 | .fromApp(srService.appId()) |
| 1048 | .makePermanent(); |
| 1049 | context = new DefaultObjectiveContext( |
| 1050 | (objective) -> log.debug("L2 multicasting versatile rule for device {}, port/vlan {}/{} populated", |
| 1051 | deviceId, |
| 1052 | pairPort, |
| 1053 | vlanId), |
| 1054 | (objective, error) -> log.warn("Failed to populate L2 multicasting versatile rule for device {}, " + |
| 1055 | "ports/vlan {}/{}: {}", deviceId, pairPort, vlanId, error)); |
| 1056 | flowObjectiveService.forward(deviceId, vlanMulticastForwardingObj.add(context)); |
| 1057 | } else { |
| 1058 | // L2_MULTICAST & BROADCAST are similar structure in subgroups; so going with BROADCAST type. |
| 1059 | vlanMulticastNextObjBuilder.withType(NextObjective.Type.BROADCAST); |
| 1060 | flowObjectiveService.next(deviceId, vlanMulticastNextObjBuilder.addToExisting(context)); |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | /** |
| 1065 | * Removes access ports from VLAN L2 multicast group on given deviceId. |
| 1066 | * |
| 1067 | * @param deviceId Device ID |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1068 | * @param vlanId VLAN ID |
| 1069 | * @param accessPorts List of access ports to be added into L2 multicast group |
| 1070 | */ |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1071 | private void revokeL2Multicast(DeviceId deviceId, VlanId vlanId, List<PortNumber> accessPorts) { |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1072 | // Ensure enough rights to program pair device |
| 1073 | if (!srService.shouldProgram(deviceId)) { |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1074 | log.debug("Abort revoke L2Multicast {}-{}: {}", deviceId, vlanId, ERROR_NOT_LEADER); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1075 | return; |
| 1076 | } |
| 1077 | |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1078 | VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId); |
| 1079 | |
| 1080 | int vlanMulticastNextId = getMulticastGroupNextObjectiveId(key); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1081 | if (vlanMulticastNextId == -1) { |
| 1082 | return; |
| 1083 | } |
| 1084 | NextObjective.Builder vlanMulticastNextObjBuilder = DefaultNextObjective |
| 1085 | .builder() |
| 1086 | .withType(NextObjective.Type.BROADCAST) |
| 1087 | .fromApp(srService.appId()) |
| 1088 | .withMeta(DefaultTrafficSelector.builder().matchVlanId(vlanId).build()) |
| 1089 | .withId(vlanMulticastNextId); |
| 1090 | accessPorts.forEach(p -> { |
| 1091 | TrafficTreatment.Builder egressAction = DefaultTrafficTreatment.builder(); |
| 1092 | // Do vlan popup action based on interface configuration |
| 1093 | if (interfaceService.getInterfacesByPort(new ConnectPoint(deviceId, p)) |
| 1094 | .stream().noneMatch(i -> i.vlanTagged().contains(vlanId))) { |
| 1095 | egressAction.popVlan(); |
| 1096 | } |
| 1097 | egressAction.setOutput(p); |
| 1098 | vlanMulticastNextObjBuilder.addTreatment(egressAction.build()); |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1099 | removeMulticastGroupPort(key, p); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1100 | }); |
| 1101 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1102 | (objective) -> |
| 1103 | log.debug("L2 multicast group installed/updated. " |
| 1104 | + "NextObject Id {} on {} for subnet {} ", |
| 1105 | vlanMulticastNextId, deviceId, vlanId), |
| 1106 | (objective, error) -> |
| 1107 | log.warn("L2 multicast group failed to install/update. " |
| 1108 | + " NextObject Id {} on {} for subnet {} : {}", |
| 1109 | vlanMulticastNextId, deviceId, vlanId, error) |
| 1110 | ); |
| 1111 | flowObjectiveService.next(deviceId, vlanMulticastNextObjBuilder.removeFromExisting(context)); |
| 1112 | } |
| 1113 | |
| 1114 | /** |
| 1115 | * Cleans up VLAN L2 multicast group on given deviceId. ACL rules for the group will also be deleted. |
| 1116 | * Normally multicast group is not removed if it contains access ports; which can be forced |
| 1117 | * by "force" flag |
| 1118 | * |
| 1119 | * @param deviceId Device ID |
| 1120 | * @param pairPort Pair port number |
| 1121 | * @param vlanId VLAN ID |
| 1122 | * @param force Forceful removal |
| 1123 | */ |
| 1124 | private void cleanupL2MulticastRule(DeviceId deviceId, PortNumber pairPort, VlanId vlanId, boolean force) { |
| 1125 | |
| 1126 | // Ensure enough rights to program pair device |
| 1127 | if (!srService.shouldProgram(deviceId)) { |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1128 | log.debug("Abort cleanup L2Multicast {}-{}: {}", deviceId, vlanId, ERROR_NOT_LEADER); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1129 | return; |
| 1130 | } |
| 1131 | |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1132 | VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId); |
| 1133 | |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1134 | // Ensure L2 multicast group doesn't contain access ports |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1135 | if (hasAccessPortInMulticastGroup(key, pairPort) && !force) { |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1136 | return; |
| 1137 | } |
| 1138 | |
| 1139 | // Load L2 multicast group details |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1140 | int vlanMulticastNextId = getMulticastGroupNextObjectiveId(key); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1141 | if (vlanMulticastNextId == -1) { |
| 1142 | return; |
| 1143 | } |
| 1144 | |
| 1145 | // Step 1 : Clear ACL rule; selector = vlan + pair-port, output = vlan L2 multicast group |
| 1146 | TrafficSelector.Builder l2MulticastSelector = DefaultTrafficSelector.builder(); |
| 1147 | l2MulticastSelector.matchEthType(Ethernet.TYPE_VLAN); |
| 1148 | l2MulticastSelector.matchInPort(pairPort); |
| 1149 | l2MulticastSelector.matchVlanId(vlanId); |
| 1150 | ForwardingObjective.Builder vlanMulticastForwardingObj = DefaultForwardingObjective.builder() |
| 1151 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 1152 | .nextStep(vlanMulticastNextId) |
| 1153 | .withSelector(l2MulticastSelector.build()) |
| 1154 | .withPriority(100) |
| 1155 | .fromApp(srService.appId()) |
| 1156 | .makePermanent(); |
| 1157 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1158 | (objective) -> log.debug("L2 multicasting rule for device {}, port/vlan {}/{} deleted", deviceId, |
| 1159 | pairPort, vlanId), |
| 1160 | (objective, error) -> log.warn("Failed to delete L2 multicasting rule for device {}, " + |
| 1161 | "ports/vlan {}/{}: {}", deviceId, pairPort, vlanId, error)); |
| 1162 | flowObjectiveService.forward(deviceId, vlanMulticastForwardingObj.remove(context)); |
| 1163 | |
| 1164 | // Step 2 : Clear L2 multicast group associated with vlan |
| 1165 | NextObjective.Builder l2MulticastGroupBuilder = DefaultNextObjective |
| 1166 | .builder() |
| 1167 | .withId(vlanMulticastNextId) |
| 1168 | .withType(NextObjective.Type.BROADCAST) |
| 1169 | .fromApp(srService.appId()) |
| 1170 | .withMeta(DefaultTrafficSelector.builder() |
| 1171 | .matchVlanId(vlanId) |
| 1172 | .matchEthDst(MacAddress.IPV4_MULTICAST).build()) |
| 1173 | .addTreatment(DefaultTrafficTreatment.builder().popVlan().setOutput(pairPort).build()); |
| 1174 | context = new DefaultObjectiveContext( |
| 1175 | (objective) -> |
| 1176 | log.debug("L2 multicast group with NextObject Id {} deleted on {} for subnet {} ", |
| 1177 | vlanMulticastNextId, deviceId, vlanId), |
| 1178 | (objective, error) -> |
| 1179 | log.warn("L2 multicast group with NextObject Id {} failed to delete on {} for subnet {} : {}", |
| 1180 | vlanMulticastNextId, deviceId, vlanId, error) |
| 1181 | ); |
| 1182 | flowObjectiveService.next(deviceId, l2MulticastGroupBuilder.remove(context)); |
| 1183 | |
| 1184 | // Finally clear store. |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1185 | removeMulticastGroup(key); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1186 | } |
| 1187 | |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1188 | private int getMulticastGroupNextObjectiveId(VlanNextObjectiveStoreKey key) { |
| 1189 | return Versioned.valueOrElse(xconnectMulticastNextStore.get(key), -1); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1190 | } |
| 1191 | |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1192 | private void addMulticastGroupNextObjectiveId(VlanNextObjectiveStoreKey key, int nextId) { |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1193 | if (nextId == -1) { |
| 1194 | return; |
| 1195 | } |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1196 | xconnectMulticastNextStore.put(key, nextId); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1197 | } |
| 1198 | |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1199 | private void addMulticastGroupPort(VlanNextObjectiveStoreKey groupKey, PortNumber port) { |
| 1200 | xconnectMulticastPortsStore.compute(groupKey, (key, ports) -> { |
| 1201 | if (ports == null) { |
| 1202 | ports = Lists.newArrayList(); |
| 1203 | } |
| 1204 | ports.add(port); |
| 1205 | return ports; |
| 1206 | }); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1207 | } |
| 1208 | |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1209 | private void removeMulticastGroupPort(VlanNextObjectiveStoreKey groupKey, PortNumber port) { |
| 1210 | xconnectMulticastPortsStore.compute(groupKey, (key, ports) -> { |
| 1211 | if (ports != null && !ports.isEmpty()) { |
| 1212 | ports.remove(port); |
| 1213 | } |
| 1214 | return ports; |
| 1215 | }); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1216 | } |
| 1217 | |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1218 | private void removeMulticastGroup(VlanNextObjectiveStoreKey groupKey) { |
| 1219 | xconnectMulticastPortsStore.remove(groupKey); |
| 1220 | xconnectMulticastNextStore.remove(groupKey); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1221 | } |
| 1222 | |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1223 | private boolean hasAccessPortInMulticastGroup(VlanNextObjectiveStoreKey groupKey, PortNumber pairPort) { |
| 1224 | List<PortNumber> ports = Versioned.valueOrElse(xconnectMulticastPortsStore.get(groupKey), ImmutableList.of()); |
jayakumarthazhath | 66c9ec1 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1225 | return ports.stream().anyMatch(p -> !p.equals(pairPort)); |
| 1226 | } |
| 1227 | |
pier | 6fd24fd | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1228 | // Custom-built function, when the device is not available we need a fallback mechanism |
| 1229 | private boolean isLocalLeader(DeviceId deviceId) { |
| 1230 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 1231 | // When the device is available we just check the mastership |
| 1232 | if (deviceService.isAvailable(deviceId)) { |
| 1233 | return false; |
| 1234 | } |
| 1235 | // Fallback with Leadership service - device id is used as topic |
| 1236 | NodeId leader = leadershipService.runForLeadership( |
| 1237 | deviceId.toString()).leaderNodeId(); |
| 1238 | // Verify if this node is the leader |
| 1239 | return clusterService.getLocalNode().id().equals(leader); |
| 1240 | } |
| 1241 | return true; |
| 1242 | } |
| 1243 | |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 1244 | private Set<PortNumber> getPhysicalPorts(DeviceId deviceId, XconnectEndpoint endpoint) { |
| 1245 | if (endpoint.type() == XconnectEndpoint.Type.PORT) { |
| 1246 | PortNumber port = ((XconnectPortEndpoint) endpoint).port(); |
| 1247 | return Sets.newHashSet(port); |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 1248 | } |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 1249 | if (endpoint.type() == XconnectEndpoint.Type.LOAD_BALANCER) { |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 1250 | PortLoadBalancerId portLoadBalancerId = new PortLoadBalancerId(deviceId, |
| 1251 | ((XconnectLoadBalancerEndpoint) endpoint).key()); |
| 1252 | Set<PortNumber> ports = portLoadBalancerService.getPortLoadBalancer(portLoadBalancerId).ports(); |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 1253 | return Sets.newHashSet(ports); |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 1254 | } |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 1255 | return Sets.newHashSet(); |
| 1256 | } |
| 1257 | |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 1258 | private NextTreatment getNextTreatment(DeviceId deviceId, XconnectEndpoint endpoint, boolean reserve) { |
| 1259 | if (endpoint.type() == XconnectEndpoint.Type.PORT) { |
| 1260 | PortNumber port = ((XconnectPortEndpoint) endpoint).port(); |
| 1261 | return DefaultNextTreatment.of(DefaultTrafficTreatment.builder().setOutput(port).build()); |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 1262 | } |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 1263 | if (endpoint.type() == XconnectEndpoint.Type.LOAD_BALANCER) { |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 1264 | PortLoadBalancerId portLoadBalancerId = new PortLoadBalancerId(deviceId, |
| 1265 | ((XconnectLoadBalancerEndpoint) endpoint).key()); |
| 1266 | NextTreatment idNextTreatment = IdNextTreatment.of(portLoadBalancerService |
| 1267 | .getPortLoadBalancerNext(portLoadBalancerId)); |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 1268 | // Reserve only one time during next objective creation |
| 1269 | if (reserve) { |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 1270 | if (!portLoadBalancerService.reserve(portLoadBalancerId, appId)) { |
| 1271 | log.warn("Reservation failed for {}", portLoadBalancerId); |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 1272 | idNextTreatment = null; |
| 1273 | } |
| 1274 | } |
| 1275 | return idNextTreatment; |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 1276 | } |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 1277 | return null; |
| 1278 | } |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 1279 | |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 1280 | private class InternalPortLoadBalancerListener implements PortLoadBalancerListener { |
| 1281 | // Populate xconnect once portloadbalancer is available |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 1282 | @Override |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 1283 | public void event(PortLoadBalancerEvent event) { |
| 1284 | portLoadBalancerExecutor.execute(() -> dequeue(event.subject().portLoadBalancerId())); |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 1285 | } |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 1286 | // When we receive INSTALLED port load balancing is ready |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 1287 | @Override |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 1288 | public boolean isRelevant(PortLoadBalancerEvent event) { |
| 1289 | return event.type() == PortLoadBalancerEvent.Type.INSTALLED; |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | // Invalidate the cache and re-start the xconnect installation |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 1294 | private void dequeue(PortLoadBalancerId portLoadBalancerId) { |
| 1295 | XconnectKey xconnectKey = portLoadBalancerCache.getIfPresent(portLoadBalancerId); |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 1296 | if (xconnectKey == null) { |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 1297 | log.trace("{} not present in the cache", portLoadBalancerId); |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 1298 | return; |
| 1299 | } |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 1300 | log.debug("Dequeue {}", portLoadBalancerId); |
| 1301 | portLoadBalancerCache.invalidate(portLoadBalancerId); |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 1302 | Set<XconnectEndpoint> endpoints = Versioned.valueOrNull(xconnectStore.get(xconnectKey)); |
| 1303 | if (endpoints == null || endpoints.isEmpty()) { |
| 1304 | log.warn("Endpoints not found for XConnect {}", xconnectKey); |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 1305 | return; |
| 1306 | } |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 1307 | populateXConnect(xconnectKey, endpoints); |
Charles Chan | c0a499b | 2019-01-16 15:30:39 -0800 | [diff] [blame^] | 1308 | log.trace("PortLoadBalancer cache size {}", portLoadBalancerCache.size()); |
pier | 567465b | 2018-11-24 11:16:28 -0800 | [diff] [blame] | 1309 | } |
| 1310 | |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 1311 | } |