blob: 6d2b8cc25a89f62edfe4189f72367ae2b782da83 [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;
Pier Luigi69f774d2018-02-28 12:10:50 +0100102import org.onosproject.segmentrouting.mcast.McastHandler;
103import org.onosproject.segmentrouting.mcast.McastRole;
Piere99511d2018-04-19 16:47:06 +0200104import org.onosproject.segmentrouting.mcast.McastRoleStoreKey;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700105import org.onosproject.segmentrouting.mcast.McastStoreKey;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700106import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700107import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800108import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700109import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800110import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700111import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
Ray Milkeyae0068a2017-08-15 11:02:29 -0700112import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800113import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Saurav Das261c3002017-06-13 15:35:54 -0700114import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Carmelo Cascone61004262020-01-16 13:36:02 -0800115import org.onosproject.segmentrouting.storekey.MacVlanNextObjectiveStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -0700116import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -0800117import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -0700118import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Charles Chan8d316332018-06-19 20:31:57 -0700119import org.onosproject.segmentrouting.xconnect.api.XconnectService;
Jonathan Hart54541d12016-04-12 15:39:44 -0700120import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700121import org.onosproject.store.service.EventuallyConsistentMap;
122import org.onosproject.store.service.EventuallyConsistentMapBuilder;
123import org.onosproject.store.service.StorageService;
124import org.onosproject.store.service.WallClockTimestamp;
Charles Chan873661e2017-11-30 15:37:50 -0800125import org.osgi.service.component.ComponentContext;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700126import org.osgi.service.component.annotations.Activate;
127import org.osgi.service.component.annotations.Component;
128import org.osgi.service.component.annotations.Deactivate;
129import org.osgi.service.component.annotations.Modified;
130import org.osgi.service.component.annotations.Reference;
131import org.osgi.service.component.annotations.ReferenceCardinality;
Charles Chanb4f9cd12019-02-27 11:46:32 -0800132import org.osgi.service.component.annotations.ReferencePolicy;
sangho80f11cb2015-04-01 13:05:26 -0700133import org.slf4j.Logger;
134import org.slf4j.LoggerFactory;
135
Saurav Das00e553b2018-04-21 17:19:48 -0700136import java.time.Instant;
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700137import java.util.ArrayList;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800138import java.util.Collections;
Charles Chan873661e2017-11-30 15:37:50 -0800139import java.util.Dictionary;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800140import java.util.HashSet;
141import java.util.List;
142import java.util.Map;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800143import java.util.Optional;
144import java.util.Set;
Andrea Campanella060cad82018-04-17 12:09:34 +0200145import java.util.concurrent.CompletableFuture;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800146import java.util.concurrent.ConcurrentHashMap;
Andrea Campanella060cad82018-04-17 12:09:34 +0200147import java.util.concurrent.CopyOnWriteArrayList;
Charles Chan77cdde42018-05-08 21:35:50 -0700148import java.util.concurrent.ExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800149import java.util.concurrent.Executors;
150import java.util.concurrent.ScheduledExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800151import java.util.concurrent.TimeUnit;
152import java.util.concurrent.atomic.AtomicBoolean;
153import java.util.stream.Collectors;
sangho80f11cb2015-04-01 13:05:26 -0700154
Charles Chand6d25332016-02-26 22:19:52 -0800155import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800156import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700157import static org.onlab.util.Tools.groupedThreads;
Saurav Dase321cff2018-02-09 17:26:45 -0800158import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
159import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700160import static org.onosproject.segmentrouting.OsgiPropertyConstants.ACTIVE_PROBING_DEFAULT;
161import static org.onosproject.segmentrouting.OsgiPropertyConstants.DEFAULT_INTERNAL_VLAN_DEFAULT;
162import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ACTIVE_PROBING;
163import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_DEFAULT_INTERNAL_VLAN;
164import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_PW_TRANSPORT_VLAN;
165import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_RESPOND_TO_UNKNOWN_HOSTS;
166import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_DOUBLE_TAGGED_HOSTS;
psneha86e60d32019-03-26 06:31:41 -0400167import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_SIMPLIFICATION;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700168import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SINGLE_HOMED_DOWN;
Ray Milkeye96d0de2018-11-02 17:12:21 -0700169import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SYMMETRIC_PROBING;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700170import static org.onosproject.segmentrouting.OsgiPropertyConstants.PW_TRANSPORT_VLAN_DEFAULT;
171import static org.onosproject.segmentrouting.OsgiPropertyConstants.RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
172import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
psneha86e60d32019-03-26 06:31:41 -0400173import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_SIMPLIFICATION_DEFAULT;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700174import static org.onosproject.segmentrouting.OsgiPropertyConstants.SINGLE_HOMED_DOWN_DEFAULT;
Ray Milkeye96d0de2018-11-02 17:12:21 -0700175import static org.onosproject.segmentrouting.OsgiPropertyConstants.SYMMETRIC_PROBING_DEFAULT;
Charles Chand6d25332016-02-26 22:19:52 -0800176
Charles Chanb7f75ac2016-01-11 18:28:54 -0800177/**
178 * Segment routing manager.
179 */
Ray Milkey05edbfc2018-10-23 14:23:16 -0700180@Component(
181 immediate = true,
182 service = SegmentRoutingService.class,
183 property = {
184 PROP_ACTIVE_PROBING + ":Boolean=" + ACTIVE_PROBING_DEFAULT,
185 PROP_SINGLE_HOMED_DOWN + ":Boolean=" + SINGLE_HOMED_DOWN_DEFAULT,
186 PROP_RESPOND_TO_UNKNOWN_HOSTS + ":Boolean=" + RESPOND_TO_UNKNOWN_HOSTS_DEFAULT,
187 PROP_ROUTE_DOUBLE_TAGGED_HOSTS + ":Boolean=" + ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT,
188 PROP_DEFAULT_INTERNAL_VLAN + ":Integer=" + DEFAULT_INTERNAL_VLAN_DEFAULT,
189 PROP_PW_TRANSPORT_VLAN + ":Integer=" + PW_TRANSPORT_VLAN_DEFAULT,
psneha86e60d32019-03-26 06:31:41 -0400190 PROP_SYMMETRIC_PROBING + ":Boolean=" + SYMMETRIC_PROBING_DEFAULT,
191 PROP_ROUTE_SIMPLIFICATION + ":Boolean=" + ROUTE_SIMPLIFICATION_DEFAULT
Ray Milkey05edbfc2018-10-23 14:23:16 -0700192 }
193)
sangho27462c62015-05-14 00:39:53 -0700194public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700195
Charles Chan46fdfaf2016-11-09 20:51:44 -0800196 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan910be6a2017-08-23 14:46:43 -0700197 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sangho80f11cb2015-04-01 13:05:26 -0700198
Ray Milkey2bd24a92018-08-17 14:54:17 -0700199 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700200 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700201
Ray Milkey2bd24a92018-08-17 14:54:17 -0700202 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chancf789822019-03-22 10:04:27 -0700203 public NeighbourResolutionService neighbourResolutionService;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800204
Ray Milkey2bd24a92018-08-17 14:54:17 -0700205 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100206 public CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700207
Ray Milkey2bd24a92018-08-17 14:54:17 -0700208 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700209 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700210
Ray Milkey2bd24a92018-08-17 14:54:17 -0700211 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700212 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700213
Ray Milkey2bd24a92018-08-17 14:54:17 -0700214 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chanc6bcdf92018-06-01 16:33:48 -0700215 HostProbingService probingService;
Charles Chan873661e2017-11-30 15:37:50 -0800216
Ray Milkey2bd24a92018-08-17 14:54:17 -0700217 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100218 public DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700219
Ray Milkey2bd24a92018-08-17 14:54:17 -0700220 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Saurav Dase6c448a2018-01-18 12:07:33 -0800221 DeviceAdminService deviceAdminService;
222
Ray Milkey2bd24a92018-08-17 14:54:17 -0700223 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800224 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700225
Ray Milkey2bd24a92018-08-17 14:54:17 -0700226 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100227 public LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700228
Ray Milkey2bd24a92018-08-17 14:54:17 -0700229 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800230 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700231
Ray Milkey2bd24a92018-08-17 14:54:17 -0700232 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800233 public StorageService storageService;
Charles Chandebfea32016-10-24 14:52:01 -0700234
Ray Milkey2bd24a92018-08-17 14:54:17 -0700235 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100236 public MulticastRouteService multicastRouteService;
Charles Chandebfea32016-10-24 14:52:01 -0700237
Ray Milkey2bd24a92018-08-17 14:54:17 -0700238 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan83163812018-01-09 13:45:07 -0800239 public TopologyService topologyService;
Charles Chandebfea32016-10-24 14:52:01 -0700240
Ray Milkey2bd24a92018-08-17 14:54:17 -0700241 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan19b70032019-04-17 14:20:26 -0700242 public RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800243
Ray Milkey2bd24a92018-08-17 14:54:17 -0700244 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800245 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800246
Ray Milkey2bd24a92018-08-17 14:54:17 -0700247 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800248 public InterfaceService interfaceService;
249
Ray Milkey2bd24a92018-08-17 14:54:17 -0700250 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi580fd8a2018-01-16 10:47:50 +0100251 public ClusterService clusterService;
252
Ray Milkey2bd24a92018-08-17 14:54:17 -0700253 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pierdb27b8d2018-04-17 16:29:56 +0200254 public WorkPartitionService workPartitionService;
Pier Luigi580fd8a2018-01-16 10:47:50 +0100255
Ray Milkey2bd24a92018-08-17 14:54:17 -0700256 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700257 public LeadershipService leadershipService;
258
Charles Chanb4f9cd12019-02-27 11:46:32 -0800259 @Reference(cardinality = ReferenceCardinality.OPTIONAL,
Charles Chanb4f9cd12019-02-27 11:46:32 -0800260 policy = ReferencePolicy.DYNAMIC)
Carmelo Cascone61004262020-01-16 13:36:02 -0800261 public volatile XconnectService xconnectService;
Charles Chan8d316332018-06-19 20:31:57 -0700262
Ray Milkey05edbfc2018-10-23 14:23:16 -0700263 /** Enable active probing to discover dual-homed hosts. */
264 boolean activeProbing = ACTIVE_PROBING_DEFAULT;
Charles Chan873661e2017-11-30 15:37:50 -0800265
Ray Milkeye96d0de2018-11-02 17:12:21 -0700266 /** Enable only send probe on the same port number of the pair device. */
267 boolean symmetricProbing = SYMMETRIC_PROBING_DEFAULT;
Mayank Tiwarif7942402018-10-29 18:27:35 -0400268
Ray Milkey05edbfc2018-10-23 14:23:16 -0700269 /** Enable administratively taking down single-homed hosts. */
270 boolean singleHomedDown = SINGLE_HOMED_DOWN_DEFAULT;
Saurav Dasec683dc2018-04-27 18:42:30 -0700271
Ray Milkey05edbfc2018-10-23 14:23:16 -0700272 /** Enable this to respond to ARP/NDP requests from unknown hosts. */
273 boolean respondToUnknownHosts = RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700274
Ray Milkey05edbfc2018-10-23 14:23:16 -0700275 /** Program flows and groups to pop and route double tagged hosts. */
276 boolean routeDoubleTaggedHosts = ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
Charles Chan4eb73bb2018-07-19 14:55:31 -0700277
Ray Milkey05edbfc2018-10-23 14:23:16 -0700278 /** internal vlan assigned by default to unconfigured ports. */
279 private int defaultInternalVlan = DEFAULT_INTERNAL_VLAN_DEFAULT;
Saurav Das9bf49582018-08-13 15:34:26 -0700280
Ray Milkey05edbfc2018-10-23 14:23:16 -0700281 /** vlan used for transport of pseudowires between switches. */
282 private int pwTransportVlan = PW_TRANSPORT_VLAN_DEFAULT;
Saurav Das9bf49582018-08-13 15:34:26 -0700283
psneha86e60d32019-03-26 06:31:41 -0400284 /** Enabling route simplification. */
285 boolean routeSimplification = ROUTE_SIMPLIFICATION_DEFAULT;
286
Charles Chandebfea32016-10-24 14:52:01 -0700287 ArpHandler arpHandler = null;
288 IcmpHandler icmpHandler = null;
289 IpHandler ipHandler = null;
290 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700291 ApplicationId appId;
292 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700293
Charles Chandebfea32016-10-24 14:52:01 -0700294 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700295 private TunnelHandler tunnelHandler = null;
296 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700297 private InternalPacketProcessor processor = null;
298 private InternalLinkListener linkListener = null;
299 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700300 private AppConfigHandler appCfgHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800301 McastHandler mcastHandler = null;
302 HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800303 private RouteHandler routeHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800304 LinkHandler linkHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800305 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800306 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigid8a15162018-02-15 16:33:08 +0100307 private TopologyHandler topologyHandler = null;
Charles Chan82ab1932016-01-30 23:22:37 -0800308 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700309 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
310 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700311 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigid8a15162018-02-15 16:33:08 +0100312 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chanfbcb8812018-04-18 18:41:05 -0700313 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
Saurav Das00e553b2018-04-21 17:19:48 -0700314 final InternalClusterListener clusterListener = new InternalClusterListener();
Andrea Campanella060cad82018-04-17 12:09:34 +0200315 //Completable future for network configuration process to buffer config events handling during activation
316 private CompletableFuture<Boolean> networkConfigCompletion = null;
Ray Milkey521e4392018-11-16 15:15:14 -0800317 private final Object networkConfigCompletionLock = new Object();
Charles Chan39b75522018-04-21 00:44:29 -0700318 private List<Event> queuedEvents = new CopyOnWriteArrayList<>();
sangho80f11cb2015-04-01 13:05:26 -0700319
Charles Chan52003922018-04-05 16:31:15 -0700320 // Handles device, link, topology and network config events
Charles Chanfbcb8812018-04-18 18:41:05 -0700321 private ScheduledExecutorService mainEventExecutor;
sangho80f11cb2015-04-01 13:05:26 -0700322
Charles Chanfbcb8812018-04-18 18:41:05 -0700323 // Handles host, route and mcast events respectively
324 private ScheduledExecutorService hostEventExecutor;
325 private ScheduledExecutorService routeEventExecutor;
326 private ScheduledExecutorService mcastEventExecutor;
Charles Chan77cdde42018-05-08 21:35:50 -0700327 private ExecutorService packetExecutor;
Charles Chana7601c92019-01-23 15:03:17 -0800328 ExecutorService neighborExecutor;
Charles Chan52003922018-04-05 16:31:15 -0700329
330 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chanb7f75ac2016-01-11 18:28:54 -0800331 /**
Saurav Das261c3002017-06-13 15:35:54 -0700332 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700333 * Used to keep track on MPLS group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800334 */
Charles Chan873661e2017-11-30 15:37:50 -0800335 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das261c3002017-06-13 15:35:54 -0700336 dsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800337 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700338 * Per device next objective ID store with (device id + vlanid) as key.
339 * Used to keep track on L2 flood group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800340 */
Charles Chan873661e2017-11-30 15:37:50 -0800341 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800342 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800343 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700344 * Per device next objective ID store with (device id + port + treatment + meta) as key.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000345 * Used to keep track on L2 interface group and L3 unicast group information for direct hosts.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800346 */
Charles Chan873661e2017-11-30 15:37:50 -0800347 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800348 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800349
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700350 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000351 * Per device next objective ID store with (device id + MAC address + vlan) as key.
352 * Used to keep track of L3 unicast group for indirect hosts.
353 */
354 private EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer>
355 macVlanNextObjStore = null;
356
Saurav Das2d94d312015-11-24 23:21:05 -0800357 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
358 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700359
Saurav Das261c3002017-06-13 15:35:54 -0700360 private AtomicBoolean programmingScheduled = new AtomicBoolean();
361
Charles Chanc91c8782016-03-30 17:54:24 -0700362 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700363 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
364 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700365 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700366 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800367 public SegmentRoutingDeviceConfig createConfig() {
368 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700369 }
370 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800371
Charles Chanc91c8782016-03-30 17:54:24 -0700372 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700373 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
374 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700375 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800376 @Override
377 public SegmentRoutingAppConfig createConfig() {
378 return new SegmentRoutingAppConfig();
379 }
380 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800381
Charles Chanc91c8782016-03-30 17:54:24 -0700382 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700383 new ConfigFactory<ApplicationId, McastConfig>(
384 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700385 McastConfig.class, "multicast") {
386 @Override
387 public McastConfig createConfig() {
388 return new McastConfig();
389 }
390 };
391
Charles Chan1963f4f2016-02-18 14:22:42 -0800392 /**
393 * Segment Routing App ID.
394 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800395 public static final String APP_NAME = "org.onosproject.segmentrouting";
Saurav Das7c305372015-10-28 12:39:42 -0700396
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700397 /**
398 * Minumum and maximum value of dummy VLAN ID to be allocated.
399 */
400 public static final int MIN_DUMMY_VLAN_ID = 2;
401 public static final int MAX_DUMMY_VLAN_ID = 4093;
402
Charles Chana7601c92019-01-23 15:03:17 -0800403 private static final int DEFAULT_POOL_SIZE = 32;
404
Saurav Dasec683dc2018-04-27 18:42:30 -0700405 Instant lastEdgePortEvent = Instant.EPOCH;
406
sangho80f11cb2015-04-01 13:05:26 -0700407 @Activate
Charles Chan873661e2017-11-30 15:37:50 -0800408 protected void activate(ComponentContext context) {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800409 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700410
Charles Chana7601c92019-01-23 15:03:17 -0800411 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(
412 groupedThreads("onos/sr", "event-main-%d", log));
413 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(
414 groupedThreads("onos/sr", "event-host-%d", log));
415 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(
416 groupedThreads("onos/sr", "event-route-%d", log));
417 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(
418 groupedThreads("onos/sr", "event-mcast-%d", log));
419 packetExecutor = Executors.newSingleThreadExecutor(groupedThreads("onos/sr", "packet-%d", log));
420 neighborExecutor = Executors.newFixedThreadPool(DEFAULT_POOL_SIZE,
421 groupedThreads("onos/sr", "neighbor-%d", log));
Charles Chanfbcb8812018-04-18 18:41:05 -0700422
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700423 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700424 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700425 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700426 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700427 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700428 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700429 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700430 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700431 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700432
Charles Chan10b0fb72017-02-02 16:20:42 -0800433 log.debug("Creating EC map vlannextobjectivestore");
434 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
435 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
436 vlanNextObjStore = vlanNextObjMapBuilder
437 .withName("vlannextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700438 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700439 .withTimestampProvider((k, v) -> new WallClockTimestamp())
440 .build();
441
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000442 log.debug("Creating EC map macvlannextobjectivestore");
443 EventuallyConsistentMapBuilder<MacVlanNextObjectiveStoreKey, Integer>
444 macVlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
445 macVlanNextObjStore = macVlanNextObjMapBuilder
446 .withName("macvlannextobjectivestore")
447 .withSerializer(createSerializer())
448 .withTimestampProvider((k, v) -> new WallClockTimestamp())
449 .build();
450
Saurav Das2d94d312015-11-24 23:21:05 -0800451 log.debug("Creating EC map subnetnextobjectivestore");
452 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
453 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
454 portNextObjStore = portNextObjMapBuilder
455 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700456 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800457 .withTimestampProvider((k, v) -> new WallClockTimestamp())
458 .build();
459
sangho4a5c42a2015-05-20 22:16:38 -0700460 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
461 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700462 tunnelStore = tunnelMapBuilder
463 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700464 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700465 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700466 .build();
467
468 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
469 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700470 policyStore = policyMapBuilder
471 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700472 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700473 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700474 .build();
475
Charles Chan97979802020-02-26 15:04:44 -0800476 processor = new InternalPacketProcessor();
477 linkListener = new InternalLinkListener();
478 deviceListener = new InternalDeviceListener();
479 appCfgHandler = new AppConfigHandler(this);
480 mcastHandler = new McastHandler(this);
481 hostHandler = new HostHandler(this);
482 linkHandler = new LinkHandler(this);
483 routeHandler = new RouteHandler(this);
484 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
485 l2TunnelHandler = new DefaultL2TunnelHandler(this);
486 topologyHandler = new TopologyHandler(this);
487
Saurav Dasc3604f12016-03-23 11:22:49 -0700488 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700489 "purgeOnDisconnection", "true", false);
Saurav Dasc3604f12016-03-23 11:22:49 -0700490 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700491 "purgeOnDisconnection", "true", false);
Pier Luigib9632ba2017-01-12 18:14:58 -0800492 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700493 "requestInterceptsEnabled", "false", false);
Charles Chanc1909e12017-08-08 15:13:37 -0700494 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700495 "requestInterceptsEnabled", "false", false);
Charles Chan9597f8d2017-07-24 15:56:10 -0700496 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700497 "arpEnabled", "false", false);
Pier Luigi0ebeb312017-02-02 22:31:34 -0800498 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700499 "greedyLearningIpv6", "true", false);
Charles Chancf8ea472017-02-28 15:15:17 -0800500 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700501 "forceUnprovision", "true", false);
Charles Chanc7b73c72017-08-10 16:57:28 -0700502 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan0fa51db2019-04-10 17:12:20 -0700503 "distributed", "true", false);
Charles Chan804772f2017-08-14 11:42:11 -0700504 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700505 "multihomingEnabled", "true", false);
Charles Chan0c9c19f2017-11-29 19:54:20 -0800506 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700507 "staleLinkAge", "15000", false);
Charles Chan0c9c19f2017-11-29 19:54:20 -0800508 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700509 "allowDuplicateIps", "false", false);
Yi Tsenga7da8d82018-08-19 03:09:54 +0800510 // For P4 switches
511 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700512 "fallbackFlowPollFrequency", "4", false);
Yi Tsenga7da8d82018-08-19 03:09:54 +0800513 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700514 "fallbackGroupPollFrequency", "3", false);
Charles Chan873661e2017-11-30 15:37:50 -0800515 compCfgService.registerProperties(getClass());
516 modified(context);
Saurav Dasc3604f12016-03-23 11:22:49 -0700517
Charles Chand6d25332016-02-26 22:19:52 -0800518 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700519 cfgService.registerConfigFactory(deviceConfigFactory);
520 cfgService.registerConfigFactory(appConfigFactory);
521 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase321cff2018-02-09 17:26:45 -0800522 log.info("Configuring network before adding listeners");
Andrea Campanella060cad82018-04-17 12:09:34 +0200523
Charles Chan6961f222018-01-04 14:26:07 -0800524 cfgListener.configureNetwork();
525
Charles Chan82ab1932016-01-30 23:22:37 -0800526 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700527 packetService.addProcessor(processor, PacketProcessor.director(2));
528 linkService.addListener(linkListener);
529 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700530 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700531 routeService.addListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100532 topologyService.addListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700533 mastershipService.addListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700534 clusterService.addListener(clusterListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700535
Charles Chanc12c3d02018-03-09 15:53:44 -0800536 linkHandler.init();
Andreas Pantelopoulos75eef062018-01-11 07:53:48 -0800537 l2TunnelHandler.init();
538
Ray Milkey521e4392018-11-16 15:15:14 -0800539 synchronized (networkConfigCompletionLock) {
540 networkConfigCompletion.whenComplete((value, ex) -> {
541 //setting to null for easier fall through
542 networkConfigCompletion = null;
543 //process all queued events
544 queuedEvents.forEach(event -> {
545 mainEventExecutor.execute(new InternalEventHandler(event));
546 });
Andrea Campanella060cad82018-04-17 12:09:34 +0200547 });
Ray Milkey521e4392018-11-16 15:15:14 -0800548 }
Andrea Campanella060cad82018-04-17 12:09:34 +0200549
sangho80f11cb2015-04-01 13:05:26 -0700550 log.info("Started");
551 }
552
Saurav Dase6c448a2018-01-18 12:07:33 -0800553 KryoNamespace.Builder createSerializer() {
Jonathan Hart54541d12016-04-12 15:39:44 -0700554 return new KryoNamespace.Builder()
555 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700556 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800557 VlanNextObjectiveStoreKey.class,
558 DestinationSet.class,
Andreas Pantelopoulos92998c92018-05-29 13:11:14 -0700559 DestinationSet.DestinationSetType.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800560 NextNeighbors.class,
561 Tunnel.class,
562 DefaultTunnel.class,
563 Policy.class,
564 TunnelPolicy.class,
565 Policy.Type.class,
566 PortNextObjectiveStoreKey.class,
567 XConnectStoreKey.class,
568 L2Tunnel.class,
569 L2TunnelPolicy.class,
570 DefaultL2Tunnel.class,
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700571 DefaultL2TunnelPolicy.class,
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000572 MacVlanNextObjectiveStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700573 );
574 }
575
sangho80f11cb2015-04-01 13:05:26 -0700576 @Deactivate
577 protected void deactivate() {
Charles Chanfbcb8812018-04-18 18:41:05 -0700578 mainEventExecutor.shutdown();
579 hostEventExecutor.shutdown();
580 routeEventExecutor.shutdown();
581 mcastEventExecutor.shutdown();
Charles Chan77cdde42018-05-08 21:35:50 -0700582 packetExecutor.shutdown();
Charles Chana7601c92019-01-23 15:03:17 -0800583 neighborExecutor.shutdown();
Charles Chanfbcb8812018-04-18 18:41:05 -0700584
Ray Milkeyaee4d3e2018-05-11 09:59:19 -0700585 mainEventExecutor = null;
586 hostEventExecutor = null;
587 routeEventExecutor = null;
588 mcastEventExecutor = null;
589 packetExecutor = null;
Charles Chana7601c92019-01-23 15:03:17 -0800590 neighborExecutor = null;
Ray Milkeyaee4d3e2018-05-11 09:59:19 -0700591
Charles Chan72f556a2015-10-05 17:50:33 -0700592 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700593 cfgService.unregisterConfigFactory(deviceConfigFactory);
594 cfgService.unregisterConfigFactory(appConfigFactory);
595 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan873661e2017-11-30 15:37:50 -0800596 compCfgService.unregisterProperties(getClass(), false);
Charles Chan72f556a2015-10-05 17:50:33 -0700597
Charles Chanfd48c222017-06-19 00:43:31 -0700598 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700599 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700600 linkService.removeListener(linkListener);
601 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700602 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700603 routeService.removeListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100604 topologyService.removeListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700605 mastershipService.removeListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700606 clusterService.removeListener(clusterListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700607
Charles Chan2e71ef32017-02-23 15:44:08 -0800608 neighbourResolutionService.unregisterNeighbourHandlers(appId);
609
sangho80f11cb2015-04-01 13:05:26 -0700610 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700611 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700612 deviceListener = null;
Charles Chan05bb1702018-04-19 13:10:01 -0700613 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chan2b078ae2015-10-14 11:24:40 -0700614 groupHandlerMap.clear();
Saurav Das49368392018-04-23 18:42:12 -0700615 defaultRoutingHandler.shutdown();
Charles Chan2b078ae2015-10-14 11:24:40 -0700616
Saurav Das261c3002017-06-13 15:35:54 -0700617 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800618 vlanNextObjStore.destroy();
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000619 macVlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700620 portNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700621 tunnelStore.destroy();
622 policyStore.destroy();
Pier Luigi35dab3f2018-01-25 16:16:02 +0100623
624 mcastHandler.terminate();
piercf557922019-05-17 20:47:06 +0200625 hostHandler.terminate();
sangho80f11cb2015-04-01 13:05:26 -0700626 log.info("Stopped");
627 }
628
Charles Chan873661e2017-11-30 15:37:50 -0800629 @Modified
630 private void modified(ComponentContext context) {
631 Dictionary<?, ?> properties = context.getProperties();
632 if (properties == null) {
633 return;
634 }
635
Ray Milkey48d10c02018-10-24 10:04:03 -0700636 String strActiveProbing = Tools.get(properties, PROP_ACTIVE_PROBING);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700637 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProbing);
Charles Chan873661e2017-11-30 15:37:50 -0800638 if (expectActiveProbing != activeProbing) {
639 activeProbing = expectActiveProbing;
640 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
641 }
Saurav Dasec683dc2018-04-27 18:42:30 -0700642
Ray Milkeye96d0de2018-11-02 17:12:21 -0700643
644 String strSymmetricProbing = Tools.get(properties, PROP_SYMMETRIC_PROBING);
645 boolean expectSymmetricProbing = Boolean.parseBoolean(strSymmetricProbing);
Mayank Tiwarif7942402018-10-29 18:27:35 -0400646 if (expectSymmetricProbing != symmetricProbing) {
647 symmetricProbing = expectSymmetricProbing;
648 log.info("{} symmetric probing", symmetricProbing ? "Enabling" : "Disabling");
649 }
650
Ray Milkey48d10c02018-10-24 10:04:03 -0700651 String strSingleHomedDown = Tools.get(properties, PROP_SINGLE_HOMED_DOWN);
Saurav Dasec683dc2018-04-27 18:42:30 -0700652 boolean expectSingleHomedDown = Boolean.parseBoolean(strSingleHomedDown);
653 if (expectSingleHomedDown != singleHomedDown) {
654 singleHomedDown = expectSingleHomedDown;
655 log.info("{} downing of single homed hosts for lost uplinks",
656 singleHomedDown ? "Enabling" : "Disabling");
657 if (singleHomedDown && linkHandler != null) {
658 hostService.getHosts().forEach(host -> host.locations()
659 .forEach(loc -> {
660 if (interfaceService.isConfigured(loc)) {
661 linkHandler.checkUplinksForHost(loc);
662 }
663 }));
664 } else {
665 log.warn("Disabling singleHomedDown does not re-enable already "
666 + "downed ports for single-homed hosts");
667 }
668 }
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700669
Ray Milkey48d10c02018-10-24 10:04:03 -0700670 String strRespondToUnknownHosts = Tools.get(properties, PROP_RESPOND_TO_UNKNOWN_HOSTS);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700671 boolean expectRespondToUnknownHosts = Boolean.parseBoolean(strRespondToUnknownHosts);
672 if (expectRespondToUnknownHosts != respondToUnknownHosts) {
673 respondToUnknownHosts = expectRespondToUnknownHosts;
674 log.info("{} responding to ARPs/NDPs from unknown hosts", respondToUnknownHosts ? "Enabling" : "Disabling");
675 }
Charles Chan4eb73bb2018-07-19 14:55:31 -0700676
Ray Milkey48d10c02018-10-24 10:04:03 -0700677 String strRouteDoubleTaggedHosts = Tools.get(properties, PROP_ROUTE_DOUBLE_TAGGED_HOSTS);
Charles Chanfbca55e2018-07-24 16:40:35 -0700678 boolean expectRouteDoubleTaggedHosts = Boolean.parseBoolean(strRouteDoubleTaggedHosts);
679 if (expectRouteDoubleTaggedHosts != routeDoubleTaggedHosts) {
680 routeDoubleTaggedHosts = expectRouteDoubleTaggedHosts;
681 log.info("{} routing for double tagged hosts", routeDoubleTaggedHosts ? "Enabling" : "Disabling");
Charles Chan4eb73bb2018-07-19 14:55:31 -0700682
Charles Chanfbca55e2018-07-24 16:40:35 -0700683 if (routeDoubleTaggedHosts) {
Charles Chan4eb73bb2018-07-19 14:55:31 -0700684 hostHandler.populateAllDoubleTaggedHost();
685 } else {
686 hostHandler.revokeAllDoubleTaggedHost();
687 }
688 }
Saurav Das9bf49582018-08-13 15:34:26 -0700689
Ray Milkey48d10c02018-10-24 10:04:03 -0700690 String strDefaultInternalVlan = Tools.get(properties, PROP_DEFAULT_INTERNAL_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700691 int defIntVlan = Integer.parseInt(strDefaultInternalVlan);
692 if (defIntVlan != defaultInternalVlan) {
693 if (canUseVlanId(defIntVlan)) {
694 log.warn("Default internal vlan value changed from {} to {}.. "
695 + "re-programming filtering rules, but NOT any groups already "
696 + "created with the former value", defaultInternalVlan, defIntVlan);
697 VlanId oldDefIntVlan = VlanId.vlanId((short) defaultInternalVlan);
698 defaultInternalVlan = defIntVlan;
699 routingRulePopulator
700 .updateSpecialVlanFilteringRules(true, oldDefIntVlan,
701 VlanId.vlanId((short) defIntVlan));
702 } else {
703 log.warn("Cannot change default internal vlan to unusable "
704 + "value {}", defIntVlan);
705 }
706 }
707
Ray Milkey48d10c02018-10-24 10:04:03 -0700708 String strPwTxpVlan = Tools.get(properties, PROP_PW_TRANSPORT_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700709 int pwTxpVlan = Integer.parseInt(strPwTxpVlan);
710 if (pwTxpVlan != pwTransportVlan) {
711 if (canUseVlanId(pwTxpVlan)) {
712 log.warn("Pseudowire transport vlan value changed from {} to {}.. "
713 + "re-programming filtering rules, but NOT any groups already "
714 + "created with the former value", pwTransportVlan,
715 pwTxpVlan);
716 VlanId oldPwTxpVlan = VlanId.vlanId((short) pwTransportVlan);
717 pwTransportVlan = pwTxpVlan;
718 routingRulePopulator
719 .updateSpecialVlanFilteringRules(false, oldPwTxpVlan,
720 VlanId.vlanId((short) pwTxpVlan));
721 } else {
722 log.warn("Cannot change pseudowire transport vlan to unusable "
723 + "value {}", pwTxpVlan);
724 }
725 }
726
psneha86e60d32019-03-26 06:31:41 -0400727 String strRouteSimplification = Tools.get(properties, PROP_ROUTE_SIMPLIFICATION);
728 boolean expectRouteSimplification = Boolean.parseBoolean(strRouteSimplification);
729 if (expectRouteSimplification != routeSimplification) {
730 routeSimplification = expectRouteSimplification;
731 log.info("{} route simplification", routeSimplification ? "Enabling" : "Disabling");
732 }
733
Saurav Das9bf49582018-08-13 15:34:26 -0700734 }
735
736 /**
737 * Returns true if given vlan id is not being used in the system currently,
738 * either as one of the default system wide vlans or as one of the
739 * configured interface vlans.
740 *
741 * @param vlanId given vlan id
742 * @return true if vlan is not currently in use
743 */
744 public boolean canUseVlanId(int vlanId) {
745 if (vlanId >= 4095 || vlanId <= 1) {
746 log.error("Vlan id {} value is not in valid range 2 <--> 4094",
747 vlanId);
748 return false;
749 }
750
751 VlanId vid = VlanId.vlanId((short) vlanId);
752 if (getDefaultInternalVlan().equals(vid) || getPwTransportVlan().equals(vid)) {
753 log.warn("Vlan id {} value is already in use system-wide. "
754 + "DefaultInternalVlan:{} PwTransportVlan:{} ", vlanId,
755 getDefaultInternalVlan(), getPwTransportVlan());
756 return false;
757 }
758
759 if (interfaceService.inUse(vid)) {
760 log.warn("Vlan id {} value is already in use on a configured "
761 + "interface in the system", vlanId);
762 return false;
763 }
764 return true;
765 }
766
767 /**
768 * Returns the VlanId assigned internally by default to unconfigured ports.
769 *
770 * @return the default internal vlan id
771 */
772 public VlanId getDefaultInternalVlan() {
773 return VlanId.vlanId((short) defaultInternalVlan);
774 }
775
776 /**
777 * Returns the Vlan id used to transport pseudowire traffic across the
778 * network.
779 *
780 * @return the pseudowire transport vlan id
781 */
782 public VlanId getPwTransportVlan() {
783 return VlanId.vlanId((short) pwTransportVlan);
Charles Chan873661e2017-11-30 15:37:50 -0800784 }
785
sangho27462c62015-05-14 00:39:53 -0700786 @Override
787 public List<Tunnel> getTunnels() {
788 return tunnelHandler.getTunnels();
789 }
790
791 @Override
sanghobd812f82015-06-29 14:58:47 -0700792 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
793 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700794 }
795
796 @Override
sanghobd812f82015-06-29 14:58:47 -0700797 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700798 for (Policy policy: policyHandler.getPolicies()) {
799 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
800 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
801 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
802 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700803 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700804 }
805 }
806 }
sanghobd812f82015-06-29 14:58:47 -0700807 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700808 }
809
810 @Override
sanghobd812f82015-06-29 14:58:47 -0700811 public PolicyHandler.Result removePolicy(Policy policy) {
812 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700813 }
814
815 @Override
sanghobd812f82015-06-29 14:58:47 -0700816 public PolicyHandler.Result createPolicy(Policy policy) {
817 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700818 }
819
820 @Override
821 public List<Policy> getPolicies() {
822 return policyHandler.getPolicies();
823 }
824
Saurav Das07c74602016-04-27 18:35:50 -0700825 @Override
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700826 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
827 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700828 }
829
830 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800831 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700832 return l2TunnelHandler.getL2Tunnels();
833 }
834
835 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800836 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700837 return l2TunnelHandler.getL2Policies();
838 }
839
Saurav Dasdebcf882018-04-06 20:16:01 -0700840 @Override
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700841 @Deprecated
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700842 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
843
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700844 // get both added and pending pseudowires
845 List<L2TunnelDescription> pseudowires = new ArrayList<>();
846 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
847 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700848 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700849
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700850 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
851
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700852 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
853 L2TunnelHandler.Result res;
854 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
855 res = addPseudowire(pw);
856 if (res != L2TunnelHandler.Result.SUCCESS) {
857 log.error("Pseudowire with id {} can not be instantiated !", res);
858 retRes = res;
859 }
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700860 }
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700861
862 return retRes;
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700863 }
864
865 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800866 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700867 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700868 }
869
870 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800871 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700872 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700873 }
874
875 @Override
Saurav Das07c74602016-04-27 18:35:50 -0700876 public void rerouteNetwork() {
877 cfgListener.configureNetwork();
Saurav Das07c74602016-04-27 18:35:50 -0700878 }
879
Charles Chand7844e52016-10-20 17:02:44 -0700880 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800881 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
882 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart61e24e12017-11-30 18:23:42 -0800883 deviceConfiguration.getRouters().forEach(device ->
884 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chand7844e52016-10-20 17:02:44 -0700885 return deviceSubnetMap;
886 }
887
Saurav Das62ae6792017-05-15 15:34:25 -0700888
889 @Override
890 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
891 if (defaultRoutingHandler != null) {
892 return defaultRoutingHandler.getCurrentEmcpSpgMap();
893 } else {
894 return null;
895 }
896 }
897
898 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700899 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDstNextObjStore() {
Saurav Das261c3002017-06-13 15:35:54 -0700900 if (dsNextObjStore != null) {
901 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700902 } else {
903 return ImmutableMap.of();
904 }
905 }
906
Saurav Dasfbe74572017-08-03 18:30:35 -0700907 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700908 public ImmutableMap<VlanNextObjectiveStoreKey, Integer> getVlanNextObjStore() {
909 if (vlanNextObjStore != null) {
910 return ImmutableMap.copyOf(vlanNextObjStore.entrySet());
911 } else {
912 return ImmutableMap.of();
913 }
914 }
915
916 @Override
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000917 public ImmutableMap<MacVlanNextObjectiveStoreKey, Integer> getMacVlanNextObjStore() {
918 if (macVlanNextObjStore != null) {
919 return ImmutableMap.copyOf(macVlanNextObjStore.entrySet());
920 } else {
921 return ImmutableMap.of();
922 }
923 }
924
925 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700926 public ImmutableMap<PortNextObjectiveStoreKey, Integer> getPortNextObjStore() {
927 if (portNextObjStore != null) {
928 return ImmutableMap.copyOf(portNextObjStore.entrySet());
929 } else {
930 return ImmutableMap.of();
931 }
932 }
933
934 @Override
935 public ImmutableMap<String, NextObjective> getPwInitNext() {
936 if (l2TunnelHandler != null) {
937 return l2TunnelHandler.getInitNext();
938 } else {
939 return ImmutableMap.of();
940 }
941 }
942
943 @Override
944 public ImmutableMap<String, NextObjective> getPwTermNext() {
945 if (l2TunnelHandler != null) {
946 return l2TunnelHandler.getTermNext();
947 } else {
948 return ImmutableMap.of();
949 }
950 }
951
952 @Override
953 public void invalidateNextObj(int nextId) {
954 if (dsNextObjStore != null) {
955 dsNextObjStore.entrySet().forEach(e -> {
956 if (e.getValue().nextId() == nextId) {
957 dsNextObjStore.remove(e.getKey());
958 }
959 });
960 }
961 if (vlanNextObjStore != null) {
962 vlanNextObjStore.entrySet().forEach(e -> {
963 if (e.getValue() == nextId) {
964 vlanNextObjStore.remove(e.getKey());
965 }
966 });
967 }
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000968 if (macVlanNextObjStore != null) {
969 macVlanNextObjStore.entrySet().forEach(e -> {
970 if (e.getValue() == nextId) {
971 macVlanNextObjStore.remove(e.getKey());
972 }
973 });
974 }
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700975 if (portNextObjStore != null) {
976 portNextObjStore.entrySet().forEach(e -> {
977 if (e.getValue() == nextId) {
978 portNextObjStore.remove(e.getKey());
979 }
980 });
981 }
982 if (mcastHandler != null) {
983 mcastHandler.removeNextId(nextId);
984 }
985 if (l2TunnelHandler != null) {
986 l2TunnelHandler.removeNextId(nextId);
987 }
988 if (xconnectService != null) {
989 xconnectService.removeNextId(nextId);
990 }
991 }
992
993 @Override
Saurav Dasfbe74572017-08-03 18:30:35 -0700994 public void verifyGroups(DeviceId id) {
995 DefaultGroupHandler gh = groupHandlerMap.get(id);
996 if (gh != null) {
997 gh.triggerBucketCorrector();
998 }
999 }
1000
Saurav Das6430f412018-01-25 09:49:01 -08001001 @Override
1002 public ImmutableMap<Link, Boolean> getSeenLinks() {
1003 return linkHandler.getSeenLinks();
1004 }
1005
1006 @Override
1007 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
1008 return linkHandler.getDownedPorts();
1009 }
1010
Pier Luigi0f9635b2018-01-15 18:06:43 +01001011 @Override
1012 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
Charles Chan0b1dd7e2018-08-19 19:21:46 -07001013 return mcastHandler.getNextIds(mcastIp);
Pier Luigi0f9635b2018-01-15 18:06:43 +01001014 }
1015
1016 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +01001017 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigi0f9635b2018-01-15 18:06:43 +01001018 return mcastHandler.getMcastRoles(mcastIp);
1019 }
1020
1021 @Override
Piere99511d2018-04-19 16:47:06 +02001022 public Map<McastRoleStoreKey, McastRole> getMcastRoles(IpAddress mcastIp, ConnectPoint sourcecp) {
1023 return mcastHandler.getMcastRoles(mcastIp, sourcecp);
1024 }
1025
1026 @Override
Pier Luigi0f9635b2018-01-15 18:06:43 +01001027 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
1028 return mcastHandler.getMcastPaths(mcastIp);
1029 }
1030
Pierdb27b8d2018-04-17 16:29:56 +02001031 @Override
Pier71c55772018-04-17 17:25:22 +02001032 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
1033 ConnectPoint sourcecp) {
1034 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
1035 }
1036
1037 @Override
Pierdb27b8d2018-04-17 16:29:56 +02001038 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
1039 return mcastHandler.getMcastLeaders(mcastIp);
1040 }
1041
Charles Chanb13e0702018-04-17 18:56:53 -07001042 @Override
1043 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
1044 return defaultRoutingHandler == null ? ImmutableMap.of() :
1045 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
1046 }
1047
1048 @Override
1049 public Map<DeviceId, Boolean> getShouldProgramCache() {
1050 return defaultRoutingHandler == null ? ImmutableMap.of() :
1051 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
1052 }
1053
Charles Chan8d316332018-06-19 20:31:57 -07001054 @Override
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301055 public boolean shouldProgram(DeviceId deviceId) {
1056 return defaultRoutingHandler.shouldProgram(deviceId);
1057 }
1058
1059 @Override
Ray Milkeyb85de082017-04-05 09:42:04 -07001060 public ApplicationId appId() {
1061 return appId;
1062 }
1063
1064 /**
1065 * Returns the device configuration.
1066 *
1067 * @return device configuration
1068 */
1069 public DeviceConfiguration deviceConfiguration() {
1070 return deviceConfiguration;
1071 }
1072
1073 /**
Saurav Das261c3002017-06-13 15:35:54 -07001074 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -07001075 * Used to keep track on MPLS group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001076 *
1077 * @return next objective ID store
1078 */
Saurav Das261c3002017-06-13 15:35:54 -07001079 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
1080 dsNextObjStore() {
1081 return dsNextObjStore;
Ray Milkeyb85de082017-04-05 09:42:04 -07001082 }
1083
1084 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001085 * Per device next objective ID store with (device id + vlanid) as key.
1086 * Used to keep track on L2 flood group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001087 *
1088 * @return vlan next object store
1089 */
1090 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
1091 return vlanNextObjStore;
1092 }
1093
1094 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001095 * Per device next objective ID store with (device id + MAC address + vlan) as key.
1096 * Used to keep track on L3 Unicast group information for indirect hosts.
1097 *
1098 * @return mac vlan next object store
1099 */
1100 public EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer> macVlanNextObjStore() {
1101 return macVlanNextObjStore;
1102 }
1103
1104 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001105 * Per device next objective ID store with (device id + port + treatment + meta) as key.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001106 * Used to keep track on L2 interface group and L3 unicast group information for direct hosts.
Ray Milkeyb85de082017-04-05 09:42:04 -07001107 *
1108 * @return port next object store.
1109 */
1110 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
1111 return portNextObjStore;
1112 }
1113
1114 /**
Saurav Das261c3002017-06-13 15:35:54 -07001115 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
1116 * labeled packets should be programmed or not.
Pier Ventre7a78de22016-10-31 15:00:01 -07001117 *
1118 * @return MPLS-ECMP value
1119 */
1120 public boolean getMplsEcmp() {
1121 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
1122 .getConfig(this.appId, SegmentRoutingAppConfig.class);
1123 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
1124 }
1125
1126 /**
sangho80f1f892015-05-19 11:57:42 -07001127 * Returns the tunnel object with the tunnel ID.
1128 *
1129 * @param tunnelId Tunnel ID
1130 * @return Tunnel reference
1131 */
sangho27462c62015-05-14 00:39:53 -07001132 public Tunnel getTunnel(String tunnelId) {
1133 return tunnelHandler.getTunnel(tunnelId);
1134 }
1135
Charles Chan8d316332018-06-19 20:31:57 -07001136 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +01001137 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chan098ca202018-05-01 11:50:20 -07001138 VlanId untaggedVlanId = interfaceService.getUntaggedVlanId(connectPoint);
1139 VlanId nativeVlanId = interfaceService.getNativeVlanId(connectPoint);
Charles Chand9265a32017-06-16 15:19:24 -07001140 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
1141 }
1142
Charles Chan8d316332018-06-19 20:31:57 -07001143 @Override
1144 public Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001145 SegmentRoutingDeviceConfig deviceConfig =
1146 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1147 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
1148 }
Charles Chan8d316332018-06-19 20:31:57 -07001149
1150 @Override
Saurav Dasec683dc2018-04-27 18:42:30 -07001151 public Optional<PortNumber> getPairLocalPort(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001152 SegmentRoutingDeviceConfig deviceConfig =
1153 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1154 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
1155 }
1156
1157 /**
Charles Chanf0ae41e2017-08-23 13:55:39 -07001158 * Returns locations of given resolved route.
1159 *
1160 * @param resolvedRoute resolved route
1161 * @return locations of nexthop. Might be empty if next hop is not found
1162 */
Charles Chan19b70032019-04-17 14:20:26 -07001163 public Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
Charles Chanf0ae41e2017-08-23 13:55:39 -07001164 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
1165 return Optional.ofNullable(hostService.getHost(hostId))
1166 .map(Host::locations).orElse(Sets.newHashSet())
1167 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
1168 }
1169
1170 /**
Charles Chan90772a72017-02-08 15:52:08 -08001171 * Returns vlan port map of given device.
1172 *
1173 * @param deviceId device id
1174 * @return vlan-port multimap
1175 */
1176 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
1177 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
1178
1179 interfaceService.getInterfaces().stream()
1180 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
1181 .forEach(intf -> {
1182 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan3ed34d82017-06-22 18:03:14 -07001183 intf.vlanTagged().forEach(vlanTagged ->
1184 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
1185 );
Charles Chan90772a72017-02-08 15:52:08 -08001186 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
1187 });
1188 vlanPortMap.removeAll(VlanId.NONE);
1189
1190 return vlanPortMap;
1191 }
1192
1193 /**
Charles Chan10b0fb72017-02-02 16:20:42 -08001194 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -08001195 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -07001196 *
1197 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -08001198 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -08001199 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -07001200 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001201 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -07001202 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -08001203 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
1204 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -07001205 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -08001206 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -08001207 + "device {} not found", deviceId);
1208 return -1;
1209 }
1210 }
1211
1212 /**
1213 * Returns the next objective ID for the given portNumber, given the treatment.
1214 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -07001215 * would result in different objectives. If the next object does not exist,
1216 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -08001217 *
1218 * @param deviceId Device ID
1219 * @param portNum port number on device for which NextObjective is queried
1220 * @param treatment the actions to apply on the packets (should include outport)
1221 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -07001222 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -07001223 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -08001224 */
1225 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
1226 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -07001227 TrafficSelector meta,
1228 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -08001229 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1230 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -07001231 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -08001232 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -08001233 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
1234 + " not found", deviceId);
1235 return -1;
1236 }
1237 }
1238
Saurav Das62ae6792017-05-15 15:34:25 -07001239 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001240 * Returns the next Objective ID for the given mac and vlan, given the treatment.
1241 * There could be multiple different treatments to the same outport, which
1242 * would result in different objectives. If the next object does not exist,
1243 * and should be created, a new one is created and its id is returned.
1244 *
1245 * @param deviceId Device ID
1246 * @param macAddr mac of host for which Next ID is required.
1247 * @param vlanId vlan of host for which Next ID is required.
Ruchi Sahota07869322019-05-09 17:26:14 -04001248 * @param port port with which to create the Next Obj.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001249 * @param createIfMissing true if a next object should be created if not found
1250 * @return next objective ID or -1 if an error occurred during retrieval or creation
1251 */
1252 public int getMacVlanNextObjectiveId(DeviceId deviceId, MacAddress macAddr, VlanId vlanId,
Ruchi Sahota07869322019-05-09 17:26:14 -04001253 PortNumber port, boolean createIfMissing) {
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001254 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1255 if (ghdlr != null) {
Ruchi Sahota07869322019-05-09 17:26:14 -04001256 return ghdlr.getMacVlanNextObjectiveId(macAddr, vlanId, port, createIfMissing);
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001257 } else {
1258 log.warn("getMacVlanNextObjectiveId query - groupHandler for device {}"
1259 + " not found", deviceId);
1260 return -1;
1261 }
1262 }
1263
1264 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001265 * Updates the next objective for the given nextId .
1266 *
1267 * @param deviceId Device ID
1268 * @param hostMac mac of host for which Next obj is to be updated.
1269 * @param hostVlanId vlan of host for which Next obj is to be updated.
1270 * @param port port with which to update the Next Obj.
1271 * @param nextId of Next Obj which needs to be updated.
1272 */
1273 public void updateMacVlanTreatment(DeviceId deviceId, MacAddress hostMac,
1274 VlanId hostVlanId, PortNumber port, int nextId) {
pierf0e37ec2019-11-26 11:48:48 +01001275 // Check if we are the king of this device
1276 // just one instance should perform this update
1277 if (!defaultRoutingHandler.shouldProgram(deviceId)) {
1278 log.debug("This instance is not handling the routing towards the "
1279 + "device {}", deviceId);
1280 return;
1281 }
1282 // Get the handler and perform the update
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001283 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1284 if (ghdlr != null) {
1285 ghdlr.updateL3UcastGroupBucket(hostMac, hostVlanId, port, nextId);
1286 } else {
1287 log.warn("updateL3UcastGroupBucket query - groupHandler for device {}"
1288 + " not found", deviceId);
1289 }
1290 }
1291
1292
1293 /**
Saurav Das62ae6792017-05-15 15:34:25 -07001294 * Returns the group handler object for the specified device id.
1295 *
1296 * @param devId the device identifier
1297 * @return the groupHandler object for the device id, or null if not found
1298 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001299 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Das62ae6792017-05-15 15:34:25 -07001300 return groupHandlerMap.get(devId);
1301 }
1302
1303 /**
Saurav Dasfbe74572017-08-03 18:30:35 -07001304 * Returns the default routing handler object.
1305 *
1306 * @return the default routing handler object
1307 */
1308 public DefaultRoutingHandler getRoutingHandler() {
1309 return defaultRoutingHandler;
1310 }
1311
sangho80f11cb2015-04-01 13:05:26 -07001312 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -07001313 @Override
1314 public void process(PacketContext context) {
Charles Chan77cdde42018-05-08 21:35:50 -07001315 packetExecutor.execute(() -> processPacketInternal(context));
1316 }
sangho80f11cb2015-04-01 13:05:26 -07001317
Charles Chan77cdde42018-05-08 21:35:50 -07001318 private void processPacketInternal(PacketContext context) {
sangho80f11cb2015-04-01 13:05:26 -07001319 if (context.isHandled()) {
1320 return;
1321 }
1322
1323 InboundPacket pkt = context.inPacket();
1324 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -08001325
1326 if (ethernet == null) {
1327 return;
1328 }
1329
Saurav Das261c3002017-06-13 15:35:54 -07001330 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1331 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001332 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -07001333 log.warn("Received unexpected ARP packet on {}",
1334 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -07001335 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001336 return;
sangho80f11cb2015-04-01 13:05:26 -07001337 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -08001338 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1339 //ipHandler.addToPacketBuffer(ipv4Packet);
1340 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1341 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001342 } else {
Charles Chand041ad82017-01-13 17:20:44 -08001343 // NOTE: We don't support IP learning at this moment so this
1344 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001345 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001346 }
Pier Ventreb6a7f342016-11-26 21:05:22 -08001347 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1348 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -08001349 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -08001350 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -08001351 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1352 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1353 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1354 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1355 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1356 } else {
Saurav Das62ae6792017-05-15 15:34:25 -07001357 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -07001358 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -08001359 }
1360 } else {
1361 // NOTE: We don't support IP learning at this moment so this
1362 // is not necessary. Also it causes duplication of DHCPv6 packets.
1363 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1364 }
sangho80f11cb2015-04-01 13:05:26 -07001365 }
1366 }
1367 }
1368
sangho80f11cb2015-04-01 13:05:26 -07001369 private class InternalEventHandler implements Runnable {
Charles Chan52003922018-04-05 16:31:15 -07001370 private Event event;
1371
1372 InternalEventHandler(Event event) {
1373 this.event = event;
1374 }
1375
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001376 @Override
sangho80f11cb2015-04-01 13:05:26 -07001377 public void run() {
Charles Chan52003922018-04-05 16:31:15 -07001378 try {
1379 // TODO We should also change SR routing and PW to listen to TopologyEvents
1380 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1381 event.type() == LinkEvent.Type.LINK_UPDATED) {
1382 linkHandler.processLinkAdded((Link) event.subject());
1383 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1384 linkHandler.processLinkRemoved((Link) event.subject());
1385 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1386 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1387 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1388 DeviceId deviceId = ((Device) event.subject()).id();
1389 if (deviceService.isAvailable(deviceId)) {
1390 log.info("** DEVICE UP Processing device event {} "
1391 + "for available device {}",
1392 event.type(), ((Device) event.subject()).id());
1393 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001394 } else {
Charles Chanb8f3af52018-08-29 21:21:51 -07001395 if (event.type() == DeviceEvent.Type.DEVICE_ADDED) {
1396 // Note: For p4 devices, the device will be added but unavailable at the beginning.
1397 // The device will later on being marked as available once the pipeline is pushed
1398 // to the device.
1399 log.info("** DEVICE ADDED but unavailable. Ignore");
1400 return;
1401 }
Charles Chan52003922018-04-05 16:31:15 -07001402 log.info(" ** DEVICE DOWN Processing device event {}"
1403 + " for unavailable device {}",
1404 event.type(), ((Device) event.subject()).id());
1405 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001406 }
Charles Chan52003922018-04-05 16:31:15 -07001407 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1408 // typically these calls come when device is added first time
1409 // so port filtering rules are handled at the device_added event.
1410 // port added calls represent all ports on the device,
1411 // enabled or not.
1412 log.trace("** PORT ADDED {}/{} -> {}",
1413 ((DeviceEvent) event).subject().id(),
1414 ((DeviceEvent) event).port().number(),
1415 event.type());
1416 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1417 // these calls happen for every subsequent event
1418 // ports enabled, disabled, switch goes away, comes back
1419 log.info("** PORT UPDATED {}/{} -> {}",
1420 event.subject(),
1421 ((DeviceEvent) event).port(),
1422 event.type());
Saurav Dasec683dc2018-04-27 18:42:30 -07001423 processPortUpdatedInternal(((Device) event.subject()),
Charles Chan52003922018-04-05 16:31:15 -07001424 ((DeviceEvent) event).port());
1425 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1426 // Process topology event, needed for all modules relying on
1427 // topology service for path computation
1428 TopologyEvent topologyEvent = (TopologyEvent) event;
1429 log.info("Processing topology event {}, topology age {}, reasons {}",
1430 event.type(), topologyEvent.subject().time(),
1431 topologyEvent.reasons().size());
1432 topologyHandler.processTopologyChange(topologyEvent.reasons());
1433 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1434 hostHandler.processHostAddedEvent((HostEvent) event);
1435 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1436 hostHandler.processHostMovedEvent((HostEvent) event);
1437 routeHandler.processHostMovedEvent((HostEvent) event);
Charles Chana2ccb582019-11-25 09:47:22 -08001438 } else if (event.type() == HostEvent.Type.HOST_AUX_MOVED) {
1439 hostHandler.processHostMovedEvent((HostEvent) event);
1440 // TODO RouteHandler also needs to process this event in order to
1441 // support nexthops that has auxLocations
Charles Chan52003922018-04-05 16:31:15 -07001442 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1443 hostHandler.processHostRemovedEvent((HostEvent) event);
1444 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1445 hostHandler.processHostUpdatedEvent((HostEvent) event);
1446 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1447 routeHandler.processRouteAdded((RouteEvent) event);
1448 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1449 routeHandler.processRouteUpdated((RouteEvent) event);
1450 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1451 routeHandler.processRouteRemoved((RouteEvent) event);
1452 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1453 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1454 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1455 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1456 event.type() == McastEvent.Type.SINKS_ADDED ||
1457 event.type() == McastEvent.Type.SINKS_REMOVED ||
1458 event.type() == McastEvent.Type.ROUTE_ADDED ||
1459 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1460 mcastHandler.processMcastEvent((McastEvent) event);
1461 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1462 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1463 Class configClass = netcfgEvent.configClass();
1464 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1465 appCfgHandler.processAppConfigAdded(netcfgEvent);
1466 log.info("App config event .. configuring network");
1467 cfgListener.configureNetwork();
1468 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1469 log.info("Segment Routing Device Config added for {}", event.subject());
1470 cfgListener.configureNetwork();
Charles Chan52003922018-04-05 16:31:15 -07001471 } else if (configClass.equals(InterfaceConfig.class)) {
1472 log.info("Interface Config added for {}", event.subject());
1473 cfgListener.configureNetwork();
1474 } else {
1475 log.error("Unhandled config class: {}", configClass);
1476 }
1477 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1478 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1479 Class configClass = netcfgEvent.configClass();
1480 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1481 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1482 log.info("App config event .. configuring network");
1483 cfgListener.configureNetwork();
1484 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1485 log.info("Segment Routing Device Config updated for {}", event.subject());
1486 createOrUpdateDeviceConfiguration();
Charles Chan52003922018-04-05 16:31:15 -07001487 } else if (configClass.equals(InterfaceConfig.class)) {
1488 log.info("Interface Config updated for {}", event.subject());
1489 createOrUpdateDeviceConfiguration();
1490 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1491 (InterfaceConfig) netcfgEvent.prevConfig().get());
1492 } else {
1493 log.error("Unhandled config class: {}", configClass);
1494 }
1495 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1496 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1497 Class configClass = netcfgEvent.configClass();
1498 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1499 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1500 log.info("App config event .. configuring network");
1501 cfgListener.configureNetwork();
1502 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1503 // TODO Handle sr device config removal
1504 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
Charles Chan52003922018-04-05 16:31:15 -07001505 } else if (configClass.equals(InterfaceConfig.class)) {
1506 // TODO Handle interface removal
1507 log.info("InterfaceConfig removal is not handled in current implementation");
1508 } else {
1509 log.error("Unhandled config class: {}", configClass);
1510 }
Saurav Das00e553b2018-04-21 17:19:48 -07001511 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1512 MastershipEvent me = (MastershipEvent) event;
1513 DeviceId deviceId = me.subject();
1514 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1515 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1516 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1517 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1518 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1519 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan52003922018-04-05 16:31:15 -07001520 } else {
1521 log.warn("Unhandled event type: {}", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001522 }
Charles Chan52003922018-04-05 16:31:15 -07001523 } catch (Exception e) {
1524 log.error("SegmentRouting event handler thread thrown an exception: {}",
1525 e.getMessage(), e);
sangho80f11cb2015-04-01 13:05:26 -07001526 }
sangho80f11cb2015-04-01 13:05:26 -07001527 }
1528 }
1529
Saurav Dase6c448a2018-01-18 12:07:33 -08001530 void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001531 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001532
1533 // NOTE: Punt ARP/NDP even when the device is not configured.
1534 // Host learning without network config is required for CORD config generator.
1535 routingRulePopulator.populateIpPunts(device.id());
1536 routingRulePopulator.populateArpNdpPunts(device.id());
1537
Charles Chan319d1a22015-11-03 10:42:14 -08001538 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001539 log.warn("Device configuration unavailable. Device {} will be "
1540 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001541 return;
1542 }
Charles Chan72779502016-04-23 17:36:10 -07001543 processDeviceAddedInternal(device.id());
1544 }
1545
1546 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001547 // Irrespective of whether the local is a MASTER or not for this device,
1548 // we need to create a SR-group-handler instance. This is because in a
1549 // multi-instance setup, any instance can initiate forwarding/next-objectives
1550 // for any switch (even if this instance is a SLAVE or not even connected
1551 // to the switch). To handle this, a default-group-handler instance is necessary
1552 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001553 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1554 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001555 DefaultGroupHandler groupHandler;
1556 try {
1557 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001558 createGroupHandler(deviceId,
1559 appId,
1560 deviceConfiguration,
1561 linkService,
1562 flowObjectiveService,
1563 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001564 } catch (DeviceConfigNotFoundException e) {
1565 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1566 return;
1567 }
Saurav Das2b6a00f2017-12-05 15:00:23 -08001568 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan72779502016-04-23 17:36:10 -07001569 deviceId);
1570 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001571 }
Saurav Dasb149be12016-06-07 10:08:06 -07001572
Charles Chan72779502016-04-23 17:36:10 -07001573 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001574 defaultRoutingHandler.populatePortAddressingRules(deviceId);
pierf331a492020-01-07 15:39:39 +01001575 defaultRoutingHandler.purgeSeenBeforeRoutes(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001576 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001577 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001578 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001579 }
Charles Chan82ab1932016-01-30 23:22:37 -08001580
Charles Chandebfea32016-10-24 14:52:01 -07001581 appCfgHandler.init(deviceId);
Charles Chan074fae62018-04-29 18:11:37 -07001582 hostEventExecutor.execute(() -> hostHandler.init(deviceId));
1583 routeEventExecutor.execute(() -> routeHandler.init(deviceId));
sangho80f11cb2015-04-01 13:05:26 -07001584 }
1585
Saurav Dasc3604f12016-03-23 11:22:49 -07001586 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001587 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001588 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan873661e2017-11-30 15:37:50 -08001589 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan10b0fb72017-02-02 16:20:42 -08001590 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001591 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001592 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001593 macVlanNextObjStore.entrySet().stream()
1594 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1595 .forEach(entry -> macVlanNextObjStore.remove(entry.getKey()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001596 portNextObjStore.entrySet().stream()
1597 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001598 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Dase6c448a2018-01-18 12:07:33 -08001599 linkHandler.processDeviceRemoved(device);
Charles Chan17d75d82017-06-15 00:44:51 -07001600
Saurav Dasfbe74572017-08-03 18:30:35 -07001601 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1602 if (gh != null) {
1603 gh.shutdown();
1604 }
Saurav Das62ae6792017-05-15 15:34:25 -07001605 // Note that a switch going down is associated with all of its links
1606 // going down as well, but it is treated as a single switch down event
Saurav Dasdebcf882018-04-06 20:16:01 -07001607 // while the link-downs are ignored. We cannot rely on the ordering of
1608 // events - i.e we cannot expect all link-downs to come before the
1609 // switch down - so we purge all seen-links for the switch before
1610 // handling route-path changes for the switch-down
Saurav Das62ae6792017-05-15 15:34:25 -07001611 defaultRoutingHandler
Saurav Dasdc7f2752018-03-18 21:28:15 -07001612 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Das6430f412018-01-25 09:49:01 -08001613 defaultRoutingHandler.purgeEcmpGraph(device.id());
Saurav Das97241862018-02-14 14:14:54 -08001614
1615 // Cleanup all internal groupHandler stores for this device. Should be
1616 // done after all rerouting or rehashing has been completed
1617 groupHandlerMap.entrySet()
1618 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001619 }
1620
Saurav Das49368392018-04-23 18:42:12 -07001621 /**
1622 * Purge the destinationSet nextObjective store of entries with this device
1623 * as key. Erases app-level knowledge of hashed groups in this device.
1624 *
1625 * @param devId the device identifier
1626 */
Saurav Das00e553b2018-04-21 17:19:48 -07001627 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Das49368392018-04-23 18:42:12 -07001628 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Das00e553b2018-04-21 17:19:48 -07001629 dsNextObjStore.entrySet().stream()
1630 .filter(entry -> entry.getKey().deviceId().equals(devId))
1631 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1632 }
1633
Saurav Dasec683dc2018-04-27 18:42:30 -07001634 private void processPortUpdatedInternal(Device device, Port port) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001635 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1636 log.warn("Device configuration uploading. Not handling port event for"
1637 + "dev: {} port: {}", device.id(), port.number());
1638 return;
1639 }
Saurav Dasf9332192017-02-18 14:05:44 -08001640
Saurav Dasec683dc2018-04-27 18:42:30 -07001641 if (interfaceService.isConfigured(new ConnectPoint(device.id(), port.number()))) {
1642 lastEdgePortEvent = Instant.now();
1643 }
1644
Saurav Dasf9332192017-02-18 14:05:44 -08001645 if (!mastershipService.isLocalMaster(device.id())) {
1646 log.debug("Not master for dev:{} .. not handling port updated event"
1647 + "for port {}", device.id(), port.number());
1648 return;
1649 }
Saurav Dasec683dc2018-04-27 18:42:30 -07001650 processPortUpdated(device.id(), port);
1651 }
Saurav Dasf9332192017-02-18 14:05:44 -08001652
Saurav Dasec683dc2018-04-27 18:42:30 -07001653 /**
1654 * Adds or remove filtering rules for the given switchport. If switchport is
1655 * an edge facing port, additionally handles host probing and broadcast
1656 * rules. Must be called by local master of device.
1657 *
1658 * @param deviceId the device identifier
1659 * @param port the port to update
1660 */
1661 void processPortUpdated(DeviceId deviceId, Port port) {
Saurav Dasf9332192017-02-18 14:05:44 -08001662 // first we handle filtering rules associated with the port
1663 if (port.isEnabled()) {
1664 log.info("Switchport {}/{} enabled..programming filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001665 deviceId, port.number());
1666 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001667 } else {
1668 log.info("Switchport {}/{} disabled..removing filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001669 deviceId, port.number());
1670 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001671 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001672
1673 // portUpdated calls are for ports that have gone down or up. For switch
1674 // to switch ports, link-events should take care of any re-routing or
1675 // group editing necessary for port up/down. Here we only process edge ports
1676 // that are already configured.
Saurav Dasec683dc2018-04-27 18:42:30 -07001677 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
Charles Chan098ca202018-05-01 11:50:20 -07001678 VlanId untaggedVlan = interfaceService.getUntaggedVlanId(cp);
1679 VlanId nativeVlan = interfaceService.getNativeVlanId(cp);
1680 Set<VlanId> taggedVlans = interfaceService.getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001681
Saurav Das3fb28272017-03-04 16:08:47 -08001682 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001683 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasec683dc2018-04-27 18:42:30 -07001684 + "dev/port: {}/{}", deviceId, port.number());
Saurav Dasf0f592d2016-11-18 15:21:57 -08001685 return;
1686 }
Saurav Das3fb28272017-03-04 16:08:47 -08001687 if (untaggedVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001688 processEdgePort(deviceId, port, untaggedVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001689 }
1690 if (nativeVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001691 processEdgePort(deviceId, port, nativeVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001692 }
1693 if (!taggedVlans.isEmpty()) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001694 taggedVlans.forEach(tag -> processEdgePort(deviceId, port, tag, false));
Saurav Das3fb28272017-03-04 16:08:47 -08001695 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001696 }
1697
Saurav Dasec683dc2018-04-27 18:42:30 -07001698 private void processEdgePort(DeviceId deviceId, Port port, VlanId vlanId,
Saurav Das3fb28272017-03-04 16:08:47 -08001699 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001700 boolean portUp = port.isEnabled();
1701 if (portUp) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001702 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001703 port.number(), vlanId);
Charles Chan074fae62018-04-29 18:11:37 -07001704 hostEventExecutor.execute(() -> hostHandler.processPortUp(new ConnectPoint(deviceId, port.number())));
Saurav Dasf0f592d2016-11-18 15:21:57 -08001705 } else {
Saurav Dasec683dc2018-04-27 18:42:30 -07001706 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001707 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001708 }
1709
Saurav Dasec683dc2018-04-27 18:42:30 -07001710 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001711 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001712 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001713 } else {
1714 log.warn("Group handler not found for dev:{}. Not handling edge port"
Saurav Dasec683dc2018-04-27 18:42:30 -07001715 + " {} event for port:{}", deviceId,
Saurav Dasf0f592d2016-11-18 15:21:57 -08001716 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001717 }
1718 }
sangho27462c62015-05-14 00:39:53 -07001719
Charles Chan8ca5a122017-10-20 16:06:55 -07001720 private void createOrUpdateDeviceConfiguration() {
1721 if (deviceConfiguration == null) {
Saurav Dase321cff2018-02-09 17:26:45 -08001722 log.info("Creating new DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001723 deviceConfiguration = new DeviceConfiguration(this);
1724 } else {
Saurav Dase321cff2018-02-09 17:26:45 -08001725 log.info("Updating DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001726 deviceConfiguration.updateConfig();
1727 }
1728 }
1729
Charles Chanfbcb8812018-04-18 18:41:05 -07001730 private void createOrUpdateDefaultRoutingHandler() {
1731 if (defaultRoutingHandler == null) {
1732 log.info("Creating new DefaultRoutingHandler");
1733 defaultRoutingHandler = new DefaultRoutingHandler(this);
1734 } else {
1735 log.info("Updating DefaultRoutingHandler");
1736 defaultRoutingHandler.update(this);
1737 }
1738 }
1739
Pier Ventreb6a7f342016-11-26 21:05:22 -08001740 /**
1741 * Registers the given connect point with the NRS, this is necessary
1742 * to receive the NDP and ARP packets from the NRS.
1743 *
1744 * @param portToRegister connect point to register
1745 */
1746 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001747 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001748 portToRegister,
1749 neighbourHandler,
1750 appId
1751 );
1752 }
1753
Charles Chan72f556a2015-10-05 17:50:33 -07001754 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001755 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001756 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001757
Charles Chanb7f75ac2016-01-11 18:28:54 -08001758 /**
1759 * Constructs the internal network config listener.
1760 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001761 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001762 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001763 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001764 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001765 }
1766
Charles Chanb7f75ac2016-01-11 18:28:54 -08001767 /**
1768 * Reads network config and initializes related data structure accordingly.
1769 */
Charles Chan873661e2017-11-30 15:37:50 -08001770 void configureNetwork() {
Saurav Dase321cff2018-02-09 17:26:45 -08001771 log.info("Configuring network ...");
Andrea Campanella060cad82018-04-17 12:09:34 +02001772
1773 // Setting handling of network configuration events completable future
1774 // The completable future is needed because of the async behaviour of the configureNetwork,
1775 // listener registration and event arrival
1776 // Enables us to buffer the events and execute them when the configure network is done.
Ray Milkey521e4392018-11-16 15:15:14 -08001777 synchronized (networkConfigCompletionLock) {
1778 networkConfigCompletion = new CompletableFuture<>();
Andrea Campanella060cad82018-04-17 12:09:34 +02001779
Ray Milkey521e4392018-11-16 15:15:14 -08001780 // add a small delay to absorb multiple network config added notifications
1781 if (!programmingScheduled.get()) {
1782 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1783 programmingScheduled.set(true);
1784 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1785 }
1786
1787 createOrUpdateDeviceConfiguration();
1788
1789 arpHandler = new ArpHandler(srManager);
1790 icmpHandler = new IcmpHandler(srManager);
1791 ipHandler = new IpHandler(srManager);
1792 routingRulePopulator = new RoutingRulePopulator(srManager);
1793 createOrUpdateDefaultRoutingHandler();
1794
1795 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1796 groupHandlerMap, tunnelStore);
1797 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1798 flowObjectiveService,
1799 tunnelHandler, policyStore);
1800 networkConfigCompletion.complete(true);
Andrea Campanella060cad82018-04-17 12:09:34 +02001801 }
1802
Charles Chan72779502016-04-23 17:36:10 -07001803 mcastHandler.init();
Andrea Campanella060cad82018-04-17 12:09:34 +02001804
Charles Chane7c61022015-10-07 14:21:45 -07001805 }
1806
Charles Chan72f556a2015-10-05 17:50:33 -07001807 @Override
1808 public void event(NetworkConfigEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001809 if (mainEventExecutor == null) {
1810 return;
1811 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001812 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan3f4aca62018-03-21 16:57:47 -07001813 switch (event.type()) {
1814 case CONFIG_ADDED:
1815 case CONFIG_UPDATED:
1816 case CONFIG_REMOVED:
1817 log.trace("Schedule Network Config event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001818 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1819 mainEventExecutor.execute(new InternalEventHandler(event));
1820 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001821 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001822 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001823 break;
1824 default:
1825 break;
Charles Chan72f556a2015-10-05 17:50:33 -07001826 }
1827 }
Saurav Das261c3002017-06-13 15:35:54 -07001828
Charles Chanc4a68c32018-01-03 16:26:32 -08001829 @Override
1830 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase321cff2018-02-09 17:26:45 -08001831 if (event.type() == CONFIG_REGISTERED ||
1832 event.type() == CONFIG_UNREGISTERED) {
1833 log.debug("Ignore event {} due to type mismatch", event);
1834 return false;
Charles Chanc4a68c32018-01-03 16:26:32 -08001835 }
Saurav Dase321cff2018-02-09 17:26:45 -08001836
1837 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1838 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
Charles Chana01d57c2019-07-19 16:25:38 -07001839 !event.configClass().equals(InterfaceConfig.class)) {
Saurav Dase321cff2018-02-09 17:26:45 -08001840 log.debug("Ignore event {} due to class mismatch", event);
1841 return false;
1842 }
1843
1844 return true;
Charles Chanc4a68c32018-01-03 16:26:32 -08001845 }
1846
Saurav Das261c3002017-06-13 15:35:54 -07001847 private final class ConfigChange implements Runnable {
1848 @Override
1849 public void run() {
1850 programmingScheduled.set(false);
Saurav Dase321cff2018-02-09 17:26:45 -08001851 log.info("Reacting to config changes after buffer delay");
Saurav Das261c3002017-06-13 15:35:54 -07001852 for (Device device : deviceService.getDevices()) {
1853 processDeviceAdded(device);
1854 }
1855 defaultRoutingHandler.startPopulationProcess();
1856 }
1857 }
Charles Chan72f556a2015-10-05 17:50:33 -07001858 }
Charles Chanf4586112015-11-09 16:37:23 -08001859
Charles Chan3f4aca62018-03-21 16:57:47 -07001860 private class InternalLinkListener implements LinkListener {
1861 @Override
1862 public void event(LinkEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001863 if (mainEventExecutor == null) {
1864 return;
1865 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001866 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1867 event.type() == LinkEvent.Type.LINK_UPDATED ||
1868 event.type() == LinkEvent.Type.LINK_REMOVED) {
1869 log.trace("Schedule Link event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001870 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1871 mainEventExecutor.execute(new InternalEventHandler(event));
1872 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001873 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001874 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001875 }
1876 }
1877 }
1878
1879 private class InternalDeviceListener implements DeviceListener {
1880 @Override
1881 public void event(DeviceEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001882 if (mainEventExecutor == null) {
1883 return;
1884 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001885 switch (event.type()) {
1886 case DEVICE_ADDED:
1887 case PORT_UPDATED:
1888 case PORT_ADDED:
1889 case DEVICE_UPDATED:
1890 case DEVICE_AVAILABILITY_CHANGED:
1891 log.trace("Schedule Device event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001892 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1893 mainEventExecutor.execute(new InternalEventHandler(event));
1894 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001895 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001896 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001897 break;
1898 default:
1899 }
1900 }
1901 }
1902
1903 private class InternalTopologyListener implements TopologyListener {
1904 @Override
1905 public void event(TopologyEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001906 if (mainEventExecutor == null) {
1907 return;
1908 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001909 switch (event.type()) {
1910 case TOPOLOGY_CHANGED:
1911 log.trace("Schedule Topology event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001912 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1913 mainEventExecutor.execute(new InternalEventHandler(event));
1914 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001915 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001916 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001917 break;
1918 default:
1919 }
1920 }
1921 }
1922
Charles Chanf4586112015-11-09 16:37:23 -08001923 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001924 @Override
1925 public void event(HostEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001926 if (hostEventExecutor == null) {
1927 return;
1928 }
Charles Chanf4586112015-11-09 16:37:23 -08001929 switch (event.type()) {
1930 case HOST_ADDED:
Charles Chanf4586112015-11-09 16:37:23 -08001931 case HOST_MOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001932 case HOST_REMOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001933 case HOST_UPDATED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001934 log.trace("Schedule Host event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001935 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chanf4586112015-11-09 16:37:23 -08001936 break;
1937 default:
1938 log.warn("Unsupported host event type: {}", event.type());
1939 break;
1940 }
1941 }
1942 }
1943
Charles Chanc91c8782016-03-30 17:54:24 -07001944 private class InternalMcastListener implements McastListener {
1945 @Override
1946 public void event(McastEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001947 if (mcastEventExecutor == null) {
1948 return;
1949 }
Charles Chanc91c8782016-03-30 17:54:24 -07001950 switch (event.type()) {
Pier1f87aca2018-03-14 16:47:32 -07001951 case SOURCES_ADDED:
1952 case SOURCES_REMOVED:
1953 case SINKS_ADDED:
1954 case SINKS_REMOVED:
Charles Chanc91c8782016-03-30 17:54:24 -07001955 case ROUTE_REMOVED:
Pierdb27b8d2018-04-17 16:29:56 +02001956 case ROUTE_ADDED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001957 log.trace("Schedule Mcast event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001958 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi6786b922018-02-02 16:19:11 +01001959 break;
Charles Chanc91c8782016-03-30 17:54:24 -07001960 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07001961 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chanc91c8782016-03-30 17:54:24 -07001962 break;
1963 }
1964 }
1965 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001966
Charles Chandebfea32016-10-24 14:52:01 -07001967 private class InternalRouteEventListener implements RouteListener {
1968 @Override
1969 public void event(RouteEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001970 if (routeEventExecutor == null) {
1971 return;
1972 }
Charles Chandebfea32016-10-24 14:52:01 -07001973 switch (event.type()) {
1974 case ROUTE_ADDED:
Charles Chandebfea32016-10-24 14:52:01 -07001975 case ROUTE_UPDATED:
Charles Chandebfea32016-10-24 14:52:01 -07001976 case ROUTE_REMOVED:
Charles Chan910be6a2017-08-23 14:46:43 -07001977 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001978 log.trace("Schedule Route event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001979 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan910be6a2017-08-23 14:46:43 -07001980 break;
Charles Chandebfea32016-10-24 14:52:01 -07001981 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07001982 log.warn("Unsupported route event type: {}", event.type());
Charles Chandebfea32016-10-24 14:52:01 -07001983 break;
1984 }
1985 }
1986 }
Saurav Das62ae6792017-05-15 15:34:25 -07001987
Charles Chanfbcb8812018-04-18 18:41:05 -07001988 private class InternalMastershipListener implements MastershipListener {
1989 @Override
1990 public void event(MastershipEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001991 if (mainEventExecutor == null) {
1992 return;
1993 }
Charles Chanfbcb8812018-04-18 18:41:05 -07001994 switch (event.type()) {
Saurav Das00e553b2018-04-21 17:19:48 -07001995 case MASTER_CHANGED:
1996 log.debug("Mastership event: {}/{}", event.subject(),
1997 event.roleInfo());
1998 mainEventExecutor.execute(new InternalEventHandler(event));
1999 break;
2000 case BACKUPS_CHANGED:
2001 case SUSPENDED:
2002 default:
2003 log.debug("Mastership event type {} not handled", event.type());
2004 break;
Charles Chanfbcb8812018-04-18 18:41:05 -07002005 }
2006 }
2007 }
2008
Saurav Das00e553b2018-04-21 17:19:48 -07002009 class InternalClusterListener implements ClusterEventListener {
2010 private Instant lastClusterEvent = Instant.EPOCH;
2011
2012 long timeSinceLastClusterEvent() {
2013 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
2014 }
2015
2016 @Override
2017 public void event(ClusterEvent event) {
2018 switch (event.type()) {
2019 case INSTANCE_ACTIVATED:
2020 case INSTANCE_ADDED:
2021 case INSTANCE_READY:
2022 log.debug("Cluster event {} ignored", event.type());
2023 break;
2024 case INSTANCE_DEACTIVATED:
2025 case INSTANCE_REMOVED:
2026 log.info("** Cluster event {}", event.type());
2027 lastClusterEvent = Instant.now();
2028 break;
2029 default:
2030 break;
2031 }
2032
2033 }
2034
2035 }
2036
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002037 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
2038 try {
2039 Set<Interface> intfs = conf.getInterfaces();
2040 Set<Interface> prevIntfs = prevConf.getInterfaces();
2041
2042 // Now we only handle one interface config at each port.
2043 if (intfs.size() != 1 || prevIntfs.size() != 1) {
2044 log.warn("Interface update aborted - one at a time is allowed, " +
2045 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
2046 return;
2047 }
2048
Andrea Campanella060cad82018-04-17 12:09:34 +02002049 //The system is in an incoherent state, abort
2050 if (defaultRoutingHandler == null) {
2051 log.warn("Interface update aborted, defaultRoutingHandler is null");
2052 return;
2053 }
2054
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002055 Interface intf = intfs.stream().findFirst().get();
2056 Interface prevIntf = prevIntfs.stream().findFirst().get();
2057
2058 DeviceId deviceId = intf.connectPoint().deviceId();
2059 PortNumber portNum = intf.connectPoint().port();
2060
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002061 removeSubnetConfig(prevIntf.connectPoint(),
2062 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
2063 new HashSet<>(intf.ipAddressesList())));
2064
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002065 if (!prevIntf.vlanNative().equals(VlanId.NONE)
2066 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
2067 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
2068 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
2069 // Update filtering objective and L2IG group bucket
2070 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
2071 } else {
2072 // RemoveVlanNative
2073 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
2074 }
2075 }
2076
2077 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
2078 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2079 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2080 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
2081 // Update filtering objective and L2IG group bucket
2082 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
2083 } else {
2084 // RemoveVlanUntagged
2085 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
2086 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002087 }
2088
2089 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2090 // RemoveVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002091 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
2092 .filter(i -> !intf.vlanUntagged().equals(i))
2093 .filter(i -> !intf.vlanNative().equals(i))
2094 .forEach(vlanId -> updateVlanConfigInternal(
2095 deviceId, portNum, vlanId, false, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002096 }
2097
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002098 if (!intf.vlanNative().equals(VlanId.NONE)
2099 && !prevIntf.vlanNative().equals(intf.vlanNative())
2100 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2101 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
2102 // Update filtering objective and L2IG group bucket
2103 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
2104 } else {
2105 // AddVlanNative
2106 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
2107 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002108 }
2109
2110 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2111 // AddVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002112 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
2113 .filter(i -> !prevIntf.vlanUntagged().equals(i))
2114 .filter(i -> !prevIntf.vlanNative().equals(i))
2115 .forEach(vlanId -> updateVlanConfigInternal(
2116 deviceId, portNum, vlanId, false, true)
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002117 );
2118 }
2119
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002120 if (!intf.vlanUntagged().equals(VlanId.NONE)
2121 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2122 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
2123 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
2124 // Update filtering objective and L2IG group bucket
2125 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
2126 } else {
2127 // AddVlanUntagged
2128 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
2129 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002130 }
2131 addSubnetConfig(prevIntf.connectPoint(),
2132 Sets.difference(new HashSet<>(intf.ipAddressesList()),
2133 new HashSet<>(prevIntf.ipAddressesList())));
2134 } catch (ConfigException e) {
2135 log.error("Error in configuration");
2136 }
2137 }
2138
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002139 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
2140 VlanId vlanId, boolean pushVlan) {
2141 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2142 if (grpHandler == null) {
2143 log.warn("Failed to retrieve group handler for device {}", deviceId);
2144 return;
2145 }
2146
2147 // Update filtering objective for a single port
2148 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
2149 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
2150
2151 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
2152 // Update L2IG bucket of the port
2153 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
2154 } else {
2155 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2156 }
2157 }
2158
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002159 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
2160 VlanId vlanId, boolean pushVlan, boolean install) {
2161 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2162 if (grpHandler == null) {
2163 log.warn("Failed to retrieve group handler for device {}", deviceId);
2164 return;
2165 }
2166
2167 // Update filtering objective for a single port
2168 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
2169
2170 // Update filtering objective for multicast ingress port
2171 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
2172
2173 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
2174
2175 if (nextId != -1 && !install) {
2176 // Update next objective for a single port as an output port
2177 // Remove a single port from L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002178 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002179 // Remove L2 Bridging rule and L3 Unicast rule to the host
Charles Chan074fae62018-04-29 18:11:37 -07002180 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2181 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002182 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
2183 // only if there is no port configured on that VLAN ID
2184 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
2185 // Remove broadcast forwarding rule for the VLAN
2186 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2187 // Remove L2FG for VLAN
2188 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
2189 } else {
2190 // Remove L2IG of the port
2191 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
2192 }
2193 } else if (install) {
2194 if (nextId != -1) {
2195 // Add a single port to L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002196 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002197 } else {
2198 // Create L2FG for VLAN
2199 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
2200 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2201 }
Charles Chan074fae62018-04-29 18:11:37 -07002202 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2203 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002204 } else {
2205 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2206 }
2207 }
2208
2209 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2210 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2211 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2212
2213 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2214 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2215 .filter(intf -> !intf.connectPoint().equals(cp))
2216 .flatMap(intf -> intf.ipAddressesList().stream())
2217 .collect(Collectors.toSet());
2218 // 1. Partial subnet population
2219 // Remove routing rules for removed subnet from previous configuration,
2220 // which does not also exist in other interfaces in the same device
2221 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2222 .map(InterfaceIpAddress::subnetAddress)
2223 .collect(Collectors.toSet());
2224
Shibu Vijayakumar5e26f8c2020-01-07 11:45:09 +00002225 Set<IpPrefix> subnetsToBeRevoked = ipPrefixSet.stream()
2226 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2227 .collect(Collectors.toSet());
2228
2229 // Check if any of the subnets to be revoked is configured in the pairDevice.
2230 // If any, repopulate the subnet with pairDevice connectPoint instead of revoking.
2231 Optional<DeviceId> pairDevice = getPairDeviceId(cp.deviceId());
2232 if (pairDevice.isPresent()) {
2233 Set<IpPrefix> pairDeviceIpPrefix = getDeviceSubnetMap().get(pairDevice.get());
2234
2235 Set<IpPrefix> subnetsExistingInPairDevice = subnetsToBeRevoked.stream()
2236 .filter(ipPrefix -> pairDeviceIpPrefix.contains(ipPrefix))
2237 .collect(Collectors.toSet());
2238
2239 // Update the subnets existing in pair device with pair device connect point.
2240 if (!subnetsExistingInPairDevice.isEmpty()) {
2241 // PortNumber of connect point is not relevant in populate subnet and hence providing as ANY.
2242 ConnectPoint pairDeviceCp = new ConnectPoint(pairDevice.get(), PortNumber.ANY);
2243 log.debug("Updating the subnets: {} with pairDevice connectPoint as it exists in the Pair device: {}",
2244 subnetsExistingInPairDevice, pairDeviceCp);
2245 defaultRoutingHandler.populateSubnet(Collections.singleton(pairDeviceCp), subnetsExistingInPairDevice);
2246 }
2247
2248 // Remove only the subnets that are not configured in the pairDevice.
2249 subnetsToBeRevoked = Sets.difference(subnetsToBeRevoked, subnetsExistingInPairDevice);
2250 }
2251
2252 if (!subnetsToBeRevoked.isEmpty()) {
2253 log.debug("Removing subnets for connectPoint: {}, subnets: {}", cp, subnetsToBeRevoked);
2254 defaultRoutingHandler.revokeSubnet(subnetsToBeRevoked);
2255 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002256
2257 // 2. Interface IP punts
2258 // Remove IP punts for old Intf address
2259 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2260 .map(InterfaceIpAddress::ipAddress)
2261 .collect(Collectors.toSet());
2262 ipAddressSet.stream()
2263 .map(InterfaceIpAddress::ipAddress)
2264 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2265 .forEach(interfaceIpAddress ->
2266 routingRulePopulator.revokeSingleIpPunts(
2267 cp.deviceId(), interfaceIpAddress));
2268
2269 // 3. Host unicast routing rule
2270 // Remove unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002271 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002272 }
2273
2274 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2275 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2276 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2277
2278 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2279 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2280 .filter(intf -> !intf.connectPoint().equals(cp))
2281 .flatMap(intf -> intf.ipAddressesList().stream())
2282 .collect(Collectors.toSet());
2283 // 1. Partial subnet population
2284 // Add routing rules for newly added subnet, which does not also exist in
2285 // other interfaces in the same device
2286 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2287 .map(InterfaceIpAddress::subnetAddress)
2288 .collect(Collectors.toSet());
Shibu Vijayakumar5e26f8c2020-01-07 11:45:09 +00002289 Set<IpPrefix> subnetsToBePopulated = ipPrefixSet.stream()
2290 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2291 .collect(Collectors.toSet());
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002292
Shibu Vijayakumar5e26f8c2020-01-07 11:45:09 +00002293 if (!subnetsToBePopulated.isEmpty()) {
2294 log.debug("Adding subnets for connectPoint: {}, subnets: {}", cp, subnetsToBePopulated);
2295
2296 // check if pair-device has the same subnet configured?
2297 Optional<DeviceId> pairDevice = getPairDeviceId(cp.deviceId());
2298 if (pairDevice.isPresent()) {
2299 Set<IpPrefix> pairDeviceIpPrefix = getDeviceSubnetMap().get(pairDevice.get());
2300
2301 Set<IpPrefix> subnetsToBePopulatedAsDualHomed = subnetsToBePopulated.stream()
2302 .filter(ipPrefix -> pairDeviceIpPrefix.contains(ipPrefix))
2303 .collect(Collectors.toSet());
2304 Set<IpPrefix> subnetsToBePopulatedAsSingleHomed = Sets.difference(subnetsToBePopulated,
2305 subnetsToBePopulatedAsDualHomed);
2306
2307 if (!subnetsToBePopulatedAsSingleHomed.isEmpty()) {
2308 defaultRoutingHandler.populateSubnet(
2309 Collections.singleton(cp),
2310 subnetsToBePopulatedAsSingleHomed);
2311 }
2312
2313 if (!subnetsToBePopulatedAsDualHomed.isEmpty()) {
2314 Set<ConnectPoint> cpts = new HashSet<>();
2315 cpts.add(cp);
2316 // As Subnets is DualHomed adding the pairDevice also as ConnectPoint.
2317 // PortNumber of connect point is not relevant in populate subnet and hence providing as ANY.
2318 ConnectPoint pairCp = new ConnectPoint(pairDevice.get(), PortNumber.ANY);
2319 cpts.add(pairCp);
2320
2321 log.debug("Adding DualHomed subnets for connectPoint: {} and its pair device: {}, subnets: {}",
2322 cp, pairCp, subnetsToBePopulatedAsDualHomed);
2323
2324 // populating the subnets as DualHomed
2325 defaultRoutingHandler.populateSubnet(
2326 cpts,
2327 subnetsToBePopulated);
2328
2329 // revoking the subnets populated in the device as it is now Dualhomed.
2330 defaultRoutingHandler.revokeSubnet(Collections.singleton(cp.deviceId()),
2331 subnetsToBePopulatedAsDualHomed);
2332 }
2333 } else {
2334 defaultRoutingHandler.populateSubnet(
2335 Collections.singleton(cp),
2336 subnetsToBePopulated);
2337 }
2338 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002339
2340 // 2. Interface IP punts
2341 // Add IP punts for new Intf address
2342 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2343 .map(InterfaceIpAddress::ipAddress)
2344 .collect(Collectors.toSet());
2345 ipAddressSet.stream()
2346 .map(InterfaceIpAddress::ipAddress)
2347 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2348 .forEach(interfaceIpAddress ->
2349 routingRulePopulator.populateSingleIpPunts(
2350 cp.deviceId(), interfaceIpAddress));
2351
2352 // 3. Host unicast routing rule
2353 // Add unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002354 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002355 }
sangho80f11cb2015-04-01 13:05:26 -07002356}