blob: 12bd9e49700221c97edd46bcf70e0432c1f7ce44 [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sangho80f11cb2015-04-01 13:05:26 -07003 *
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;
17
Jonathan Hart61e24e12017-11-30 18:23:42 -080018import com.google.common.collect.HashMultimap;
19import com.google.common.collect.ImmutableMap;
20import com.google.common.collect.Maps;
21import com.google.common.collect.Multimap;
Charles Chanf0ae41e2017-08-23 13:55:39 -070022import com.google.common.collect.Sets;
sangho80f11cb2015-04-01 13:05:26 -070023import org.onlab.packet.Ethernet;
Pier Ventreb6b81d52016-12-02 08:16:05 -080024import org.onlab.packet.ICMP6;
Charles Chan77277672015-10-20 16:24:19 -070025import org.onlab.packet.IPv4;
Pier Ventreb6a7f342016-11-26 21:05:22 -080026import org.onlab.packet.IPv6;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070027import org.onlab.packet.IpAddress;
Charles Chan77277672015-10-20 16:24:19 -070028import org.onlab.packet.IpPrefix;
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +000029import org.onlab.packet.MacAddress;
Carmelo Cascone61004262020-01-16 13:36:02 -080030import org.onlab.packet.VlanId;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070031import org.onlab.util.KryoNamespace;
Charles Chan873661e2017-11-30 15:37:50 -080032import org.onlab.util.Tools;
Saurav Dasc3604f12016-03-23 11:22:49 -070033import org.onosproject.cfg.ComponentConfigService;
Saurav Das00e553b2018-04-21 17:19:48 -070034import org.onosproject.cluster.ClusterEvent;
35import org.onosproject.cluster.ClusterEventListener;
Pier Luigi580fd8a2018-01-16 10:47:50 +010036import org.onosproject.cluster.ClusterService;
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -070037import org.onosproject.cluster.LeadershipService;
Pierdb27b8d2018-04-17 16:29:56 +020038import org.onosproject.cluster.NodeId;
sangho80f11cb2015-04-01 13:05:26 -070039import org.onosproject.core.ApplicationId;
40import org.onosproject.core.CoreService;
41import org.onosproject.event.Event;
Charles Chanfbcb8812018-04-18 18:41:05 -070042import org.onosproject.mastership.MastershipEvent;
43import org.onosproject.mastership.MastershipListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070044import org.onosproject.mastership.MastershipService;
Pier1f87aca2018-03-14 16:47:32 -070045import org.onosproject.mcast.api.McastEvent;
46import org.onosproject.mcast.api.McastListener;
47import org.onosproject.mcast.api.MulticastRouteService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080048import org.onosproject.net.ConnectPoint;
Jonathan Hart54541d12016-04-12 15:39:44 -070049import org.onosproject.net.Device;
50import org.onosproject.net.DeviceId;
Charles Chanf0ae41e2017-08-23 13:55:39 -070051import org.onosproject.net.Host;
52import org.onosproject.net.HostId;
Jonathan Hart54541d12016-04-12 15:39:44 -070053import org.onosproject.net.Link;
54import org.onosproject.net.Port;
Charles Chanf4586112015-11-09 16:37:23 -080055import org.onosproject.net.PortNumber;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070056import org.onosproject.net.config.ConfigException;
Charles Chan72f556a2015-10-05 17:50:33 -070057import org.onosproject.net.config.ConfigFactory;
58import org.onosproject.net.config.NetworkConfigEvent;
Charles Chan72f556a2015-10-05 17:50:33 -070059import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070060import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkeyae0068a2017-08-15 11:02:29 -070061import org.onosproject.net.config.basics.InterfaceConfig;
62import org.onosproject.net.config.basics.McastConfig;
Charles Chan72f556a2015-10-05 17:50:33 -070063import org.onosproject.net.config.basics.SubjectFactories;
Saurav Dase6c448a2018-01-18 12:07:33 -080064import org.onosproject.net.device.DeviceAdminService;
Jonathan Hart54541d12016-04-12 15:39:44 -070065import org.onosproject.net.device.DeviceEvent;
66import org.onosproject.net.device.DeviceListener;
67import org.onosproject.net.device.DeviceService;
Charles Chanf4586112015-11-09 16:37:23 -080068import org.onosproject.net.flow.TrafficSelector;
69import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart54541d12016-04-12 15:39:44 -070070import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan0b1dd7e2018-08-19 19:21:46 -070071import org.onosproject.net.flowobjective.NextObjective;
Charles Chanf4586112015-11-09 16:37:23 -080072import org.onosproject.net.host.HostEvent;
73import org.onosproject.net.host.HostListener;
Charles Chanc6bcdf92018-06-01 16:33:48 -070074import org.onosproject.net.host.HostProbingService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080075import org.onosproject.net.host.HostService;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070076import org.onosproject.net.host.InterfaceIpAddress;
Pierdb27b8d2018-04-17 16:29:56 +020077import org.onosproject.net.intent.WorkPartitionService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070078import org.onosproject.net.intf.Interface;
79import org.onosproject.net.intf.InterfaceService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080080import org.onosproject.net.link.LinkEvent;
81import org.onosproject.net.link.LinkListener;
82import org.onosproject.net.link.LinkService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070083import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080084import org.onosproject.net.packet.InboundPacket;
85import org.onosproject.net.packet.PacketContext;
86import org.onosproject.net.packet.PacketProcessor;
87import org.onosproject.net.packet.PacketService;
Pier Luigid8a15162018-02-15 16:33:08 +010088import org.onosproject.net.topology.TopologyEvent;
89import org.onosproject.net.topology.TopologyListener;
Charles Chanc91c8782016-03-30 17:54:24 -070090import org.onosproject.net.topology.TopologyService;
Charles Chanf0ae41e2017-08-23 13:55:39 -070091import org.onosproject.routeservice.ResolvedRoute;
Ray Milkeyae0068a2017-08-15 11:02:29 -070092import org.onosproject.routeservice.RouteEvent;
93import org.onosproject.routeservice.RouteListener;
94import org.onosproject.routeservice.RouteService;
Charles Chanc91c8782016-03-30 17:54:24 -070095import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
96import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventre6b19e482016-11-07 16:21:04 -080097import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkeyae0068a2017-08-15 11:02:29 -070098import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanc91c8782016-03-30 17:54:24 -070099import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -0700100import org.onosproject.segmentrouting.grouphandler.DestinationSet;
101import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Harshada Chaundkar9204f312019-07-02 16:01:24 +0000102import org.onosproject.segmentrouting.mcast.McastFilteringObjStoreKey;
Pier Luigi69f774d2018-02-28 12:10:50 +0100103import org.onosproject.segmentrouting.mcast.McastHandler;
104import org.onosproject.segmentrouting.mcast.McastRole;
Piere99511d2018-04-19 16:47:06 +0200105import org.onosproject.segmentrouting.mcast.McastRoleStoreKey;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700106import org.onosproject.segmentrouting.mcast.McastStoreKey;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700107import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700108import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800109import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700110import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800111import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700112import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
Ray Milkeyae0068a2017-08-15 11:02:29 -0700113import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800114import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Saurav Das261c3002017-06-13 15:35:54 -0700115import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Carmelo Cascone61004262020-01-16 13:36:02 -0800116import org.onosproject.segmentrouting.storekey.MacVlanNextObjectiveStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -0700117import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -0800118import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -0700119import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Charles Chan8d316332018-06-19 20:31:57 -0700120import org.onosproject.segmentrouting.xconnect.api.XconnectService;
Jonathan Hart54541d12016-04-12 15:39:44 -0700121import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700122import org.onosproject.store.service.EventuallyConsistentMap;
123import org.onosproject.store.service.EventuallyConsistentMapBuilder;
124import org.onosproject.store.service.StorageService;
125import org.onosproject.store.service.WallClockTimestamp;
Charles Chan873661e2017-11-30 15:37:50 -0800126import org.osgi.service.component.ComponentContext;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700127import org.osgi.service.component.annotations.Activate;
128import org.osgi.service.component.annotations.Component;
129import org.osgi.service.component.annotations.Deactivate;
130import org.osgi.service.component.annotations.Modified;
131import org.osgi.service.component.annotations.Reference;
132import org.osgi.service.component.annotations.ReferenceCardinality;
Charles Chanb4f9cd12019-02-27 11:46:32 -0800133import org.osgi.service.component.annotations.ReferencePolicy;
sangho80f11cb2015-04-01 13:05:26 -0700134import org.slf4j.Logger;
135import org.slf4j.LoggerFactory;
136
Saurav Das00e553b2018-04-21 17:19:48 -0700137import java.time.Instant;
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700138import java.util.ArrayList;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800139import java.util.Collections;
Charles Chan873661e2017-11-30 15:37:50 -0800140import java.util.Dictionary;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800141import java.util.HashSet;
142import java.util.List;
143import java.util.Map;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800144import java.util.Optional;
145import java.util.Set;
Andrea Campanella060cad82018-04-17 12:09:34 +0200146import java.util.concurrent.CompletableFuture;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800147import java.util.concurrent.ConcurrentHashMap;
Andrea Campanella060cad82018-04-17 12:09:34 +0200148import java.util.concurrent.CopyOnWriteArrayList;
Charles Chan77cdde42018-05-08 21:35:50 -0700149import java.util.concurrent.ExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800150import java.util.concurrent.Executors;
151import java.util.concurrent.ScheduledExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800152import java.util.concurrent.TimeUnit;
153import java.util.concurrent.atomic.AtomicBoolean;
154import java.util.stream.Collectors;
sangho80f11cb2015-04-01 13:05:26 -0700155
Charles Chand6d25332016-02-26 22:19:52 -0800156import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800157import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700158import static org.onlab.util.Tools.groupedThreads;
Saurav Dase321cff2018-02-09 17:26:45 -0800159import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
160import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700161import static org.onosproject.segmentrouting.OsgiPropertyConstants.ACTIVE_PROBING_DEFAULT;
162import static org.onosproject.segmentrouting.OsgiPropertyConstants.DEFAULT_INTERNAL_VLAN_DEFAULT;
163import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ACTIVE_PROBING;
164import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_DEFAULT_INTERNAL_VLAN;
165import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_PW_TRANSPORT_VLAN;
166import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_RESPOND_TO_UNKNOWN_HOSTS;
167import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_DOUBLE_TAGGED_HOSTS;
psneha86e60d32019-03-26 06:31:41 -0400168import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_SIMPLIFICATION;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700169import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SINGLE_HOMED_DOWN;
Ray Milkeye96d0de2018-11-02 17:12:21 -0700170import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SYMMETRIC_PROBING;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700171import static org.onosproject.segmentrouting.OsgiPropertyConstants.PW_TRANSPORT_VLAN_DEFAULT;
172import static org.onosproject.segmentrouting.OsgiPropertyConstants.RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
173import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
psneha86e60d32019-03-26 06:31:41 -0400174import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_SIMPLIFICATION_DEFAULT;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700175import static org.onosproject.segmentrouting.OsgiPropertyConstants.SINGLE_HOMED_DOWN_DEFAULT;
Ray Milkeye96d0de2018-11-02 17:12:21 -0700176import static org.onosproject.segmentrouting.OsgiPropertyConstants.SYMMETRIC_PROBING_DEFAULT;
Charles Chand6d25332016-02-26 22:19:52 -0800177
Charles Chanb7f75ac2016-01-11 18:28:54 -0800178/**
179 * Segment routing manager.
180 */
Ray Milkey05edbfc2018-10-23 14:23:16 -0700181@Component(
182 immediate = true,
183 service = SegmentRoutingService.class,
184 property = {
185 PROP_ACTIVE_PROBING + ":Boolean=" + ACTIVE_PROBING_DEFAULT,
186 PROP_SINGLE_HOMED_DOWN + ":Boolean=" + SINGLE_HOMED_DOWN_DEFAULT,
187 PROP_RESPOND_TO_UNKNOWN_HOSTS + ":Boolean=" + RESPOND_TO_UNKNOWN_HOSTS_DEFAULT,
188 PROP_ROUTE_DOUBLE_TAGGED_HOSTS + ":Boolean=" + ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT,
189 PROP_DEFAULT_INTERNAL_VLAN + ":Integer=" + DEFAULT_INTERNAL_VLAN_DEFAULT,
190 PROP_PW_TRANSPORT_VLAN + ":Integer=" + PW_TRANSPORT_VLAN_DEFAULT,
psneha86e60d32019-03-26 06:31:41 -0400191 PROP_SYMMETRIC_PROBING + ":Boolean=" + SYMMETRIC_PROBING_DEFAULT,
192 PROP_ROUTE_SIMPLIFICATION + ":Boolean=" + ROUTE_SIMPLIFICATION_DEFAULT
Ray Milkey05edbfc2018-10-23 14:23:16 -0700193 }
194)
sangho27462c62015-05-14 00:39:53 -0700195public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700196
Charles Chan46fdfaf2016-11-09 20:51:44 -0800197 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan910be6a2017-08-23 14:46:43 -0700198 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sangho80f11cb2015-04-01 13:05:26 -0700199
Ray Milkey2bd24a92018-08-17 14:54:17 -0700200 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700201 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700202
Ray Milkey2bd24a92018-08-17 14:54:17 -0700203 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chancf789822019-03-22 10:04:27 -0700204 public NeighbourResolutionService neighbourResolutionService;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800205
Ray Milkey2bd24a92018-08-17 14:54:17 -0700206 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100207 public CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700208
Ray Milkey2bd24a92018-08-17 14:54:17 -0700209 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700210 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700211
Ray Milkey2bd24a92018-08-17 14:54:17 -0700212 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700213 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700214
Ray Milkey2bd24a92018-08-17 14:54:17 -0700215 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chanc6bcdf92018-06-01 16:33:48 -0700216 HostProbingService probingService;
Charles Chan873661e2017-11-30 15:37:50 -0800217
Ray Milkey2bd24a92018-08-17 14:54:17 -0700218 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100219 public DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700220
Ray Milkey2bd24a92018-08-17 14:54:17 -0700221 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Saurav Dase6c448a2018-01-18 12:07:33 -0800222 DeviceAdminService deviceAdminService;
223
Ray Milkey2bd24a92018-08-17 14:54:17 -0700224 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800225 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700226
Ray Milkey2bd24a92018-08-17 14:54:17 -0700227 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100228 public LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700229
Ray Milkey2bd24a92018-08-17 14:54:17 -0700230 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800231 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700232
Ray Milkey2bd24a92018-08-17 14:54:17 -0700233 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800234 public StorageService storageService;
Charles Chandebfea32016-10-24 14:52:01 -0700235
Ray Milkey2bd24a92018-08-17 14:54:17 -0700236 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100237 public MulticastRouteService multicastRouteService;
Charles Chandebfea32016-10-24 14:52:01 -0700238
Ray Milkey2bd24a92018-08-17 14:54:17 -0700239 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan83163812018-01-09 13:45:07 -0800240 public TopologyService topologyService;
Charles Chandebfea32016-10-24 14:52:01 -0700241
Ray Milkey2bd24a92018-08-17 14:54:17 -0700242 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan19b70032019-04-17 14:20:26 -0700243 public RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800244
Ray Milkey2bd24a92018-08-17 14:54:17 -0700245 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800246 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800247
Ray Milkey2bd24a92018-08-17 14:54:17 -0700248 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800249 public InterfaceService interfaceService;
250
Ray Milkey2bd24a92018-08-17 14:54:17 -0700251 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi580fd8a2018-01-16 10:47:50 +0100252 public ClusterService clusterService;
253
Ray Milkey2bd24a92018-08-17 14:54:17 -0700254 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pierdb27b8d2018-04-17 16:29:56 +0200255 public WorkPartitionService workPartitionService;
Pier Luigi580fd8a2018-01-16 10:47:50 +0100256
Ray Milkey2bd24a92018-08-17 14:54:17 -0700257 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700258 public LeadershipService leadershipService;
259
Charles Chanb4f9cd12019-02-27 11:46:32 -0800260 @Reference(cardinality = ReferenceCardinality.OPTIONAL,
Charles Chanb4f9cd12019-02-27 11:46:32 -0800261 policy = ReferencePolicy.DYNAMIC)
Carmelo Cascone61004262020-01-16 13:36:02 -0800262 public volatile XconnectService xconnectService;
Charles Chan8d316332018-06-19 20:31:57 -0700263
Ray Milkey05edbfc2018-10-23 14:23:16 -0700264 /** Enable active probing to discover dual-homed hosts. */
265 boolean activeProbing = ACTIVE_PROBING_DEFAULT;
Charles Chan873661e2017-11-30 15:37:50 -0800266
Ray Milkeye96d0de2018-11-02 17:12:21 -0700267 /** Enable only send probe on the same port number of the pair device. */
268 boolean symmetricProbing = SYMMETRIC_PROBING_DEFAULT;
Mayank Tiwarif7942402018-10-29 18:27:35 -0400269
Ray Milkey05edbfc2018-10-23 14:23:16 -0700270 /** Enable administratively taking down single-homed hosts. */
271 boolean singleHomedDown = SINGLE_HOMED_DOWN_DEFAULT;
Saurav Dasec683dc2018-04-27 18:42:30 -0700272
Ray Milkey05edbfc2018-10-23 14:23:16 -0700273 /** Enable this to respond to ARP/NDP requests from unknown hosts. */
274 boolean respondToUnknownHosts = RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700275
Ray Milkey05edbfc2018-10-23 14:23:16 -0700276 /** Program flows and groups to pop and route double tagged hosts. */
277 boolean routeDoubleTaggedHosts = ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
Charles Chan4eb73bb2018-07-19 14:55:31 -0700278
Ray Milkey05edbfc2018-10-23 14:23:16 -0700279 /** internal vlan assigned by default to unconfigured ports. */
280 private int defaultInternalVlan = DEFAULT_INTERNAL_VLAN_DEFAULT;
Saurav Das9bf49582018-08-13 15:34:26 -0700281
Ray Milkey05edbfc2018-10-23 14:23:16 -0700282 /** vlan used for transport of pseudowires between switches. */
283 private int pwTransportVlan = PW_TRANSPORT_VLAN_DEFAULT;
Saurav Das9bf49582018-08-13 15:34:26 -0700284
psneha86e60d32019-03-26 06:31:41 -0400285 /** Enabling route simplification. */
286 boolean routeSimplification = ROUTE_SIMPLIFICATION_DEFAULT;
287
Charles Chandebfea32016-10-24 14:52:01 -0700288 ArpHandler arpHandler = null;
289 IcmpHandler icmpHandler = null;
290 IpHandler ipHandler = null;
291 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700292 ApplicationId appId;
293 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700294
Charles Chandebfea32016-10-24 14:52:01 -0700295 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700296 private TunnelHandler tunnelHandler = null;
297 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700298 private InternalPacketProcessor processor = null;
299 private InternalLinkListener linkListener = null;
300 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700301 private AppConfigHandler appCfgHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800302 McastHandler mcastHandler = null;
303 HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800304 private RouteHandler routeHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800305 LinkHandler linkHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800306 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800307 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigid8a15162018-02-15 16:33:08 +0100308 private TopologyHandler topologyHandler = null;
Charles Chan82ab1932016-01-30 23:22:37 -0800309 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700310 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
311 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700312 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigid8a15162018-02-15 16:33:08 +0100313 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chanfbcb8812018-04-18 18:41:05 -0700314 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
Saurav Das00e553b2018-04-21 17:19:48 -0700315 final InternalClusterListener clusterListener = new InternalClusterListener();
Andrea Campanella060cad82018-04-17 12:09:34 +0200316 //Completable future for network configuration process to buffer config events handling during activation
317 private CompletableFuture<Boolean> networkConfigCompletion = null;
Ray Milkey521e4392018-11-16 15:15:14 -0800318 private final Object networkConfigCompletionLock = new Object();
Charles Chan39b75522018-04-21 00:44:29 -0700319 private List<Event> queuedEvents = new CopyOnWriteArrayList<>();
sangho80f11cb2015-04-01 13:05:26 -0700320
Charles Chan52003922018-04-05 16:31:15 -0700321 // Handles device, link, topology and network config events
Charles Chanfbcb8812018-04-18 18:41:05 -0700322 private ScheduledExecutorService mainEventExecutor;
sangho80f11cb2015-04-01 13:05:26 -0700323
Charles Chanfbcb8812018-04-18 18:41:05 -0700324 // Handles host, route and mcast events respectively
325 private ScheduledExecutorService hostEventExecutor;
326 private ScheduledExecutorService routeEventExecutor;
327 private ScheduledExecutorService mcastEventExecutor;
Charles Chan77cdde42018-05-08 21:35:50 -0700328 private ExecutorService packetExecutor;
Charles Chana7601c92019-01-23 15:03:17 -0800329 ExecutorService neighborExecutor;
Charles Chan52003922018-04-05 16:31:15 -0700330
331 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chanb7f75ac2016-01-11 18:28:54 -0800332 /**
Saurav Das261c3002017-06-13 15:35:54 -0700333 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700334 * Used to keep track on MPLS group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800335 */
Charles Chan873661e2017-11-30 15:37:50 -0800336 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das261c3002017-06-13 15:35:54 -0700337 dsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800338 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700339 * Per device next objective ID store with (device id + vlanid) as key.
340 * Used to keep track on L2 flood group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800341 */
Charles Chan873661e2017-11-30 15:37:50 -0800342 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800343 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800344 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700345 * Per device next objective ID store with (device id + port + treatment + meta) as key.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000346 * Used to keep track on L2 interface group and L3 unicast group information for direct hosts.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800347 */
Charles Chan873661e2017-11-30 15:37:50 -0800348 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800349 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800350
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700351 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000352 * Per device next objective ID store with (device id + MAC address + vlan) as key.
353 * Used to keep track of L3 unicast group for indirect hosts.
354 */
355 private EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer>
356 macVlanNextObjStore = null;
357
Saurav Das2d94d312015-11-24 23:21:05 -0800358 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
359 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700360
Saurav Das261c3002017-06-13 15:35:54 -0700361 private AtomicBoolean programmingScheduled = new AtomicBoolean();
362
Charles Chanc91c8782016-03-30 17:54:24 -0700363 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700364 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
365 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700366 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700367 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800368 public SegmentRoutingDeviceConfig createConfig() {
369 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700370 }
371 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800372
Charles Chanc91c8782016-03-30 17:54:24 -0700373 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700374 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
375 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700376 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800377 @Override
378 public SegmentRoutingAppConfig createConfig() {
379 return new SegmentRoutingAppConfig();
380 }
381 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800382
Charles Chanc91c8782016-03-30 17:54:24 -0700383 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700384 new ConfigFactory<ApplicationId, McastConfig>(
385 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700386 McastConfig.class, "multicast") {
387 @Override
388 public McastConfig createConfig() {
389 return new McastConfig();
390 }
391 };
392
Charles Chan1963f4f2016-02-18 14:22:42 -0800393 /**
394 * Segment Routing App ID.
395 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800396 public static final String APP_NAME = "org.onosproject.segmentrouting";
Saurav Das7c305372015-10-28 12:39:42 -0700397
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700398 /**
399 * Minumum and maximum value of dummy VLAN ID to be allocated.
400 */
401 public static final int MIN_DUMMY_VLAN_ID = 2;
402 public static final int MAX_DUMMY_VLAN_ID = 4093;
403
Charles Chana7601c92019-01-23 15:03:17 -0800404 private static final int DEFAULT_POOL_SIZE = 32;
405
Saurav Dasec683dc2018-04-27 18:42:30 -0700406 Instant lastEdgePortEvent = Instant.EPOCH;
407
sangho80f11cb2015-04-01 13:05:26 -0700408 @Activate
Charles Chan873661e2017-11-30 15:37:50 -0800409 protected void activate(ComponentContext context) {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800410 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700411
Charles Chana7601c92019-01-23 15:03:17 -0800412 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(
413 groupedThreads("onos/sr", "event-main-%d", log));
414 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(
415 groupedThreads("onos/sr", "event-host-%d", log));
416 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(
417 groupedThreads("onos/sr", "event-route-%d", log));
418 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(
419 groupedThreads("onos/sr", "event-mcast-%d", log));
420 packetExecutor = Executors.newSingleThreadExecutor(groupedThreads("onos/sr", "packet-%d", log));
421 neighborExecutor = Executors.newFixedThreadPool(DEFAULT_POOL_SIZE,
422 groupedThreads("onos/sr", "neighbor-%d", log));
Charles Chanfbcb8812018-04-18 18:41:05 -0700423
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700424 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700425 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700426 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700427 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700428 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700429 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700430 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700431 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700432 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700433
Charles Chan10b0fb72017-02-02 16:20:42 -0800434 log.debug("Creating EC map vlannextobjectivestore");
435 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
436 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
437 vlanNextObjStore = vlanNextObjMapBuilder
438 .withName("vlannextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700439 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700440 .withTimestampProvider((k, v) -> new WallClockTimestamp())
441 .build();
442
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000443 log.debug("Creating EC map macvlannextobjectivestore");
444 EventuallyConsistentMapBuilder<MacVlanNextObjectiveStoreKey, Integer>
445 macVlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
446 macVlanNextObjStore = macVlanNextObjMapBuilder
447 .withName("macvlannextobjectivestore")
448 .withSerializer(createSerializer())
449 .withTimestampProvider((k, v) -> new WallClockTimestamp())
450 .build();
451
Saurav Das2d94d312015-11-24 23:21:05 -0800452 log.debug("Creating EC map subnetnextobjectivestore");
453 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
454 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
455 portNextObjStore = portNextObjMapBuilder
456 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700457 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800458 .withTimestampProvider((k, v) -> new WallClockTimestamp())
459 .build();
460
sangho4a5c42a2015-05-20 22:16:38 -0700461 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
462 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700463 tunnelStore = tunnelMapBuilder
464 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700465 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700466 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700467 .build();
468
469 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
470 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700471 policyStore = policyMapBuilder
472 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700473 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700474 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700475 .build();
476
Charles Chan97979802020-02-26 15:04:44 -0800477 processor = new InternalPacketProcessor();
478 linkListener = new InternalLinkListener();
479 deviceListener = new InternalDeviceListener();
480 appCfgHandler = new AppConfigHandler(this);
481 mcastHandler = new McastHandler(this);
482 hostHandler = new HostHandler(this);
483 linkHandler = new LinkHandler(this);
484 routeHandler = new RouteHandler(this);
485 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
486 l2TunnelHandler = new DefaultL2TunnelHandler(this);
487 topologyHandler = new TopologyHandler(this);
488
Pier Luigib9632ba2017-01-12 18:14:58 -0800489 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700490 "requestInterceptsEnabled", "false", false);
Charles Chanc1909e12017-08-08 15:13:37 -0700491 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700492 "requestInterceptsEnabled", "false", false);
Charles Chan9597f8d2017-07-24 15:56:10 -0700493 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700494 "arpEnabled", "false", false);
Pier Luigi0ebeb312017-02-02 22:31:34 -0800495 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700496 "greedyLearningIpv6", "true", false);
Charles Chancf8ea472017-02-28 15:15:17 -0800497 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700498 "forceUnprovision", "true", false);
Charles Chanc7b73c72017-08-10 16:57:28 -0700499 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan0fa51db2019-04-10 17:12:20 -0700500 "distributed", "true", false);
Charles Chan804772f2017-08-14 11:42:11 -0700501 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700502 "multihomingEnabled", "true", false);
Charles Chan0c9c19f2017-11-29 19:54:20 -0800503 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700504 "staleLinkAge", "15000", false);
Charles Chan0c9c19f2017-11-29 19:54:20 -0800505 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700506 "allowDuplicateIps", "false", false);
Yi Tsenga7da8d82018-08-19 03:09:54 +0800507 // For P4 switches
508 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700509 "fallbackFlowPollFrequency", "4", false);
Yi Tsenga7da8d82018-08-19 03:09:54 +0800510 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700511 "fallbackGroupPollFrequency", "3", false);
Charles Chan873661e2017-11-30 15:37:50 -0800512 compCfgService.registerProperties(getClass());
513 modified(context);
Saurav Dasc3604f12016-03-23 11:22:49 -0700514
Charles Chand6d25332016-02-26 22:19:52 -0800515 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700516 cfgService.registerConfigFactory(deviceConfigFactory);
517 cfgService.registerConfigFactory(appConfigFactory);
518 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase321cff2018-02-09 17:26:45 -0800519 log.info("Configuring network before adding listeners");
Andrea Campanella060cad82018-04-17 12:09:34 +0200520
Charles Chan6961f222018-01-04 14:26:07 -0800521 cfgListener.configureNetwork();
522
Charles Chan82ab1932016-01-30 23:22:37 -0800523 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700524 packetService.addProcessor(processor, PacketProcessor.director(2));
525 linkService.addListener(linkListener);
526 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700527 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700528 routeService.addListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100529 topologyService.addListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700530 mastershipService.addListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700531 clusterService.addListener(clusterListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700532
Charles Chanc12c3d02018-03-09 15:53:44 -0800533 linkHandler.init();
Andreas Pantelopoulos75eef062018-01-11 07:53:48 -0800534 l2TunnelHandler.init();
535
Ray Milkey521e4392018-11-16 15:15:14 -0800536 synchronized (networkConfigCompletionLock) {
537 networkConfigCompletion.whenComplete((value, ex) -> {
538 //setting to null for easier fall through
539 networkConfigCompletion = null;
540 //process all queued events
541 queuedEvents.forEach(event -> {
542 mainEventExecutor.execute(new InternalEventHandler(event));
543 });
Andrea Campanella060cad82018-04-17 12:09:34 +0200544 });
Ray Milkey521e4392018-11-16 15:15:14 -0800545 }
Andrea Campanella060cad82018-04-17 12:09:34 +0200546
sangho80f11cb2015-04-01 13:05:26 -0700547 log.info("Started");
548 }
549
Saurav Dase6c448a2018-01-18 12:07:33 -0800550 KryoNamespace.Builder createSerializer() {
Jonathan Hart54541d12016-04-12 15:39:44 -0700551 return new KryoNamespace.Builder()
552 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700553 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800554 VlanNextObjectiveStoreKey.class,
555 DestinationSet.class,
Andreas Pantelopoulos92998c92018-05-29 13:11:14 -0700556 DestinationSet.DestinationSetType.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800557 NextNeighbors.class,
558 Tunnel.class,
559 DefaultTunnel.class,
560 Policy.class,
561 TunnelPolicy.class,
562 Policy.Type.class,
563 PortNextObjectiveStoreKey.class,
564 XConnectStoreKey.class,
565 L2Tunnel.class,
566 L2TunnelPolicy.class,
567 DefaultL2Tunnel.class,
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700568 DefaultL2TunnelPolicy.class,
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000569 MacVlanNextObjectiveStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700570 );
571 }
572
sangho80f11cb2015-04-01 13:05:26 -0700573 @Deactivate
574 protected void deactivate() {
Charles Chanfbcb8812018-04-18 18:41:05 -0700575 mainEventExecutor.shutdown();
576 hostEventExecutor.shutdown();
577 routeEventExecutor.shutdown();
578 mcastEventExecutor.shutdown();
Charles Chan77cdde42018-05-08 21:35:50 -0700579 packetExecutor.shutdown();
Charles Chana7601c92019-01-23 15:03:17 -0800580 neighborExecutor.shutdown();
Charles Chanfbcb8812018-04-18 18:41:05 -0700581
Ray Milkeyaee4d3e2018-05-11 09:59:19 -0700582 mainEventExecutor = null;
583 hostEventExecutor = null;
584 routeEventExecutor = null;
585 mcastEventExecutor = null;
586 packetExecutor = null;
Charles Chana7601c92019-01-23 15:03:17 -0800587 neighborExecutor = null;
Ray Milkeyaee4d3e2018-05-11 09:59:19 -0700588
Charles Chan72f556a2015-10-05 17:50:33 -0700589 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700590 cfgService.unregisterConfigFactory(deviceConfigFactory);
591 cfgService.unregisterConfigFactory(appConfigFactory);
592 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan873661e2017-11-30 15:37:50 -0800593 compCfgService.unregisterProperties(getClass(), false);
Charles Chan72f556a2015-10-05 17:50:33 -0700594
Charles Chanfd48c222017-06-19 00:43:31 -0700595 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700596 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700597 linkService.removeListener(linkListener);
598 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700599 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700600 routeService.removeListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100601 topologyService.removeListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700602 mastershipService.removeListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700603 clusterService.removeListener(clusterListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700604
Charles Chan2e71ef32017-02-23 15:44:08 -0800605 neighbourResolutionService.unregisterNeighbourHandlers(appId);
606
sangho80f11cb2015-04-01 13:05:26 -0700607 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700608 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700609 deviceListener = null;
Charles Chan05bb1702018-04-19 13:10:01 -0700610 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chan2b078ae2015-10-14 11:24:40 -0700611 groupHandlerMap.clear();
Saurav Das49368392018-04-23 18:42:12 -0700612 defaultRoutingHandler.shutdown();
Charles Chan2b078ae2015-10-14 11:24:40 -0700613
Saurav Das261c3002017-06-13 15:35:54 -0700614 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800615 vlanNextObjStore.destroy();
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000616 macVlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700617 portNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700618 tunnelStore.destroy();
619 policyStore.destroy();
Pier Luigi35dab3f2018-01-25 16:16:02 +0100620
621 mcastHandler.terminate();
piercf557922019-05-17 20:47:06 +0200622 hostHandler.terminate();
sangho80f11cb2015-04-01 13:05:26 -0700623 log.info("Stopped");
624 }
625
Charles Chan873661e2017-11-30 15:37:50 -0800626 @Modified
627 private void modified(ComponentContext context) {
628 Dictionary<?, ?> properties = context.getProperties();
629 if (properties == null) {
630 return;
631 }
632
Ray Milkey48d10c02018-10-24 10:04:03 -0700633 String strActiveProbing = Tools.get(properties, PROP_ACTIVE_PROBING);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700634 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProbing);
Charles Chan873661e2017-11-30 15:37:50 -0800635 if (expectActiveProbing != activeProbing) {
636 activeProbing = expectActiveProbing;
637 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
638 }
Saurav Dasec683dc2018-04-27 18:42:30 -0700639
Ray Milkeye96d0de2018-11-02 17:12:21 -0700640
641 String strSymmetricProbing = Tools.get(properties, PROP_SYMMETRIC_PROBING);
642 boolean expectSymmetricProbing = Boolean.parseBoolean(strSymmetricProbing);
Mayank Tiwarif7942402018-10-29 18:27:35 -0400643 if (expectSymmetricProbing != symmetricProbing) {
644 symmetricProbing = expectSymmetricProbing;
645 log.info("{} symmetric probing", symmetricProbing ? "Enabling" : "Disabling");
646 }
647
Ray Milkey48d10c02018-10-24 10:04:03 -0700648 String strSingleHomedDown = Tools.get(properties, PROP_SINGLE_HOMED_DOWN);
Saurav Dasec683dc2018-04-27 18:42:30 -0700649 boolean expectSingleHomedDown = Boolean.parseBoolean(strSingleHomedDown);
650 if (expectSingleHomedDown != singleHomedDown) {
651 singleHomedDown = expectSingleHomedDown;
652 log.info("{} downing of single homed hosts for lost uplinks",
653 singleHomedDown ? "Enabling" : "Disabling");
654 if (singleHomedDown && linkHandler != null) {
655 hostService.getHosts().forEach(host -> host.locations()
656 .forEach(loc -> {
657 if (interfaceService.isConfigured(loc)) {
658 linkHandler.checkUplinksForHost(loc);
659 }
660 }));
661 } else {
662 log.warn("Disabling singleHomedDown does not re-enable already "
663 + "downed ports for single-homed hosts");
664 }
665 }
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700666
Ray Milkey48d10c02018-10-24 10:04:03 -0700667 String strRespondToUnknownHosts = Tools.get(properties, PROP_RESPOND_TO_UNKNOWN_HOSTS);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700668 boolean expectRespondToUnknownHosts = Boolean.parseBoolean(strRespondToUnknownHosts);
669 if (expectRespondToUnknownHosts != respondToUnknownHosts) {
670 respondToUnknownHosts = expectRespondToUnknownHosts;
671 log.info("{} responding to ARPs/NDPs from unknown hosts", respondToUnknownHosts ? "Enabling" : "Disabling");
672 }
Charles Chan4eb73bb2018-07-19 14:55:31 -0700673
Ray Milkey48d10c02018-10-24 10:04:03 -0700674 String strRouteDoubleTaggedHosts = Tools.get(properties, PROP_ROUTE_DOUBLE_TAGGED_HOSTS);
Charles Chanfbca55e2018-07-24 16:40:35 -0700675 boolean expectRouteDoubleTaggedHosts = Boolean.parseBoolean(strRouteDoubleTaggedHosts);
676 if (expectRouteDoubleTaggedHosts != routeDoubleTaggedHosts) {
677 routeDoubleTaggedHosts = expectRouteDoubleTaggedHosts;
678 log.info("{} routing for double tagged hosts", routeDoubleTaggedHosts ? "Enabling" : "Disabling");
Charles Chan4eb73bb2018-07-19 14:55:31 -0700679
Charles Chanfbca55e2018-07-24 16:40:35 -0700680 if (routeDoubleTaggedHosts) {
Charles Chan4eb73bb2018-07-19 14:55:31 -0700681 hostHandler.populateAllDoubleTaggedHost();
682 } else {
683 hostHandler.revokeAllDoubleTaggedHost();
684 }
685 }
Saurav Das9bf49582018-08-13 15:34:26 -0700686
Ray Milkey48d10c02018-10-24 10:04:03 -0700687 String strDefaultInternalVlan = Tools.get(properties, PROP_DEFAULT_INTERNAL_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700688 int defIntVlan = Integer.parseInt(strDefaultInternalVlan);
689 if (defIntVlan != defaultInternalVlan) {
690 if (canUseVlanId(defIntVlan)) {
691 log.warn("Default internal vlan value changed from {} to {}.. "
692 + "re-programming filtering rules, but NOT any groups already "
693 + "created with the former value", defaultInternalVlan, defIntVlan);
694 VlanId oldDefIntVlan = VlanId.vlanId((short) defaultInternalVlan);
695 defaultInternalVlan = defIntVlan;
696 routingRulePopulator
697 .updateSpecialVlanFilteringRules(true, oldDefIntVlan,
698 VlanId.vlanId((short) defIntVlan));
699 } else {
700 log.warn("Cannot change default internal vlan to unusable "
701 + "value {}", defIntVlan);
702 }
703 }
704
Ray Milkey48d10c02018-10-24 10:04:03 -0700705 String strPwTxpVlan = Tools.get(properties, PROP_PW_TRANSPORT_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700706 int pwTxpVlan = Integer.parseInt(strPwTxpVlan);
707 if (pwTxpVlan != pwTransportVlan) {
708 if (canUseVlanId(pwTxpVlan)) {
709 log.warn("Pseudowire transport vlan value changed from {} to {}.. "
710 + "re-programming filtering rules, but NOT any groups already "
711 + "created with the former value", pwTransportVlan,
712 pwTxpVlan);
713 VlanId oldPwTxpVlan = VlanId.vlanId((short) pwTransportVlan);
714 pwTransportVlan = pwTxpVlan;
715 routingRulePopulator
716 .updateSpecialVlanFilteringRules(false, oldPwTxpVlan,
717 VlanId.vlanId((short) pwTxpVlan));
718 } else {
719 log.warn("Cannot change pseudowire transport vlan to unusable "
720 + "value {}", pwTxpVlan);
721 }
722 }
723
psneha86e60d32019-03-26 06:31:41 -0400724 String strRouteSimplification = Tools.get(properties, PROP_ROUTE_SIMPLIFICATION);
725 boolean expectRouteSimplification = Boolean.parseBoolean(strRouteSimplification);
726 if (expectRouteSimplification != routeSimplification) {
727 routeSimplification = expectRouteSimplification;
728 log.info("{} route simplification", routeSimplification ? "Enabling" : "Disabling");
729 }
730
Saurav Das9bf49582018-08-13 15:34:26 -0700731 }
732
733 /**
734 * Returns true if given vlan id is not being used in the system currently,
735 * either as one of the default system wide vlans or as one of the
736 * configured interface vlans.
737 *
738 * @param vlanId given vlan id
739 * @return true if vlan is not currently in use
740 */
741 public boolean canUseVlanId(int vlanId) {
742 if (vlanId >= 4095 || vlanId <= 1) {
743 log.error("Vlan id {} value is not in valid range 2 <--> 4094",
744 vlanId);
745 return false;
746 }
747
748 VlanId vid = VlanId.vlanId((short) vlanId);
749 if (getDefaultInternalVlan().equals(vid) || getPwTransportVlan().equals(vid)) {
750 log.warn("Vlan id {} value is already in use system-wide. "
751 + "DefaultInternalVlan:{} PwTransportVlan:{} ", vlanId,
752 getDefaultInternalVlan(), getPwTransportVlan());
753 return false;
754 }
755
756 if (interfaceService.inUse(vid)) {
757 log.warn("Vlan id {} value is already in use on a configured "
758 + "interface in the system", vlanId);
759 return false;
760 }
761 return true;
762 }
763
764 /**
765 * Returns the VlanId assigned internally by default to unconfigured ports.
766 *
767 * @return the default internal vlan id
768 */
769 public VlanId getDefaultInternalVlan() {
770 return VlanId.vlanId((short) defaultInternalVlan);
771 }
772
773 /**
774 * Returns the Vlan id used to transport pseudowire traffic across the
775 * network.
776 *
777 * @return the pseudowire transport vlan id
778 */
779 public VlanId getPwTransportVlan() {
780 return VlanId.vlanId((short) pwTransportVlan);
Charles Chan873661e2017-11-30 15:37:50 -0800781 }
782
sangho27462c62015-05-14 00:39:53 -0700783 @Override
784 public List<Tunnel> getTunnels() {
785 return tunnelHandler.getTunnels();
786 }
787
788 @Override
sanghobd812f82015-06-29 14:58:47 -0700789 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
790 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700791 }
792
793 @Override
sanghobd812f82015-06-29 14:58:47 -0700794 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700795 for (Policy policy: policyHandler.getPolicies()) {
796 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
797 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
798 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
799 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700800 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700801 }
802 }
803 }
sanghobd812f82015-06-29 14:58:47 -0700804 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700805 }
806
807 @Override
sanghobd812f82015-06-29 14:58:47 -0700808 public PolicyHandler.Result removePolicy(Policy policy) {
809 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700810 }
811
812 @Override
sanghobd812f82015-06-29 14:58:47 -0700813 public PolicyHandler.Result createPolicy(Policy policy) {
814 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700815 }
816
817 @Override
818 public List<Policy> getPolicies() {
819 return policyHandler.getPolicies();
820 }
821
Saurav Das07c74602016-04-27 18:35:50 -0700822 @Override
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700823 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
824 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700825 }
826
827 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800828 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700829 return l2TunnelHandler.getL2Tunnels();
830 }
831
832 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800833 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700834 return l2TunnelHandler.getL2Policies();
835 }
836
Saurav Dasdebcf882018-04-06 20:16:01 -0700837 @Override
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700838 @Deprecated
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700839 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
840
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700841 // get both added and pending pseudowires
842 List<L2TunnelDescription> pseudowires = new ArrayList<>();
843 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
844 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700845 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700846
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700847 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
848
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700849 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
850 L2TunnelHandler.Result res;
851 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
852 res = addPseudowire(pw);
853 if (res != L2TunnelHandler.Result.SUCCESS) {
854 log.error("Pseudowire with id {} can not be instantiated !", res);
855 retRes = res;
856 }
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700857 }
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700858
859 return retRes;
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700860 }
861
862 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800863 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700864 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700865 }
866
867 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800868 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700869 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700870 }
871
872 @Override
Saurav Das07c74602016-04-27 18:35:50 -0700873 public void rerouteNetwork() {
874 cfgListener.configureNetwork();
Saurav Das07c74602016-04-27 18:35:50 -0700875 }
876
Charles Chand7844e52016-10-20 17:02:44 -0700877 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800878 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
879 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart61e24e12017-11-30 18:23:42 -0800880 deviceConfiguration.getRouters().forEach(device ->
881 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chand7844e52016-10-20 17:02:44 -0700882 return deviceSubnetMap;
883 }
884
Saurav Das62ae6792017-05-15 15:34:25 -0700885
886 @Override
887 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
888 if (defaultRoutingHandler != null) {
889 return defaultRoutingHandler.getCurrentEmcpSpgMap();
890 } else {
891 return null;
892 }
893 }
894
895 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700896 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDstNextObjStore() {
Saurav Das261c3002017-06-13 15:35:54 -0700897 if (dsNextObjStore != null) {
898 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700899 } else {
900 return ImmutableMap.of();
901 }
902 }
903
Saurav Dasfbe74572017-08-03 18:30:35 -0700904 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700905 public ImmutableMap<VlanNextObjectiveStoreKey, Integer> getVlanNextObjStore() {
906 if (vlanNextObjStore != null) {
907 return ImmutableMap.copyOf(vlanNextObjStore.entrySet());
908 } else {
909 return ImmutableMap.of();
910 }
911 }
912
913 @Override
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000914 public ImmutableMap<MacVlanNextObjectiveStoreKey, Integer> getMacVlanNextObjStore() {
915 if (macVlanNextObjStore != null) {
916 return ImmutableMap.copyOf(macVlanNextObjStore.entrySet());
917 } else {
918 return ImmutableMap.of();
919 }
920 }
921
922 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700923 public ImmutableMap<PortNextObjectiveStoreKey, Integer> getPortNextObjStore() {
924 if (portNextObjStore != null) {
925 return ImmutableMap.copyOf(portNextObjStore.entrySet());
926 } else {
927 return ImmutableMap.of();
928 }
929 }
930
931 @Override
932 public ImmutableMap<String, NextObjective> getPwInitNext() {
933 if (l2TunnelHandler != null) {
934 return l2TunnelHandler.getInitNext();
935 } else {
936 return ImmutableMap.of();
937 }
938 }
939
940 @Override
941 public ImmutableMap<String, NextObjective> getPwTermNext() {
942 if (l2TunnelHandler != null) {
943 return l2TunnelHandler.getTermNext();
944 } else {
945 return ImmutableMap.of();
946 }
947 }
948
949 @Override
950 public void invalidateNextObj(int nextId) {
951 if (dsNextObjStore != null) {
952 dsNextObjStore.entrySet().forEach(e -> {
953 if (e.getValue().nextId() == nextId) {
954 dsNextObjStore.remove(e.getKey());
955 }
956 });
957 }
958 if (vlanNextObjStore != null) {
959 vlanNextObjStore.entrySet().forEach(e -> {
960 if (e.getValue() == nextId) {
961 vlanNextObjStore.remove(e.getKey());
962 }
963 });
964 }
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000965 if (macVlanNextObjStore != null) {
966 macVlanNextObjStore.entrySet().forEach(e -> {
967 if (e.getValue() == nextId) {
968 macVlanNextObjStore.remove(e.getKey());
969 }
970 });
971 }
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700972 if (portNextObjStore != null) {
973 portNextObjStore.entrySet().forEach(e -> {
974 if (e.getValue() == nextId) {
975 portNextObjStore.remove(e.getKey());
976 }
977 });
978 }
979 if (mcastHandler != null) {
980 mcastHandler.removeNextId(nextId);
981 }
982 if (l2TunnelHandler != null) {
983 l2TunnelHandler.removeNextId(nextId);
984 }
985 if (xconnectService != null) {
986 xconnectService.removeNextId(nextId);
987 }
988 }
989
990 @Override
Saurav Dasfbe74572017-08-03 18:30:35 -0700991 public void verifyGroups(DeviceId id) {
992 DefaultGroupHandler gh = groupHandlerMap.get(id);
993 if (gh != null) {
994 gh.triggerBucketCorrector();
995 }
996 }
997
Saurav Das6430f412018-01-25 09:49:01 -0800998 @Override
999 public ImmutableMap<Link, Boolean> getSeenLinks() {
1000 return linkHandler.getSeenLinks();
1001 }
1002
1003 @Override
1004 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
1005 return linkHandler.getDownedPorts();
1006 }
1007
Pier Luigi0f9635b2018-01-15 18:06:43 +01001008 @Override
1009 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
Charles Chan0b1dd7e2018-08-19 19:21:46 -07001010 return mcastHandler.getNextIds(mcastIp);
Pier Luigi0f9635b2018-01-15 18:06:43 +01001011 }
1012
1013 @Override
Piere99511d2018-04-19 16:47:06 +02001014 public Map<McastRoleStoreKey, McastRole> getMcastRoles(IpAddress mcastIp, ConnectPoint sourcecp) {
1015 return mcastHandler.getMcastRoles(mcastIp, sourcecp);
1016 }
1017
1018 @Override
Pier71c55772018-04-17 17:25:22 +02001019 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
1020 ConnectPoint sourcecp) {
1021 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
1022 }
1023
1024 @Override
Pierdb27b8d2018-04-17 16:29:56 +02001025 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
1026 return mcastHandler.getMcastLeaders(mcastIp);
1027 }
1028
Charles Chanb13e0702018-04-17 18:56:53 -07001029 @Override
Harshada Chaundkar9204f312019-07-02 16:01:24 +00001030 public Map<DeviceId, List<McastFilteringObjStoreKey>> getMcastFilters() {
1031 return mcastHandler.getMcastFilters();
1032 }
1033
1034 @Override
Charles Chanb13e0702018-04-17 18:56:53 -07001035 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
1036 return defaultRoutingHandler == null ? ImmutableMap.of() :
1037 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
1038 }
1039
1040 @Override
1041 public Map<DeviceId, Boolean> getShouldProgramCache() {
1042 return defaultRoutingHandler == null ? ImmutableMap.of() :
1043 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
1044 }
1045
Charles Chan8d316332018-06-19 20:31:57 -07001046 @Override
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301047 public boolean shouldProgram(DeviceId deviceId) {
1048 return defaultRoutingHandler.shouldProgram(deviceId);
1049 }
1050
1051 @Override
Ray Milkeyb85de082017-04-05 09:42:04 -07001052 public ApplicationId appId() {
1053 return appId;
1054 }
1055
1056 /**
1057 * Returns the device configuration.
1058 *
1059 * @return device configuration
1060 */
1061 public DeviceConfiguration deviceConfiguration() {
1062 return deviceConfiguration;
1063 }
1064
1065 /**
Saurav Das261c3002017-06-13 15:35:54 -07001066 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -07001067 * Used to keep track on MPLS group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001068 *
1069 * @return next objective ID store
1070 */
Saurav Das261c3002017-06-13 15:35:54 -07001071 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
1072 dsNextObjStore() {
1073 return dsNextObjStore;
Ray Milkeyb85de082017-04-05 09:42:04 -07001074 }
1075
1076 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001077 * Per device next objective ID store with (device id + vlanid) as key.
1078 * Used to keep track on L2 flood group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001079 *
1080 * @return vlan next object store
1081 */
1082 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
1083 return vlanNextObjStore;
1084 }
1085
1086 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001087 * Per device next objective ID store with (device id + MAC address + vlan) as key.
1088 * Used to keep track on L3 Unicast group information for indirect hosts.
1089 *
1090 * @return mac vlan next object store
1091 */
1092 public EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer> macVlanNextObjStore() {
1093 return macVlanNextObjStore;
1094 }
1095
1096 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001097 * Per device next objective ID store with (device id + port + treatment + meta) as key.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001098 * Used to keep track on L2 interface group and L3 unicast group information for direct hosts.
Ray Milkeyb85de082017-04-05 09:42:04 -07001099 *
1100 * @return port next object store.
1101 */
1102 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
1103 return portNextObjStore;
1104 }
1105
1106 /**
Saurav Das261c3002017-06-13 15:35:54 -07001107 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
1108 * labeled packets should be programmed or not.
Pier Ventre7a78de22016-10-31 15:00:01 -07001109 *
1110 * @return MPLS-ECMP value
1111 */
1112 public boolean getMplsEcmp() {
1113 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
1114 .getConfig(this.appId, SegmentRoutingAppConfig.class);
1115 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
1116 }
1117
1118 /**
sangho80f1f892015-05-19 11:57:42 -07001119 * Returns the tunnel object with the tunnel ID.
1120 *
1121 * @param tunnelId Tunnel ID
1122 * @return Tunnel reference
1123 */
sangho27462c62015-05-14 00:39:53 -07001124 public Tunnel getTunnel(String tunnelId) {
1125 return tunnelHandler.getTunnel(tunnelId);
1126 }
1127
Charles Chan8d316332018-06-19 20:31:57 -07001128 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +01001129 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chan098ca202018-05-01 11:50:20 -07001130 VlanId untaggedVlanId = interfaceService.getUntaggedVlanId(connectPoint);
1131 VlanId nativeVlanId = interfaceService.getNativeVlanId(connectPoint);
Charles Chand9265a32017-06-16 15:19:24 -07001132 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
1133 }
1134
Charles Chan8d316332018-06-19 20:31:57 -07001135 @Override
1136 public Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001137 SegmentRoutingDeviceConfig deviceConfig =
1138 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1139 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
1140 }
Charles Chan8d316332018-06-19 20:31:57 -07001141
1142 @Override
Saurav Dasec683dc2018-04-27 18:42:30 -07001143 public Optional<PortNumber> getPairLocalPort(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001144 SegmentRoutingDeviceConfig deviceConfig =
1145 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1146 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
1147 }
1148
1149 /**
Charles Chanf0ae41e2017-08-23 13:55:39 -07001150 * Returns locations of given resolved route.
1151 *
1152 * @param resolvedRoute resolved route
1153 * @return locations of nexthop. Might be empty if next hop is not found
1154 */
Charles Chan19b70032019-04-17 14:20:26 -07001155 public Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
Charles Chanf0ae41e2017-08-23 13:55:39 -07001156 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
1157 return Optional.ofNullable(hostService.getHost(hostId))
1158 .map(Host::locations).orElse(Sets.newHashSet())
1159 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
1160 }
1161
1162 /**
Charles Chan90772a72017-02-08 15:52:08 -08001163 * Returns vlan port map of given device.
1164 *
1165 * @param deviceId device id
1166 * @return vlan-port multimap
1167 */
1168 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
1169 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
1170
1171 interfaceService.getInterfaces().stream()
1172 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
1173 .forEach(intf -> {
1174 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan3ed34d82017-06-22 18:03:14 -07001175 intf.vlanTagged().forEach(vlanTagged ->
1176 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
1177 );
Charles Chan90772a72017-02-08 15:52:08 -08001178 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
1179 });
1180 vlanPortMap.removeAll(VlanId.NONE);
1181
1182 return vlanPortMap;
1183 }
1184
1185 /**
Charles Chan10b0fb72017-02-02 16:20:42 -08001186 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -08001187 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -07001188 *
1189 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -08001190 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -08001191 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -07001192 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001193 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -07001194 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -08001195 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
1196 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -07001197 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -08001198 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -08001199 + "device {} not found", deviceId);
1200 return -1;
1201 }
1202 }
1203
1204 /**
1205 * Returns the next objective ID for the given portNumber, given the treatment.
1206 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -07001207 * would result in different objectives. If the next object does not exist,
1208 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -08001209 *
1210 * @param deviceId Device ID
1211 * @param portNum port number on device for which NextObjective is queried
1212 * @param treatment the actions to apply on the packets (should include outport)
1213 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -07001214 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -07001215 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -08001216 */
1217 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
1218 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -07001219 TrafficSelector meta,
1220 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -08001221 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1222 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -07001223 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -08001224 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -08001225 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
1226 + " not found", deviceId);
1227 return -1;
1228 }
1229 }
1230
Saurav Das62ae6792017-05-15 15:34:25 -07001231 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001232 * Returns the next Objective ID for the given mac and vlan, given the treatment.
1233 * There could be multiple different treatments to the same outport, which
1234 * would result in different objectives. If the next object does not exist,
1235 * and should be created, a new one is created and its id is returned.
1236 *
1237 * @param deviceId Device ID
1238 * @param macAddr mac of host for which Next ID is required.
1239 * @param vlanId vlan of host for which Next ID is required.
Ruchi Sahota07869322019-05-09 17:26:14 -04001240 * @param port port with which to create the Next Obj.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001241 * @param createIfMissing true if a next object should be created if not found
1242 * @return next objective ID or -1 if an error occurred during retrieval or creation
1243 */
1244 public int getMacVlanNextObjectiveId(DeviceId deviceId, MacAddress macAddr, VlanId vlanId,
Ruchi Sahota07869322019-05-09 17:26:14 -04001245 PortNumber port, boolean createIfMissing) {
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001246 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1247 if (ghdlr != null) {
Ruchi Sahota07869322019-05-09 17:26:14 -04001248 return ghdlr.getMacVlanNextObjectiveId(macAddr, vlanId, port, createIfMissing);
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001249 } else {
1250 log.warn("getMacVlanNextObjectiveId query - groupHandler for device {}"
1251 + " not found", deviceId);
1252 return -1;
1253 }
1254 }
1255
1256 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001257 * Updates the next objective for the given nextId .
1258 *
1259 * @param deviceId Device ID
1260 * @param hostMac mac of host for which Next obj is to be updated.
1261 * @param hostVlanId vlan of host for which Next obj is to be updated.
1262 * @param port port with which to update the Next Obj.
1263 * @param nextId of Next Obj which needs to be updated.
1264 */
1265 public void updateMacVlanTreatment(DeviceId deviceId, MacAddress hostMac,
1266 VlanId hostVlanId, PortNumber port, int nextId) {
pierf0e37ec2019-11-26 11:48:48 +01001267 // Check if we are the king of this device
1268 // just one instance should perform this update
1269 if (!defaultRoutingHandler.shouldProgram(deviceId)) {
1270 log.debug("This instance is not handling the routing towards the "
1271 + "device {}", deviceId);
1272 return;
1273 }
1274 // Get the handler and perform the update
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001275 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1276 if (ghdlr != null) {
1277 ghdlr.updateL3UcastGroupBucket(hostMac, hostVlanId, port, nextId);
1278 } else {
1279 log.warn("updateL3UcastGroupBucket query - groupHandler for device {}"
1280 + " not found", deviceId);
1281 }
1282 }
1283
1284
1285 /**
Saurav Das62ae6792017-05-15 15:34:25 -07001286 * Returns the group handler object for the specified device id.
1287 *
1288 * @param devId the device identifier
1289 * @return the groupHandler object for the device id, or null if not found
1290 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001291 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Das62ae6792017-05-15 15:34:25 -07001292 return groupHandlerMap.get(devId);
1293 }
1294
1295 /**
Saurav Dasfbe74572017-08-03 18:30:35 -07001296 * Returns the default routing handler object.
1297 *
1298 * @return the default routing handler object
1299 */
1300 public DefaultRoutingHandler getRoutingHandler() {
1301 return defaultRoutingHandler;
1302 }
1303
sangho80f11cb2015-04-01 13:05:26 -07001304 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -07001305 @Override
1306 public void process(PacketContext context) {
Charles Chan77cdde42018-05-08 21:35:50 -07001307 packetExecutor.execute(() -> processPacketInternal(context));
1308 }
sangho80f11cb2015-04-01 13:05:26 -07001309
Charles Chan77cdde42018-05-08 21:35:50 -07001310 private void processPacketInternal(PacketContext context) {
sangho80f11cb2015-04-01 13:05:26 -07001311 if (context.isHandled()) {
1312 return;
1313 }
1314
1315 InboundPacket pkt = context.inPacket();
1316 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -08001317
1318 if (ethernet == null) {
1319 return;
1320 }
1321
Saurav Das261c3002017-06-13 15:35:54 -07001322 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1323 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001324 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -07001325 log.warn("Received unexpected ARP packet on {}",
1326 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -07001327 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001328 return;
sangho80f11cb2015-04-01 13:05:26 -07001329 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -08001330 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1331 //ipHandler.addToPacketBuffer(ipv4Packet);
1332 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1333 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001334 } else {
Charles Chand041ad82017-01-13 17:20:44 -08001335 // NOTE: We don't support IP learning at this moment so this
1336 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001337 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001338 }
Pier Ventreb6a7f342016-11-26 21:05:22 -08001339 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1340 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -08001341 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -08001342 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -08001343 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1344 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1345 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1346 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1347 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1348 } else {
Saurav Das62ae6792017-05-15 15:34:25 -07001349 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -07001350 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -08001351 }
1352 } else {
1353 // NOTE: We don't support IP learning at this moment so this
1354 // is not necessary. Also it causes duplication of DHCPv6 packets.
1355 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1356 }
sangho80f11cb2015-04-01 13:05:26 -07001357 }
1358 }
1359 }
1360
sangho80f11cb2015-04-01 13:05:26 -07001361 private class InternalEventHandler implements Runnable {
Charles Chan52003922018-04-05 16:31:15 -07001362 private Event event;
1363
1364 InternalEventHandler(Event event) {
1365 this.event = event;
1366 }
1367
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001368 @Override
sangho80f11cb2015-04-01 13:05:26 -07001369 public void run() {
Charles Chan52003922018-04-05 16:31:15 -07001370 try {
1371 // TODO We should also change SR routing and PW to listen to TopologyEvents
1372 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1373 event.type() == LinkEvent.Type.LINK_UPDATED) {
1374 linkHandler.processLinkAdded((Link) event.subject());
1375 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1376 linkHandler.processLinkRemoved((Link) event.subject());
1377 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1378 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1379 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1380 DeviceId deviceId = ((Device) event.subject()).id();
1381 if (deviceService.isAvailable(deviceId)) {
1382 log.info("** DEVICE UP Processing device event {} "
1383 + "for available device {}",
1384 event.type(), ((Device) event.subject()).id());
1385 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001386 } else {
Charles Chanb8f3af52018-08-29 21:21:51 -07001387 if (event.type() == DeviceEvent.Type.DEVICE_ADDED) {
1388 // Note: For p4 devices, the device will be added but unavailable at the beginning.
1389 // The device will later on being marked as available once the pipeline is pushed
1390 // to the device.
1391 log.info("** DEVICE ADDED but unavailable. Ignore");
1392 return;
1393 }
Charles Chan52003922018-04-05 16:31:15 -07001394 log.info(" ** DEVICE DOWN Processing device event {}"
1395 + " for unavailable device {}",
1396 event.type(), ((Device) event.subject()).id());
1397 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001398 }
Charles Chan52003922018-04-05 16:31:15 -07001399 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1400 // typically these calls come when device is added first time
1401 // so port filtering rules are handled at the device_added event.
1402 // port added calls represent all ports on the device,
1403 // enabled or not.
1404 log.trace("** PORT ADDED {}/{} -> {}",
1405 ((DeviceEvent) event).subject().id(),
1406 ((DeviceEvent) event).port().number(),
1407 event.type());
1408 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1409 // these calls happen for every subsequent event
1410 // ports enabled, disabled, switch goes away, comes back
1411 log.info("** PORT UPDATED {}/{} -> {}",
1412 event.subject(),
1413 ((DeviceEvent) event).port(),
1414 event.type());
Saurav Dasec683dc2018-04-27 18:42:30 -07001415 processPortUpdatedInternal(((Device) event.subject()),
Charles Chan52003922018-04-05 16:31:15 -07001416 ((DeviceEvent) event).port());
Harshada Chaundkar9204f312019-07-02 16:01:24 +00001417 mcastHandler.processPortUpdate(((Device) event.subject()),
1418 ((DeviceEvent) event).port());
Charles Chan52003922018-04-05 16:31:15 -07001419 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1420 // Process topology event, needed for all modules relying on
1421 // topology service for path computation
1422 TopologyEvent topologyEvent = (TopologyEvent) event;
1423 log.info("Processing topology event {}, topology age {}, reasons {}",
1424 event.type(), topologyEvent.subject().time(),
1425 topologyEvent.reasons().size());
1426 topologyHandler.processTopologyChange(topologyEvent.reasons());
1427 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1428 hostHandler.processHostAddedEvent((HostEvent) event);
1429 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1430 hostHandler.processHostMovedEvent((HostEvent) event);
1431 routeHandler.processHostMovedEvent((HostEvent) event);
Charles Chana2ccb582019-11-25 09:47:22 -08001432 } else if (event.type() == HostEvent.Type.HOST_AUX_MOVED) {
1433 hostHandler.processHostMovedEvent((HostEvent) event);
1434 // TODO RouteHandler also needs to process this event in order to
1435 // support nexthops that has auxLocations
Charles Chan52003922018-04-05 16:31:15 -07001436 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1437 hostHandler.processHostRemovedEvent((HostEvent) event);
1438 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1439 hostHandler.processHostUpdatedEvent((HostEvent) event);
1440 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1441 routeHandler.processRouteAdded((RouteEvent) event);
1442 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1443 routeHandler.processRouteUpdated((RouteEvent) event);
1444 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1445 routeHandler.processRouteRemoved((RouteEvent) event);
1446 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1447 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1448 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1449 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1450 event.type() == McastEvent.Type.SINKS_ADDED ||
1451 event.type() == McastEvent.Type.SINKS_REMOVED ||
1452 event.type() == McastEvent.Type.ROUTE_ADDED ||
1453 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1454 mcastHandler.processMcastEvent((McastEvent) event);
1455 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1456 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1457 Class configClass = netcfgEvent.configClass();
1458 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1459 appCfgHandler.processAppConfigAdded(netcfgEvent);
1460 log.info("App config event .. configuring network");
1461 cfgListener.configureNetwork();
1462 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1463 log.info("Segment Routing Device Config added for {}", event.subject());
1464 cfgListener.configureNetwork();
Charles Chan52003922018-04-05 16:31:15 -07001465 } else if (configClass.equals(InterfaceConfig.class)) {
1466 log.info("Interface Config added for {}", event.subject());
1467 cfgListener.configureNetwork();
1468 } else {
1469 log.error("Unhandled config class: {}", configClass);
1470 }
1471 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1472 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1473 Class configClass = netcfgEvent.configClass();
1474 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1475 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1476 log.info("App config event .. configuring network");
1477 cfgListener.configureNetwork();
1478 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1479 log.info("Segment Routing Device Config updated for {}", event.subject());
1480 createOrUpdateDeviceConfiguration();
Charles Chan52003922018-04-05 16:31:15 -07001481 } else if (configClass.equals(InterfaceConfig.class)) {
1482 log.info("Interface Config updated for {}", event.subject());
1483 createOrUpdateDeviceConfiguration();
1484 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1485 (InterfaceConfig) netcfgEvent.prevConfig().get());
1486 } else {
1487 log.error("Unhandled config class: {}", configClass);
1488 }
1489 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1490 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1491 Class configClass = netcfgEvent.configClass();
1492 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1493 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1494 log.info("App config event .. configuring network");
1495 cfgListener.configureNetwork();
1496 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1497 // TODO Handle sr device config removal
1498 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
Charles Chan52003922018-04-05 16:31:15 -07001499 } else if (configClass.equals(InterfaceConfig.class)) {
1500 // TODO Handle interface removal
1501 log.info("InterfaceConfig removal is not handled in current implementation");
1502 } else {
1503 log.error("Unhandled config class: {}", configClass);
1504 }
Saurav Das00e553b2018-04-21 17:19:48 -07001505 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1506 MastershipEvent me = (MastershipEvent) event;
1507 DeviceId deviceId = me.subject();
1508 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1509 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1510 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1511 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1512 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1513 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan52003922018-04-05 16:31:15 -07001514 } else {
1515 log.warn("Unhandled event type: {}", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001516 }
Charles Chan52003922018-04-05 16:31:15 -07001517 } catch (Exception e) {
1518 log.error("SegmentRouting event handler thread thrown an exception: {}",
1519 e.getMessage(), e);
sangho80f11cb2015-04-01 13:05:26 -07001520 }
sangho80f11cb2015-04-01 13:05:26 -07001521 }
1522 }
1523
Saurav Dase6c448a2018-01-18 12:07:33 -08001524 void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001525 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001526
1527 // NOTE: Punt ARP/NDP even when the device is not configured.
1528 // Host learning without network config is required for CORD config generator.
1529 routingRulePopulator.populateIpPunts(device.id());
1530 routingRulePopulator.populateArpNdpPunts(device.id());
1531
Charles Chan319d1a22015-11-03 10:42:14 -08001532 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001533 log.warn("Device configuration unavailable. Device {} will be "
1534 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001535 return;
1536 }
Charles Chan72779502016-04-23 17:36:10 -07001537 processDeviceAddedInternal(device.id());
1538 }
1539
1540 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001541 // Irrespective of whether the local is a MASTER or not for this device,
1542 // we need to create a SR-group-handler instance. This is because in a
1543 // multi-instance setup, any instance can initiate forwarding/next-objectives
1544 // for any switch (even if this instance is a SLAVE or not even connected
1545 // to the switch). To handle this, a default-group-handler instance is necessary
1546 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001547 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1548 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001549 DefaultGroupHandler groupHandler;
1550 try {
1551 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001552 createGroupHandler(deviceId,
1553 appId,
1554 deviceConfiguration,
1555 linkService,
1556 flowObjectiveService,
1557 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001558 } catch (DeviceConfigNotFoundException e) {
1559 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1560 return;
1561 }
Saurav Das2b6a00f2017-12-05 15:00:23 -08001562 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan72779502016-04-23 17:36:10 -07001563 deviceId);
1564 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001565 }
Saurav Dasb149be12016-06-07 10:08:06 -07001566
Charles Chan72779502016-04-23 17:36:10 -07001567 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001568 defaultRoutingHandler.populatePortAddressingRules(deviceId);
pierf331a492020-01-07 15:39:39 +01001569 defaultRoutingHandler.purgeSeenBeforeRoutes(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001570 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001571 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001572 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001573 }
Charles Chan82ab1932016-01-30 23:22:37 -08001574
Charles Chandebfea32016-10-24 14:52:01 -07001575 appCfgHandler.init(deviceId);
Charles Chan074fae62018-04-29 18:11:37 -07001576 hostEventExecutor.execute(() -> hostHandler.init(deviceId));
1577 routeEventExecutor.execute(() -> routeHandler.init(deviceId));
sangho80f11cb2015-04-01 13:05:26 -07001578 }
1579
Saurav Dasc3604f12016-03-23 11:22:49 -07001580 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001581 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001582 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan873661e2017-11-30 15:37:50 -08001583 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan10b0fb72017-02-02 16:20:42 -08001584 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001585 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001586 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001587 macVlanNextObjStore.entrySet().stream()
1588 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1589 .forEach(entry -> macVlanNextObjStore.remove(entry.getKey()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001590 portNextObjStore.entrySet().stream()
1591 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001592 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Dase6c448a2018-01-18 12:07:33 -08001593 linkHandler.processDeviceRemoved(device);
Charles Chan17d75d82017-06-15 00:44:51 -07001594
Saurav Dasfbe74572017-08-03 18:30:35 -07001595 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1596 if (gh != null) {
1597 gh.shutdown();
1598 }
Saurav Das62ae6792017-05-15 15:34:25 -07001599 // Note that a switch going down is associated with all of its links
1600 // going down as well, but it is treated as a single switch down event
Saurav Dasdebcf882018-04-06 20:16:01 -07001601 // while the link-downs are ignored. We cannot rely on the ordering of
1602 // events - i.e we cannot expect all link-downs to come before the
1603 // switch down - so we purge all seen-links for the switch before
1604 // handling route-path changes for the switch-down
Saurav Das62ae6792017-05-15 15:34:25 -07001605 defaultRoutingHandler
Saurav Dasdc7f2752018-03-18 21:28:15 -07001606 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Das6430f412018-01-25 09:49:01 -08001607 defaultRoutingHandler.purgeEcmpGraph(device.id());
Saurav Das97241862018-02-14 14:14:54 -08001608
1609 // Cleanup all internal groupHandler stores for this device. Should be
1610 // done after all rerouting or rehashing has been completed
1611 groupHandlerMap.entrySet()
1612 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001613 }
1614
Saurav Das49368392018-04-23 18:42:12 -07001615 /**
1616 * Purge the destinationSet nextObjective store of entries with this device
1617 * as key. Erases app-level knowledge of hashed groups in this device.
1618 *
1619 * @param devId the device identifier
1620 */
Saurav Das00e553b2018-04-21 17:19:48 -07001621 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Das49368392018-04-23 18:42:12 -07001622 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Das00e553b2018-04-21 17:19:48 -07001623 dsNextObjStore.entrySet().stream()
1624 .filter(entry -> entry.getKey().deviceId().equals(devId))
1625 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1626 }
1627
Saurav Dasec683dc2018-04-27 18:42:30 -07001628 private void processPortUpdatedInternal(Device device, Port port) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001629 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1630 log.warn("Device configuration uploading. Not handling port event for"
1631 + "dev: {} port: {}", device.id(), port.number());
1632 return;
1633 }
Saurav Dasf9332192017-02-18 14:05:44 -08001634
Saurav Dasec683dc2018-04-27 18:42:30 -07001635 if (interfaceService.isConfigured(new ConnectPoint(device.id(), port.number()))) {
1636 lastEdgePortEvent = Instant.now();
1637 }
1638
Saurav Dasf9332192017-02-18 14:05:44 -08001639 if (!mastershipService.isLocalMaster(device.id())) {
1640 log.debug("Not master for dev:{} .. not handling port updated event"
1641 + "for port {}", device.id(), port.number());
1642 return;
1643 }
Saurav Dasec683dc2018-04-27 18:42:30 -07001644 processPortUpdated(device.id(), port);
1645 }
Saurav Dasf9332192017-02-18 14:05:44 -08001646
Saurav Dasec683dc2018-04-27 18:42:30 -07001647 /**
1648 * Adds or remove filtering rules for the given switchport. If switchport is
1649 * an edge facing port, additionally handles host probing and broadcast
1650 * rules. Must be called by local master of device.
1651 *
1652 * @param deviceId the device identifier
1653 * @param port the port to update
1654 */
1655 void processPortUpdated(DeviceId deviceId, Port port) {
Saurav Dasf9332192017-02-18 14:05:44 -08001656 // first we handle filtering rules associated with the port
1657 if (port.isEnabled()) {
1658 log.info("Switchport {}/{} enabled..programming filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001659 deviceId, port.number());
1660 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001661 } else {
1662 log.info("Switchport {}/{} disabled..removing filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001663 deviceId, port.number());
1664 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001665 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001666
1667 // portUpdated calls are for ports that have gone down or up. For switch
1668 // to switch ports, link-events should take care of any re-routing or
1669 // group editing necessary for port up/down. Here we only process edge ports
1670 // that are already configured.
Saurav Dasec683dc2018-04-27 18:42:30 -07001671 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
Charles Chan098ca202018-05-01 11:50:20 -07001672 VlanId untaggedVlan = interfaceService.getUntaggedVlanId(cp);
1673 VlanId nativeVlan = interfaceService.getNativeVlanId(cp);
1674 Set<VlanId> taggedVlans = interfaceService.getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001675
Saurav Das3fb28272017-03-04 16:08:47 -08001676 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001677 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasec683dc2018-04-27 18:42:30 -07001678 + "dev/port: {}/{}", deviceId, port.number());
Saurav Dasf0f592d2016-11-18 15:21:57 -08001679 return;
1680 }
Saurav Das3fb28272017-03-04 16:08:47 -08001681 if (untaggedVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001682 processEdgePort(deviceId, port, untaggedVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001683 }
1684 if (nativeVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001685 processEdgePort(deviceId, port, nativeVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001686 }
1687 if (!taggedVlans.isEmpty()) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001688 taggedVlans.forEach(tag -> processEdgePort(deviceId, port, tag, false));
Saurav Das3fb28272017-03-04 16:08:47 -08001689 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001690 }
1691
Saurav Dasec683dc2018-04-27 18:42:30 -07001692 private void processEdgePort(DeviceId deviceId, Port port, VlanId vlanId,
Saurav Das3fb28272017-03-04 16:08:47 -08001693 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001694 boolean portUp = port.isEnabled();
1695 if (portUp) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001696 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001697 port.number(), vlanId);
Charles Chan074fae62018-04-29 18:11:37 -07001698 hostEventExecutor.execute(() -> hostHandler.processPortUp(new ConnectPoint(deviceId, port.number())));
Saurav Dasf0f592d2016-11-18 15:21:57 -08001699 } else {
Saurav Dasec683dc2018-04-27 18:42:30 -07001700 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001701 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001702 }
1703
Saurav Dasec683dc2018-04-27 18:42:30 -07001704 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001705 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001706 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001707 } else {
1708 log.warn("Group handler not found for dev:{}. Not handling edge port"
Saurav Dasec683dc2018-04-27 18:42:30 -07001709 + " {} event for port:{}", deviceId,
Saurav Dasf0f592d2016-11-18 15:21:57 -08001710 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001711 }
1712 }
sangho27462c62015-05-14 00:39:53 -07001713
Charles Chan8ca5a122017-10-20 16:06:55 -07001714 private void createOrUpdateDeviceConfiguration() {
1715 if (deviceConfiguration == null) {
Saurav Dase321cff2018-02-09 17:26:45 -08001716 log.info("Creating new DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001717 deviceConfiguration = new DeviceConfiguration(this);
1718 } else {
Saurav Dase321cff2018-02-09 17:26:45 -08001719 log.info("Updating DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001720 deviceConfiguration.updateConfig();
1721 }
1722 }
1723
Charles Chanfbcb8812018-04-18 18:41:05 -07001724 private void createOrUpdateDefaultRoutingHandler() {
1725 if (defaultRoutingHandler == null) {
1726 log.info("Creating new DefaultRoutingHandler");
1727 defaultRoutingHandler = new DefaultRoutingHandler(this);
1728 } else {
1729 log.info("Updating DefaultRoutingHandler");
1730 defaultRoutingHandler.update(this);
1731 }
1732 }
1733
Pier Ventreb6a7f342016-11-26 21:05:22 -08001734 /**
1735 * Registers the given connect point with the NRS, this is necessary
1736 * to receive the NDP and ARP packets from the NRS.
1737 *
1738 * @param portToRegister connect point to register
1739 */
1740 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001741 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001742 portToRegister,
1743 neighbourHandler,
1744 appId
1745 );
1746 }
1747
Charles Chan72f556a2015-10-05 17:50:33 -07001748 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001749 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001750 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001751
Charles Chanb7f75ac2016-01-11 18:28:54 -08001752 /**
1753 * Constructs the internal network config listener.
1754 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001755 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001756 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001757 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001758 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001759 }
1760
Charles Chanb7f75ac2016-01-11 18:28:54 -08001761 /**
1762 * Reads network config and initializes related data structure accordingly.
1763 */
Charles Chan873661e2017-11-30 15:37:50 -08001764 void configureNetwork() {
Saurav Dase321cff2018-02-09 17:26:45 -08001765 log.info("Configuring network ...");
Andrea Campanella060cad82018-04-17 12:09:34 +02001766
1767 // Setting handling of network configuration events completable future
1768 // The completable future is needed because of the async behaviour of the configureNetwork,
1769 // listener registration and event arrival
1770 // Enables us to buffer the events and execute them when the configure network is done.
Ray Milkey521e4392018-11-16 15:15:14 -08001771 synchronized (networkConfigCompletionLock) {
1772 networkConfigCompletion = new CompletableFuture<>();
Andrea Campanella060cad82018-04-17 12:09:34 +02001773
Ray Milkey521e4392018-11-16 15:15:14 -08001774 // add a small delay to absorb multiple network config added notifications
1775 if (!programmingScheduled.get()) {
1776 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1777 programmingScheduled.set(true);
1778 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1779 }
1780
1781 createOrUpdateDeviceConfiguration();
1782
1783 arpHandler = new ArpHandler(srManager);
1784 icmpHandler = new IcmpHandler(srManager);
1785 ipHandler = new IpHandler(srManager);
1786 routingRulePopulator = new RoutingRulePopulator(srManager);
1787 createOrUpdateDefaultRoutingHandler();
1788
1789 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1790 groupHandlerMap, tunnelStore);
1791 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1792 flowObjectiveService,
1793 tunnelHandler, policyStore);
1794 networkConfigCompletion.complete(true);
Andrea Campanella060cad82018-04-17 12:09:34 +02001795 }
1796
Charles Chan72779502016-04-23 17:36:10 -07001797 mcastHandler.init();
Andrea Campanella060cad82018-04-17 12:09:34 +02001798
Charles Chane7c61022015-10-07 14:21:45 -07001799 }
1800
Charles Chan72f556a2015-10-05 17:50:33 -07001801 @Override
1802 public void event(NetworkConfigEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001803 if (mainEventExecutor == null) {
1804 return;
1805 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001806 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan3f4aca62018-03-21 16:57:47 -07001807 switch (event.type()) {
1808 case CONFIG_ADDED:
1809 case CONFIG_UPDATED:
1810 case CONFIG_REMOVED:
1811 log.trace("Schedule Network Config event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001812 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1813 mainEventExecutor.execute(new InternalEventHandler(event));
1814 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001815 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001816 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001817 break;
1818 default:
1819 break;
Charles Chan72f556a2015-10-05 17:50:33 -07001820 }
1821 }
Saurav Das261c3002017-06-13 15:35:54 -07001822
Charles Chanc4a68c32018-01-03 16:26:32 -08001823 @Override
1824 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase321cff2018-02-09 17:26:45 -08001825 if (event.type() == CONFIG_REGISTERED ||
1826 event.type() == CONFIG_UNREGISTERED) {
1827 log.debug("Ignore event {} due to type mismatch", event);
1828 return false;
Charles Chanc4a68c32018-01-03 16:26:32 -08001829 }
Saurav Dase321cff2018-02-09 17:26:45 -08001830
1831 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1832 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
Charles Chana01d57c2019-07-19 16:25:38 -07001833 !event.configClass().equals(InterfaceConfig.class)) {
Saurav Dase321cff2018-02-09 17:26:45 -08001834 log.debug("Ignore event {} due to class mismatch", event);
1835 return false;
1836 }
1837
1838 return true;
Charles Chanc4a68c32018-01-03 16:26:32 -08001839 }
1840
Saurav Das261c3002017-06-13 15:35:54 -07001841 private final class ConfigChange implements Runnable {
1842 @Override
1843 public void run() {
1844 programmingScheduled.set(false);
Saurav Dase321cff2018-02-09 17:26:45 -08001845 log.info("Reacting to config changes after buffer delay");
Saurav Das261c3002017-06-13 15:35:54 -07001846 for (Device device : deviceService.getDevices()) {
1847 processDeviceAdded(device);
1848 }
1849 defaultRoutingHandler.startPopulationProcess();
1850 }
1851 }
Charles Chan72f556a2015-10-05 17:50:33 -07001852 }
Charles Chanf4586112015-11-09 16:37:23 -08001853
Charles Chan3f4aca62018-03-21 16:57:47 -07001854 private class InternalLinkListener implements LinkListener {
1855 @Override
1856 public void event(LinkEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001857 if (mainEventExecutor == null) {
1858 return;
1859 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001860 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1861 event.type() == LinkEvent.Type.LINK_UPDATED ||
1862 event.type() == LinkEvent.Type.LINK_REMOVED) {
1863 log.trace("Schedule Link event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001864 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1865 mainEventExecutor.execute(new InternalEventHandler(event));
1866 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001867 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001868 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001869 }
1870 }
1871 }
1872
1873 private class InternalDeviceListener implements DeviceListener {
1874 @Override
1875 public void event(DeviceEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001876 if (mainEventExecutor == null) {
1877 return;
1878 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001879 switch (event.type()) {
1880 case DEVICE_ADDED:
1881 case PORT_UPDATED:
1882 case PORT_ADDED:
1883 case DEVICE_UPDATED:
1884 case DEVICE_AVAILABILITY_CHANGED:
1885 log.trace("Schedule Device event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001886 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1887 mainEventExecutor.execute(new InternalEventHandler(event));
1888 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001889 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001890 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001891 break;
1892 default:
1893 }
1894 }
1895 }
1896
1897 private class InternalTopologyListener implements TopologyListener {
1898 @Override
1899 public void event(TopologyEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001900 if (mainEventExecutor == null) {
1901 return;
1902 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001903 switch (event.type()) {
1904 case TOPOLOGY_CHANGED:
1905 log.trace("Schedule Topology event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001906 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1907 mainEventExecutor.execute(new InternalEventHandler(event));
1908 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001909 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001910 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001911 break;
1912 default:
1913 }
1914 }
1915 }
1916
Charles Chanf4586112015-11-09 16:37:23 -08001917 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001918 @Override
1919 public void event(HostEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001920 if (hostEventExecutor == null) {
1921 return;
1922 }
Charles Chanf4586112015-11-09 16:37:23 -08001923 switch (event.type()) {
1924 case HOST_ADDED:
Charles Chanf4586112015-11-09 16:37:23 -08001925 case HOST_MOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001926 case HOST_REMOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001927 case HOST_UPDATED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001928 log.trace("Schedule Host event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001929 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chanf4586112015-11-09 16:37:23 -08001930 break;
1931 default:
1932 log.warn("Unsupported host event type: {}", event.type());
1933 break;
1934 }
1935 }
1936 }
1937
Charles Chanc91c8782016-03-30 17:54:24 -07001938 private class InternalMcastListener implements McastListener {
1939 @Override
1940 public void event(McastEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001941 if (mcastEventExecutor == null) {
1942 return;
1943 }
Charles Chanc91c8782016-03-30 17:54:24 -07001944 switch (event.type()) {
Pier1f87aca2018-03-14 16:47:32 -07001945 case SOURCES_ADDED:
1946 case SOURCES_REMOVED:
1947 case SINKS_ADDED:
1948 case SINKS_REMOVED:
Charles Chanc91c8782016-03-30 17:54:24 -07001949 case ROUTE_REMOVED:
Pierdb27b8d2018-04-17 16:29:56 +02001950 case ROUTE_ADDED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001951 log.trace("Schedule Mcast event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001952 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi6786b922018-02-02 16:19:11 +01001953 break;
Charles Chanc91c8782016-03-30 17:54:24 -07001954 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07001955 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chanc91c8782016-03-30 17:54:24 -07001956 break;
1957 }
1958 }
1959 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001960
Charles Chandebfea32016-10-24 14:52:01 -07001961 private class InternalRouteEventListener implements RouteListener {
1962 @Override
1963 public void event(RouteEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001964 if (routeEventExecutor == null) {
1965 return;
1966 }
Charles Chandebfea32016-10-24 14:52:01 -07001967 switch (event.type()) {
1968 case ROUTE_ADDED:
Charles Chandebfea32016-10-24 14:52:01 -07001969 case ROUTE_UPDATED:
Charles Chandebfea32016-10-24 14:52:01 -07001970 case ROUTE_REMOVED:
Charles Chan910be6a2017-08-23 14:46:43 -07001971 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001972 log.trace("Schedule Route event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001973 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan910be6a2017-08-23 14:46:43 -07001974 break;
Charles Chandebfea32016-10-24 14:52:01 -07001975 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07001976 log.warn("Unsupported route event type: {}", event.type());
Charles Chandebfea32016-10-24 14:52:01 -07001977 break;
1978 }
1979 }
1980 }
Saurav Das62ae6792017-05-15 15:34:25 -07001981
Charles Chanfbcb8812018-04-18 18:41:05 -07001982 private class InternalMastershipListener implements MastershipListener {
1983 @Override
1984 public void event(MastershipEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001985 if (mainEventExecutor == null) {
1986 return;
1987 }
Charles Chanfbcb8812018-04-18 18:41:05 -07001988 switch (event.type()) {
Saurav Das00e553b2018-04-21 17:19:48 -07001989 case MASTER_CHANGED:
1990 log.debug("Mastership event: {}/{}", event.subject(),
1991 event.roleInfo());
1992 mainEventExecutor.execute(new InternalEventHandler(event));
1993 break;
1994 case BACKUPS_CHANGED:
1995 case SUSPENDED:
1996 default:
1997 log.debug("Mastership event type {} not handled", event.type());
1998 break;
Charles Chanfbcb8812018-04-18 18:41:05 -07001999 }
2000 }
2001 }
2002
Saurav Das00e553b2018-04-21 17:19:48 -07002003 class InternalClusterListener implements ClusterEventListener {
2004 private Instant lastClusterEvent = Instant.EPOCH;
2005
2006 long timeSinceLastClusterEvent() {
2007 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
2008 }
2009
2010 @Override
2011 public void event(ClusterEvent event) {
2012 switch (event.type()) {
2013 case INSTANCE_ACTIVATED:
2014 case INSTANCE_ADDED:
2015 case INSTANCE_READY:
2016 log.debug("Cluster event {} ignored", event.type());
2017 break;
2018 case INSTANCE_DEACTIVATED:
2019 case INSTANCE_REMOVED:
2020 log.info("** Cluster event {}", event.type());
2021 lastClusterEvent = Instant.now();
2022 break;
2023 default:
2024 break;
2025 }
2026
2027 }
2028
2029 }
2030
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002031 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
2032 try {
2033 Set<Interface> intfs = conf.getInterfaces();
2034 Set<Interface> prevIntfs = prevConf.getInterfaces();
2035
2036 // Now we only handle one interface config at each port.
2037 if (intfs.size() != 1 || prevIntfs.size() != 1) {
2038 log.warn("Interface update aborted - one at a time is allowed, " +
2039 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
2040 return;
2041 }
2042
Andrea Campanella060cad82018-04-17 12:09:34 +02002043 //The system is in an incoherent state, abort
2044 if (defaultRoutingHandler == null) {
2045 log.warn("Interface update aborted, defaultRoutingHandler is null");
2046 return;
2047 }
2048
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002049 Interface intf = intfs.stream().findFirst().get();
2050 Interface prevIntf = prevIntfs.stream().findFirst().get();
2051
2052 DeviceId deviceId = intf.connectPoint().deviceId();
2053 PortNumber portNum = intf.connectPoint().port();
2054
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002055 removeSubnetConfig(prevIntf.connectPoint(),
2056 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
2057 new HashSet<>(intf.ipAddressesList())));
2058
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002059 if (!prevIntf.vlanNative().equals(VlanId.NONE)
2060 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
2061 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
2062 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
2063 // Update filtering objective and L2IG group bucket
2064 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
2065 } else {
2066 // RemoveVlanNative
2067 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
2068 }
2069 }
2070
2071 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
2072 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2073 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2074 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
2075 // Update filtering objective and L2IG group bucket
2076 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
2077 } else {
2078 // RemoveVlanUntagged
2079 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
2080 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002081 }
2082
2083 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2084 // RemoveVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002085 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
2086 .filter(i -> !intf.vlanUntagged().equals(i))
2087 .filter(i -> !intf.vlanNative().equals(i))
2088 .forEach(vlanId -> updateVlanConfigInternal(
2089 deviceId, portNum, vlanId, false, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002090 }
2091
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002092 if (!intf.vlanNative().equals(VlanId.NONE)
2093 && !prevIntf.vlanNative().equals(intf.vlanNative())
2094 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2095 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
2096 // Update filtering objective and L2IG group bucket
2097 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
2098 } else {
2099 // AddVlanNative
2100 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
2101 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002102 }
2103
2104 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2105 // AddVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002106 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
2107 .filter(i -> !prevIntf.vlanUntagged().equals(i))
2108 .filter(i -> !prevIntf.vlanNative().equals(i))
2109 .forEach(vlanId -> updateVlanConfigInternal(
2110 deviceId, portNum, vlanId, false, true)
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002111 );
2112 }
2113
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002114 if (!intf.vlanUntagged().equals(VlanId.NONE)
2115 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2116 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
2117 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
2118 // Update filtering objective and L2IG group bucket
2119 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
2120 } else {
2121 // AddVlanUntagged
2122 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
2123 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002124 }
2125 addSubnetConfig(prevIntf.connectPoint(),
2126 Sets.difference(new HashSet<>(intf.ipAddressesList()),
2127 new HashSet<>(prevIntf.ipAddressesList())));
2128 } catch (ConfigException e) {
2129 log.error("Error in configuration");
2130 }
2131 }
2132
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002133 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
2134 VlanId vlanId, boolean pushVlan) {
2135 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2136 if (grpHandler == null) {
2137 log.warn("Failed to retrieve group handler for device {}", deviceId);
2138 return;
2139 }
2140
2141 // Update filtering objective for a single port
2142 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
2143 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
2144
2145 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
2146 // Update L2IG bucket of the port
2147 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
Shibu Vijayakumar632dd642018-03-01 15:45:59 -08002148 // Update bridging and unicast routing rule for each host
2149 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2150 vlanId, !pushVlan, false));
2151 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2152 vlanId, pushVlan, true));
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002153 } else {
2154 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2155 }
2156 }
2157
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002158 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
2159 VlanId vlanId, boolean pushVlan, boolean install) {
2160 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2161 if (grpHandler == null) {
2162 log.warn("Failed to retrieve group handler for device {}", deviceId);
2163 return;
2164 }
2165
2166 // Update filtering objective for a single port
2167 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
2168
2169 // Update filtering objective for multicast ingress port
2170 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
2171
2172 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
2173
2174 if (nextId != -1 && !install) {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002175 // Remove L2 Bridging rule and L3 Unicast rule to the host
Charles Chan074fae62018-04-29 18:11:37 -07002176 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2177 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002178 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
2179 // only if there is no port configured on that VLAN ID
2180 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
2181 // Remove broadcast forwarding rule for the VLAN
2182 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2183 // Remove L2FG for VLAN
2184 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
2185 } else {
Shibu Vijayakumar632dd642018-03-01 15:45:59 -08002186 // Remove a single port from L2FG
2187 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002188 }
Shibu Vijayakumar632dd642018-03-01 15:45:59 -08002189 // Remove L2IG of the port
2190 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002191 } else if (install) {
Shibu Vijayakumar632dd642018-03-01 15:45:59 -08002192 // Create L2IG of the port
2193 grpHandler.createPortNextObjective(deviceId, portNum, vlanId, pushVlan);
2194 // Create L2 Bridging rule and L3 Unicast rule to the host
2195 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2196 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002197 if (nextId != -1) {
2198 // Add a single port to L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002199 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002200 } else {
2201 // Create L2FG for VLAN
2202 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
2203 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2204 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002205 } else {
2206 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2207 }
2208 }
2209
2210 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2211 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2212 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2213
2214 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2215 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2216 .filter(intf -> !intf.connectPoint().equals(cp))
2217 .flatMap(intf -> intf.ipAddressesList().stream())
2218 .collect(Collectors.toSet());
2219 // 1. Partial subnet population
2220 // Remove routing rules for removed subnet from previous configuration,
2221 // which does not also exist in other interfaces in the same device
2222 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2223 .map(InterfaceIpAddress::subnetAddress)
2224 .collect(Collectors.toSet());
2225
Shibu Vijayakumar5e26f8c2020-01-07 11:45:09 +00002226 Set<IpPrefix> subnetsToBeRevoked = ipPrefixSet.stream()
2227 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2228 .collect(Collectors.toSet());
2229
2230 // Check if any of the subnets to be revoked is configured in the pairDevice.
2231 // If any, repopulate the subnet with pairDevice connectPoint instead of revoking.
2232 Optional<DeviceId> pairDevice = getPairDeviceId(cp.deviceId());
2233 if (pairDevice.isPresent()) {
2234 Set<IpPrefix> pairDeviceIpPrefix = getDeviceSubnetMap().get(pairDevice.get());
2235
2236 Set<IpPrefix> subnetsExistingInPairDevice = subnetsToBeRevoked.stream()
2237 .filter(ipPrefix -> pairDeviceIpPrefix.contains(ipPrefix))
2238 .collect(Collectors.toSet());
2239
2240 // Update the subnets existing in pair device with pair device connect point.
2241 if (!subnetsExistingInPairDevice.isEmpty()) {
2242 // PortNumber of connect point is not relevant in populate subnet and hence providing as ANY.
2243 ConnectPoint pairDeviceCp = new ConnectPoint(pairDevice.get(), PortNumber.ANY);
2244 log.debug("Updating the subnets: {} with pairDevice connectPoint as it exists in the Pair device: {}",
2245 subnetsExistingInPairDevice, pairDeviceCp);
2246 defaultRoutingHandler.populateSubnet(Collections.singleton(pairDeviceCp), subnetsExistingInPairDevice);
2247 }
2248
2249 // Remove only the subnets that are not configured in the pairDevice.
2250 subnetsToBeRevoked = Sets.difference(subnetsToBeRevoked, subnetsExistingInPairDevice);
2251 }
2252
2253 if (!subnetsToBeRevoked.isEmpty()) {
2254 log.debug("Removing subnets for connectPoint: {}, subnets: {}", cp, subnetsToBeRevoked);
2255 defaultRoutingHandler.revokeSubnet(subnetsToBeRevoked);
2256 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002257
2258 // 2. Interface IP punts
2259 // Remove IP punts for old Intf address
2260 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2261 .map(InterfaceIpAddress::ipAddress)
2262 .collect(Collectors.toSet());
2263 ipAddressSet.stream()
2264 .map(InterfaceIpAddress::ipAddress)
2265 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2266 .forEach(interfaceIpAddress ->
2267 routingRulePopulator.revokeSingleIpPunts(
2268 cp.deviceId(), interfaceIpAddress));
2269
2270 // 3. Host unicast routing rule
2271 // Remove unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002272 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002273 }
2274
2275 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2276 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2277 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2278
2279 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2280 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2281 .filter(intf -> !intf.connectPoint().equals(cp))
2282 .flatMap(intf -> intf.ipAddressesList().stream())
2283 .collect(Collectors.toSet());
2284 // 1. Partial subnet population
2285 // Add routing rules for newly added subnet, which does not also exist in
2286 // other interfaces in the same device
2287 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2288 .map(InterfaceIpAddress::subnetAddress)
2289 .collect(Collectors.toSet());
Shibu Vijayakumar5e26f8c2020-01-07 11:45:09 +00002290 Set<IpPrefix> subnetsToBePopulated = ipPrefixSet.stream()
2291 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2292 .collect(Collectors.toSet());
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002293
Shibu Vijayakumar5e26f8c2020-01-07 11:45:09 +00002294 if (!subnetsToBePopulated.isEmpty()) {
2295 log.debug("Adding subnets for connectPoint: {}, subnets: {}", cp, subnetsToBePopulated);
2296
2297 // check if pair-device has the same subnet configured?
2298 Optional<DeviceId> pairDevice = getPairDeviceId(cp.deviceId());
2299 if (pairDevice.isPresent()) {
2300 Set<IpPrefix> pairDeviceIpPrefix = getDeviceSubnetMap().get(pairDevice.get());
2301
2302 Set<IpPrefix> subnetsToBePopulatedAsDualHomed = subnetsToBePopulated.stream()
2303 .filter(ipPrefix -> pairDeviceIpPrefix.contains(ipPrefix))
2304 .collect(Collectors.toSet());
2305 Set<IpPrefix> subnetsToBePopulatedAsSingleHomed = Sets.difference(subnetsToBePopulated,
2306 subnetsToBePopulatedAsDualHomed);
2307
2308 if (!subnetsToBePopulatedAsSingleHomed.isEmpty()) {
2309 defaultRoutingHandler.populateSubnet(
2310 Collections.singleton(cp),
2311 subnetsToBePopulatedAsSingleHomed);
2312 }
2313
2314 if (!subnetsToBePopulatedAsDualHomed.isEmpty()) {
2315 Set<ConnectPoint> cpts = new HashSet<>();
2316 cpts.add(cp);
2317 // As Subnets is DualHomed adding the pairDevice also as ConnectPoint.
2318 // PortNumber of connect point is not relevant in populate subnet and hence providing as ANY.
2319 ConnectPoint pairCp = new ConnectPoint(pairDevice.get(), PortNumber.ANY);
2320 cpts.add(pairCp);
2321
2322 log.debug("Adding DualHomed subnets for connectPoint: {} and its pair device: {}, subnets: {}",
2323 cp, pairCp, subnetsToBePopulatedAsDualHomed);
2324
2325 // populating the subnets as DualHomed
2326 defaultRoutingHandler.populateSubnet(
2327 cpts,
2328 subnetsToBePopulated);
2329
2330 // revoking the subnets populated in the device as it is now Dualhomed.
2331 defaultRoutingHandler.revokeSubnet(Collections.singleton(cp.deviceId()),
2332 subnetsToBePopulatedAsDualHomed);
2333 }
2334 } else {
2335 defaultRoutingHandler.populateSubnet(
2336 Collections.singleton(cp),
2337 subnetsToBePopulated);
2338 }
2339 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002340
2341 // 2. Interface IP punts
2342 // Add IP punts for new Intf address
2343 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2344 .map(InterfaceIpAddress::ipAddress)
2345 .collect(Collectors.toSet());
2346 ipAddressSet.stream()
2347 .map(InterfaceIpAddress::ipAddress)
2348 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2349 .forEach(interfaceIpAddress ->
2350 routingRulePopulator.populateSingleIpPunts(
2351 cp.deviceId(), interfaceIpAddress));
2352
2353 // 3. Host unicast routing rule
2354 // Add unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002355 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002356 }
sangho80f11cb2015-04-01 13:05:26 -07002357}