blob: 7a579b2594b774a310e91363d822b91dee0431ae [file] [log] [blame]
Charles Chan8d316332018-06-19 20:31:57 -07001/*
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 */
16package org.onosproject.segmentrouting.xconnect.impl;
17
pier6fd24fd2018-11-27 11:23:50 -080018import com.google.common.collect.ImmutableList;
pier567465b2018-11-24 11:16:28 -080019import com.google.common.cache.Cache;
20import com.google.common.cache.CacheBuilder;
21import com.google.common.cache.RemovalNotification;
Charles Chan0b1dd7e2018-08-19 19:21:46 -070022import com.google.common.collect.ImmutableMap;
Charles Chan8d316332018-06-19 20:31:57 -070023import com.google.common.collect.ImmutableSet;
pier6fd24fd2018-11-27 11:23:50 -080024import com.google.common.collect.Lists;
Charles Chan8d316332018-06-19 20:31:57 -070025import com.google.common.collect.Sets;
jayakumarthazhath66c9ec12018-10-01 00:51:54 +053026import org.onlab.packet.Ethernet;
Charles Chan8d316332018-06-19 20:31:57 -070027import org.onlab.packet.MacAddress;
28import org.onlab.packet.VlanId;
29import org.onlab.util.KryoNamespace;
pier6fd24fd2018-11-27 11:23:50 -080030import org.onosproject.cluster.ClusterService;
31import org.onosproject.cluster.LeadershipService;
32import org.onosproject.cluster.NodeId;
Charles Chan8d316332018-06-19 20:31:57 -070033import org.onosproject.codec.CodecService;
34import org.onosproject.core.ApplicationId;
35import org.onosproject.core.CoreService;
Charles Chanc0a499b2019-01-16 15:30:39 -080036import org.onosproject.portloadbalancer.api.PortLoadBalancerEvent;
37import org.onosproject.portloadbalancer.api.PortLoadBalancerId;
38import org.onosproject.portloadbalancer.api.PortLoadBalancerListener;
39import org.onosproject.portloadbalancer.api.PortLoadBalancerService;
Charles Chan8d316332018-06-19 20:31:57 -070040import org.onosproject.mastership.MastershipService;
41import org.onosproject.net.ConnectPoint;
42import org.onosproject.net.DeviceId;
jayakumarthazhath66c9ec12018-10-01 00:51:54 +053043import org.onosproject.net.Host;
44import org.onosproject.net.HostLocation;
Charles Chan8d316332018-06-19 20:31:57 -070045import org.onosproject.net.PortNumber;
46import org.onosproject.net.config.NetworkConfigService;
47import org.onosproject.net.device.DeviceEvent;
48import org.onosproject.net.device.DeviceListener;
49import org.onosproject.net.device.DeviceService;
50import org.onosproject.net.flow.DefaultTrafficSelector;
51import org.onosproject.net.flow.DefaultTrafficTreatment;
52import org.onosproject.net.flow.TrafficSelector;
53import org.onosproject.net.flow.TrafficTreatment;
54import org.onosproject.net.flow.criteria.Criteria;
55import org.onosproject.net.flowobjective.DefaultFilteringObjective;
56import org.onosproject.net.flowobjective.DefaultForwardingObjective;
57import org.onosproject.net.flowobjective.DefaultNextObjective;
Charles Chan48df9ad2018-10-30 18:08:59 -070058import org.onosproject.net.flowobjective.DefaultNextTreatment;
Charles Chan8d316332018-06-19 20:31:57 -070059import org.onosproject.net.flowobjective.DefaultObjectiveContext;
60import org.onosproject.net.flowobjective.FilteringObjective;
61import org.onosproject.net.flowobjective.FlowObjectiveService;
62import org.onosproject.net.flowobjective.ForwardingObjective;
Charles Chan48df9ad2018-10-30 18:08:59 -070063import org.onosproject.net.flowobjective.IdNextTreatment;
Charles Chan8d316332018-06-19 20:31:57 -070064import org.onosproject.net.flowobjective.NextObjective;
Charles Chan48df9ad2018-10-30 18:08:59 -070065import org.onosproject.net.flowobjective.NextTreatment;
Charles Chan8d316332018-06-19 20:31:57 -070066import org.onosproject.net.flowobjective.Objective;
67import org.onosproject.net.flowobjective.ObjectiveContext;
68import org.onosproject.net.flowobjective.ObjectiveError;
jayakumarthazhath66c9ec12018-10-01 00:51:54 +053069import org.onosproject.net.host.HostEvent;
70import org.onosproject.net.host.HostListener;
71import org.onosproject.net.host.HostService;
72import org.onosproject.net.intf.InterfaceService;
Charles Chan8d316332018-06-19 20:31:57 -070073import org.onosproject.segmentrouting.SegmentRoutingService;
jayakumarthazhath66c9ec12018-10-01 00:51:54 +053074import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chan8d316332018-06-19 20:31:57 -070075import org.onosproject.segmentrouting.xconnect.api.XconnectCodec;
76import org.onosproject.segmentrouting.xconnect.api.XconnectDesc;
Charles Chan445659f2019-01-02 13:46:16 -080077import org.onosproject.segmentrouting.xconnect.api.XconnectEndpoint;
Charles Chan8d316332018-06-19 20:31:57 -070078import org.onosproject.segmentrouting.xconnect.api.XconnectKey;
Charles Chan445659f2019-01-02 13:46:16 -080079import org.onosproject.segmentrouting.xconnect.api.XconnectLoadBalancerEndpoint;
80import org.onosproject.segmentrouting.xconnect.api.XconnectPortEndpoint;
Charles Chan8d316332018-06-19 20:31:57 -070081import org.onosproject.segmentrouting.xconnect.api.XconnectService;
82import org.onosproject.store.serializers.KryoNamespaces;
83import org.onosproject.store.service.ConsistentMap;
84import org.onosproject.store.service.MapEvent;
85import org.onosproject.store.service.MapEventListener;
86import org.onosproject.store.service.Serializer;
87import org.onosproject.store.service.StorageService;
88import org.onosproject.store.service.Versioned;
Ray Milkey2bd24a92018-08-17 14:54:17 -070089import org.osgi.service.component.annotations.Activate;
90import org.osgi.service.component.annotations.Component;
91import org.osgi.service.component.annotations.Deactivate;
92import org.osgi.service.component.annotations.Reference;
93import org.osgi.service.component.annotations.ReferenceCardinality;
Charles Chan8d316332018-06-19 20:31:57 -070094import org.slf4j.Logger;
95import org.slf4j.LoggerFactory;
96
97import java.io.Serializable;
jayakumarthazhath66c9ec12018-10-01 00:51:54 +053098import java.util.Collections;
99import java.util.List;
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530100import java.util.Optional;
Charles Chan8d316332018-06-19 20:31:57 -0700101import java.util.Set;
102import java.util.concurrent.CompletableFuture;
Charles Chan56542b62018-08-07 12:48:36 -0700103import java.util.concurrent.ExecutorService;
104import java.util.concurrent.Executors;
pier567465b2018-11-24 11:16:28 -0800105import java.util.concurrent.ScheduledExecutorService;
106import java.util.concurrent.TimeUnit;
Charles Chan8d316332018-06-19 20:31:57 -0700107import java.util.function.BiConsumer;
108import java.util.function.Consumer;
109import java.util.stream.Collectors;
110
pier567465b2018-11-24 11:16:28 -0800111import static java.util.concurrent.Executors.newScheduledThreadPool;
Charles Chan56542b62018-08-07 12:48:36 -0700112import static org.onlab.util.Tools.groupedThreads;
113
Ray Milkey2bd24a92018-08-17 14:54:17 -0700114@Component(immediate = true, service = XconnectService.class)
Charles Chan8d316332018-06-19 20:31:57 -0700115public class XconnectManager implements XconnectService {
Ray Milkey2bd24a92018-08-17 14:54:17 -0700116 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan8d316332018-06-19 20:31:57 -0700117 private CoreService coreService;
118
Ray Milkey2bd24a92018-08-17 14:54:17 -0700119 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan8d316332018-06-19 20:31:57 -0700120 private CodecService codecService;
121
Ray Milkey2bd24a92018-08-17 14:54:17 -0700122 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan8d316332018-06-19 20:31:57 -0700123 private StorageService storageService;
124
Ray Milkey2bd24a92018-08-17 14:54:17 -0700125 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan8d316332018-06-19 20:31:57 -0700126 public NetworkConfigService netCfgService;
127
Ray Milkey2bd24a92018-08-17 14:54:17 -0700128 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan8d316332018-06-19 20:31:57 -0700129 public DeviceService deviceService;
130
Ray Milkey2bd24a92018-08-17 14:54:17 -0700131 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan8d316332018-06-19 20:31:57 -0700132 public FlowObjectiveService flowObjectiveService;
133
Ray Milkey2bd24a92018-08-17 14:54:17 -0700134 @Reference(cardinality = ReferenceCardinality.MANDATORY)
pier6fd24fd2018-11-27 11:23:50 -0800135 private LeadershipService leadershipService;
136
137 @Reference(cardinality = ReferenceCardinality.MANDATORY)
138 private ClusterService clusterService;
139
140 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan8d316332018-06-19 20:31:57 -0700141 public MastershipService mastershipService;
142
Ray Milkey2bd24a92018-08-17 14:54:17 -0700143 @Reference(cardinality = ReferenceCardinality.OPTIONAL)
Charles Chan8d316332018-06-19 20:31:57 -0700144 public SegmentRoutingService srService;
145
Ray Milkey3cad4db2018-10-04 15:13:33 -0700146 @Reference(cardinality = ReferenceCardinality.MANDATORY)
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530147 public InterfaceService interfaceService;
148
Ray Milkey3cad4db2018-10-04 15:13:33 -0700149 @Reference(cardinality = ReferenceCardinality.MANDATORY)
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530150 HostService hostService;
151
Charles Chan48df9ad2018-10-30 18:08:59 -0700152 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chanc0a499b2019-01-16 15:30:39 -0800153 private PortLoadBalancerService portLoadBalancerService;
Charles Chan48df9ad2018-10-30 18:08:59 -0700154
Charles Chan8d316332018-06-19 20:31:57 -0700155 private static final String APP_NAME = "org.onosproject.xconnect";
pier6fd24fd2018-11-27 11:23:50 -0800156 private static final String ERROR_NOT_LEADER = "Not leader controller";
Charles Chan48df9ad2018-10-30 18:08:59 -0700157 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 Chan8d316332018-06-19 20:31:57 -0700159
160 private static Logger log = LoggerFactory.getLogger(XconnectManager.class);
161
162 private ApplicationId appId;
Charles Chan445659f2019-01-02 13:46:16 -0800163 private ConsistentMap<XconnectKey, Set<XconnectEndpoint>> xconnectStore;
Charles Chan1fb65132018-09-21 11:29:12 -0700164 private ConsistentMap<XconnectKey, Integer> xconnectNextObjStore;
Charles Chan8d316332018-06-19 20:31:57 -0700165
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530166 private ConsistentMap<VlanNextObjectiveStoreKey, Integer> xconnectMulticastNextStore;
167 private ConsistentMap<VlanNextObjectiveStoreKey, List<PortNumber>> xconnectMulticastPortsStore;
168
Charles Chan445659f2019-01-02 13:46:16 -0800169 private final MapEventListener<XconnectKey, Set<XconnectEndpoint>> xconnectListener = new XconnectMapListener();
pier6fd24fd2018-11-27 11:23:50 -0800170 private ExecutorService xConnectExecutor;
Charles Chan8d316332018-06-19 20:31:57 -0700171
pier6fd24fd2018-11-27 11:23:50 -0800172 private final DeviceListener deviceListener = new InternalDeviceListener();
Charles Chan56542b62018-08-07 12:48:36 -0700173 private ExecutorService deviceEventExecutor;
174
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530175 private final HostListener hostListener = new InternalHostListener();
176 private ExecutorService hostEventExecutor;
177
pier567465b2018-11-24 11:16:28 -0800178 // Wait time for the cache
179 private static final int WAIT_TIME_MS = 15000;
Charles Chanc0a499b2019-01-16 15:30:39 -0800180 //The cache is implemented as buffer for waiting the installation of PortLoadBalancer when present
181 private Cache<PortLoadBalancerId, XconnectKey> portLoadBalancerCache;
pier567465b2018-11-24 11:16:28 -0800182 // Executor for the cache
Charles Chanc0a499b2019-01-16 15:30:39 -0800183 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();
pier567465b2018-11-24 11:16:28 -0800186
Charles Chan8d316332018-06-19 20:31:57 -0700187 @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 Chanfbaad962018-07-23 12:53:16 -0700194 .register(XconnectManager.class)
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530195 .register(XconnectKey.class)
Charles Chan445659f2019-01-02 13:46:16 -0800196 .register(XconnectEndpoint.class)
197 .register(XconnectPortEndpoint.class)
198 .register(XconnectLoadBalancerEndpoint.class)
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530199 .register(VlanNextObjectiveStoreKey.class);
Charles Chan8d316332018-06-19 20:31:57 -0700200
Charles Chan445659f2019-01-02 13:46:16 -0800201 xconnectStore = storageService.<XconnectKey, Set<XconnectEndpoint>>consistentMapBuilder()
Charles Chan8d316332018-06-19 20:31:57 -0700202 .withName("onos-sr-xconnect")
203 .withRelaxedReadConsistency()
204 .withSerializer(Serializer.using(serializer.build()))
205 .build();
pier6fd24fd2018-11-27 11:23:50 -0800206 xConnectExecutor = Executors.newSingleThreadScheduledExecutor(
207 groupedThreads("sr-xconnect-event", "%d", log));
208 xconnectStore.addListener(xconnectListener, xConnectExecutor);
Charles Chan8d316332018-06-19 20:31:57 -0700209
Charles Chan1fb65132018-09-21 11:29:12 -0700210 xconnectNextObjStore = storageService.<XconnectKey, Integer>consistentMapBuilder()
Charles Chan8d316332018-06-19 20:31:57 -0700211 .withName("onos-sr-xconnect-next")
212 .withRelaxedReadConsistency()
213 .withSerializer(Serializer.using(serializer.build()))
214 .build();
215
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530216 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 Chan56542b62018-08-07 12:48:36 -0700225 deviceEventExecutor = Executors.newSingleThreadScheduledExecutor(
226 groupedThreads("sr-xconnect-device-event", "%d", log));
Charles Chan8d316332018-06-19 20:31:57 -0700227 deviceService.addListener(deviceListener);
228
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530229 hostEventExecutor = Executors.newSingleThreadExecutor(
230 groupedThreads("sr-xconnect-host-event", "%d", log));
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530231 hostService.addListener(hostListener);
232
Charles Chanc0a499b2019-01-16 15:30:39 -0800233 portLoadBalancerCache = CacheBuilder.newBuilder()
pier567465b2018-11-24 11:16:28 -0800234 .expireAfterWrite(WAIT_TIME_MS, TimeUnit.MILLISECONDS)
Charles Chanc0a499b2019-01-16 15:30:39 -0800235 .removalListener((RemovalNotification<PortLoadBalancerId, XconnectKey> notification) ->
236 log.debug("PortLoadBalancer cache removal event. portLoadBalancerId={}, xConnectKey={}",
pier567465b2018-11-24 11:16:28 -0800237 notification.getKey(), notification.getValue())).build();
Charles Chanc0a499b2019-01-16 15:30:39 -0800238 portLoadBalancerExecutor = newScheduledThreadPool(1,
239 groupedThreads("portLoadBalancerCacheWorker", "-%d", log));
pier567465b2018-11-24 11:16:28 -0800240 // Let's schedule the cleanup of the cache
Charles Chanc0a499b2019-01-16 15:30:39 -0800241 portLoadBalancerExecutor.scheduleAtFixedRate(portLoadBalancerCache::cleanUp, 0,
pier567465b2018-11-24 11:16:28 -0800242 WAIT_TIME_MS, TimeUnit.MILLISECONDS);
Charles Chanc0a499b2019-01-16 15:30:39 -0800243 portLoadBalancerService.addListener(portLoadBalancerListener);
pier567465b2018-11-24 11:16:28 -0800244
Charles Chan8d316332018-06-19 20:31:57 -0700245 log.info("Started");
246 }
247
248 @Deactivate
249 void deactivate() {
250 xconnectStore.removeListener(xconnectListener);
251 deviceService.removeListener(deviceListener);
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530252 hostService.removeListener(hostListener);
Charles Chan8d316332018-06-19 20:31:57 -0700253 codecService.unregisterCodec(XconnectDesc.class);
254
Charles Chan56542b62018-08-07 12:48:36 -0700255 deviceEventExecutor.shutdown();
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530256 hostEventExecutor.shutdown();
pier6fd24fd2018-11-27 11:23:50 -0800257 xConnectExecutor.shutdown();
Charles Chanc0a499b2019-01-16 15:30:39 -0800258 portLoadBalancerExecutor.shutdown();
Charles Chan56542b62018-08-07 12:48:36 -0700259
Charles Chan8d316332018-06-19 20:31:57 -0700260 log.info("Stopped");
261 }
262
263 @Override
Charles Chan445659f2019-01-02 13:46:16 -0800264 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 Chan8d316332018-06-19 20:31:57 -0700267 final XconnectKey key = new XconnectKey(deviceId, vlanId);
Charles Chan445659f2019-01-02 13:46:16 -0800268 xconnectStore.put(key, endpoints);
Charles Chan8d316332018-06-19 20:31:57 -0700269 }
270
271 @Override
272 public void removeXonnect(DeviceId deviceId, VlanId vlanId) {
273 log.info("Removing xconnect. deviceId={}, vlanId={}",
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530274 deviceId, vlanId);
Charles Chan8d316332018-06-19 20:31:57 -0700275 final XconnectKey key = new XconnectKey(deviceId, vlanId);
276 xconnectStore.remove(key);
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530277
278 // Cleanup multicasting support, if any.
Charles Chan445659f2019-01-02 13:46:16 -0800279 srService.getPairDeviceId(deviceId).ifPresent(pairDeviceId ->
280 cleanupL2MulticastRule(pairDeviceId, srService.getPairLocalPort(pairDeviceId).get(), vlanId, true)
281 );
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530282
Charles Chan8d316332018-06-19 20:31:57 -0700283 }
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 Chan445659f2019-01-02 13:46:16 -0800294 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 Chan8d316332018-06-19 20:31:57 -0700298 );
299 }
300
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700301 @Override
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530302 public List<VlanId> getXconnectVlans(DeviceId deviceId, PortNumber port) {
303 return getXconnects().stream()
Charles Chan445659f2019-01-02 13:46:16 -0800304 .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)
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530308 .collect(Collectors.toList());
309 }
310
311 @Override
312 public boolean isXconnectVlan(DeviceId deviceId, VlanId vlanId) {
pier6fd24fd2018-11-27 11:23:50 -0800313 XconnectKey key = new XconnectKey(deviceId, vlanId);
314 return Versioned.valueOrNull(xconnectStore.get(key)) != null;
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530315 }
316
317 @Override
Charles Chan1fb65132018-09-21 11:29:12 -0700318 public ImmutableMap<XconnectKey, Integer> getNext() {
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700319 if (xconnectNextObjStore != null) {
320 return ImmutableMap.copyOf(xconnectNextObjStore.asJavaMap());
321 } else {
322 return ImmutableMap.of();
323 }
324 }
325
326 @Override
pier6fd24fd2018-11-27 11:23:50 -0800327 public int getNextId(DeviceId deviceId, VlanId vlanId) {
328 return Versioned.valueOrElse(xconnectNextObjStore.get(new XconnectKey(deviceId, vlanId)), -1);
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530329 }
330
331 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700332 public void removeNextId(int nextId) {
333 xconnectNextObjStore.entrySet().forEach(e -> {
Charles Chan1fb65132018-09-21 11:29:12 -0700334 if (e.getValue().value() == nextId) {
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700335 xconnectNextObjStore.remove(e.getKey());
336 }
337 });
338 }
339
Charles Chan445659f2019-01-02 13:46:16 -0800340 private class XconnectMapListener implements MapEventListener<XconnectKey, Set<XconnectEndpoint>> {
Charles Chan8d316332018-06-19 20:31:57 -0700341 @Override
Charles Chan445659f2019-01-02 13:46:16 -0800342 public void event(MapEvent<XconnectKey, Set<XconnectEndpoint>> event) {
Charles Chan8d316332018-06-19 20:31:57 -0700343 XconnectKey key = event.key();
Charles Chan445659f2019-01-02 13:46:16 -0800344 Set<XconnectEndpoint> ports = Versioned.valueOrNull(event.newValue());
345 Set<XconnectEndpoint> oldPorts = Versioned.valueOrNull(event.oldValue());
Charles Chan8d316332018-06-19 20:31:57 -0700346
347 switch (event.type()) {
348 case INSERT:
Charles Chan48df9ad2018-10-30 18:08:59 -0700349 populateXConnect(key, ports);
Charles Chan8d316332018-06-19 20:31:57 -0700350 break;
351 case UPDATE:
Charles Chan48df9ad2018-10-30 18:08:59 -0700352 updateXConnect(key, oldPorts, ports);
Charles Chan8d316332018-06-19 20:31:57 -0700353 break;
354 case REMOVE:
Charles Chan48df9ad2018-10-30 18:08:59 -0700355 revokeXConnect(key, oldPorts);
Charles Chan8d316332018-06-19 20:31:57 -0700356 break;
357 default:
358 break;
359 }
360 }
361 }
362
363 private class InternalDeviceListener implements DeviceListener {
pier6fd24fd2018-11-27 11:23:50 -0800364 // Offload the execution to an executor and then process the event
365 // if this instance is the leader of the device
Charles Chan8d316332018-06-19 20:31:57 -0700366 @Override
367 public void event(DeviceEvent event) {
Charles Chan56542b62018-08-07 12:48:36 -0700368 deviceEventExecutor.execute(() -> {
369 DeviceId deviceId = event.subject().id();
pier6fd24fd2018-11-27 11:23:50 -0800370 // Just skip if we are not the leader
371 if (!isLocalLeader(deviceId)) {
372 log.debug("Not the leader of {}. Skip event {}", deviceId, event);
Charles Chan56542b62018-08-07 12:48:36 -0700373 return;
374 }
pier6fd24fd2018-11-27 11:23:50 -0800375 // Populate or revoke according to the device availability
376 if (deviceService.isAvailable(deviceId)) {
377 init(deviceId);
378 } else {
379 cleanup(deviceId);
Charles Chan56542b62018-08-07 12:48:36 -0700380 }
381 });
Charles Chan8d316332018-06-19 20:31:57 -0700382 }
pier6fd24fd2018-11-27 11:23:50 -0800383 // 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 Chan8d316332018-06-19 20:31:57 -0700390 }
391
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530392 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())));
pier6fd24fd2018-11-27 11:23:50 -0800429 // Ensure pair-port attached to xconnect vlan flooding group
430 // at dual home failed device.
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530431 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()))
pier6fd24fd2018-11-27 11:23:50 -0800450 .forEach(prevLocation -> revokeL2Multicast(
451 prevLocation.deviceId(),
452 xconnectVlan,
453 Collections.singletonList(newLocation.port()))
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530454 );
455
pier6fd24fd2018-11-27 11:23:50 -0800456 // 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())) {
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530460 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 Chan1fb65132018-09-21 11:29:12 -0700484 private void init(DeviceId deviceId) {
Charles Chan8d316332018-06-19 20:31:57 -0700485 getXconnects().stream()
486 .filter(desc -> desc.key().deviceId().equals(deviceId))
Charles Chan445659f2019-01-02 13:46:16 -0800487 .forEach(desc -> populateXConnect(desc.key(), desc.endpoints()));
Charles Chan8d316332018-06-19 20:31:57 -0700488 }
489
Charles Chan1fb65132018-09-21 11:29:12 -0700490 private void cleanup(DeviceId deviceId) {
Charles Chan8d316332018-06-19 20:31:57 -0700491 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 Chan445659f2019-01-02 13:46:16 -0800500 * @param key XConnect key
501 * @param endpoints a set of endpoints to be cross-connected
Charles Chan8d316332018-06-19 20:31:57 -0700502 */
Charles Chan445659f2019-01-02 13:46:16 -0800503 private void populateXConnect(XconnectKey key, Set<XconnectEndpoint> endpoints) {
pier6fd24fd2018-11-27 11:23:50 -0800504 if (!isLocalLeader(key.deviceId())) {
505 log.debug("Abort populating XConnect {}: {}", key, ERROR_NOT_LEADER);
Charles Chan8d316332018-06-19 20:31:57 -0700506 return;
507 }
508
Charles Chan445659f2019-01-02 13:46:16 -0800509 int nextId = populateNext(key, endpoints);
Charles Chan48df9ad2018-10-30 18:08:59 -0700510 if (nextId == -1) {
511 log.warn("Fail to populateXConnect {}: {}", key, ERROR_NEXT_ID);
512 return;
513 }
Charles Chan445659f2019-01-02 13:46:16 -0800514 populateFilter(key, endpoints);
Charles Chan48df9ad2018-10-30 18:08:59 -0700515 populateFwd(key, nextId);
Charles Chan8d316332018-06-19 20:31:57 -0700516 populateAcl(key);
517 }
518
519 /**
520 * Populates filtering objectives for given XConnect.
521 *
Charles Chan445659f2019-01-02 13:46:16 -0800522 * @param key XConnect store key
523 * @param endpoints XConnect endpoints
Charles Chan8d316332018-06-19 20:31:57 -0700524 */
Charles Chan445659f2019-01-02 13:46:16 -0800525 private void populateFilter(XconnectKey key, Set<XconnectEndpoint> endpoints) {
Charles Chan48df9ad2018-10-30 18:08:59 -0700526 // FIXME Improve the logic
Charles Chanc0a499b2019-01-16 15:30:39 -0800527 // If port load balancer is not involved, use filtered port. Otherwise, use unfiltered port.
Charles Chan48df9ad2018-10-30 18:08:59 -0700528 // The purpose is to make sure existing XConnect logic can still work on a configured port.
Charles Chan445659f2019-01-02 13:46:16 -0800529 boolean filtered = endpoints.stream()
530 .map(ep -> getNextTreatment(key.deviceId(), ep, false))
Charles Chan48df9ad2018-10-30 18:08:59 -0700531 .allMatch(t -> t.type().equals(NextTreatment.Type.TREATMENT));
532
Charles Chan445659f2019-01-02 13:46:16 -0800533 endpoints.stream()
534 .map(ep -> getPhysicalPorts(key.deviceId(), ep))
Charles Chan48df9ad2018-10-30 18:08:59 -0700535 .flatMap(Set::stream).forEach(port -> {
Charles Chan445659f2019-01-02 13:46:16 -0800536 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 Chan8d316332018-06-19 20:31:57 -0700545 }
546
547 /**
548 * Populates next objectives for given XConnect.
549 *
Charles Chan445659f2019-01-02 13:46:16 -0800550 * @param key XConnect store key
551 * @param endpoints XConnect endpoints
Charles Chan48df9ad2018-10-30 18:08:59 -0700552 * @return next id
Charles Chan8d316332018-06-19 20:31:57 -0700553 */
Charles Chan445659f2019-01-02 13:46:16 -0800554 private int populateNext(XconnectKey key, Set<XconnectEndpoint> endpoints) {
pier6fd24fd2018-11-27 11:23:50 -0800555 int nextId = Versioned.valueOrElse(xconnectNextObjStore.get(key), -1);
556 if (nextId != -1) {
Charles Chan1fb65132018-09-21 11:29:12 -0700557 log.debug("NextObj for {} found, id={}", key, nextId);
558 return nextId;
Charles Chan8d316332018-06-19 20:31:57 -0700559 } else {
Charles Chan445659f2019-01-02 13:46:16 -0800560 NextObjective.Builder nextObjBuilder = nextObjBuilder(key, endpoints);
Charles Chan48df9ad2018-10-30 18:08:59 -0700561 if (nextObjBuilder == null) {
562 log.warn("Fail to populate {}: {}", key, ERROR_NEXT_OBJ_BUILDER);
563 return -1;
564 }
Charles Chan8d316332018-06-19 20:31:57 -0700565 ObjectiveContext nextContext = new DefaultObjectiveContext(
566 // To serialize this with kryo
567 (Serializable & Consumer<Objective>) (objective) ->
568 log.debug("XConnect NextObj for {} added", key),
Charles Chanfacfbef2018-08-23 14:30:33 -0700569 (Serializable & BiConsumer<Objective, ObjectiveError>) (objective, error) -> {
570 log.warn("Failed to add XConnect NextObj for {}: {}", key, error);
571 srService.invalidateNextObj(objective.id());
572 });
Charles Chan1fb65132018-09-21 11:29:12 -0700573 NextObjective nextObj = nextObjBuilder.add(nextContext);
Charles Chan8d316332018-06-19 20:31:57 -0700574 flowObjectiveService.next(key.deviceId(), nextObj);
Charles Chan1fb65132018-09-21 11:29:12 -0700575 xconnectNextObjStore.put(key, nextObj.id());
Charles Chan8d316332018-06-19 20:31:57 -0700576 log.debug("NextObj for {} not found. Creating new NextObj with id={}", key, nextObj.id());
Charles Chan1fb65132018-09-21 11:29:12 -0700577 return nextObj.id();
Charles Chan8d316332018-06-19 20:31:57 -0700578 }
Charles Chan8d316332018-06-19 20:31:57 -0700579 }
580
581 /**
582 * Populates bridging forwarding objectives for given XConnect.
583 *
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530584 * @param key XConnect store key
Charles Chan1fb65132018-09-21 11:29:12 -0700585 * @param nextId next objective id
Charles Chan8d316332018-06-19 20:31:57 -0700586 */
Charles Chan1fb65132018-09-21 11:29:12 -0700587 private void populateFwd(XconnectKey key, int nextId) {
588 ForwardingObjective.Builder fwdObjBuilder = fwdObjBuilder(key, nextId);
Charles Chan8d316332018-06-19 20:31:57 -0700589 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 Chan445659f2019-01-02 13:46:16 -0800613 * @param key XConnect key
614 * @param endpoints XConnect endpoints
Charles Chan8d316332018-06-19 20:31:57 -0700615 */
Charles Chan445659f2019-01-02 13:46:16 -0800616 private void revokeXConnect(XconnectKey key, Set<XconnectEndpoint> endpoints) {
pier6fd24fd2018-11-27 11:23:50 -0800617 if (!isLocalLeader(key.deviceId())) {
618 log.debug("Abort revoking XConnect {}: {}", key, ERROR_NOT_LEADER);
Charles Chan8d316332018-06-19 20:31:57 -0700619 return;
620 }
621
Charles Chan445659f2019-01-02 13:46:16 -0800622 revokeFilter(key, endpoints);
pier6fd24fd2018-11-27 11:23:50 -0800623 int nextId = Versioned.valueOrElse(xconnectNextObjStore.get(key), -1);
624 if (nextId != -1) {
Charles Chan1fb65132018-09-21 11:29:12 -0700625 revokeFwd(key, nextId, null);
Charles Chan445659f2019-01-02 13:46:16 -0800626 revokeNext(key, endpoints, nextId, null);
Charles Chan8d316332018-06-19 20:31:57 -0700627 } else {
628 log.warn("NextObj for {} does not exist in the store.", key);
629 }
Charles Chan445659f2019-01-02 13:46:16 -0800630 revokeFilter(key, endpoints);
Charles Chan8d316332018-06-19 20:31:57 -0700631 revokeAcl(key);
632 }
633
634 /**
635 * Revokes filtering objectives for given XConnect.
636 *
Charles Chan445659f2019-01-02 13:46:16 -0800637 * @param key XConnect store key
638 * @param endpoints XConnect endpoints
Charles Chan8d316332018-06-19 20:31:57 -0700639 */
Charles Chan445659f2019-01-02 13:46:16 -0800640 private void revokeFilter(XconnectKey key, Set<XconnectEndpoint> endpoints) {
Charles Chan48df9ad2018-10-30 18:08:59 -0700641 // FIXME Improve the logic
Charles Chanc0a499b2019-01-16 15:30:39 -0800642 // If port load balancer is not involved, use filtered port. Otherwise, use unfiltered port.
Charles Chan48df9ad2018-10-30 18:08:59 -0700643 // The purpose is to make sure existing XConnect logic can still work on a configured port.
Charles Chan445659f2019-01-02 13:46:16 -0800644 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 Chan8d316332018-06-19 20:31:57 -0700660 }
661
662 /**
663 * Revokes next objectives for given XConnect.
664 *
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530665 * @param key XConnect store key
Charles Chan445659f2019-01-02 13:46:16 -0800666 * @param endpoints XConnect endpoints
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530667 * @param nextId next objective id
Charles Chan8d316332018-06-19 20:31:57 -0700668 * @param nextFuture completable future for this next objective operation
669 */
Charles Chan445659f2019-01-02 13:46:16 -0800670 private void revokeNext(XconnectKey key, Set<XconnectEndpoint> endpoints, int nextId,
Charles Chan8d316332018-06-19 20:31:57 -0700671 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 Chanfacfbef2018-08-23 14:30:33 -0700687 srService.invalidateNextObj(objective.id());
Charles Chan8d316332018-06-19 20:31:57 -0700688 }
689 };
Charles Chan48df9ad2018-10-30 18:08:59 -0700690
Charles Chan445659f2019-01-02 13:46:16 -0800691 NextObjective.Builder nextObjBuilder = nextObjBuilder(key, endpoints, nextId);
Charles Chan48df9ad2018-10-30 18:08:59 -0700692 if (nextObjBuilder == null) {
693 log.warn("Fail to revokeNext {}: {}", key, ERROR_NEXT_OBJ_BUILDER);
694 return;
695 }
Charles Chanc0a499b2019-01-16 15:30:39 -0800696 // Release the port load balancer if present
Charles Chan445659f2019-01-02 13:46:16 -0800697 endpoints.stream()
698 .filter(endpoint -> endpoint.type() == XconnectEndpoint.Type.LOAD_BALANCER)
699 .forEach(endpoint -> {
Charles Chanc0a499b2019-01-16 15:30:39 -0800700 String portLoadBalancerKey = String.valueOf(((XconnectLoadBalancerEndpoint) endpoint).key());
701 portLoadBalancerService.release(new PortLoadBalancerId(key.deviceId(),
702 Integer.parseInt(portLoadBalancerKey)), appId);
Charles Chan445659f2019-01-02 13:46:16 -0800703 });
Charles Chan48df9ad2018-10-30 18:08:59 -0700704 flowObjectiveService.next(key.deviceId(), nextObjBuilder.remove(context));
Charles Chan8d316332018-06-19 20:31:57 -0700705 xconnectNextObjStore.remove(key);
706 }
707
708 /**
709 * Revokes bridging forwarding objectives for given XConnect.
710 *
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530711 * @param key XConnect store key
712 * @param nextId next objective id
Charles Chan8d316332018-06-19 20:31:57 -0700713 * @param fwdFuture completable future for this forwarding objective operation
714 */
Charles Chan1fb65132018-09-21 11:29:12 -0700715 private void revokeFwd(XconnectKey key, int nextId, CompletableFuture<ObjectiveError> fwdFuture) {
716 ForwardingObjective.Builder fwdObjBuilder = fwdObjBuilder(key, nextId);
Charles Chan8d316332018-06-19 20:31:57 -0700717 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 Chan445659f2019-01-02 13:46:16 -0800754 * @param key XConnect key
755 * @param prevEndpoints previous XConnect endpoints
756 * @param endpoints new XConnect endpoints
Charles Chan8d316332018-06-19 20:31:57 -0700757 */
Charles Chan445659f2019-01-02 13:46:16 -0800758 private void updateXConnect(XconnectKey key, Set<XconnectEndpoint> prevEndpoints,
759 Set<XconnectEndpoint> endpoints) {
pier6fd24fd2018-11-27 11:23:50 -0800760 if (!isLocalLeader(key.deviceId())) {
761 log.debug("Abort updating XConnect {}: {}", key, ERROR_NOT_LEADER);
762 return;
763 }
Charles Chan8d316332018-06-19 20:31:57 -0700764 // 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 Chan445659f2019-01-02 13:46:16 -0800768 prevEndpoints.stream().filter(prevEndpoint -> !endpoints.contains(prevEndpoint)).forEach(prevEndpoint ->
769 revokeFilter(key, ImmutableSet.of(prevEndpoint)));
Charles Chan8d316332018-06-19 20:31:57 -0700770 // install new filter
Charles Chan445659f2019-01-02 13:46:16 -0800771 endpoints.stream().filter(endpoint -> !prevEndpoints.contains(endpoint)).forEach(endpoint ->
772 populateFilter(key, ImmutableSet.of(endpoint)));
Charles Chan8d316332018-06-19 20:31:57 -0700773
774 CompletableFuture<ObjectiveError> fwdFuture = new CompletableFuture<>();
775 CompletableFuture<ObjectiveError> nextFuture = new CompletableFuture<>();
776
pier6fd24fd2018-11-27 11:23:50 -0800777 int nextId = Versioned.valueOrElse(xconnectNextObjStore.get(key), -1);
778 if (nextId != -1) {
Charles Chan1fb65132018-09-21 11:29:12 -0700779 revokeFwd(key, nextId, fwdFuture);
Charles Chan8d316332018-06-19 20:31:57 -0700780
781 fwdFuture.thenAcceptAsync(fwdStatus -> {
782 if (fwdStatus == null) {
783 log.debug("Fwd removed. Now remove group {}", key);
Charles Chan445659f2019-01-02 13:46:16 -0800784 revokeNext(key, prevEndpoints, nextId, nextFuture);
Charles Chan8d316332018-06-19 20:31:57 -0700785 }
786 });
787
788 nextFuture.thenAcceptAsync(nextStatus -> {
789 if (nextStatus == null) {
790 log.debug("Installing new group and flow for {}", key);
Charles Chan445659f2019-01-02 13:46:16 -0800791 int newNextId = populateNext(key, endpoints);
Charles Chan48df9ad2018-10-30 18:08:59 -0700792 if (newNextId == -1) {
793 log.warn("Fail to updateXConnect {}: {}", key, ERROR_NEXT_ID);
794 return;
795 }
796 populateFwd(key, newNextId);
Charles Chan8d316332018-06-19 20:31:57 -0700797 }
798 });
799 } else {
800 log.warn("NextObj for {} does not exist in the store.", key);
801 }
802 }
803
804 /**
Charles Chan1fb65132018-09-21 11:29:12 -0700805 * Creates a next objective builder for XConnect with given nextId.
Charles Chan8d316332018-06-19 20:31:57 -0700806 *
Charles Chan445659f2019-01-02 13:46:16 -0800807 * @param key XConnect key
808 * @param endpoints XConnect endpoints
Charles Chan48df9ad2018-10-30 18:08:59 -0700809 * @param nextId next objective id
Charles Chan8d316332018-06-19 20:31:57 -0700810 * @return next objective builder
811 */
Charles Chan445659f2019-01-02 13:46:16 -0800812 private NextObjective.Builder nextObjBuilder(XconnectKey key, Set<XconnectEndpoint> endpoints, int nextId) {
Charles Chan8d316332018-06-19 20:31:57 -0700813 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 Chan48df9ad2018-10-30 18:08:59 -0700819
Charles Chan445659f2019-01-02 13:46:16 -0800820 for (XconnectEndpoint endpoint : endpoints) {
821 NextTreatment nextTreatment = getNextTreatment(key.deviceId(), endpoint, true);
Charles Chan48df9ad2018-10-30 18:08:59 -0700822 if (nextTreatment == null) {
Charles Chanc0a499b2019-01-16 15:30:39 -0800823 // If a PortLoadBalancer is used in the XConnect - putting on hold
Charles Chan445659f2019-01-02 13:46:16 -0800824 if (endpoint.type() == XconnectEndpoint.Type.LOAD_BALANCER) {
Charles Chanc0a499b2019-01-16 15:30:39 -0800825 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);
pier567465b2018-11-24 11:16:28 -0800829 } else {
830 log.warn("Unable to create nextObj. Null NextTreatment");
831 }
Charles Chan48df9ad2018-10-30 18:08:59 -0700832 return null;
833 }
834 nextObjBuilder.addTreatment(nextTreatment);
835 }
836
Charles Chan8d316332018-06-19 20:31:57 -0700837 return nextObjBuilder;
838 }
839
840 /**
Charles Chan1fb65132018-09-21 11:29:12 -0700841 * Creates a next objective builder for XConnect.
842 *
Charles Chan445659f2019-01-02 13:46:16 -0800843 * @param key XConnect key
844 * @param endpoints Xconnect endpoints
Charles Chan1fb65132018-09-21 11:29:12 -0700845 * @return next objective builder
846 */
Charles Chan445659f2019-01-02 13:46:16 -0800847 private NextObjective.Builder nextObjBuilder(XconnectKey key, Set<XconnectEndpoint> endpoints) {
Charles Chan1fb65132018-09-21 11:29:12 -0700848 int nextId = flowObjectiveService.allocateNextId();
Charles Chan445659f2019-01-02 13:46:16 -0800849 return nextObjBuilder(key, endpoints, nextId);
Charles Chan1fb65132018-09-21 11:29:12 -0700850 }
851
852
853 /**
Charles Chan8d316332018-06-19 20:31:57 -0700854 * Creates a bridging forwarding objective builder for XConnect.
855 *
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530856 * @param key XConnect key
Charles Chan8d316332018-06-19 20:31:57 -0700857 * @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 *
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530904 * @param key XConnect key
Charles Chan8d316332018-06-19 20:31:57 -0700905 * @param port XConnect ports
Charles Chan48df9ad2018-10-30 18:08:59 -0700906 * @param filtered true if this is a filtered port
Charles Chan8d316332018-06-19 20:31:57 -0700907 * @return next objective builder
908 */
Charles Chan48df9ad2018-10-30 18:08:59 -0700909 private FilteringObjective.Builder filterObjBuilder(XconnectKey key, PortNumber port, boolean filtered) {
Charles Chan8d316332018-06-19 20:31:57 -0700910 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
911 fob.withKey(Criteria.matchInPort(port))
Charles Chan8d316332018-06-19 20:31:57 -0700912 .addCondition(Criteria.matchEthDst(MacAddress.NONE))
913 .withPriority(XCONNECT_PRIORITY);
Charles Chan48df9ad2018-10-30 18:08:59 -0700914 if (filtered) {
915 fob.addCondition(Criteria.matchVlanId(key.vlanId()));
916 } else {
917 fob.addCondition(Criteria.matchVlanId(VlanId.ANY));
918 }
Charles Chan8d316332018-06-19 20:31:57 -0700919 return fob.permit().fromApp(appId);
920 }
921
922 /**
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530923 * Updates L2 flooding groups; add pair link into L2 flooding group of given xconnect vlan.
Charles Chan8d316332018-06-19 20:31:57 -0700924 *
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530925 * @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 Chan8d316332018-06-19 20:31:57 -0700929 */
pier6fd24fd2018-11-27 11:23:50 -0800930 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);
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530935 return;
Charles Chan8d316332018-06-19 20:31:57 -0700936 }
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530937
938 // Locate L2 flooding group details for given xconnect vlan
pier6fd24fd2018-11-27 11:23:50 -0800939 int nextId = Versioned.valueOrElse(xconnectNextObjStore.get(key), -1);
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530940 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 Chan8d316332018-06-19 20:31:57 -0700971 }
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530972
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 */
pier6fd24fd2018-11-27 11:23:50 -0800982 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 }
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530989
990 boolean multicastGroupExists = true;
991 int vlanMulticastNextId;
pier6fd24fd2018-11-27 11:23:50 -0800992 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
jayakumarthazhath66c9ec12018-10-01 00:51:54 +0530993
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());
pier6fd24fd2018-11-27 11:23:50 -08001001 vlanMulticastNextId = getMulticastGroupNextObjectiveId(key);
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301002 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();
pier6fd24fd2018-11-27 11:23:50 -08001006 addMulticastGroupNextObjectiveId(key, vlanMulticastNextId);
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301007 vlanMulticastNextObjBuilder.addTreatment(
1008 DefaultTrafficTreatment.builder().popVlan().setOutput(pairPort).build()
1009 );
1010 }
1011 vlanMulticastNextObjBuilder.withId(vlanMulticastNextId);
pier6fd24fd2018-11-27 11:23:50 -08001012 int nextId = vlanMulticastNextId;
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301013 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());
pier6fd24fd2018-11-27 11:23:50 -08001022 addMulticastGroupPort(key, p);
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301023 });
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
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301068 * @param vlanId VLAN ID
1069 * @param accessPorts List of access ports to be added into L2 multicast group
1070 */
pier6fd24fd2018-11-27 11:23:50 -08001071 private void revokeL2Multicast(DeviceId deviceId, VlanId vlanId, List<PortNumber> accessPorts) {
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301072 // Ensure enough rights to program pair device
1073 if (!srService.shouldProgram(deviceId)) {
pier6fd24fd2018-11-27 11:23:50 -08001074 log.debug("Abort revoke L2Multicast {}-{}: {}", deviceId, vlanId, ERROR_NOT_LEADER);
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301075 return;
1076 }
1077
pier6fd24fd2018-11-27 11:23:50 -08001078 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
1079
1080 int vlanMulticastNextId = getMulticastGroupNextObjectiveId(key);
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301081 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());
pier6fd24fd2018-11-27 11:23:50 -08001099 removeMulticastGroupPort(key, p);
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301100 });
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)) {
pier6fd24fd2018-11-27 11:23:50 -08001128 log.debug("Abort cleanup L2Multicast {}-{}: {}", deviceId, vlanId, ERROR_NOT_LEADER);
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301129 return;
1130 }
1131
pier6fd24fd2018-11-27 11:23:50 -08001132 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
1133
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301134 // Ensure L2 multicast group doesn't contain access ports
pier6fd24fd2018-11-27 11:23:50 -08001135 if (hasAccessPortInMulticastGroup(key, pairPort) && !force) {
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301136 return;
1137 }
1138
1139 // Load L2 multicast group details
pier6fd24fd2018-11-27 11:23:50 -08001140 int vlanMulticastNextId = getMulticastGroupNextObjectiveId(key);
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301141 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.
pier6fd24fd2018-11-27 11:23:50 -08001185 removeMulticastGroup(key);
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301186 }
1187
pier6fd24fd2018-11-27 11:23:50 -08001188 private int getMulticastGroupNextObjectiveId(VlanNextObjectiveStoreKey key) {
1189 return Versioned.valueOrElse(xconnectMulticastNextStore.get(key), -1);
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301190 }
1191
pier6fd24fd2018-11-27 11:23:50 -08001192 private void addMulticastGroupNextObjectiveId(VlanNextObjectiveStoreKey key, int nextId) {
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301193 if (nextId == -1) {
1194 return;
1195 }
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301196 xconnectMulticastNextStore.put(key, nextId);
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301197 }
1198
pier6fd24fd2018-11-27 11:23:50 -08001199 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 });
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301207 }
1208
pier6fd24fd2018-11-27 11:23:50 -08001209 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 });
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301216 }
1217
pier6fd24fd2018-11-27 11:23:50 -08001218 private void removeMulticastGroup(VlanNextObjectiveStoreKey groupKey) {
1219 xconnectMulticastPortsStore.remove(groupKey);
1220 xconnectMulticastNextStore.remove(groupKey);
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301221 }
1222
pier6fd24fd2018-11-27 11:23:50 -08001223 private boolean hasAccessPortInMulticastGroup(VlanNextObjectiveStoreKey groupKey, PortNumber pairPort) {
1224 List<PortNumber> ports = Versioned.valueOrElse(xconnectMulticastPortsStore.get(groupKey), ImmutableList.of());
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301225 return ports.stream().anyMatch(p -> !p.equals(pairPort));
1226 }
1227
pier6fd24fd2018-11-27 11:23:50 -08001228 // 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 Chan445659f2019-01-02 13:46:16 -08001244 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 Chan48df9ad2018-10-30 18:08:59 -07001248 }
Charles Chan445659f2019-01-02 13:46:16 -08001249 if (endpoint.type() == XconnectEndpoint.Type.LOAD_BALANCER) {
Charles Chanc0a499b2019-01-16 15:30:39 -08001250 PortLoadBalancerId portLoadBalancerId = new PortLoadBalancerId(deviceId,
1251 ((XconnectLoadBalancerEndpoint) endpoint).key());
1252 Set<PortNumber> ports = portLoadBalancerService.getPortLoadBalancer(portLoadBalancerId).ports();
Charles Chan445659f2019-01-02 13:46:16 -08001253 return Sets.newHashSet(ports);
Charles Chan48df9ad2018-10-30 18:08:59 -07001254 }
Charles Chan48df9ad2018-10-30 18:08:59 -07001255 return Sets.newHashSet();
1256 }
1257
Charles Chan445659f2019-01-02 13:46:16 -08001258 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 Chan48df9ad2018-10-30 18:08:59 -07001262 }
Charles Chan445659f2019-01-02 13:46:16 -08001263 if (endpoint.type() == XconnectEndpoint.Type.LOAD_BALANCER) {
Charles Chanc0a499b2019-01-16 15:30:39 -08001264 PortLoadBalancerId portLoadBalancerId = new PortLoadBalancerId(deviceId,
1265 ((XconnectLoadBalancerEndpoint) endpoint).key());
1266 NextTreatment idNextTreatment = IdNextTreatment.of(portLoadBalancerService
1267 .getPortLoadBalancerNext(portLoadBalancerId));
pier567465b2018-11-24 11:16:28 -08001268 // Reserve only one time during next objective creation
1269 if (reserve) {
Charles Chanc0a499b2019-01-16 15:30:39 -08001270 if (!portLoadBalancerService.reserve(portLoadBalancerId, appId)) {
1271 log.warn("Reservation failed for {}", portLoadBalancerId);
pier567465b2018-11-24 11:16:28 -08001272 idNextTreatment = null;
1273 }
1274 }
1275 return idNextTreatment;
Charles Chan48df9ad2018-10-30 18:08:59 -07001276 }
Charles Chan48df9ad2018-10-30 18:08:59 -07001277 return null;
1278 }
pier567465b2018-11-24 11:16:28 -08001279
Charles Chanc0a499b2019-01-16 15:30:39 -08001280 private class InternalPortLoadBalancerListener implements PortLoadBalancerListener {
1281 // Populate xconnect once portloadbalancer is available
pier567465b2018-11-24 11:16:28 -08001282 @Override
Charles Chanc0a499b2019-01-16 15:30:39 -08001283 public void event(PortLoadBalancerEvent event) {
1284 portLoadBalancerExecutor.execute(() -> dequeue(event.subject().portLoadBalancerId()));
pier567465b2018-11-24 11:16:28 -08001285 }
Charles Chanc0a499b2019-01-16 15:30:39 -08001286 // When we receive INSTALLED port load balancing is ready
pier567465b2018-11-24 11:16:28 -08001287 @Override
Charles Chanc0a499b2019-01-16 15:30:39 -08001288 public boolean isRelevant(PortLoadBalancerEvent event) {
1289 return event.type() == PortLoadBalancerEvent.Type.INSTALLED;
pier567465b2018-11-24 11:16:28 -08001290 }
1291 }
1292
1293 // Invalidate the cache and re-start the xconnect installation
Charles Chanc0a499b2019-01-16 15:30:39 -08001294 private void dequeue(PortLoadBalancerId portLoadBalancerId) {
1295 XconnectKey xconnectKey = portLoadBalancerCache.getIfPresent(portLoadBalancerId);
pier567465b2018-11-24 11:16:28 -08001296 if (xconnectKey == null) {
Charles Chanc0a499b2019-01-16 15:30:39 -08001297 log.trace("{} not present in the cache", portLoadBalancerId);
pier567465b2018-11-24 11:16:28 -08001298 return;
1299 }
Charles Chanc0a499b2019-01-16 15:30:39 -08001300 log.debug("Dequeue {}", portLoadBalancerId);
1301 portLoadBalancerCache.invalidate(portLoadBalancerId);
Charles Chan445659f2019-01-02 13:46:16 -08001302 Set<XconnectEndpoint> endpoints = Versioned.valueOrNull(xconnectStore.get(xconnectKey));
1303 if (endpoints == null || endpoints.isEmpty()) {
1304 log.warn("Endpoints not found for XConnect {}", xconnectKey);
pier567465b2018-11-24 11:16:28 -08001305 return;
1306 }
Charles Chan445659f2019-01-02 13:46:16 -08001307 populateXConnect(xconnectKey, endpoints);
Charles Chanc0a499b2019-01-16 15:30:39 -08001308 log.trace("PortLoadBalancer cache size {}", portLoadBalancerCache.size());
pier567465b2018-11-24 11:16:28 -08001309 }
1310
Charles Chan8d316332018-06-19 20:31:57 -07001311}