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