blob: a8ccd685521215d1c91941449d2ebfd7f14d1bf7 [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sangho80f11cb2015-04-01 13:05:26 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.segmentrouting;
17
Jonathan Hart61e24e12017-11-30 18:23:42 -080018import com.google.common.collect.HashMultimap;
19import com.google.common.collect.ImmutableMap;
20import com.google.common.collect.Maps;
21import com.google.common.collect.Multimap;
Charles Chanf0ae41e2017-08-23 13:55:39 -070022import com.google.common.collect.Sets;
sangho80f11cb2015-04-01 13:05:26 -070023import org.onlab.packet.Ethernet;
Pier Ventreb6b81d52016-12-02 08:16:05 -080024import org.onlab.packet.ICMP6;
Charles Chan77277672015-10-20 16:24:19 -070025import org.onlab.packet.IPv4;
Pier Ventreb6a7f342016-11-26 21:05:22 -080026import org.onlab.packet.IPv6;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070027import org.onlab.packet.IpAddress;
Charles Chan77277672015-10-20 16:24:19 -070028import org.onlab.packet.IpPrefix;
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +000029import org.onlab.packet.MacAddress;
Carmelo Cascone61004262020-01-16 13:36:02 -080030import org.onlab.packet.VlanId;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070031import org.onlab.util.KryoNamespace;
Charles Chan873661e2017-11-30 15:37:50 -080032import org.onlab.util.Tools;
Saurav Dasc3604f12016-03-23 11:22:49 -070033import org.onosproject.cfg.ComponentConfigService;
Saurav Das00e553b2018-04-21 17:19:48 -070034import org.onosproject.cluster.ClusterEvent;
35import org.onosproject.cluster.ClusterEventListener;
Pier Luigi580fd8a2018-01-16 10:47:50 +010036import org.onosproject.cluster.ClusterService;
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -070037import org.onosproject.cluster.LeadershipService;
Pierdb27b8d2018-04-17 16:29:56 +020038import org.onosproject.cluster.NodeId;
sangho80f11cb2015-04-01 13:05:26 -070039import org.onosproject.core.ApplicationId;
40import org.onosproject.core.CoreService;
41import org.onosproject.event.Event;
Charles Chanfbcb8812018-04-18 18:41:05 -070042import org.onosproject.mastership.MastershipEvent;
43import org.onosproject.mastership.MastershipListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070044import org.onosproject.mastership.MastershipService;
Pier1f87aca2018-03-14 16:47:32 -070045import org.onosproject.mcast.api.McastEvent;
46import org.onosproject.mcast.api.McastListener;
47import org.onosproject.mcast.api.MulticastRouteService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080048import org.onosproject.net.ConnectPoint;
Jonathan Hart54541d12016-04-12 15:39:44 -070049import org.onosproject.net.Device;
50import org.onosproject.net.DeviceId;
Charles Chanf0ae41e2017-08-23 13:55:39 -070051import org.onosproject.net.Host;
52import org.onosproject.net.HostId;
Jonathan Hart54541d12016-04-12 15:39:44 -070053import org.onosproject.net.Link;
54import org.onosproject.net.Port;
Charles Chanf4586112015-11-09 16:37:23 -080055import org.onosproject.net.PortNumber;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070056import org.onosproject.net.config.ConfigException;
Charles Chan72f556a2015-10-05 17:50:33 -070057import org.onosproject.net.config.ConfigFactory;
58import org.onosproject.net.config.NetworkConfigEvent;
Charles Chan72f556a2015-10-05 17:50:33 -070059import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070060import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkeyae0068a2017-08-15 11:02:29 -070061import org.onosproject.net.config.basics.InterfaceConfig;
62import org.onosproject.net.config.basics.McastConfig;
Charles Chan72f556a2015-10-05 17:50:33 -070063import org.onosproject.net.config.basics.SubjectFactories;
Saurav Dase6c448a2018-01-18 12:07:33 -080064import org.onosproject.net.device.DeviceAdminService;
Jonathan Hart54541d12016-04-12 15:39:44 -070065import org.onosproject.net.device.DeviceEvent;
66import org.onosproject.net.device.DeviceListener;
67import org.onosproject.net.device.DeviceService;
Charles Chanf4586112015-11-09 16:37:23 -080068import org.onosproject.net.flow.TrafficSelector;
69import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart54541d12016-04-12 15:39:44 -070070import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan0b1dd7e2018-08-19 19:21:46 -070071import org.onosproject.net.flowobjective.NextObjective;
Charles Chanf4586112015-11-09 16:37:23 -080072import org.onosproject.net.host.HostEvent;
73import org.onosproject.net.host.HostListener;
Charles Chanc6bcdf92018-06-01 16:33:48 -070074import org.onosproject.net.host.HostProbingService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080075import org.onosproject.net.host.HostService;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070076import org.onosproject.net.host.InterfaceIpAddress;
Pierdb27b8d2018-04-17 16:29:56 +020077import org.onosproject.net.intent.WorkPartitionService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070078import org.onosproject.net.intf.Interface;
79import org.onosproject.net.intf.InterfaceService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080080import org.onosproject.net.link.LinkEvent;
81import org.onosproject.net.link.LinkListener;
82import org.onosproject.net.link.LinkService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070083import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080084import org.onosproject.net.packet.InboundPacket;
85import org.onosproject.net.packet.PacketContext;
86import org.onosproject.net.packet.PacketProcessor;
87import org.onosproject.net.packet.PacketService;
Pier Luigid8a15162018-02-15 16:33:08 +010088import org.onosproject.net.topology.TopologyEvent;
89import org.onosproject.net.topology.TopologyListener;
Charles Chanc91c8782016-03-30 17:54:24 -070090import org.onosproject.net.topology.TopologyService;
Charles Chanf0ae41e2017-08-23 13:55:39 -070091import org.onosproject.routeservice.ResolvedRoute;
Ray Milkeyae0068a2017-08-15 11:02:29 -070092import org.onosproject.routeservice.RouteEvent;
93import org.onosproject.routeservice.RouteListener;
94import org.onosproject.routeservice.RouteService;
Charles Chanc91c8782016-03-30 17:54:24 -070095import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
96import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventre6b19e482016-11-07 16:21:04 -080097import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkeyae0068a2017-08-15 11:02:29 -070098import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanc91c8782016-03-30 17:54:24 -070099import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -0700100import org.onosproject.segmentrouting.grouphandler.DestinationSet;
101import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Harshada Chaundkar9204f312019-07-02 16:01:24 +0000102import org.onosproject.segmentrouting.mcast.McastFilteringObjStoreKey;
Pier Luigi69f774d2018-02-28 12:10:50 +0100103import org.onosproject.segmentrouting.mcast.McastHandler;
104import org.onosproject.segmentrouting.mcast.McastRole;
Piere99511d2018-04-19 16:47:06 +0200105import org.onosproject.segmentrouting.mcast.McastRoleStoreKey;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700106import org.onosproject.segmentrouting.mcast.McastStoreKey;
Charles Chan12a8a842020-02-14 13:23:57 -0800107import org.onosproject.segmentrouting.phasedrecovery.api.PhasedRecoveryService;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700108import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700109import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800110import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700111import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800112import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700113import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
Ray Milkeyae0068a2017-08-15 11:02:29 -0700114import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800115import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Saurav Das261c3002017-06-13 15:35:54 -0700116import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Carmelo Cascone61004262020-01-16 13:36:02 -0800117import org.onosproject.segmentrouting.storekey.MacVlanNextObjectiveStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -0700118import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -0800119import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -0700120import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Charles Chan8d316332018-06-19 20:31:57 -0700121import org.onosproject.segmentrouting.xconnect.api.XconnectService;
Jonathan Hart54541d12016-04-12 15:39:44 -0700122import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700123import org.onosproject.store.service.EventuallyConsistentMap;
124import org.onosproject.store.service.EventuallyConsistentMapBuilder;
125import org.onosproject.store.service.StorageService;
126import org.onosproject.store.service.WallClockTimestamp;
Charles Chan873661e2017-11-30 15:37:50 -0800127import org.osgi.service.component.ComponentContext;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700128import org.osgi.service.component.annotations.Activate;
129import org.osgi.service.component.annotations.Component;
130import org.osgi.service.component.annotations.Deactivate;
131import org.osgi.service.component.annotations.Modified;
132import org.osgi.service.component.annotations.Reference;
133import org.osgi.service.component.annotations.ReferenceCardinality;
Charles Chanb4f9cd12019-02-27 11:46:32 -0800134import org.osgi.service.component.annotations.ReferencePolicy;
sangho80f11cb2015-04-01 13:05:26 -0700135import org.slf4j.Logger;
136import org.slf4j.LoggerFactory;
137
Saurav Das00e553b2018-04-21 17:19:48 -0700138import java.time.Instant;
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700139import java.util.ArrayList;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800140import java.util.Collections;
Charles Chan873661e2017-11-30 15:37:50 -0800141import java.util.Dictionary;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800142import java.util.HashSet;
143import java.util.List;
144import java.util.Map;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800145import java.util.Optional;
146import java.util.Set;
Andrea Campanella060cad82018-04-17 12:09:34 +0200147import java.util.concurrent.CompletableFuture;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800148import java.util.concurrent.ConcurrentHashMap;
Andrea Campanella060cad82018-04-17 12:09:34 +0200149import java.util.concurrent.CopyOnWriteArrayList;
Charles Chan77cdde42018-05-08 21:35:50 -0700150import java.util.concurrent.ExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800151import java.util.concurrent.Executors;
152import java.util.concurrent.ScheduledExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800153import java.util.concurrent.TimeUnit;
154import java.util.concurrent.atomic.AtomicBoolean;
155import java.util.stream.Collectors;
sangho80f11cb2015-04-01 13:05:26 -0700156
Charles Chand6d25332016-02-26 22:19:52 -0800157import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800158import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700159import static org.onlab.util.Tools.groupedThreads;
Saurav Dase321cff2018-02-09 17:26:45 -0800160import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
161import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700162import static org.onosproject.segmentrouting.OsgiPropertyConstants.ACTIVE_PROBING_DEFAULT;
163import static org.onosproject.segmentrouting.OsgiPropertyConstants.DEFAULT_INTERNAL_VLAN_DEFAULT;
164import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ACTIVE_PROBING;
165import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_DEFAULT_INTERNAL_VLAN;
166import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_PW_TRANSPORT_VLAN;
167import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_RESPOND_TO_UNKNOWN_HOSTS;
168import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_DOUBLE_TAGGED_HOSTS;
psneha86e60d32019-03-26 06:31:41 -0400169import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_SIMPLIFICATION;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700170import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SINGLE_HOMED_DOWN;
Ray Milkeye96d0de2018-11-02 17:12:21 -0700171import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SYMMETRIC_PROBING;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700172import static org.onosproject.segmentrouting.OsgiPropertyConstants.PW_TRANSPORT_VLAN_DEFAULT;
173import static org.onosproject.segmentrouting.OsgiPropertyConstants.RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
174import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
psneha86e60d32019-03-26 06:31:41 -0400175import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_SIMPLIFICATION_DEFAULT;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700176import static org.onosproject.segmentrouting.OsgiPropertyConstants.SINGLE_HOMED_DOWN_DEFAULT;
Ray Milkeye96d0de2018-11-02 17:12:21 -0700177import static org.onosproject.segmentrouting.OsgiPropertyConstants.SYMMETRIC_PROBING_DEFAULT;
Charles Chand6d25332016-02-26 22:19:52 -0800178
Charles Chanb7f75ac2016-01-11 18:28:54 -0800179/**
180 * Segment routing manager.
181 */
Ray Milkey05edbfc2018-10-23 14:23:16 -0700182@Component(
183 immediate = true,
184 service = SegmentRoutingService.class,
185 property = {
186 PROP_ACTIVE_PROBING + ":Boolean=" + ACTIVE_PROBING_DEFAULT,
187 PROP_SINGLE_HOMED_DOWN + ":Boolean=" + SINGLE_HOMED_DOWN_DEFAULT,
188 PROP_RESPOND_TO_UNKNOWN_HOSTS + ":Boolean=" + RESPOND_TO_UNKNOWN_HOSTS_DEFAULT,
189 PROP_ROUTE_DOUBLE_TAGGED_HOSTS + ":Boolean=" + ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT,
190 PROP_DEFAULT_INTERNAL_VLAN + ":Integer=" + DEFAULT_INTERNAL_VLAN_DEFAULT,
191 PROP_PW_TRANSPORT_VLAN + ":Integer=" + PW_TRANSPORT_VLAN_DEFAULT,
psneha86e60d32019-03-26 06:31:41 -0400192 PROP_SYMMETRIC_PROBING + ":Boolean=" + SYMMETRIC_PROBING_DEFAULT,
193 PROP_ROUTE_SIMPLIFICATION + ":Boolean=" + ROUTE_SIMPLIFICATION_DEFAULT
Ray Milkey05edbfc2018-10-23 14:23:16 -0700194 }
195)
sangho27462c62015-05-14 00:39:53 -0700196public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700197
Charles Chan46fdfaf2016-11-09 20:51:44 -0800198 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan910be6a2017-08-23 14:46:43 -0700199 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sangho80f11cb2015-04-01 13:05:26 -0700200
Ray Milkey2bd24a92018-08-17 14:54:17 -0700201 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700202 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700203
Ray Milkey2bd24a92018-08-17 14:54:17 -0700204 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chancf789822019-03-22 10:04:27 -0700205 public NeighbourResolutionService neighbourResolutionService;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800206
Ray Milkey2bd24a92018-08-17 14:54:17 -0700207 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100208 public CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700209
Ray Milkey2bd24a92018-08-17 14:54:17 -0700210 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700211 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700212
Ray Milkey2bd24a92018-08-17 14:54:17 -0700213 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700214 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700215
Ray Milkey2bd24a92018-08-17 14:54:17 -0700216 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chanc6bcdf92018-06-01 16:33:48 -0700217 HostProbingService probingService;
Charles Chan873661e2017-11-30 15:37:50 -0800218
Ray Milkey2bd24a92018-08-17 14:54:17 -0700219 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100220 public DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700221
Ray Milkey2bd24a92018-08-17 14:54:17 -0700222 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Saurav Dase6c448a2018-01-18 12:07:33 -0800223 DeviceAdminService deviceAdminService;
224
Ray Milkey2bd24a92018-08-17 14:54:17 -0700225 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800226 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700227
Ray Milkey2bd24a92018-08-17 14:54:17 -0700228 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100229 public LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700230
Ray Milkey2bd24a92018-08-17 14:54:17 -0700231 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800232 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700233
Ray Milkey2bd24a92018-08-17 14:54:17 -0700234 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800235 public StorageService storageService;
Charles Chandebfea32016-10-24 14:52:01 -0700236
Ray Milkey2bd24a92018-08-17 14:54:17 -0700237 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100238 public MulticastRouteService multicastRouteService;
Charles Chandebfea32016-10-24 14:52:01 -0700239
Ray Milkey2bd24a92018-08-17 14:54:17 -0700240 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan83163812018-01-09 13:45:07 -0800241 public TopologyService topologyService;
Charles Chandebfea32016-10-24 14:52:01 -0700242
Ray Milkey2bd24a92018-08-17 14:54:17 -0700243 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan19b70032019-04-17 14:20:26 -0700244 public RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800245
Ray Milkey2bd24a92018-08-17 14:54:17 -0700246 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800247 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800248
Ray Milkey2bd24a92018-08-17 14:54:17 -0700249 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800250 public InterfaceService interfaceService;
251
Ray Milkey2bd24a92018-08-17 14:54:17 -0700252 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi580fd8a2018-01-16 10:47:50 +0100253 public ClusterService clusterService;
254
Ray Milkey2bd24a92018-08-17 14:54:17 -0700255 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pierdb27b8d2018-04-17 16:29:56 +0200256 public WorkPartitionService workPartitionService;
Pier Luigi580fd8a2018-01-16 10:47:50 +0100257
Ray Milkey2bd24a92018-08-17 14:54:17 -0700258 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700259 public LeadershipService leadershipService;
260
Charles Chanb4f9cd12019-02-27 11:46:32 -0800261 @Reference(cardinality = ReferenceCardinality.OPTIONAL,
Charles Chanb4f9cd12019-02-27 11:46:32 -0800262 policy = ReferencePolicy.DYNAMIC)
Carmelo Cascone61004262020-01-16 13:36:02 -0800263 public volatile XconnectService xconnectService;
Charles Chan8d316332018-06-19 20:31:57 -0700264
Charles Chan12a8a842020-02-14 13:23:57 -0800265 @Reference(cardinality = ReferenceCardinality.OPTIONAL,
266 policy = ReferencePolicy.DYNAMIC)
267 volatile PhasedRecoveryService phasedRecoveryService;
268
Ray Milkey05edbfc2018-10-23 14:23:16 -0700269 /** Enable active probing to discover dual-homed hosts. */
270 boolean activeProbing = ACTIVE_PROBING_DEFAULT;
Charles Chan873661e2017-11-30 15:37:50 -0800271
Ray Milkeye96d0de2018-11-02 17:12:21 -0700272 /** Enable only send probe on the same port number of the pair device. */
273 boolean symmetricProbing = SYMMETRIC_PROBING_DEFAULT;
Mayank Tiwarif7942402018-10-29 18:27:35 -0400274
Ray Milkey05edbfc2018-10-23 14:23:16 -0700275 /** Enable administratively taking down single-homed hosts. */
276 boolean singleHomedDown = SINGLE_HOMED_DOWN_DEFAULT;
Saurav Dasec683dc2018-04-27 18:42:30 -0700277
Ray Milkey05edbfc2018-10-23 14:23:16 -0700278 /** Enable this to respond to ARP/NDP requests from unknown hosts. */
279 boolean respondToUnknownHosts = RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700280
Ray Milkey05edbfc2018-10-23 14:23:16 -0700281 /** Program flows and groups to pop and route double tagged hosts. */
282 boolean routeDoubleTaggedHosts = ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
Charles Chan4eb73bb2018-07-19 14:55:31 -0700283
Ray Milkey05edbfc2018-10-23 14:23:16 -0700284 /** internal vlan assigned by default to unconfigured ports. */
285 private int defaultInternalVlan = DEFAULT_INTERNAL_VLAN_DEFAULT;
Saurav Das9bf49582018-08-13 15:34:26 -0700286
Ray Milkey05edbfc2018-10-23 14:23:16 -0700287 /** vlan used for transport of pseudowires between switches. */
288 private int pwTransportVlan = PW_TRANSPORT_VLAN_DEFAULT;
Saurav Das9bf49582018-08-13 15:34:26 -0700289
psneha86e60d32019-03-26 06:31:41 -0400290 /** Enabling route simplification. */
291 boolean routeSimplification = ROUTE_SIMPLIFICATION_DEFAULT;
292
Charles Chandebfea32016-10-24 14:52:01 -0700293 ArpHandler arpHandler = null;
294 IcmpHandler icmpHandler = null;
295 IpHandler ipHandler = null;
296 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700297 ApplicationId appId;
298 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700299
Charles Chandebfea32016-10-24 14:52:01 -0700300 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700301 private TunnelHandler tunnelHandler = null;
302 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700303 private InternalPacketProcessor processor = null;
304 private InternalLinkListener linkListener = null;
305 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700306 private AppConfigHandler appCfgHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800307 McastHandler mcastHandler = null;
308 HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800309 private RouteHandler routeHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800310 LinkHandler linkHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800311 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800312 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigid8a15162018-02-15 16:33:08 +0100313 private TopologyHandler topologyHandler = null;
Charles Chan82ab1932016-01-30 23:22:37 -0800314 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700315 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
316 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700317 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigid8a15162018-02-15 16:33:08 +0100318 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chanfbcb8812018-04-18 18:41:05 -0700319 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
Saurav Das00e553b2018-04-21 17:19:48 -0700320 final InternalClusterListener clusterListener = new InternalClusterListener();
Andrea Campanella060cad82018-04-17 12:09:34 +0200321 //Completable future for network configuration process to buffer config events handling during activation
322 private CompletableFuture<Boolean> networkConfigCompletion = null;
Ray Milkey521e4392018-11-16 15:15:14 -0800323 private final Object networkConfigCompletionLock = new Object();
Charles Chan39b75522018-04-21 00:44:29 -0700324 private List<Event> queuedEvents = new CopyOnWriteArrayList<>();
sangho80f11cb2015-04-01 13:05:26 -0700325
Charles Chan52003922018-04-05 16:31:15 -0700326 // Handles device, link, topology and network config events
Charles Chanfbcb8812018-04-18 18:41:05 -0700327 private ScheduledExecutorService mainEventExecutor;
sangho80f11cb2015-04-01 13:05:26 -0700328
Charles Chanfbcb8812018-04-18 18:41:05 -0700329 // Handles host, route and mcast events respectively
330 private ScheduledExecutorService hostEventExecutor;
331 private ScheduledExecutorService routeEventExecutor;
332 private ScheduledExecutorService mcastEventExecutor;
Charles Chan77cdde42018-05-08 21:35:50 -0700333 private ExecutorService packetExecutor;
Charles Chana7601c92019-01-23 15:03:17 -0800334 ExecutorService neighborExecutor;
Charles Chan52003922018-04-05 16:31:15 -0700335
336 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chanb7f75ac2016-01-11 18:28:54 -0800337 /**
Saurav Das261c3002017-06-13 15:35:54 -0700338 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700339 * Used to keep track on MPLS group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800340 */
Charles Chan873661e2017-11-30 15:37:50 -0800341 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das261c3002017-06-13 15:35:54 -0700342 dsNextObjStore = 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 + vlanid) as key.
345 * Used to keep track on L2 flood group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800346 */
Charles Chan873661e2017-11-30 15:37:50 -0800347 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800348 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800349 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700350 * Per device next objective ID store with (device id + port + treatment + meta) as key.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000351 * Used to keep track on L2 interface group and L3 unicast group information for direct hosts.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800352 */
Charles Chan873661e2017-11-30 15:37:50 -0800353 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800354 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800355
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700356 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000357 * Per device next objective ID store with (device id + MAC address + vlan) as key.
358 * Used to keep track of L3 unicast group for indirect hosts.
359 */
360 private EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer>
361 macVlanNextObjStore = null;
362
Saurav Das2d94d312015-11-24 23:21:05 -0800363 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
364 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700365
Saurav Das261c3002017-06-13 15:35:54 -0700366 private AtomicBoolean programmingScheduled = new AtomicBoolean();
367
Charles Chanc91c8782016-03-30 17:54:24 -0700368 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700369 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
370 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700371 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700372 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800373 public SegmentRoutingDeviceConfig createConfig() {
374 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700375 }
376 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800377
Charles Chanc91c8782016-03-30 17:54:24 -0700378 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700379 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
380 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700381 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800382 @Override
383 public SegmentRoutingAppConfig createConfig() {
384 return new SegmentRoutingAppConfig();
385 }
386 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800387
Charles Chanc91c8782016-03-30 17:54:24 -0700388 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700389 new ConfigFactory<ApplicationId, McastConfig>(
390 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700391 McastConfig.class, "multicast") {
392 @Override
393 public McastConfig createConfig() {
394 return new McastConfig();
395 }
396 };
397
Charles Chan1963f4f2016-02-18 14:22:42 -0800398 /**
399 * Segment Routing App ID.
400 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800401 public static final String APP_NAME = "org.onosproject.segmentrouting";
Saurav Das7c305372015-10-28 12:39:42 -0700402
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700403 /**
404 * Minumum and maximum value of dummy VLAN ID to be allocated.
405 */
406 public static final int MIN_DUMMY_VLAN_ID = 2;
407 public static final int MAX_DUMMY_VLAN_ID = 4093;
408
Charles Chana7601c92019-01-23 15:03:17 -0800409 private static final int DEFAULT_POOL_SIZE = 32;
410
Saurav Dasec683dc2018-04-27 18:42:30 -0700411 Instant lastEdgePortEvent = Instant.EPOCH;
412
sangho80f11cb2015-04-01 13:05:26 -0700413 @Activate
Charles Chan873661e2017-11-30 15:37:50 -0800414 protected void activate(ComponentContext context) {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800415 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700416
Charles Chana7601c92019-01-23 15:03:17 -0800417 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(
418 groupedThreads("onos/sr", "event-main-%d", log));
419 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(
420 groupedThreads("onos/sr", "event-host-%d", log));
421 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(
422 groupedThreads("onos/sr", "event-route-%d", log));
423 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(
424 groupedThreads("onos/sr", "event-mcast-%d", log));
425 packetExecutor = Executors.newSingleThreadExecutor(groupedThreads("onos/sr", "packet-%d", log));
426 neighborExecutor = Executors.newFixedThreadPool(DEFAULT_POOL_SIZE,
427 groupedThreads("onos/sr", "neighbor-%d", log));
Charles Chanfbcb8812018-04-18 18:41:05 -0700428
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700429 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700430 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700431 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700432 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700433 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700434 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700435 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700436 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700437 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700438
Charles Chan10b0fb72017-02-02 16:20:42 -0800439 log.debug("Creating EC map vlannextobjectivestore");
440 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
441 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
442 vlanNextObjStore = vlanNextObjMapBuilder
443 .withName("vlannextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700444 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700445 .withTimestampProvider((k, v) -> new WallClockTimestamp())
446 .build();
447
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000448 log.debug("Creating EC map macvlannextobjectivestore");
449 EventuallyConsistentMapBuilder<MacVlanNextObjectiveStoreKey, Integer>
450 macVlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
451 macVlanNextObjStore = macVlanNextObjMapBuilder
452 .withName("macvlannextobjectivestore")
453 .withSerializer(createSerializer())
454 .withTimestampProvider((k, v) -> new WallClockTimestamp())
455 .build();
456
Saurav Das2d94d312015-11-24 23:21:05 -0800457 log.debug("Creating EC map subnetnextobjectivestore");
458 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
459 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
460 portNextObjStore = portNextObjMapBuilder
461 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700462 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800463 .withTimestampProvider((k, v) -> new WallClockTimestamp())
464 .build();
465
sangho4a5c42a2015-05-20 22:16:38 -0700466 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
467 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700468 tunnelStore = tunnelMapBuilder
469 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700470 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700471 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700472 .build();
473
474 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
475 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700476 policyStore = policyMapBuilder
477 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700478 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700479 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700480 .build();
481
Charles Chan97979802020-02-26 15:04:44 -0800482 processor = new InternalPacketProcessor();
483 linkListener = new InternalLinkListener();
484 deviceListener = new InternalDeviceListener();
485 appCfgHandler = new AppConfigHandler(this);
486 mcastHandler = new McastHandler(this);
487 hostHandler = new HostHandler(this);
488 linkHandler = new LinkHandler(this);
489 routeHandler = new RouteHandler(this);
490 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
491 l2TunnelHandler = new DefaultL2TunnelHandler(this);
492 topologyHandler = new TopologyHandler(this);
493
Pier Luigib9632ba2017-01-12 18:14:58 -0800494 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700495 "requestInterceptsEnabled", "false", false);
Charles Chanc1909e12017-08-08 15:13:37 -0700496 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700497 "requestInterceptsEnabled", "false", false);
Charles Chan9597f8d2017-07-24 15:56:10 -0700498 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700499 "arpEnabled", "false", false);
Pier Luigi0ebeb312017-02-02 22:31:34 -0800500 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700501 "greedyLearningIpv6", "true", false);
Charles Chancf8ea472017-02-28 15:15:17 -0800502 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700503 "forceUnprovision", "true", false);
Charles Chanc7b73c72017-08-10 16:57:28 -0700504 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan0fa51db2019-04-10 17:12:20 -0700505 "distributed", "true", false);
Charles Chan804772f2017-08-14 11:42:11 -0700506 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700507 "multihomingEnabled", "true", false);
Charles Chan0c9c19f2017-11-29 19:54:20 -0800508 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700509 "staleLinkAge", "15000", false);
Charles Chan0c9c19f2017-11-29 19:54:20 -0800510 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700511 "allowDuplicateIps", "false", false);
Yi Tsenga7da8d82018-08-19 03:09:54 +0800512 // For P4 switches
513 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700514 "fallbackFlowPollFrequency", "4", false);
Yi Tsenga7da8d82018-08-19 03:09:54 +0800515 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700516 "fallbackGroupPollFrequency", "3", false);
Charles Chan873661e2017-11-30 15:37:50 -0800517 compCfgService.registerProperties(getClass());
518 modified(context);
Saurav Dasc3604f12016-03-23 11:22:49 -0700519
Charles Chand6d25332016-02-26 22:19:52 -0800520 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700521 cfgService.registerConfigFactory(deviceConfigFactory);
522 cfgService.registerConfigFactory(appConfigFactory);
523 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase321cff2018-02-09 17:26:45 -0800524 log.info("Configuring network before adding listeners");
Andrea Campanella060cad82018-04-17 12:09:34 +0200525
Charles Chan6961f222018-01-04 14:26:07 -0800526 cfgListener.configureNetwork();
527
Charles Chan82ab1932016-01-30 23:22:37 -0800528 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700529 packetService.addProcessor(processor, PacketProcessor.director(2));
530 linkService.addListener(linkListener);
531 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700532 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700533 routeService.addListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100534 topologyService.addListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700535 mastershipService.addListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700536 clusterService.addListener(clusterListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700537
Charles Chanc12c3d02018-03-09 15:53:44 -0800538 linkHandler.init();
Andreas Pantelopoulos75eef062018-01-11 07:53:48 -0800539 l2TunnelHandler.init();
540
Ray Milkey521e4392018-11-16 15:15:14 -0800541 synchronized (networkConfigCompletionLock) {
542 networkConfigCompletion.whenComplete((value, ex) -> {
543 //setting to null for easier fall through
544 networkConfigCompletion = null;
545 //process all queued events
546 queuedEvents.forEach(event -> {
547 mainEventExecutor.execute(new InternalEventHandler(event));
548 });
Andrea Campanella060cad82018-04-17 12:09:34 +0200549 });
Ray Milkey521e4392018-11-16 15:15:14 -0800550 }
Andrea Campanella060cad82018-04-17 12:09:34 +0200551
sangho80f11cb2015-04-01 13:05:26 -0700552 log.info("Started");
553 }
554
Saurav Dase6c448a2018-01-18 12:07:33 -0800555 KryoNamespace.Builder createSerializer() {
Jonathan Hart54541d12016-04-12 15:39:44 -0700556 return new KryoNamespace.Builder()
557 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700558 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800559 VlanNextObjectiveStoreKey.class,
560 DestinationSet.class,
Andreas Pantelopoulos92998c92018-05-29 13:11:14 -0700561 DestinationSet.DestinationSetType.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800562 NextNeighbors.class,
563 Tunnel.class,
564 DefaultTunnel.class,
565 Policy.class,
566 TunnelPolicy.class,
567 Policy.Type.class,
568 PortNextObjectiveStoreKey.class,
569 XConnectStoreKey.class,
570 L2Tunnel.class,
571 L2TunnelPolicy.class,
572 DefaultL2Tunnel.class,
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700573 DefaultL2TunnelPolicy.class,
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000574 MacVlanNextObjectiveStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700575 );
576 }
577
sangho80f11cb2015-04-01 13:05:26 -0700578 @Deactivate
579 protected void deactivate() {
Charles Chanfbcb8812018-04-18 18:41:05 -0700580 mainEventExecutor.shutdown();
581 hostEventExecutor.shutdown();
582 routeEventExecutor.shutdown();
583 mcastEventExecutor.shutdown();
Charles Chan77cdde42018-05-08 21:35:50 -0700584 packetExecutor.shutdown();
Charles Chana7601c92019-01-23 15:03:17 -0800585 neighborExecutor.shutdown();
Charles Chanfbcb8812018-04-18 18:41:05 -0700586
Ray Milkeyaee4d3e2018-05-11 09:59:19 -0700587 mainEventExecutor = null;
588 hostEventExecutor = null;
589 routeEventExecutor = null;
590 mcastEventExecutor = null;
591 packetExecutor = null;
Charles Chana7601c92019-01-23 15:03:17 -0800592 neighborExecutor = null;
Ray Milkeyaee4d3e2018-05-11 09:59:19 -0700593
Danny Chang44a7b952020-09-08 15:27:04 -0700594 cancelPackets();
595
Charles Chan72f556a2015-10-05 17:50:33 -0700596 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700597 cfgService.unregisterConfigFactory(deviceConfigFactory);
598 cfgService.unregisterConfigFactory(appConfigFactory);
599 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan873661e2017-11-30 15:37:50 -0800600 compCfgService.unregisterProperties(getClass(), false);
Charles Chan72f556a2015-10-05 17:50:33 -0700601
Charles Chanfd48c222017-06-19 00:43:31 -0700602 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700603 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700604 linkService.removeListener(linkListener);
605 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700606 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700607 routeService.removeListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100608 topologyService.removeListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700609 mastershipService.removeListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700610 clusterService.removeListener(clusterListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700611
Charles Chan2e71ef32017-02-23 15:44:08 -0800612 neighbourResolutionService.unregisterNeighbourHandlers(appId);
613
sangho80f11cb2015-04-01 13:05:26 -0700614 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700615 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700616 deviceListener = null;
Charles Chan05bb1702018-04-19 13:10:01 -0700617 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chan2b078ae2015-10-14 11:24:40 -0700618 groupHandlerMap.clear();
Saurav Das49368392018-04-23 18:42:12 -0700619 defaultRoutingHandler.shutdown();
Charles Chan2b078ae2015-10-14 11:24:40 -0700620
Saurav Das261c3002017-06-13 15:35:54 -0700621 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800622 vlanNextObjStore.destroy();
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000623 macVlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700624 portNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700625 tunnelStore.destroy();
626 policyStore.destroy();
Pier Luigi35dab3f2018-01-25 16:16:02 +0100627
628 mcastHandler.terminate();
piercf557922019-05-17 20:47:06 +0200629 hostHandler.terminate();
sangho80f11cb2015-04-01 13:05:26 -0700630 log.info("Stopped");
631 }
632
Charles Chan873661e2017-11-30 15:37:50 -0800633 @Modified
634 private void modified(ComponentContext context) {
635 Dictionary<?, ?> properties = context.getProperties();
636 if (properties == null) {
637 return;
638 }
639
Ray Milkey48d10c02018-10-24 10:04:03 -0700640 String strActiveProbing = Tools.get(properties, PROP_ACTIVE_PROBING);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700641 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProbing);
Charles Chan873661e2017-11-30 15:37:50 -0800642 if (expectActiveProbing != activeProbing) {
643 activeProbing = expectActiveProbing;
644 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
645 }
Saurav Dasec683dc2018-04-27 18:42:30 -0700646
Ray Milkeye96d0de2018-11-02 17:12:21 -0700647
648 String strSymmetricProbing = Tools.get(properties, PROP_SYMMETRIC_PROBING);
649 boolean expectSymmetricProbing = Boolean.parseBoolean(strSymmetricProbing);
Mayank Tiwarif7942402018-10-29 18:27:35 -0400650 if (expectSymmetricProbing != symmetricProbing) {
651 symmetricProbing = expectSymmetricProbing;
652 log.info("{} symmetric probing", symmetricProbing ? "Enabling" : "Disabling");
653 }
654
Ray Milkey48d10c02018-10-24 10:04:03 -0700655 String strSingleHomedDown = Tools.get(properties, PROP_SINGLE_HOMED_DOWN);
Saurav Dasec683dc2018-04-27 18:42:30 -0700656 boolean expectSingleHomedDown = Boolean.parseBoolean(strSingleHomedDown);
657 if (expectSingleHomedDown != singleHomedDown) {
658 singleHomedDown = expectSingleHomedDown;
659 log.info("{} downing of single homed hosts for lost uplinks",
660 singleHomedDown ? "Enabling" : "Disabling");
661 if (singleHomedDown && linkHandler != null) {
662 hostService.getHosts().forEach(host -> host.locations()
663 .forEach(loc -> {
664 if (interfaceService.isConfigured(loc)) {
665 linkHandler.checkUplinksForHost(loc);
666 }
667 }));
668 } else {
669 log.warn("Disabling singleHomedDown does not re-enable already "
670 + "downed ports for single-homed hosts");
671 }
672 }
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700673
Ray Milkey48d10c02018-10-24 10:04:03 -0700674 String strRespondToUnknownHosts = Tools.get(properties, PROP_RESPOND_TO_UNKNOWN_HOSTS);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700675 boolean expectRespondToUnknownHosts = Boolean.parseBoolean(strRespondToUnknownHosts);
676 if (expectRespondToUnknownHosts != respondToUnknownHosts) {
677 respondToUnknownHosts = expectRespondToUnknownHosts;
678 log.info("{} responding to ARPs/NDPs from unknown hosts", respondToUnknownHosts ? "Enabling" : "Disabling");
679 }
Charles Chan4eb73bb2018-07-19 14:55:31 -0700680
Ray Milkey48d10c02018-10-24 10:04:03 -0700681 String strRouteDoubleTaggedHosts = Tools.get(properties, PROP_ROUTE_DOUBLE_TAGGED_HOSTS);
Charles Chanfbca55e2018-07-24 16:40:35 -0700682 boolean expectRouteDoubleTaggedHosts = Boolean.parseBoolean(strRouteDoubleTaggedHosts);
683 if (expectRouteDoubleTaggedHosts != routeDoubleTaggedHosts) {
684 routeDoubleTaggedHosts = expectRouteDoubleTaggedHosts;
685 log.info("{} routing for double tagged hosts", routeDoubleTaggedHosts ? "Enabling" : "Disabling");
Charles Chan4eb73bb2018-07-19 14:55:31 -0700686
Charles Chanfbca55e2018-07-24 16:40:35 -0700687 if (routeDoubleTaggedHosts) {
Charles Chan4eb73bb2018-07-19 14:55:31 -0700688 hostHandler.populateAllDoubleTaggedHost();
689 } else {
690 hostHandler.revokeAllDoubleTaggedHost();
691 }
692 }
Saurav Das9bf49582018-08-13 15:34:26 -0700693
Ray Milkey48d10c02018-10-24 10:04:03 -0700694 String strDefaultInternalVlan = Tools.get(properties, PROP_DEFAULT_INTERNAL_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700695 int defIntVlan = Integer.parseInt(strDefaultInternalVlan);
696 if (defIntVlan != defaultInternalVlan) {
697 if (canUseVlanId(defIntVlan)) {
698 log.warn("Default internal vlan value changed from {} to {}.. "
699 + "re-programming filtering rules, but NOT any groups already "
700 + "created with the former value", defaultInternalVlan, defIntVlan);
701 VlanId oldDefIntVlan = VlanId.vlanId((short) defaultInternalVlan);
702 defaultInternalVlan = defIntVlan;
703 routingRulePopulator
704 .updateSpecialVlanFilteringRules(true, oldDefIntVlan,
705 VlanId.vlanId((short) defIntVlan));
706 } else {
707 log.warn("Cannot change default internal vlan to unusable "
708 + "value {}", defIntVlan);
709 }
710 }
711
Ray Milkey48d10c02018-10-24 10:04:03 -0700712 String strPwTxpVlan = Tools.get(properties, PROP_PW_TRANSPORT_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700713 int pwTxpVlan = Integer.parseInt(strPwTxpVlan);
714 if (pwTxpVlan != pwTransportVlan) {
715 if (canUseVlanId(pwTxpVlan)) {
716 log.warn("Pseudowire transport vlan value changed from {} to {}.. "
717 + "re-programming filtering rules, but NOT any groups already "
718 + "created with the former value", pwTransportVlan,
719 pwTxpVlan);
720 VlanId oldPwTxpVlan = VlanId.vlanId((short) pwTransportVlan);
721 pwTransportVlan = pwTxpVlan;
722 routingRulePopulator
723 .updateSpecialVlanFilteringRules(false, oldPwTxpVlan,
724 VlanId.vlanId((short) pwTxpVlan));
725 } else {
726 log.warn("Cannot change pseudowire transport vlan to unusable "
727 + "value {}", pwTxpVlan);
728 }
729 }
730
psneha86e60d32019-03-26 06:31:41 -0400731 String strRouteSimplification = Tools.get(properties, PROP_ROUTE_SIMPLIFICATION);
732 boolean expectRouteSimplification = Boolean.parseBoolean(strRouteSimplification);
733 if (expectRouteSimplification != routeSimplification) {
734 routeSimplification = expectRouteSimplification;
735 log.info("{} route simplification", routeSimplification ? "Enabling" : "Disabling");
736 }
737
Saurav Das9bf49582018-08-13 15:34:26 -0700738 }
739
740 /**
Danny Chang44a7b952020-09-08 15:27:04 -0700741 * Removes all packet requests.
742 */
743 private void cancelPackets() {
744 if (routingRulePopulator == null) {
745 return;
746 }
747 log.info("Removing packet requests.");
748 routingRulePopulator.revokePacketsPunts();
749 }
750
751 /**
Saurav Das9bf49582018-08-13 15:34:26 -0700752 * Returns true if given vlan id is not being used in the system currently,
753 * either as one of the default system wide vlans or as one of the
754 * configured interface vlans.
755 *
756 * @param vlanId given vlan id
757 * @return true if vlan is not currently in use
758 */
759 public boolean canUseVlanId(int vlanId) {
760 if (vlanId >= 4095 || vlanId <= 1) {
761 log.error("Vlan id {} value is not in valid range 2 <--> 4094",
762 vlanId);
763 return false;
764 }
765
766 VlanId vid = VlanId.vlanId((short) vlanId);
767 if (getDefaultInternalVlan().equals(vid) || getPwTransportVlan().equals(vid)) {
768 log.warn("Vlan id {} value is already in use system-wide. "
769 + "DefaultInternalVlan:{} PwTransportVlan:{} ", vlanId,
770 getDefaultInternalVlan(), getPwTransportVlan());
771 return false;
772 }
773
774 if (interfaceService.inUse(vid)) {
775 log.warn("Vlan id {} value is already in use on a configured "
776 + "interface in the system", vlanId);
777 return false;
778 }
779 return true;
780 }
781
782 /**
783 * Returns the VlanId assigned internally by default to unconfigured ports.
784 *
785 * @return the default internal vlan id
786 */
787 public VlanId getDefaultInternalVlan() {
788 return VlanId.vlanId((short) defaultInternalVlan);
789 }
790
791 /**
792 * Returns the Vlan id used to transport pseudowire traffic across the
793 * network.
794 *
795 * @return the pseudowire transport vlan id
796 */
797 public VlanId getPwTransportVlan() {
798 return VlanId.vlanId((short) pwTransportVlan);
Charles Chan873661e2017-11-30 15:37:50 -0800799 }
800
sangho27462c62015-05-14 00:39:53 -0700801 @Override
802 public List<Tunnel> getTunnels() {
803 return tunnelHandler.getTunnels();
804 }
805
806 @Override
sanghobd812f82015-06-29 14:58:47 -0700807 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
808 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700809 }
810
811 @Override
sanghobd812f82015-06-29 14:58:47 -0700812 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700813 for (Policy policy: policyHandler.getPolicies()) {
814 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
815 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
816 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
817 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700818 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700819 }
820 }
821 }
sanghobd812f82015-06-29 14:58:47 -0700822 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700823 }
824
825 @Override
sanghobd812f82015-06-29 14:58:47 -0700826 public PolicyHandler.Result removePolicy(Policy policy) {
827 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700828 }
829
830 @Override
sanghobd812f82015-06-29 14:58:47 -0700831 public PolicyHandler.Result createPolicy(Policy policy) {
832 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700833 }
834
835 @Override
836 public List<Policy> getPolicies() {
837 return policyHandler.getPolicies();
838 }
839
Saurav Das07c74602016-04-27 18:35:50 -0700840 @Override
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700841 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
842 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700843 }
844
845 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800846 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700847 return l2TunnelHandler.getL2Tunnels();
848 }
849
850 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800851 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700852 return l2TunnelHandler.getL2Policies();
853 }
854
Saurav Dasdebcf882018-04-06 20:16:01 -0700855 @Override
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700856 @Deprecated
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700857 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
858
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700859 // get both added and pending pseudowires
860 List<L2TunnelDescription> pseudowires = new ArrayList<>();
861 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
862 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700863 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700864
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700865 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
866
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700867 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
868 L2TunnelHandler.Result res;
869 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
870 res = addPseudowire(pw);
871 if (res != L2TunnelHandler.Result.SUCCESS) {
872 log.error("Pseudowire with id {} can not be instantiated !", res);
873 retRes = res;
874 }
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700875 }
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700876
877 return retRes;
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700878 }
879
880 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800881 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700882 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700883 }
884
885 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800886 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700887 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700888 }
889
890 @Override
Saurav Das07c74602016-04-27 18:35:50 -0700891 public void rerouteNetwork() {
892 cfgListener.configureNetwork();
Saurav Das07c74602016-04-27 18:35:50 -0700893 }
894
Charles Chand7844e52016-10-20 17:02:44 -0700895 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800896 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
897 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart61e24e12017-11-30 18:23:42 -0800898 deviceConfiguration.getRouters().forEach(device ->
899 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chand7844e52016-10-20 17:02:44 -0700900 return deviceSubnetMap;
901 }
902
Saurav Das62ae6792017-05-15 15:34:25 -0700903
904 @Override
905 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
906 if (defaultRoutingHandler != null) {
907 return defaultRoutingHandler.getCurrentEmcpSpgMap();
908 } else {
909 return null;
910 }
911 }
912
913 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700914 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDstNextObjStore() {
Saurav Das261c3002017-06-13 15:35:54 -0700915 if (dsNextObjStore != null) {
916 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700917 } else {
918 return ImmutableMap.of();
919 }
920 }
921
Saurav Dasfbe74572017-08-03 18:30:35 -0700922 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700923 public ImmutableMap<VlanNextObjectiveStoreKey, Integer> getVlanNextObjStore() {
924 if (vlanNextObjStore != null) {
925 return ImmutableMap.copyOf(vlanNextObjStore.entrySet());
926 } else {
927 return ImmutableMap.of();
928 }
929 }
930
931 @Override
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000932 public ImmutableMap<MacVlanNextObjectiveStoreKey, Integer> getMacVlanNextObjStore() {
933 if (macVlanNextObjStore != null) {
934 return ImmutableMap.copyOf(macVlanNextObjStore.entrySet());
935 } else {
936 return ImmutableMap.of();
937 }
938 }
939
940 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700941 public ImmutableMap<PortNextObjectiveStoreKey, Integer> getPortNextObjStore() {
942 if (portNextObjStore != null) {
943 return ImmutableMap.copyOf(portNextObjStore.entrySet());
944 } else {
945 return ImmutableMap.of();
946 }
947 }
948
949 @Override
950 public ImmutableMap<String, NextObjective> getPwInitNext() {
951 if (l2TunnelHandler != null) {
952 return l2TunnelHandler.getInitNext();
953 } else {
954 return ImmutableMap.of();
955 }
956 }
957
958 @Override
959 public ImmutableMap<String, NextObjective> getPwTermNext() {
960 if (l2TunnelHandler != null) {
961 return l2TunnelHandler.getTermNext();
962 } else {
963 return ImmutableMap.of();
964 }
965 }
966
967 @Override
968 public void invalidateNextObj(int nextId) {
969 if (dsNextObjStore != null) {
970 dsNextObjStore.entrySet().forEach(e -> {
971 if (e.getValue().nextId() == nextId) {
972 dsNextObjStore.remove(e.getKey());
973 }
974 });
975 }
976 if (vlanNextObjStore != null) {
977 vlanNextObjStore.entrySet().forEach(e -> {
978 if (e.getValue() == nextId) {
979 vlanNextObjStore.remove(e.getKey());
980 }
981 });
982 }
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000983 if (macVlanNextObjStore != null) {
984 macVlanNextObjStore.entrySet().forEach(e -> {
985 if (e.getValue() == nextId) {
986 macVlanNextObjStore.remove(e.getKey());
987 }
988 });
989 }
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700990 if (portNextObjStore != null) {
991 portNextObjStore.entrySet().forEach(e -> {
992 if (e.getValue() == nextId) {
993 portNextObjStore.remove(e.getKey());
994 }
995 });
996 }
997 if (mcastHandler != null) {
998 mcastHandler.removeNextId(nextId);
999 }
1000 if (l2TunnelHandler != null) {
1001 l2TunnelHandler.removeNextId(nextId);
1002 }
1003 if (xconnectService != null) {
1004 xconnectService.removeNextId(nextId);
1005 }
1006 }
1007
1008 @Override
Saurav Dasfbe74572017-08-03 18:30:35 -07001009 public void verifyGroups(DeviceId id) {
1010 DefaultGroupHandler gh = groupHandlerMap.get(id);
1011 if (gh != null) {
1012 gh.triggerBucketCorrector();
1013 }
1014 }
1015
Saurav Das6430f412018-01-25 09:49:01 -08001016 @Override
1017 public ImmutableMap<Link, Boolean> getSeenLinks() {
1018 return linkHandler.getSeenLinks();
1019 }
1020
1021 @Override
1022 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
1023 return linkHandler.getDownedPorts();
1024 }
1025
Pier Luigi0f9635b2018-01-15 18:06:43 +01001026 @Override
1027 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
Charles Chan0b1dd7e2018-08-19 19:21:46 -07001028 return mcastHandler.getNextIds(mcastIp);
Pier Luigi0f9635b2018-01-15 18:06:43 +01001029 }
1030
1031 @Override
Piere99511d2018-04-19 16:47:06 +02001032 public Map<McastRoleStoreKey, McastRole> getMcastRoles(IpAddress mcastIp, ConnectPoint sourcecp) {
1033 return mcastHandler.getMcastRoles(mcastIp, sourcecp);
1034 }
1035
1036 @Override
Pier71c55772018-04-17 17:25:22 +02001037 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
1038 ConnectPoint sourcecp) {
1039 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
1040 }
1041
1042 @Override
Pierdb27b8d2018-04-17 16:29:56 +02001043 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
1044 return mcastHandler.getMcastLeaders(mcastIp);
1045 }
1046
Charles Chanb13e0702018-04-17 18:56:53 -07001047 @Override
Harshada Chaundkar9204f312019-07-02 16:01:24 +00001048 public Map<DeviceId, List<McastFilteringObjStoreKey>> getMcastFilters() {
1049 return mcastHandler.getMcastFilters();
1050 }
1051
1052 @Override
Charles Chanb13e0702018-04-17 18:56:53 -07001053 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
1054 return defaultRoutingHandler == null ? ImmutableMap.of() :
1055 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
1056 }
1057
1058 @Override
1059 public Map<DeviceId, Boolean> getShouldProgramCache() {
1060 return defaultRoutingHandler == null ? ImmutableMap.of() :
1061 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
1062 }
1063
Charles Chan8d316332018-06-19 20:31:57 -07001064 @Override
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301065 public boolean shouldProgram(DeviceId deviceId) {
1066 return defaultRoutingHandler.shouldProgram(deviceId);
1067 }
1068
1069 @Override
Charles Chan12a8a842020-02-14 13:23:57 -08001070 public boolean isRoutingStable() {
1071 return defaultRoutingHandler.isRoutingStable();
1072 }
1073
1074 @Override
1075 public void initHost(DeviceId deviceId) {
1076 hostEventExecutor.execute(() -> hostHandler.init(deviceId));
1077 }
1078
1079 @Override
1080 public void initRoute(DeviceId deviceId) {
1081 routeEventExecutor.execute(() -> routeHandler.init(deviceId));
1082 }
1083
1084 @Override
Ray Milkeyb85de082017-04-05 09:42:04 -07001085 public ApplicationId appId() {
1086 return appId;
1087 }
1088
1089 /**
1090 * Returns the device configuration.
1091 *
1092 * @return device configuration
1093 */
1094 public DeviceConfiguration deviceConfiguration() {
1095 return deviceConfiguration;
1096 }
1097
1098 /**
Saurav Das261c3002017-06-13 15:35:54 -07001099 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -07001100 * Used to keep track on MPLS group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001101 *
1102 * @return next objective ID store
1103 */
Saurav Das261c3002017-06-13 15:35:54 -07001104 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
1105 dsNextObjStore() {
1106 return dsNextObjStore;
Ray Milkeyb85de082017-04-05 09:42:04 -07001107 }
1108
1109 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001110 * Per device next objective ID store with (device id + vlanid) as key.
1111 * Used to keep track on L2 flood group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001112 *
1113 * @return vlan next object store
1114 */
1115 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
1116 return vlanNextObjStore;
1117 }
1118
1119 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001120 * Per device next objective ID store with (device id + MAC address + vlan) as key.
1121 * Used to keep track on L3 Unicast group information for indirect hosts.
1122 *
1123 * @return mac vlan next object store
1124 */
1125 public EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer> macVlanNextObjStore() {
1126 return macVlanNextObjStore;
1127 }
1128
1129 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001130 * Per device next objective ID store with (device id + port + treatment + meta) as key.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001131 * Used to keep track on L2 interface group and L3 unicast group information for direct hosts.
Ray Milkeyb85de082017-04-05 09:42:04 -07001132 *
1133 * @return port next object store.
1134 */
1135 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
1136 return portNextObjStore;
1137 }
1138
1139 /**
Saurav Das261c3002017-06-13 15:35:54 -07001140 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
1141 * labeled packets should be programmed or not.
Pier Ventre7a78de22016-10-31 15:00:01 -07001142 *
1143 * @return MPLS-ECMP value
1144 */
1145 public boolean getMplsEcmp() {
1146 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
1147 .getConfig(this.appId, SegmentRoutingAppConfig.class);
1148 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
1149 }
1150
1151 /**
sangho80f1f892015-05-19 11:57:42 -07001152 * Returns the tunnel object with the tunnel ID.
1153 *
1154 * @param tunnelId Tunnel ID
1155 * @return Tunnel reference
1156 */
sangho27462c62015-05-14 00:39:53 -07001157 public Tunnel getTunnel(String tunnelId) {
1158 return tunnelHandler.getTunnel(tunnelId);
1159 }
1160
Charles Chan8d316332018-06-19 20:31:57 -07001161 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +01001162 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chan098ca202018-05-01 11:50:20 -07001163 VlanId untaggedVlanId = interfaceService.getUntaggedVlanId(connectPoint);
1164 VlanId nativeVlanId = interfaceService.getNativeVlanId(connectPoint);
Charles Chand9265a32017-06-16 15:19:24 -07001165 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
1166 }
1167
Charles Chan8d316332018-06-19 20:31:57 -07001168 @Override
1169 public Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001170 SegmentRoutingDeviceConfig deviceConfig =
1171 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1172 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
1173 }
Charles Chan8d316332018-06-19 20:31:57 -07001174
1175 @Override
Saurav Dasec683dc2018-04-27 18:42:30 -07001176 public Optional<PortNumber> getPairLocalPort(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001177 SegmentRoutingDeviceConfig deviceConfig =
1178 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1179 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
1180 }
1181
Charles Chan12a8a842020-02-14 13:23:57 -08001182 @Override
1183 public Set<PortNumber> getInfraPorts(DeviceId deviceId) {
1184 return deviceService.getPorts(deviceId).stream()
1185 .map(port -> new ConnectPoint(port.element().id(), port.number()))
1186 .filter(cp -> interfaceService.getInterfacesByPort(cp).isEmpty())
1187 .map(ConnectPoint::port)
1188 .collect(Collectors.toSet());
1189 }
1190
1191 @Override
1192 public Set<PortNumber> getEdgePorts(DeviceId deviceId) {
1193 return deviceService.getPorts(deviceId).stream()
1194 .map(port -> new ConnectPoint(port.element().id(), port.number()))
1195 .filter(cp -> !interfaceService.getInterfacesByPort(cp).isEmpty() &&
1196 !cp.port().equals(getPairLocalPort(deviceId).orElse(null)))
1197 .map(ConnectPoint::port)
1198 .collect(Collectors.toSet());
1199 }
1200
Charles Chan3ed34d82017-06-22 18:03:14 -07001201 /**
Charles Chanf0ae41e2017-08-23 13:55:39 -07001202 * Returns locations of given resolved route.
1203 *
1204 * @param resolvedRoute resolved route
1205 * @return locations of nexthop. Might be empty if next hop is not found
1206 */
Charles Chan19b70032019-04-17 14:20:26 -07001207 public Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
Charles Chanf0ae41e2017-08-23 13:55:39 -07001208 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
1209 return Optional.ofNullable(hostService.getHost(hostId))
1210 .map(Host::locations).orElse(Sets.newHashSet())
1211 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
1212 }
1213
1214 /**
Charles Chan90772a72017-02-08 15:52:08 -08001215 * Returns vlan port map of given device.
1216 *
1217 * @param deviceId device id
1218 * @return vlan-port multimap
1219 */
1220 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
1221 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
1222
1223 interfaceService.getInterfaces().stream()
1224 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
1225 .forEach(intf -> {
1226 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan3ed34d82017-06-22 18:03:14 -07001227 intf.vlanTagged().forEach(vlanTagged ->
1228 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
1229 );
Charles Chan90772a72017-02-08 15:52:08 -08001230 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
1231 });
1232 vlanPortMap.removeAll(VlanId.NONE);
1233
1234 return vlanPortMap;
1235 }
1236
1237 /**
Charles Chan10b0fb72017-02-02 16:20:42 -08001238 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -08001239 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -07001240 *
1241 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -08001242 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -08001243 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -07001244 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001245 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -07001246 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -08001247 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
1248 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -07001249 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -08001250 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -08001251 + "device {} not found", deviceId);
1252 return -1;
1253 }
1254 }
1255
1256 /**
1257 * Returns the next objective ID for the given portNumber, given the treatment.
1258 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -07001259 * would result in different objectives. If the next object does not exist,
1260 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -08001261 *
1262 * @param deviceId Device ID
1263 * @param portNum port number on device for which NextObjective is queried
1264 * @param treatment the actions to apply on the packets (should include outport)
1265 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -07001266 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -07001267 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -08001268 */
1269 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
1270 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -07001271 TrafficSelector meta,
1272 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -08001273 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1274 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -07001275 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -08001276 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -08001277 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
1278 + " not found", deviceId);
1279 return -1;
1280 }
1281 }
1282
Saurav Das62ae6792017-05-15 15:34:25 -07001283 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001284 * Returns the next Objective ID for the given mac and vlan, given the treatment.
1285 * There could be multiple different treatments to the same outport, which
1286 * would result in different objectives. If the next object does not exist,
1287 * and should be created, a new one is created and its id is returned.
1288 *
1289 * @param deviceId Device ID
1290 * @param macAddr mac of host for which Next ID is required.
1291 * @param vlanId vlan of host for which Next ID is required.
Ruchi Sahota07869322019-05-09 17:26:14 -04001292 * @param port port with which to create the Next Obj.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001293 * @param createIfMissing true if a next object should be created if not found
1294 * @return next objective ID or -1 if an error occurred during retrieval or creation
1295 */
1296 public int getMacVlanNextObjectiveId(DeviceId deviceId, MacAddress macAddr, VlanId vlanId,
Ruchi Sahota07869322019-05-09 17:26:14 -04001297 PortNumber port, boolean createIfMissing) {
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001298 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1299 if (ghdlr != null) {
Ruchi Sahota07869322019-05-09 17:26:14 -04001300 return ghdlr.getMacVlanNextObjectiveId(macAddr, vlanId, port, createIfMissing);
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001301 } else {
1302 log.warn("getMacVlanNextObjectiveId query - groupHandler for device {}"
1303 + " not found", deviceId);
1304 return -1;
1305 }
1306 }
1307
1308 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001309 * Updates the next objective for the given nextId .
1310 *
1311 * @param deviceId Device ID
1312 * @param hostMac mac of host for which Next obj is to be updated.
1313 * @param hostVlanId vlan of host for which Next obj is to be updated.
1314 * @param port port with which to update the Next Obj.
1315 * @param nextId of Next Obj which needs to be updated.
1316 */
1317 public void updateMacVlanTreatment(DeviceId deviceId, MacAddress hostMac,
1318 VlanId hostVlanId, PortNumber port, int nextId) {
pierf0e37ec2019-11-26 11:48:48 +01001319 // Check if we are the king of this device
1320 // just one instance should perform this update
1321 if (!defaultRoutingHandler.shouldProgram(deviceId)) {
1322 log.debug("This instance is not handling the routing towards the "
1323 + "device {}", deviceId);
1324 return;
1325 }
1326 // Get the handler and perform the update
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001327 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1328 if (ghdlr != null) {
1329 ghdlr.updateL3UcastGroupBucket(hostMac, hostVlanId, port, nextId);
1330 } else {
1331 log.warn("updateL3UcastGroupBucket query - groupHandler for device {}"
1332 + " not found", deviceId);
1333 }
1334 }
1335
1336
1337 /**
Saurav Das62ae6792017-05-15 15:34:25 -07001338 * Returns the group handler object for the specified device id.
1339 *
1340 * @param devId the device identifier
1341 * @return the groupHandler object for the device id, or null if not found
1342 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001343 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Das62ae6792017-05-15 15:34:25 -07001344 return groupHandlerMap.get(devId);
1345 }
1346
1347 /**
Saurav Dasfbe74572017-08-03 18:30:35 -07001348 * Returns the default routing handler object.
1349 *
1350 * @return the default routing handler object
1351 */
1352 public DefaultRoutingHandler getRoutingHandler() {
1353 return defaultRoutingHandler;
1354 }
1355
sangho80f11cb2015-04-01 13:05:26 -07001356 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -07001357 @Override
1358 public void process(PacketContext context) {
Charles Chan77cdde42018-05-08 21:35:50 -07001359 packetExecutor.execute(() -> processPacketInternal(context));
1360 }
sangho80f11cb2015-04-01 13:05:26 -07001361
Charles Chan77cdde42018-05-08 21:35:50 -07001362 private void processPacketInternal(PacketContext context) {
sangho80f11cb2015-04-01 13:05:26 -07001363 if (context.isHandled()) {
1364 return;
1365 }
1366
1367 InboundPacket pkt = context.inPacket();
1368 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -08001369
1370 if (ethernet == null) {
1371 return;
1372 }
1373
Saurav Das261c3002017-06-13 15:35:54 -07001374 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1375 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001376 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -07001377 log.warn("Received unexpected ARP packet on {}",
1378 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -07001379 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001380 return;
sangho80f11cb2015-04-01 13:05:26 -07001381 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -08001382 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1383 //ipHandler.addToPacketBuffer(ipv4Packet);
1384 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1385 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001386 } else {
Charles Chand041ad82017-01-13 17:20:44 -08001387 // NOTE: We don't support IP learning at this moment so this
1388 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001389 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001390 }
Pier Ventreb6a7f342016-11-26 21:05:22 -08001391 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1392 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -08001393 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -08001394 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -08001395 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1396 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1397 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1398 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1399 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1400 } else {
Saurav Das62ae6792017-05-15 15:34:25 -07001401 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -07001402 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -08001403 }
1404 } else {
1405 // NOTE: We don't support IP learning at this moment so this
1406 // is not necessary. Also it causes duplication of DHCPv6 packets.
1407 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1408 }
sangho80f11cb2015-04-01 13:05:26 -07001409 }
1410 }
1411 }
1412
sangho80f11cb2015-04-01 13:05:26 -07001413 private class InternalEventHandler implements Runnable {
Charles Chan52003922018-04-05 16:31:15 -07001414 private Event event;
1415
1416 InternalEventHandler(Event event) {
1417 this.event = event;
1418 }
1419
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001420 @Override
sangho80f11cb2015-04-01 13:05:26 -07001421 public void run() {
Charles Chan52003922018-04-05 16:31:15 -07001422 try {
1423 // TODO We should also change SR routing and PW to listen to TopologyEvents
1424 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1425 event.type() == LinkEvent.Type.LINK_UPDATED) {
1426 linkHandler.processLinkAdded((Link) event.subject());
1427 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1428 linkHandler.processLinkRemoved((Link) event.subject());
1429 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1430 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1431 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1432 DeviceId deviceId = ((Device) event.subject()).id();
1433 if (deviceService.isAvailable(deviceId)) {
1434 log.info("** DEVICE UP Processing device event {} "
1435 + "for available device {}",
1436 event.type(), ((Device) event.subject()).id());
1437 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001438 } else {
Charles Chanb8f3af52018-08-29 21:21:51 -07001439 if (event.type() == DeviceEvent.Type.DEVICE_ADDED) {
1440 // Note: For p4 devices, the device will be added but unavailable at the beginning.
1441 // The device will later on being marked as available once the pipeline is pushed
1442 // to the device.
1443 log.info("** DEVICE ADDED but unavailable. Ignore");
1444 return;
1445 }
Charles Chan52003922018-04-05 16:31:15 -07001446 log.info(" ** DEVICE DOWN Processing device event {}"
1447 + " for unavailable device {}",
1448 event.type(), ((Device) event.subject()).id());
1449 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001450 }
Charles Chan52003922018-04-05 16:31:15 -07001451 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1452 // typically these calls come when device is added first time
1453 // so port filtering rules are handled at the device_added event.
1454 // port added calls represent all ports on the device,
1455 // enabled or not.
1456 log.trace("** PORT ADDED {}/{} -> {}",
1457 ((DeviceEvent) event).subject().id(),
1458 ((DeviceEvent) event).port().number(),
1459 event.type());
1460 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1461 // these calls happen for every subsequent event
1462 // ports enabled, disabled, switch goes away, comes back
1463 log.info("** PORT UPDATED {}/{} -> {}",
1464 event.subject(),
1465 ((DeviceEvent) event).port(),
1466 event.type());
Saurav Dasec683dc2018-04-27 18:42:30 -07001467 processPortUpdatedInternal(((Device) event.subject()),
Charles Chan52003922018-04-05 16:31:15 -07001468 ((DeviceEvent) event).port());
Harshada Chaundkar9204f312019-07-02 16:01:24 +00001469 mcastHandler.processPortUpdate(((Device) event.subject()),
1470 ((DeviceEvent) event).port());
Charles Chan52003922018-04-05 16:31:15 -07001471 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1472 // Process topology event, needed for all modules relying on
1473 // topology service for path computation
1474 TopologyEvent topologyEvent = (TopologyEvent) event;
1475 log.info("Processing topology event {}, topology age {}, reasons {}",
1476 event.type(), topologyEvent.subject().time(),
1477 topologyEvent.reasons().size());
1478 topologyHandler.processTopologyChange(topologyEvent.reasons());
1479 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1480 hostHandler.processHostAddedEvent((HostEvent) event);
1481 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1482 hostHandler.processHostMovedEvent((HostEvent) event);
1483 routeHandler.processHostMovedEvent((HostEvent) event);
Charles Chana2ccb582019-11-25 09:47:22 -08001484 } else if (event.type() == HostEvent.Type.HOST_AUX_MOVED) {
1485 hostHandler.processHostMovedEvent((HostEvent) event);
1486 // TODO RouteHandler also needs to process this event in order to
1487 // support nexthops that has auxLocations
Charles Chan52003922018-04-05 16:31:15 -07001488 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1489 hostHandler.processHostRemovedEvent((HostEvent) event);
1490 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1491 hostHandler.processHostUpdatedEvent((HostEvent) event);
1492 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1493 routeHandler.processRouteAdded((RouteEvent) event);
1494 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1495 routeHandler.processRouteUpdated((RouteEvent) event);
1496 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1497 routeHandler.processRouteRemoved((RouteEvent) event);
1498 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1499 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1500 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1501 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1502 event.type() == McastEvent.Type.SINKS_ADDED ||
1503 event.type() == McastEvent.Type.SINKS_REMOVED ||
1504 event.type() == McastEvent.Type.ROUTE_ADDED ||
1505 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1506 mcastHandler.processMcastEvent((McastEvent) event);
1507 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1508 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1509 Class configClass = netcfgEvent.configClass();
1510 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1511 appCfgHandler.processAppConfigAdded(netcfgEvent);
1512 log.info("App config event .. configuring network");
1513 cfgListener.configureNetwork();
1514 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1515 log.info("Segment Routing Device Config added for {}", event.subject());
1516 cfgListener.configureNetwork();
Charles Chan52003922018-04-05 16:31:15 -07001517 } else if (configClass.equals(InterfaceConfig.class)) {
1518 log.info("Interface Config added for {}", event.subject());
1519 cfgListener.configureNetwork();
1520 } else {
1521 log.error("Unhandled config class: {}", configClass);
1522 }
1523 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1524 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1525 Class configClass = netcfgEvent.configClass();
1526 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1527 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1528 log.info("App config event .. configuring network");
1529 cfgListener.configureNetwork();
1530 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1531 log.info("Segment Routing Device Config updated for {}", event.subject());
1532 createOrUpdateDeviceConfiguration();
Charles Chan52003922018-04-05 16:31:15 -07001533 } else if (configClass.equals(InterfaceConfig.class)) {
1534 log.info("Interface Config updated for {}", event.subject());
1535 createOrUpdateDeviceConfiguration();
1536 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1537 (InterfaceConfig) netcfgEvent.prevConfig().get());
1538 } else {
1539 log.error("Unhandled config class: {}", configClass);
1540 }
1541 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1542 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1543 Class configClass = netcfgEvent.configClass();
1544 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1545 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1546 log.info("App config event .. configuring network");
1547 cfgListener.configureNetwork();
1548 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1549 // TODO Handle sr device config removal
1550 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
Charles Chan52003922018-04-05 16:31:15 -07001551 } else if (configClass.equals(InterfaceConfig.class)) {
1552 // TODO Handle interface removal
1553 log.info("InterfaceConfig removal is not handled in current implementation");
1554 } else {
1555 log.error("Unhandled config class: {}", configClass);
1556 }
Saurav Das00e553b2018-04-21 17:19:48 -07001557 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1558 MastershipEvent me = (MastershipEvent) event;
1559 DeviceId deviceId = me.subject();
1560 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1561 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1562 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1563 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1564 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1565 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan52003922018-04-05 16:31:15 -07001566 } else {
1567 log.warn("Unhandled event type: {}", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001568 }
Charles Chan52003922018-04-05 16:31:15 -07001569 } catch (Exception e) {
1570 log.error("SegmentRouting event handler thread thrown an exception: {}",
1571 e.getMessage(), e);
sangho80f11cb2015-04-01 13:05:26 -07001572 }
sangho80f11cb2015-04-01 13:05:26 -07001573 }
1574 }
1575
Saurav Dase6c448a2018-01-18 12:07:33 -08001576 void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001577 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001578
1579 // NOTE: Punt ARP/NDP even when the device is not configured.
1580 // Host learning without network config is required for CORD config generator.
1581 routingRulePopulator.populateIpPunts(device.id());
1582 routingRulePopulator.populateArpNdpPunts(device.id());
1583
Charles Chan319d1a22015-11-03 10:42:14 -08001584 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001585 log.warn("Device configuration unavailable. Device {} will be "
1586 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001587 return;
1588 }
Charles Chan72779502016-04-23 17:36:10 -07001589 processDeviceAddedInternal(device.id());
1590 }
1591
1592 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001593 // Irrespective of whether the local is a MASTER or not for this device,
1594 // we need to create a SR-group-handler instance. This is because in a
1595 // multi-instance setup, any instance can initiate forwarding/next-objectives
1596 // for any switch (even if this instance is a SLAVE or not even connected
1597 // to the switch). To handle this, a default-group-handler instance is necessary
1598 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001599 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1600 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001601 DefaultGroupHandler groupHandler;
1602 try {
1603 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001604 createGroupHandler(deviceId,
1605 appId,
1606 deviceConfiguration,
1607 linkService,
1608 flowObjectiveService,
1609 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001610 } catch (DeviceConfigNotFoundException e) {
1611 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1612 return;
1613 }
Saurav Das2b6a00f2017-12-05 15:00:23 -08001614 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan72779502016-04-23 17:36:10 -07001615 deviceId);
1616 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001617 }
Saurav Dasb149be12016-06-07 10:08:06 -07001618
Charles Chan72779502016-04-23 17:36:10 -07001619 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001620 defaultRoutingHandler.populatePortAddressingRules(deviceId);
pierf331a492020-01-07 15:39:39 +01001621 defaultRoutingHandler.purgeSeenBeforeRoutes(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001622 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001623 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001624 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan12a8a842020-02-14 13:23:57 -08001625 phasedRecoveryService.init(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001626 }
Charles Chan82ab1932016-01-30 23:22:37 -08001627
Charles Chandebfea32016-10-24 14:52:01 -07001628 appCfgHandler.init(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001629 }
1630
Saurav Dasc3604f12016-03-23 11:22:49 -07001631 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001632 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001633 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan873661e2017-11-30 15:37:50 -08001634 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan10b0fb72017-02-02 16:20:42 -08001635 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001636 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001637 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001638 macVlanNextObjStore.entrySet().stream()
1639 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1640 .forEach(entry -> macVlanNextObjStore.remove(entry.getKey()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001641 portNextObjStore.entrySet().stream()
1642 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001643 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Dase6c448a2018-01-18 12:07:33 -08001644 linkHandler.processDeviceRemoved(device);
Charles Chan17d75d82017-06-15 00:44:51 -07001645
Saurav Dasfbe74572017-08-03 18:30:35 -07001646 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1647 if (gh != null) {
1648 gh.shutdown();
1649 }
Saurav Das62ae6792017-05-15 15:34:25 -07001650 // Note that a switch going down is associated with all of its links
1651 // going down as well, but it is treated as a single switch down event
Saurav Dasdebcf882018-04-06 20:16:01 -07001652 // while the link-downs are ignored. We cannot rely on the ordering of
1653 // events - i.e we cannot expect all link-downs to come before the
1654 // switch down - so we purge all seen-links for the switch before
1655 // handling route-path changes for the switch-down
Saurav Das62ae6792017-05-15 15:34:25 -07001656 defaultRoutingHandler
Saurav Dasdc7f2752018-03-18 21:28:15 -07001657 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Das6430f412018-01-25 09:49:01 -08001658 defaultRoutingHandler.purgeEcmpGraph(device.id());
Saurav Das97241862018-02-14 14:14:54 -08001659
1660 // Cleanup all internal groupHandler stores for this device. Should be
1661 // done after all rerouting or rehashing has been completed
1662 groupHandlerMap.entrySet()
1663 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Charles Chan12a8a842020-02-14 13:23:57 -08001664
1665 phasedRecoveryService.reset(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -07001666 }
1667
Saurav Das49368392018-04-23 18:42:12 -07001668 /**
1669 * Purge the destinationSet nextObjective store of entries with this device
1670 * as key. Erases app-level knowledge of hashed groups in this device.
1671 *
1672 * @param devId the device identifier
1673 */
Saurav Das00e553b2018-04-21 17:19:48 -07001674 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Das49368392018-04-23 18:42:12 -07001675 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Das00e553b2018-04-21 17:19:48 -07001676 dsNextObjStore.entrySet().stream()
1677 .filter(entry -> entry.getKey().deviceId().equals(devId))
1678 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1679 }
1680
Saurav Dasec683dc2018-04-27 18:42:30 -07001681 private void processPortUpdatedInternal(Device device, Port port) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001682 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1683 log.warn("Device configuration uploading. Not handling port event for"
1684 + "dev: {} port: {}", device.id(), port.number());
1685 return;
1686 }
Saurav Dasf9332192017-02-18 14:05:44 -08001687
Saurav Dasec683dc2018-04-27 18:42:30 -07001688 if (interfaceService.isConfigured(new ConnectPoint(device.id(), port.number()))) {
1689 lastEdgePortEvent = Instant.now();
1690 }
1691
Saurav Dasf9332192017-02-18 14:05:44 -08001692 if (!mastershipService.isLocalMaster(device.id())) {
1693 log.debug("Not master for dev:{} .. not handling port updated event"
1694 + "for port {}", device.id(), port.number());
1695 return;
1696 }
Saurav Dasec683dc2018-04-27 18:42:30 -07001697 processPortUpdated(device.id(), port);
1698 }
Saurav Dasf9332192017-02-18 14:05:44 -08001699
Saurav Dasec683dc2018-04-27 18:42:30 -07001700 /**
1701 * Adds or remove filtering rules for the given switchport. If switchport is
1702 * an edge facing port, additionally handles host probing and broadcast
1703 * rules. Must be called by local master of device.
1704 *
1705 * @param deviceId the device identifier
1706 * @param port the port to update
1707 */
1708 void processPortUpdated(DeviceId deviceId, Port port) {
Saurav Dasf9332192017-02-18 14:05:44 -08001709 // first we handle filtering rules associated with the port
1710 if (port.isEnabled()) {
1711 log.info("Switchport {}/{} enabled..programming filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001712 deviceId, port.number());
1713 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001714 } else {
1715 log.info("Switchport {}/{} disabled..removing filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001716 deviceId, port.number());
1717 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001718 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001719
1720 // portUpdated calls are for ports that have gone down or up. For switch
1721 // to switch ports, link-events should take care of any re-routing or
1722 // group editing necessary for port up/down. Here we only process edge ports
1723 // that are already configured.
Saurav Dasec683dc2018-04-27 18:42:30 -07001724 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
Charles Chan098ca202018-05-01 11:50:20 -07001725 VlanId untaggedVlan = interfaceService.getUntaggedVlanId(cp);
1726 VlanId nativeVlan = interfaceService.getNativeVlanId(cp);
1727 Set<VlanId> taggedVlans = interfaceService.getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001728
Saurav Das3fb28272017-03-04 16:08:47 -08001729 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001730 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasec683dc2018-04-27 18:42:30 -07001731 + "dev/port: {}/{}", deviceId, port.number());
Saurav Dasf0f592d2016-11-18 15:21:57 -08001732 return;
1733 }
Saurav Das3fb28272017-03-04 16:08:47 -08001734 if (untaggedVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001735 processEdgePort(deviceId, port, untaggedVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001736 }
1737 if (nativeVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001738 processEdgePort(deviceId, port, nativeVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001739 }
1740 if (!taggedVlans.isEmpty()) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001741 taggedVlans.forEach(tag -> processEdgePort(deviceId, port, tag, false));
Saurav Das3fb28272017-03-04 16:08:47 -08001742 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001743 }
1744
Saurav Dasec683dc2018-04-27 18:42:30 -07001745 private void processEdgePort(DeviceId deviceId, Port port, VlanId vlanId,
Saurav Das3fb28272017-03-04 16:08:47 -08001746 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001747 boolean portUp = port.isEnabled();
1748 if (portUp) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001749 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001750 port.number(), vlanId);
Charles Chan074fae62018-04-29 18:11:37 -07001751 hostEventExecutor.execute(() -> hostHandler.processPortUp(new ConnectPoint(deviceId, port.number())));
Saurav Dasf0f592d2016-11-18 15:21:57 -08001752 } else {
Saurav Dasec683dc2018-04-27 18:42:30 -07001753 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001754 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001755 }
1756
Saurav Dasec683dc2018-04-27 18:42:30 -07001757 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001758 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001759 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001760 } else {
1761 log.warn("Group handler not found for dev:{}. Not handling edge port"
Saurav Dasec683dc2018-04-27 18:42:30 -07001762 + " {} event for port:{}", deviceId,
Saurav Dasf0f592d2016-11-18 15:21:57 -08001763 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001764 }
1765 }
sangho27462c62015-05-14 00:39:53 -07001766
Charles Chan8ca5a122017-10-20 16:06:55 -07001767 private void createOrUpdateDeviceConfiguration() {
1768 if (deviceConfiguration == null) {
Saurav Dase321cff2018-02-09 17:26:45 -08001769 log.info("Creating new DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001770 deviceConfiguration = new DeviceConfiguration(this);
1771 } else {
Saurav Dase321cff2018-02-09 17:26:45 -08001772 log.info("Updating DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001773 deviceConfiguration.updateConfig();
1774 }
1775 }
1776
Charles Chanfbcb8812018-04-18 18:41:05 -07001777 private void createOrUpdateDefaultRoutingHandler() {
1778 if (defaultRoutingHandler == null) {
1779 log.info("Creating new DefaultRoutingHandler");
1780 defaultRoutingHandler = new DefaultRoutingHandler(this);
1781 } else {
1782 log.info("Updating DefaultRoutingHandler");
1783 defaultRoutingHandler.update(this);
1784 }
1785 }
1786
Pier Ventreb6a7f342016-11-26 21:05:22 -08001787 /**
1788 * Registers the given connect point with the NRS, this is necessary
1789 * to receive the NDP and ARP packets from the NRS.
1790 *
1791 * @param portToRegister connect point to register
1792 */
1793 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001794 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001795 portToRegister,
1796 neighbourHandler,
1797 appId
1798 );
1799 }
1800
Charles Chan72f556a2015-10-05 17:50:33 -07001801 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001802 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001803 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001804
Charles Chanb7f75ac2016-01-11 18:28:54 -08001805 /**
1806 * Constructs the internal network config listener.
1807 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001808 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001809 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001810 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001811 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001812 }
1813
Charles Chanb7f75ac2016-01-11 18:28:54 -08001814 /**
1815 * Reads network config and initializes related data structure accordingly.
1816 */
Charles Chan873661e2017-11-30 15:37:50 -08001817 void configureNetwork() {
Saurav Dase321cff2018-02-09 17:26:45 -08001818 log.info("Configuring network ...");
Andrea Campanella060cad82018-04-17 12:09:34 +02001819
1820 // Setting handling of network configuration events completable future
1821 // The completable future is needed because of the async behaviour of the configureNetwork,
1822 // listener registration and event arrival
1823 // Enables us to buffer the events and execute them when the configure network is done.
Ray Milkey521e4392018-11-16 15:15:14 -08001824 synchronized (networkConfigCompletionLock) {
1825 networkConfigCompletion = new CompletableFuture<>();
Andrea Campanella060cad82018-04-17 12:09:34 +02001826
Ray Milkey521e4392018-11-16 15:15:14 -08001827 // add a small delay to absorb multiple network config added notifications
1828 if (!programmingScheduled.get()) {
1829 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1830 programmingScheduled.set(true);
1831 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1832 }
1833
1834 createOrUpdateDeviceConfiguration();
1835
1836 arpHandler = new ArpHandler(srManager);
1837 icmpHandler = new IcmpHandler(srManager);
1838 ipHandler = new IpHandler(srManager);
1839 routingRulePopulator = new RoutingRulePopulator(srManager);
1840 createOrUpdateDefaultRoutingHandler();
1841
1842 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1843 groupHandlerMap, tunnelStore);
1844 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1845 flowObjectiveService,
1846 tunnelHandler, policyStore);
1847 networkConfigCompletion.complete(true);
Andrea Campanella060cad82018-04-17 12:09:34 +02001848 }
1849
Charles Chan72779502016-04-23 17:36:10 -07001850 mcastHandler.init();
Andrea Campanella060cad82018-04-17 12:09:34 +02001851
Charles Chane7c61022015-10-07 14:21:45 -07001852 }
1853
Charles Chan72f556a2015-10-05 17:50:33 -07001854 @Override
1855 public void event(NetworkConfigEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001856 if (mainEventExecutor == null) {
1857 return;
1858 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001859 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan3f4aca62018-03-21 16:57:47 -07001860 switch (event.type()) {
1861 case CONFIG_ADDED:
1862 case CONFIG_UPDATED:
1863 case CONFIG_REMOVED:
Andrea Campanella060cad82018-04-17 12:09:34 +02001864 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
Charles Chan152dd942020-07-21 15:33:31 -07001865 log.debug("Process netcfg {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001866 mainEventExecutor.execute(new InternalEventHandler(event));
1867 } else {
Charles Chan152dd942020-07-21 15:33:31 -07001868 log.debug("Queue netcfg {}", event);
Charles Chan39b75522018-04-21 00:44:29 -07001869 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001870 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001871 break;
1872 default:
1873 break;
Charles Chan72f556a2015-10-05 17:50:33 -07001874 }
1875 }
Saurav Das261c3002017-06-13 15:35:54 -07001876
Charles Chanc4a68c32018-01-03 16:26:32 -08001877 @Override
1878 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase321cff2018-02-09 17:26:45 -08001879 if (event.type() == CONFIG_REGISTERED ||
1880 event.type() == CONFIG_UNREGISTERED) {
1881 log.debug("Ignore event {} due to type mismatch", event);
1882 return false;
Charles Chanc4a68c32018-01-03 16:26:32 -08001883 }
Saurav Dase321cff2018-02-09 17:26:45 -08001884
1885 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1886 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
Charles Chana01d57c2019-07-19 16:25:38 -07001887 !event.configClass().equals(InterfaceConfig.class)) {
Saurav Dase321cff2018-02-09 17:26:45 -08001888 log.debug("Ignore event {} due to class mismatch", event);
1889 return false;
1890 }
1891
1892 return true;
Charles Chanc4a68c32018-01-03 16:26:32 -08001893 }
1894
Saurav Das261c3002017-06-13 15:35:54 -07001895 private final class ConfigChange implements Runnable {
1896 @Override
1897 public void run() {
1898 programmingScheduled.set(false);
Saurav Dase321cff2018-02-09 17:26:45 -08001899 log.info("Reacting to config changes after buffer delay");
Saurav Das261c3002017-06-13 15:35:54 -07001900 for (Device device : deviceService.getDevices()) {
1901 processDeviceAdded(device);
1902 }
1903 defaultRoutingHandler.startPopulationProcess();
1904 }
1905 }
Charles Chan72f556a2015-10-05 17:50:33 -07001906 }
Charles Chanf4586112015-11-09 16:37:23 -08001907
Charles Chan3f4aca62018-03-21 16:57:47 -07001908 private class InternalLinkListener implements LinkListener {
1909 @Override
1910 public void event(LinkEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001911 if (mainEventExecutor == null) {
1912 return;
1913 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001914 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1915 event.type() == LinkEvent.Type.LINK_UPDATED ||
1916 event.type() == LinkEvent.Type.LINK_REMOVED) {
1917 log.trace("Schedule Link event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001918 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1919 mainEventExecutor.execute(new InternalEventHandler(event));
1920 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001921 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001922 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001923 }
1924 }
1925 }
1926
1927 private class InternalDeviceListener implements DeviceListener {
1928 @Override
1929 public void event(DeviceEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001930 if (mainEventExecutor == null) {
1931 return;
1932 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001933 switch (event.type()) {
1934 case DEVICE_ADDED:
1935 case PORT_UPDATED:
1936 case PORT_ADDED:
1937 case DEVICE_UPDATED:
1938 case DEVICE_AVAILABILITY_CHANGED:
1939 log.trace("Schedule Device event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001940 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1941 mainEventExecutor.execute(new InternalEventHandler(event));
1942 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001943 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001944 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001945 break;
1946 default:
1947 }
1948 }
1949 }
1950
1951 private class InternalTopologyListener implements TopologyListener {
1952 @Override
1953 public void event(TopologyEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001954 if (mainEventExecutor == null) {
1955 return;
1956 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001957 switch (event.type()) {
1958 case TOPOLOGY_CHANGED:
1959 log.trace("Schedule Topology event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001960 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1961 mainEventExecutor.execute(new InternalEventHandler(event));
1962 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001963 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001964 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001965 break;
1966 default:
1967 }
1968 }
1969 }
1970
Charles Chanf4586112015-11-09 16:37:23 -08001971 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001972 @Override
1973 public void event(HostEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001974 if (hostEventExecutor == null) {
1975 return;
1976 }
Charles Chanf4586112015-11-09 16:37:23 -08001977 switch (event.type()) {
1978 case HOST_ADDED:
Charles Chanf4586112015-11-09 16:37:23 -08001979 case HOST_MOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001980 case HOST_REMOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001981 case HOST_UPDATED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001982 log.trace("Schedule Host event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001983 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chanf4586112015-11-09 16:37:23 -08001984 break;
1985 default:
1986 log.warn("Unsupported host event type: {}", event.type());
1987 break;
1988 }
1989 }
1990 }
1991
Charles Chanc91c8782016-03-30 17:54:24 -07001992 private class InternalMcastListener implements McastListener {
1993 @Override
1994 public void event(McastEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001995 if (mcastEventExecutor == null) {
1996 return;
1997 }
Charles Chanc91c8782016-03-30 17:54:24 -07001998 switch (event.type()) {
Pier1f87aca2018-03-14 16:47:32 -07001999 case SOURCES_ADDED:
2000 case SOURCES_REMOVED:
2001 case SINKS_ADDED:
2002 case SINKS_REMOVED:
Charles Chanc91c8782016-03-30 17:54:24 -07002003 case ROUTE_REMOVED:
Pierdb27b8d2018-04-17 16:29:56 +02002004 case ROUTE_ADDED:
Charles Chan3f4aca62018-03-21 16:57:47 -07002005 log.trace("Schedule Mcast event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07002006 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi6786b922018-02-02 16:19:11 +01002007 break;
Charles Chanc91c8782016-03-30 17:54:24 -07002008 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07002009 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chanc91c8782016-03-30 17:54:24 -07002010 break;
2011 }
2012 }
2013 }
Charles Chan41f5ec02016-06-13 18:54:31 -07002014
Charles Chandebfea32016-10-24 14:52:01 -07002015 private class InternalRouteEventListener implements RouteListener {
2016 @Override
2017 public void event(RouteEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07002018 if (routeEventExecutor == null) {
2019 return;
2020 }
Charles Chandebfea32016-10-24 14:52:01 -07002021 switch (event.type()) {
2022 case ROUTE_ADDED:
Charles Chandebfea32016-10-24 14:52:01 -07002023 case ROUTE_UPDATED:
Charles Chandebfea32016-10-24 14:52:01 -07002024 case ROUTE_REMOVED:
Charles Chan910be6a2017-08-23 14:46:43 -07002025 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan3f4aca62018-03-21 16:57:47 -07002026 log.trace("Schedule Route event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07002027 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan910be6a2017-08-23 14:46:43 -07002028 break;
Charles Chandebfea32016-10-24 14:52:01 -07002029 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07002030 log.warn("Unsupported route event type: {}", event.type());
Charles Chandebfea32016-10-24 14:52:01 -07002031 break;
2032 }
2033 }
2034 }
Saurav Das62ae6792017-05-15 15:34:25 -07002035
Charles Chanfbcb8812018-04-18 18:41:05 -07002036 private class InternalMastershipListener implements MastershipListener {
2037 @Override
2038 public void event(MastershipEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07002039 if (mainEventExecutor == null) {
2040 return;
2041 }
Charles Chanfbcb8812018-04-18 18:41:05 -07002042 switch (event.type()) {
Saurav Das00e553b2018-04-21 17:19:48 -07002043 case MASTER_CHANGED:
2044 log.debug("Mastership event: {}/{}", event.subject(),
2045 event.roleInfo());
2046 mainEventExecutor.execute(new InternalEventHandler(event));
2047 break;
2048 case BACKUPS_CHANGED:
2049 case SUSPENDED:
2050 default:
2051 log.debug("Mastership event type {} not handled", event.type());
2052 break;
Charles Chanfbcb8812018-04-18 18:41:05 -07002053 }
2054 }
2055 }
2056
Saurav Das00e553b2018-04-21 17:19:48 -07002057 class InternalClusterListener implements ClusterEventListener {
2058 private Instant lastClusterEvent = Instant.EPOCH;
2059
2060 long timeSinceLastClusterEvent() {
2061 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
2062 }
2063
2064 @Override
2065 public void event(ClusterEvent event) {
2066 switch (event.type()) {
2067 case INSTANCE_ACTIVATED:
2068 case INSTANCE_ADDED:
2069 case INSTANCE_READY:
2070 log.debug("Cluster event {} ignored", event.type());
2071 break;
2072 case INSTANCE_DEACTIVATED:
2073 case INSTANCE_REMOVED:
2074 log.info("** Cluster event {}", event.type());
2075 lastClusterEvent = Instant.now();
2076 break;
2077 default:
2078 break;
2079 }
2080
2081 }
2082
2083 }
2084
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002085 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
2086 try {
2087 Set<Interface> intfs = conf.getInterfaces();
2088 Set<Interface> prevIntfs = prevConf.getInterfaces();
2089
2090 // Now we only handle one interface config at each port.
2091 if (intfs.size() != 1 || prevIntfs.size() != 1) {
2092 log.warn("Interface update aborted - one at a time is allowed, " +
2093 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
2094 return;
2095 }
2096
Andrea Campanella060cad82018-04-17 12:09:34 +02002097 //The system is in an incoherent state, abort
2098 if (defaultRoutingHandler == null) {
2099 log.warn("Interface update aborted, defaultRoutingHandler is null");
2100 return;
2101 }
2102
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002103 Interface intf = intfs.stream().findFirst().get();
2104 Interface prevIntf = prevIntfs.stream().findFirst().get();
2105
2106 DeviceId deviceId = intf.connectPoint().deviceId();
2107 PortNumber portNum = intf.connectPoint().port();
2108
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002109 removeSubnetConfig(prevIntf.connectPoint(),
2110 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
2111 new HashSet<>(intf.ipAddressesList())));
2112
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002113 if (!prevIntf.vlanNative().equals(VlanId.NONE)
2114 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
2115 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
2116 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
2117 // Update filtering objective and L2IG group bucket
2118 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
2119 } else {
2120 // RemoveVlanNative
2121 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
2122 }
2123 }
2124
2125 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
2126 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2127 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2128 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
2129 // Update filtering objective and L2IG group bucket
2130 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
2131 } else {
2132 // RemoveVlanUntagged
2133 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
2134 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002135 }
2136
2137 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2138 // RemoveVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002139 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
2140 .filter(i -> !intf.vlanUntagged().equals(i))
2141 .filter(i -> !intf.vlanNative().equals(i))
2142 .forEach(vlanId -> updateVlanConfigInternal(
2143 deviceId, portNum, vlanId, false, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002144 }
2145
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002146 if (!intf.vlanNative().equals(VlanId.NONE)
2147 && !prevIntf.vlanNative().equals(intf.vlanNative())
2148 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2149 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
2150 // Update filtering objective and L2IG group bucket
2151 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
2152 } else {
2153 // AddVlanNative
2154 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
2155 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002156 }
2157
2158 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2159 // AddVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002160 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
2161 .filter(i -> !prevIntf.vlanUntagged().equals(i))
2162 .filter(i -> !prevIntf.vlanNative().equals(i))
2163 .forEach(vlanId -> updateVlanConfigInternal(
2164 deviceId, portNum, vlanId, false, true)
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002165 );
2166 }
2167
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002168 if (!intf.vlanUntagged().equals(VlanId.NONE)
2169 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2170 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
2171 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
2172 // Update filtering objective and L2IG group bucket
2173 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
2174 } else {
2175 // AddVlanUntagged
2176 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
2177 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002178 }
2179 addSubnetConfig(prevIntf.connectPoint(),
2180 Sets.difference(new HashSet<>(intf.ipAddressesList()),
2181 new HashSet<>(prevIntf.ipAddressesList())));
2182 } catch (ConfigException e) {
2183 log.error("Error in configuration");
2184 }
2185 }
2186
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002187 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
2188 VlanId vlanId, boolean pushVlan) {
2189 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2190 if (grpHandler == null) {
2191 log.warn("Failed to retrieve group handler for device {}", deviceId);
2192 return;
2193 }
2194
2195 // Update filtering objective for a single port
2196 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
2197 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
2198
2199 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
2200 // Update L2IG bucket of the port
2201 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
Shibu Vijayakumar632dd642018-03-01 15:45:59 -08002202 // Update bridging and unicast routing rule for each host
2203 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2204 vlanId, !pushVlan, false));
2205 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2206 vlanId, pushVlan, true));
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002207 } else {
2208 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2209 }
2210 }
2211
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002212 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
2213 VlanId vlanId, boolean pushVlan, boolean install) {
2214 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2215 if (grpHandler == null) {
2216 log.warn("Failed to retrieve group handler for device {}", deviceId);
2217 return;
2218 }
2219
2220 // Update filtering objective for a single port
2221 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
2222
2223 // Update filtering objective for multicast ingress port
2224 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
2225
2226 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
2227
2228 if (nextId != -1 && !install) {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002229 // Remove L2 Bridging rule and L3 Unicast rule to the host
Charles Chan074fae62018-04-29 18:11:37 -07002230 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2231 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002232 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
2233 // only if there is no port configured on that VLAN ID
2234 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
2235 // Remove broadcast forwarding rule for the VLAN
2236 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2237 // Remove L2FG for VLAN
2238 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
2239 } else {
Shibu Vijayakumar632dd642018-03-01 15:45:59 -08002240 // Remove a single port from L2FG
2241 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002242 }
Shibu Vijayakumar632dd642018-03-01 15:45:59 -08002243 // Remove L2IG of the port
2244 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002245 } else if (install) {
Shibu Vijayakumar632dd642018-03-01 15:45:59 -08002246 // Create L2IG of the port
2247 grpHandler.createPortNextObjective(deviceId, portNum, vlanId, pushVlan);
2248 // Create L2 Bridging rule and L3 Unicast rule to the host
2249 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2250 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002251 if (nextId != -1) {
2252 // Add a single port to L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002253 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002254 } else {
2255 // Create L2FG for VLAN
2256 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
2257 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2258 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002259 } else {
2260 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2261 }
2262 }
2263
2264 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2265 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2266 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2267
2268 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2269 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2270 .filter(intf -> !intf.connectPoint().equals(cp))
2271 .flatMap(intf -> intf.ipAddressesList().stream())
2272 .collect(Collectors.toSet());
2273 // 1. Partial subnet population
2274 // Remove routing rules for removed subnet from previous configuration,
2275 // which does not also exist in other interfaces in the same device
2276 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2277 .map(InterfaceIpAddress::subnetAddress)
2278 .collect(Collectors.toSet());
2279
Shibu Vijayakumar5e26f8c2020-01-07 11:45:09 +00002280 Set<IpPrefix> subnetsToBeRevoked = ipPrefixSet.stream()
2281 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2282 .collect(Collectors.toSet());
2283
2284 // Check if any of the subnets to be revoked is configured in the pairDevice.
2285 // If any, repopulate the subnet with pairDevice connectPoint instead of revoking.
2286 Optional<DeviceId> pairDevice = getPairDeviceId(cp.deviceId());
2287 if (pairDevice.isPresent()) {
2288 Set<IpPrefix> pairDeviceIpPrefix = getDeviceSubnetMap().get(pairDevice.get());
2289
2290 Set<IpPrefix> subnetsExistingInPairDevice = subnetsToBeRevoked.stream()
2291 .filter(ipPrefix -> pairDeviceIpPrefix.contains(ipPrefix))
2292 .collect(Collectors.toSet());
2293
2294 // Update the subnets existing in pair device with pair device connect point.
2295 if (!subnetsExistingInPairDevice.isEmpty()) {
2296 // PortNumber of connect point is not relevant in populate subnet and hence providing as ANY.
2297 ConnectPoint pairDeviceCp = new ConnectPoint(pairDevice.get(), PortNumber.ANY);
2298 log.debug("Updating the subnets: {} with pairDevice connectPoint as it exists in the Pair device: {}",
2299 subnetsExistingInPairDevice, pairDeviceCp);
2300 defaultRoutingHandler.populateSubnet(Collections.singleton(pairDeviceCp), subnetsExistingInPairDevice);
2301 }
2302
2303 // Remove only the subnets that are not configured in the pairDevice.
2304 subnetsToBeRevoked = Sets.difference(subnetsToBeRevoked, subnetsExistingInPairDevice);
2305 }
2306
2307 if (!subnetsToBeRevoked.isEmpty()) {
2308 log.debug("Removing subnets for connectPoint: {}, subnets: {}", cp, subnetsToBeRevoked);
2309 defaultRoutingHandler.revokeSubnet(subnetsToBeRevoked);
2310 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002311
2312 // 2. Interface IP punts
2313 // Remove IP punts for old Intf address
2314 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2315 .map(InterfaceIpAddress::ipAddress)
2316 .collect(Collectors.toSet());
2317 ipAddressSet.stream()
2318 .map(InterfaceIpAddress::ipAddress)
2319 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2320 .forEach(interfaceIpAddress ->
2321 routingRulePopulator.revokeSingleIpPunts(
2322 cp.deviceId(), interfaceIpAddress));
2323
2324 // 3. Host unicast routing rule
2325 // Remove unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002326 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002327 }
2328
2329 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2330 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2331 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2332
2333 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2334 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2335 .filter(intf -> !intf.connectPoint().equals(cp))
2336 .flatMap(intf -> intf.ipAddressesList().stream())
2337 .collect(Collectors.toSet());
2338 // 1. Partial subnet population
2339 // Add routing rules for newly added subnet, which does not also exist in
2340 // other interfaces in the same device
2341 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2342 .map(InterfaceIpAddress::subnetAddress)
2343 .collect(Collectors.toSet());
Shibu Vijayakumar5e26f8c2020-01-07 11:45:09 +00002344 Set<IpPrefix> subnetsToBePopulated = ipPrefixSet.stream()
2345 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2346 .collect(Collectors.toSet());
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002347
Shibu Vijayakumar5e26f8c2020-01-07 11:45:09 +00002348 if (!subnetsToBePopulated.isEmpty()) {
2349 log.debug("Adding subnets for connectPoint: {}, subnets: {}", cp, subnetsToBePopulated);
2350
2351 // check if pair-device has the same subnet configured?
2352 Optional<DeviceId> pairDevice = getPairDeviceId(cp.deviceId());
2353 if (pairDevice.isPresent()) {
2354 Set<IpPrefix> pairDeviceIpPrefix = getDeviceSubnetMap().get(pairDevice.get());
2355
2356 Set<IpPrefix> subnetsToBePopulatedAsDualHomed = subnetsToBePopulated.stream()
2357 .filter(ipPrefix -> pairDeviceIpPrefix.contains(ipPrefix))
2358 .collect(Collectors.toSet());
2359 Set<IpPrefix> subnetsToBePopulatedAsSingleHomed = Sets.difference(subnetsToBePopulated,
2360 subnetsToBePopulatedAsDualHomed);
2361
2362 if (!subnetsToBePopulatedAsSingleHomed.isEmpty()) {
2363 defaultRoutingHandler.populateSubnet(
2364 Collections.singleton(cp),
2365 subnetsToBePopulatedAsSingleHomed);
2366 }
2367
2368 if (!subnetsToBePopulatedAsDualHomed.isEmpty()) {
2369 Set<ConnectPoint> cpts = new HashSet<>();
2370 cpts.add(cp);
2371 // As Subnets is DualHomed adding the pairDevice also as ConnectPoint.
2372 // PortNumber of connect point is not relevant in populate subnet and hence providing as ANY.
2373 ConnectPoint pairCp = new ConnectPoint(pairDevice.get(), PortNumber.ANY);
2374 cpts.add(pairCp);
2375
2376 log.debug("Adding DualHomed subnets for connectPoint: {} and its pair device: {}, subnets: {}",
2377 cp, pairCp, subnetsToBePopulatedAsDualHomed);
2378
2379 // populating the subnets as DualHomed
2380 defaultRoutingHandler.populateSubnet(
2381 cpts,
2382 subnetsToBePopulated);
2383
2384 // revoking the subnets populated in the device as it is now Dualhomed.
2385 defaultRoutingHandler.revokeSubnet(Collections.singleton(cp.deviceId()),
2386 subnetsToBePopulatedAsDualHomed);
2387 }
2388 } else {
2389 defaultRoutingHandler.populateSubnet(
2390 Collections.singleton(cp),
2391 subnetsToBePopulated);
2392 }
2393 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002394
2395 // 2. Interface IP punts
2396 // Add IP punts for new Intf address
2397 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2398 .map(InterfaceIpAddress::ipAddress)
2399 .collect(Collectors.toSet());
2400 ipAddressSet.stream()
2401 .map(InterfaceIpAddress::ipAddress)
2402 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2403 .forEach(interfaceIpAddress ->
2404 routingRulePopulator.populateSingleIpPunts(
2405 cp.deviceId(), interfaceIpAddress));
2406
2407 // 3. Host unicast routing rule
2408 // Add unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002409 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002410 }
sangho80f11cb2015-04-01 13:05:26 -07002411}