blob: bb7a47a63b71c396957235dc6072d93161e4d14c [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;
Jonathan Hart54541d12016-04-12 15:39:44 -070029import org.onlab.packet.VlanId;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070030import org.onlab.util.KryoNamespace;
Charles Chan873661e2017-11-30 15:37:50 -080031import org.onlab.util.Tools;
Saurav Dasc3604f12016-03-23 11:22:49 -070032import org.onosproject.cfg.ComponentConfigService;
Saurav Das00e553b2018-04-21 17:19:48 -070033import org.onosproject.cluster.ClusterEvent;
34import org.onosproject.cluster.ClusterEventListener;
Pier Luigi580fd8a2018-01-16 10:47:50 +010035import org.onosproject.cluster.ClusterService;
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -070036import org.onosproject.cluster.LeadershipService;
Pierdb27b8d2018-04-17 16:29:56 +020037import org.onosproject.cluster.NodeId;
sangho80f11cb2015-04-01 13:05:26 -070038import org.onosproject.core.ApplicationId;
39import org.onosproject.core.CoreService;
40import org.onosproject.event.Event;
Charles Chanfbcb8812018-04-18 18:41:05 -070041import org.onosproject.mastership.MastershipEvent;
42import org.onosproject.mastership.MastershipListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070043import org.onosproject.mastership.MastershipService;
Pier1f87aca2018-03-14 16:47:32 -070044import org.onosproject.mcast.api.McastEvent;
45import org.onosproject.mcast.api.McastListener;
46import org.onosproject.mcast.api.MulticastRouteService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080047import org.onosproject.net.ConnectPoint;
Jonathan Hart54541d12016-04-12 15:39:44 -070048import org.onosproject.net.Device;
49import org.onosproject.net.DeviceId;
Charles Chanf0ae41e2017-08-23 13:55:39 -070050import org.onosproject.net.Host;
51import org.onosproject.net.HostId;
Jonathan Hart54541d12016-04-12 15:39:44 -070052import org.onosproject.net.Link;
53import org.onosproject.net.Port;
Charles Chanf4586112015-11-09 16:37:23 -080054import org.onosproject.net.PortNumber;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070055import org.onosproject.net.config.ConfigException;
Charles Chan72f556a2015-10-05 17:50:33 -070056import org.onosproject.net.config.ConfigFactory;
57import org.onosproject.net.config.NetworkConfigEvent;
Charles Chan72f556a2015-10-05 17:50:33 -070058import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070059import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkeyae0068a2017-08-15 11:02:29 -070060import org.onosproject.net.config.basics.InterfaceConfig;
61import org.onosproject.net.config.basics.McastConfig;
Charles Chan72f556a2015-10-05 17:50:33 -070062import org.onosproject.net.config.basics.SubjectFactories;
Saurav Dase6c448a2018-01-18 12:07:33 -080063import org.onosproject.net.device.DeviceAdminService;
Jonathan Hart54541d12016-04-12 15:39:44 -070064import org.onosproject.net.device.DeviceEvent;
65import org.onosproject.net.device.DeviceListener;
66import org.onosproject.net.device.DeviceService;
Charles Chanf4586112015-11-09 16:37:23 -080067import org.onosproject.net.flow.TrafficSelector;
68import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart54541d12016-04-12 15:39:44 -070069import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan0b1dd7e2018-08-19 19:21:46 -070070import org.onosproject.net.flowobjective.NextObjective;
Charles Chanf4586112015-11-09 16:37:23 -080071import org.onosproject.net.host.HostEvent;
72import org.onosproject.net.host.HostListener;
Charles Chanc6bcdf92018-06-01 16:33:48 -070073import org.onosproject.net.host.HostProbingService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080074import org.onosproject.net.host.HostService;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070075import org.onosproject.net.host.InterfaceIpAddress;
Pierdb27b8d2018-04-17 16:29:56 +020076import org.onosproject.net.intent.WorkPartitionService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070077import org.onosproject.net.intf.Interface;
78import org.onosproject.net.intf.InterfaceService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080079import org.onosproject.net.link.LinkEvent;
80import org.onosproject.net.link.LinkListener;
81import org.onosproject.net.link.LinkService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070082import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080083import org.onosproject.net.packet.InboundPacket;
84import org.onosproject.net.packet.PacketContext;
85import org.onosproject.net.packet.PacketProcessor;
86import org.onosproject.net.packet.PacketService;
Pier Luigid8a15162018-02-15 16:33:08 +010087import org.onosproject.net.topology.TopologyEvent;
88import org.onosproject.net.topology.TopologyListener;
Charles Chanc91c8782016-03-30 17:54:24 -070089import org.onosproject.net.topology.TopologyService;
Charles Chanf0ae41e2017-08-23 13:55:39 -070090import org.onosproject.routeservice.ResolvedRoute;
Ray Milkeyae0068a2017-08-15 11:02:29 -070091import org.onosproject.routeservice.RouteEvent;
92import org.onosproject.routeservice.RouteListener;
93import org.onosproject.routeservice.RouteService;
Charles Chanc91c8782016-03-30 17:54:24 -070094import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
95import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventre6b19e482016-11-07 16:21:04 -080096import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkeyae0068a2017-08-15 11:02:29 -070097import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chan82f19972016-05-17 13:13:55 -070098import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chanc91c8782016-03-30 17:54:24 -070099import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -0700100import org.onosproject.segmentrouting.grouphandler.DestinationSet;
101import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Pier Luigi69f774d2018-02-28 12:10:50 +0100102import org.onosproject.segmentrouting.mcast.McastHandler;
103import org.onosproject.segmentrouting.mcast.McastRole;
Piere99511d2018-04-19 16:47:06 +0200104import org.onosproject.segmentrouting.mcast.McastRoleStoreKey;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700105import org.onosproject.segmentrouting.mcast.McastStoreKey;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700106import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700107import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800108import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700109import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800110import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700111import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
Ray Milkeyae0068a2017-08-15 11:02:29 -0700112import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800113import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Saurav Das261c3002017-06-13 15:35:54 -0700114import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700115import org.onosproject.segmentrouting.storekey.DummyVlanIdStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -0700116import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -0800117import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -0700118import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Charles Chan8d316332018-06-19 20:31:57 -0700119import org.onosproject.segmentrouting.xconnect.api.XconnectService;
Jonathan Hart54541d12016-04-12 15:39:44 -0700120import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700121import org.onosproject.store.service.EventuallyConsistentMap;
122import org.onosproject.store.service.EventuallyConsistentMapBuilder;
123import org.onosproject.store.service.StorageService;
124import org.onosproject.store.service.WallClockTimestamp;
Charles Chan873661e2017-11-30 15:37:50 -0800125import org.osgi.service.component.ComponentContext;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700126import org.osgi.service.component.annotations.Activate;
127import org.osgi.service.component.annotations.Component;
128import org.osgi.service.component.annotations.Deactivate;
129import org.osgi.service.component.annotations.Modified;
130import org.osgi.service.component.annotations.Reference;
131import org.osgi.service.component.annotations.ReferenceCardinality;
Charles Chanb4f9cd12019-02-27 11:46:32 -0800132import org.osgi.service.component.annotations.ReferencePolicy;
sangho80f11cb2015-04-01 13:05:26 -0700133import org.slf4j.Logger;
134import org.slf4j.LoggerFactory;
135
Saurav Das00e553b2018-04-21 17:19:48 -0700136import java.time.Instant;
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700137import java.util.ArrayList;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800138import java.util.Collections;
Charles Chan873661e2017-11-30 15:37:50 -0800139import java.util.Dictionary;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800140import java.util.HashSet;
141import java.util.List;
142import java.util.Map;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800143import java.util.Optional;
144import java.util.Set;
Andrea Campanella060cad82018-04-17 12:09:34 +0200145import java.util.concurrent.CompletableFuture;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800146import java.util.concurrent.ConcurrentHashMap;
Andrea Campanella060cad82018-04-17 12:09:34 +0200147import java.util.concurrent.CopyOnWriteArrayList;
Charles Chan77cdde42018-05-08 21:35:50 -0700148import java.util.concurrent.ExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800149import java.util.concurrent.Executors;
150import java.util.concurrent.ScheduledExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800151import java.util.concurrent.TimeUnit;
152import java.util.concurrent.atomic.AtomicBoolean;
153import java.util.stream.Collectors;
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700154import java.util.stream.IntStream;
sangho80f11cb2015-04-01 13:05:26 -0700155
Charles Chand6d25332016-02-26 22:19:52 -0800156import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800157import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700158import static org.onlab.util.Tools.groupedThreads;
Saurav Dase321cff2018-02-09 17:26:45 -0800159import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
160import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700161import static org.onosproject.segmentrouting.OsgiPropertyConstants.ACTIVE_PROBING_DEFAULT;
162import static org.onosproject.segmentrouting.OsgiPropertyConstants.DEFAULT_INTERNAL_VLAN_DEFAULT;
163import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ACTIVE_PROBING;
164import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_DEFAULT_INTERNAL_VLAN;
165import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_PW_TRANSPORT_VLAN;
166import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_RESPOND_TO_UNKNOWN_HOSTS;
167import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_DOUBLE_TAGGED_HOSTS;
psneha86e60d32019-03-26 06:31:41 -0400168import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_SIMPLIFICATION;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700169import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SINGLE_HOMED_DOWN;
Ray Milkeye96d0de2018-11-02 17:12:21 -0700170import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SYMMETRIC_PROBING;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700171import static org.onosproject.segmentrouting.OsgiPropertyConstants.PW_TRANSPORT_VLAN_DEFAULT;
172import static org.onosproject.segmentrouting.OsgiPropertyConstants.RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
173import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
psneha86e60d32019-03-26 06:31:41 -0400174import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_SIMPLIFICATION_DEFAULT;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700175import static org.onosproject.segmentrouting.OsgiPropertyConstants.SINGLE_HOMED_DOWN_DEFAULT;
Ray Milkeye96d0de2018-11-02 17:12:21 -0700176import static org.onosproject.segmentrouting.OsgiPropertyConstants.SYMMETRIC_PROBING_DEFAULT;
Charles Chand6d25332016-02-26 22:19:52 -0800177
Charles Chanb7f75ac2016-01-11 18:28:54 -0800178/**
179 * Segment routing manager.
180 */
Ray Milkey05edbfc2018-10-23 14:23:16 -0700181@Component(
182 immediate = true,
183 service = SegmentRoutingService.class,
184 property = {
185 PROP_ACTIVE_PROBING + ":Boolean=" + ACTIVE_PROBING_DEFAULT,
186 PROP_SINGLE_HOMED_DOWN + ":Boolean=" + SINGLE_HOMED_DOWN_DEFAULT,
187 PROP_RESPOND_TO_UNKNOWN_HOSTS + ":Boolean=" + RESPOND_TO_UNKNOWN_HOSTS_DEFAULT,
188 PROP_ROUTE_DOUBLE_TAGGED_HOSTS + ":Boolean=" + ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT,
189 PROP_DEFAULT_INTERNAL_VLAN + ":Integer=" + DEFAULT_INTERNAL_VLAN_DEFAULT,
190 PROP_PW_TRANSPORT_VLAN + ":Integer=" + PW_TRANSPORT_VLAN_DEFAULT,
psneha86e60d32019-03-26 06:31:41 -0400191 PROP_SYMMETRIC_PROBING + ":Boolean=" + SYMMETRIC_PROBING_DEFAULT,
192 PROP_ROUTE_SIMPLIFICATION + ":Boolean=" + ROUTE_SIMPLIFICATION_DEFAULT
Ray Milkey05edbfc2018-10-23 14:23:16 -0700193 }
194)
sangho27462c62015-05-14 00:39:53 -0700195public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700196
Charles Chan46fdfaf2016-11-09 20:51:44 -0800197 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan910be6a2017-08-23 14:46:43 -0700198 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sangho80f11cb2015-04-01 13:05:26 -0700199
Ray Milkey2bd24a92018-08-17 14:54:17 -0700200 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700201 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700202
Ray Milkey2bd24a92018-08-17 14:54:17 -0700203 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventreb6a7f342016-11-26 21:05:22 -0800204 private NeighbourResolutionService neighbourResolutionService;
205
Ray Milkey2bd24a92018-08-17 14:54:17 -0700206 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100207 public CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700208
Ray Milkey2bd24a92018-08-17 14:54:17 -0700209 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700210 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700211
Ray Milkey2bd24a92018-08-17 14:54:17 -0700212 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700213 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700214
Ray Milkey2bd24a92018-08-17 14:54:17 -0700215 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chanc6bcdf92018-06-01 16:33:48 -0700216 HostProbingService probingService;
Charles Chan873661e2017-11-30 15:37:50 -0800217
Ray Milkey2bd24a92018-08-17 14:54:17 -0700218 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100219 public DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700220
Ray Milkey2bd24a92018-08-17 14:54:17 -0700221 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Saurav Dase6c448a2018-01-18 12:07:33 -0800222 DeviceAdminService deviceAdminService;
223
Ray Milkey2bd24a92018-08-17 14:54:17 -0700224 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800225 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700226
Ray Milkey2bd24a92018-08-17 14:54:17 -0700227 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100228 public LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700229
Ray Milkey2bd24a92018-08-17 14:54:17 -0700230 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800231 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700232
Ray Milkey2bd24a92018-08-17 14:54:17 -0700233 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800234 public StorageService storageService;
Charles Chandebfea32016-10-24 14:52:01 -0700235
Ray Milkey2bd24a92018-08-17 14:54:17 -0700236 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100237 public MulticastRouteService multicastRouteService;
Charles Chandebfea32016-10-24 14:52:01 -0700238
Ray Milkey2bd24a92018-08-17 14:54:17 -0700239 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan83163812018-01-09 13:45:07 -0800240 public TopologyService topologyService;
Charles Chandebfea32016-10-24 14:52:01 -0700241
Ray Milkey2bd24a92018-08-17 14:54:17 -0700242 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700243 RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800244
Ray Milkey2bd24a92018-08-17 14:54:17 -0700245 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800246 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800247
Ray Milkey2bd24a92018-08-17 14:54:17 -0700248 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800249 public InterfaceService interfaceService;
250
Ray Milkey2bd24a92018-08-17 14:54:17 -0700251 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi580fd8a2018-01-16 10:47:50 +0100252 public ClusterService clusterService;
253
Ray Milkey2bd24a92018-08-17 14:54:17 -0700254 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pierdb27b8d2018-04-17 16:29:56 +0200255 public WorkPartitionService workPartitionService;
Pier Luigi580fd8a2018-01-16 10:47:50 +0100256
Ray Milkey2bd24a92018-08-17 14:54:17 -0700257 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700258 public LeadershipService leadershipService;
259
Charles Chanb4f9cd12019-02-27 11:46:32 -0800260 @Reference(cardinality = ReferenceCardinality.OPTIONAL,
261 bind = "bindXconnectService",
262 unbind = "unbindXconnectService",
263 policy = ReferencePolicy.DYNAMIC)
Charles Chan8d316332018-06-19 20:31:57 -0700264 public XconnectService xconnectService;
265
Ray Milkey05edbfc2018-10-23 14:23:16 -0700266 /** Enable active probing to discover dual-homed hosts. */
267 boolean activeProbing = ACTIVE_PROBING_DEFAULT;
Charles Chan873661e2017-11-30 15:37:50 -0800268
Ray Milkeye96d0de2018-11-02 17:12:21 -0700269 /** Enable only send probe on the same port number of the pair device. */
270 boolean symmetricProbing = SYMMETRIC_PROBING_DEFAULT;
Mayank Tiwarif7942402018-10-29 18:27:35 -0400271
Ray Milkey05edbfc2018-10-23 14:23:16 -0700272 /** Enable administratively taking down single-homed hosts. */
273 boolean singleHomedDown = SINGLE_HOMED_DOWN_DEFAULT;
Saurav Dasec683dc2018-04-27 18:42:30 -0700274
Ray Milkey05edbfc2018-10-23 14:23:16 -0700275 /** Enable this to respond to ARP/NDP requests from unknown hosts. */
276 boolean respondToUnknownHosts = RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700277
Ray Milkey05edbfc2018-10-23 14:23:16 -0700278 /** Program flows and groups to pop and route double tagged hosts. */
279 boolean routeDoubleTaggedHosts = ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
Charles Chan4eb73bb2018-07-19 14:55:31 -0700280
Ray Milkey05edbfc2018-10-23 14:23:16 -0700281 /** internal vlan assigned by default to unconfigured ports. */
282 private int defaultInternalVlan = DEFAULT_INTERNAL_VLAN_DEFAULT;
Saurav Das9bf49582018-08-13 15:34:26 -0700283
Ray Milkey05edbfc2018-10-23 14:23:16 -0700284 /** vlan used for transport of pseudowires between switches. */
285 private int pwTransportVlan = PW_TRANSPORT_VLAN_DEFAULT;
Saurav Das9bf49582018-08-13 15:34:26 -0700286
psneha86e60d32019-03-26 06:31:41 -0400287 /** Enabling route simplification. */
288 boolean routeSimplification = ROUTE_SIMPLIFICATION_DEFAULT;
289
Charles Chandebfea32016-10-24 14:52:01 -0700290 ArpHandler arpHandler = null;
291 IcmpHandler icmpHandler = null;
292 IpHandler ipHandler = null;
293 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700294 ApplicationId appId;
295 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700296
Charles Chandebfea32016-10-24 14:52:01 -0700297 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700298 private TunnelHandler tunnelHandler = null;
299 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700300 private InternalPacketProcessor processor = null;
301 private InternalLinkListener linkListener = null;
302 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700303 private AppConfigHandler appCfgHandler = null;
Pier Luigi69f774d2018-02-28 12:10:50 +0100304 public XConnectHandler xConnectHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800305 McastHandler mcastHandler = null;
306 HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800307 private RouteHandler routeHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800308 LinkHandler linkHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800309 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800310 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigid8a15162018-02-15 16:33:08 +0100311 private TopologyHandler topologyHandler = null;
Charles Chan82ab1932016-01-30 23:22:37 -0800312 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700313 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
314 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700315 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigid8a15162018-02-15 16:33:08 +0100316 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chanfbcb8812018-04-18 18:41:05 -0700317 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
Saurav Das00e553b2018-04-21 17:19:48 -0700318 final InternalClusterListener clusterListener = new InternalClusterListener();
Andrea Campanella060cad82018-04-17 12:09:34 +0200319 //Completable future for network configuration process to buffer config events handling during activation
320 private CompletableFuture<Boolean> networkConfigCompletion = null;
Ray Milkey521e4392018-11-16 15:15:14 -0800321 private final Object networkConfigCompletionLock = new Object();
Charles Chan39b75522018-04-21 00:44:29 -0700322 private List<Event> queuedEvents = new CopyOnWriteArrayList<>();
sangho80f11cb2015-04-01 13:05:26 -0700323
Charles Chan52003922018-04-05 16:31:15 -0700324 // Handles device, link, topology and network config events
Charles Chanfbcb8812018-04-18 18:41:05 -0700325 private ScheduledExecutorService mainEventExecutor;
sangho80f11cb2015-04-01 13:05:26 -0700326
Charles Chanfbcb8812018-04-18 18:41:05 -0700327 // Handles host, route and mcast events respectively
328 private ScheduledExecutorService hostEventExecutor;
329 private ScheduledExecutorService routeEventExecutor;
330 private ScheduledExecutorService mcastEventExecutor;
Charles Chan77cdde42018-05-08 21:35:50 -0700331 private ExecutorService packetExecutor;
Charles Chana7601c92019-01-23 15:03:17 -0800332 ExecutorService neighborExecutor;
Charles Chan52003922018-04-05 16:31:15 -0700333
334 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chanb7f75ac2016-01-11 18:28:54 -0800335 /**
Saurav Das261c3002017-06-13 15:35:54 -0700336 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700337 * Used to keep track on MPLS group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800338 */
Charles Chan873661e2017-11-30 15:37:50 -0800339 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das261c3002017-06-13 15:35:54 -0700340 dsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800341 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700342 * Per device next objective ID store with (device id + vlanid) as key.
343 * Used to keep track on L2 flood group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800344 */
Charles Chan873661e2017-11-30 15:37:50 -0800345 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800346 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800347 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700348 * Per device next objective ID store with (device id + port + treatment + meta) as key.
349 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800350 */
Charles Chan873661e2017-11-30 15:37:50 -0800351 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800352 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800353
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700354 /**
355 * Per port dummy VLAN ID store with (connect point + ip address) as key.
356 * Used to keep track on dummy VLAN ID allocation.
357 */
358 private EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId>
359 dummyVlanIdStore = null;
360
Saurav Das2d94d312015-11-24 23:21:05 -0800361 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
362 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700363
Saurav Das261c3002017-06-13 15:35:54 -0700364 private AtomicBoolean programmingScheduled = new AtomicBoolean();
365
Charles Chanc91c8782016-03-30 17:54:24 -0700366 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700367 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
368 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700369 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700370 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800371 public SegmentRoutingDeviceConfig createConfig() {
372 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700373 }
374 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800375
Charles Chanc91c8782016-03-30 17:54:24 -0700376 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700377 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
378 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700379 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800380 @Override
381 public SegmentRoutingAppConfig createConfig() {
382 return new SegmentRoutingAppConfig();
383 }
384 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800385
Charles Chan82f19972016-05-17 13:13:55 -0700386 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
387 new ConfigFactory<ApplicationId, XConnectConfig>(
388 SubjectFactories.APP_SUBJECT_FACTORY,
389 XConnectConfig.class, "xconnect") {
390 @Override
391 public XConnectConfig createConfig() {
392 return new XConnectConfig();
393 }
394 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800395
Charles Chanc91c8782016-03-30 17:54:24 -0700396 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700397 new ConfigFactory<ApplicationId, McastConfig>(
398 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700399 McastConfig.class, "multicast") {
400 @Override
401 public McastConfig createConfig() {
402 return new McastConfig();
403 }
404 };
405
Charles Chan1963f4f2016-02-18 14:22:42 -0800406 /**
407 * Segment Routing App ID.
408 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800409 public static final String APP_NAME = "org.onosproject.segmentrouting";
Saurav Das7c305372015-10-28 12:39:42 -0700410
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700411 /**
412 * Minumum and maximum value of dummy VLAN ID to be allocated.
413 */
414 public static final int MIN_DUMMY_VLAN_ID = 2;
415 public static final int MAX_DUMMY_VLAN_ID = 4093;
416
Charles Chana7601c92019-01-23 15:03:17 -0800417 private static final int DEFAULT_POOL_SIZE = 32;
418
Saurav Dasec683dc2018-04-27 18:42:30 -0700419 Instant lastEdgePortEvent = Instant.EPOCH;
420
Charles Chanb4f9cd12019-02-27 11:46:32 -0800421 protected void bindXconnectService(XconnectService xconnectService) {
422 if (this.xconnectService == null) {
423 log.info("Binding XconnectService");
424 this.xconnectService = xconnectService;
425 } else {
426 log.warn("Trying to bind XconnectService but it is already bound");
427 }
428 }
429
430 protected void unbindXconnectService(XconnectService xconnectService) {
431 if (this.xconnectService == xconnectService) {
432 log.info("Unbinding XconnectService");
433 this.xconnectService = null;
434 } else {
435 log.warn("Trying to unbind XconnectService but it is already unbound");
436 }
437 }
438
sangho80f11cb2015-04-01 13:05:26 -0700439 @Activate
Charles Chan873661e2017-11-30 15:37:50 -0800440 protected void activate(ComponentContext context) {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800441 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700442
Charles Chana7601c92019-01-23 15:03:17 -0800443 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(
444 groupedThreads("onos/sr", "event-main-%d", log));
445 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(
446 groupedThreads("onos/sr", "event-host-%d", log));
447 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(
448 groupedThreads("onos/sr", "event-route-%d", log));
449 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(
450 groupedThreads("onos/sr", "event-mcast-%d", log));
451 packetExecutor = Executors.newSingleThreadExecutor(groupedThreads("onos/sr", "packet-%d", log));
452 neighborExecutor = Executors.newFixedThreadPool(DEFAULT_POOL_SIZE,
453 groupedThreads("onos/sr", "neighbor-%d", log));
Charles Chanfbcb8812018-04-18 18:41:05 -0700454
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700455 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700456 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700457 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700458 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700459 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700460 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700461 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700462 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700463 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700464
Charles Chan10b0fb72017-02-02 16:20:42 -0800465 log.debug("Creating EC map vlannextobjectivestore");
466 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
467 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
468 vlanNextObjStore = vlanNextObjMapBuilder
469 .withName("vlannextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700470 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700471 .withTimestampProvider((k, v) -> new WallClockTimestamp())
472 .build();
473
Saurav Das2d94d312015-11-24 23:21:05 -0800474 log.debug("Creating EC map subnetnextobjectivestore");
475 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
476 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
477 portNextObjStore = portNextObjMapBuilder
478 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700479 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800480 .withTimestampProvider((k, v) -> new WallClockTimestamp())
481 .build();
482
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700483 EventuallyConsistentMapBuilder<DummyVlanIdStoreKey, VlanId>
484 dummyVlanIdMapBuilder = storageService.eventuallyConsistentMapBuilder();
485 dummyVlanIdStore = dummyVlanIdMapBuilder
486 .withName("dummyvlanidstore")
487 .withSerializer(createSerializer())
488 .withTimestampProvider((k, v) -> new WallClockTimestamp())
489 .build();
490
sangho4a5c42a2015-05-20 22:16:38 -0700491 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
492 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700493 tunnelStore = tunnelMapBuilder
494 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700495 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700496 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700497 .build();
498
499 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
500 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700501 policyStore = policyMapBuilder
502 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700503 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700504 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700505 .build();
506
Saurav Dasc3604f12016-03-23 11:22:49 -0700507 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800508 "purgeOnDisconnection", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700509 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800510 "purgeOnDisconnection", "true");
Pier Luigib9632ba2017-01-12 18:14:58 -0800511 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
512 "requestInterceptsEnabled", "false");
Charles Chanc1909e12017-08-08 15:13:37 -0700513 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800514 "requestInterceptsEnabled", "false");
Charles Chan9597f8d2017-07-24 15:56:10 -0700515 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800516 "arpEnabled", "false");
Pier Luigi0ebeb312017-02-02 22:31:34 -0800517 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
518 "greedyLearningIpv6", "true");
Charles Chancf8ea472017-02-28 15:15:17 -0800519 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
520 "forceUnprovision", "true");
Charles Chanc7b73c72017-08-10 16:57:28 -0700521 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan4c95c0d2017-07-20 16:16:25 -0700522 "distributed", "true");
Charles Chan804772f2017-08-14 11:42:11 -0700523 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
524 "multihomingEnabled", "true");
Charles Chan0c9c19f2017-11-29 19:54:20 -0800525 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
526 "staleLinkAge", "15000");
527 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
528 "allowDuplicateIps", "false");
Yi Tsenga7da8d82018-08-19 03:09:54 +0800529 // For P4 switches
530 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Charles Chan02298512018-08-30 17:30:45 -0700531 "fallbackFlowPollFrequency", "4");
Yi Tsenga7da8d82018-08-19 03:09:54 +0800532 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Charles Chan02298512018-08-30 17:30:45 -0700533 "fallbackGroupPollFrequency", "3");
Charles Chan873661e2017-11-30 15:37:50 -0800534 compCfgService.registerProperties(getClass());
535 modified(context);
Saurav Dasc3604f12016-03-23 11:22:49 -0700536
Charles Chan2b078ae2015-10-14 11:24:40 -0700537 processor = new InternalPacketProcessor();
538 linkListener = new InternalLinkListener();
539 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700540 appCfgHandler = new AppConfigHandler(this);
541 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700542 mcastHandler = new McastHandler(this);
543 hostHandler = new HostHandler(this);
Saurav Dase6c448a2018-01-18 12:07:33 -0800544 linkHandler = new LinkHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700545 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800546 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800547 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigid8a15162018-02-15 16:33:08 +0100548 topologyHandler = new TopologyHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700549
Charles Chand6d25332016-02-26 22:19:52 -0800550 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700551 cfgService.registerConfigFactory(deviceConfigFactory);
552 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700553 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700554 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase321cff2018-02-09 17:26:45 -0800555 log.info("Configuring network before adding listeners");
Andrea Campanella060cad82018-04-17 12:09:34 +0200556
Charles Chan6961f222018-01-04 14:26:07 -0800557 cfgListener.configureNetwork();
558
Charles Chan82ab1932016-01-30 23:22:37 -0800559 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700560 packetService.addProcessor(processor, PacketProcessor.director(2));
561 linkService.addListener(linkListener);
562 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700563 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700564 routeService.addListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100565 topologyService.addListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700566 mastershipService.addListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700567 clusterService.addListener(clusterListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700568
Charles Chanc12c3d02018-03-09 15:53:44 -0800569 linkHandler.init();
Andreas Pantelopoulos75eef062018-01-11 07:53:48 -0800570 l2TunnelHandler.init();
571
Ray Milkey521e4392018-11-16 15:15:14 -0800572 synchronized (networkConfigCompletionLock) {
573 networkConfigCompletion.whenComplete((value, ex) -> {
574 //setting to null for easier fall through
575 networkConfigCompletion = null;
576 //process all queued events
577 queuedEvents.forEach(event -> {
578 mainEventExecutor.execute(new InternalEventHandler(event));
579 });
Andrea Campanella060cad82018-04-17 12:09:34 +0200580 });
Ray Milkey521e4392018-11-16 15:15:14 -0800581 }
Andrea Campanella060cad82018-04-17 12:09:34 +0200582
sangho80f11cb2015-04-01 13:05:26 -0700583 log.info("Started");
584 }
585
Saurav Dase6c448a2018-01-18 12:07:33 -0800586 KryoNamespace.Builder createSerializer() {
Jonathan Hart54541d12016-04-12 15:39:44 -0700587 return new KryoNamespace.Builder()
588 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700589 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800590 VlanNextObjectiveStoreKey.class,
591 DestinationSet.class,
Andreas Pantelopoulos92998c92018-05-29 13:11:14 -0700592 DestinationSet.DestinationSetType.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800593 NextNeighbors.class,
594 Tunnel.class,
595 DefaultTunnel.class,
596 Policy.class,
597 TunnelPolicy.class,
598 Policy.Type.class,
599 PortNextObjectiveStoreKey.class,
600 XConnectStoreKey.class,
601 L2Tunnel.class,
602 L2TunnelPolicy.class,
603 DefaultL2Tunnel.class,
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700604 DefaultL2TunnelPolicy.class,
605 DummyVlanIdStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700606 );
607 }
608
sangho80f11cb2015-04-01 13:05:26 -0700609 @Deactivate
610 protected void deactivate() {
Charles Chanfbcb8812018-04-18 18:41:05 -0700611 mainEventExecutor.shutdown();
612 hostEventExecutor.shutdown();
613 routeEventExecutor.shutdown();
614 mcastEventExecutor.shutdown();
Charles Chan77cdde42018-05-08 21:35:50 -0700615 packetExecutor.shutdown();
Charles Chana7601c92019-01-23 15:03:17 -0800616 neighborExecutor.shutdown();
Charles Chanfbcb8812018-04-18 18:41:05 -0700617
Ray Milkeyaee4d3e2018-05-11 09:59:19 -0700618 mainEventExecutor = null;
619 hostEventExecutor = null;
620 routeEventExecutor = null;
621 mcastEventExecutor = null;
622 packetExecutor = null;
Charles Chana7601c92019-01-23 15:03:17 -0800623 neighborExecutor = null;
Ray Milkeyaee4d3e2018-05-11 09:59:19 -0700624
Charles Chan72f556a2015-10-05 17:50:33 -0700625 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700626 cfgService.unregisterConfigFactory(deviceConfigFactory);
627 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700628 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700629 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan873661e2017-11-30 15:37:50 -0800630 compCfgService.unregisterProperties(getClass(), false);
Charles Chan72f556a2015-10-05 17:50:33 -0700631
Charles Chanfd48c222017-06-19 00:43:31 -0700632 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700633 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700634 linkService.removeListener(linkListener);
635 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700636 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700637 routeService.removeListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100638 topologyService.removeListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700639 mastershipService.removeListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700640 clusterService.removeListener(clusterListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700641
Charles Chan2e71ef32017-02-23 15:44:08 -0800642 neighbourResolutionService.unregisterNeighbourHandlers(appId);
643
sangho80f11cb2015-04-01 13:05:26 -0700644 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700645 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700646 deviceListener = null;
Charles Chan05bb1702018-04-19 13:10:01 -0700647 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chan2b078ae2015-10-14 11:24:40 -0700648 groupHandlerMap.clear();
Saurav Das49368392018-04-23 18:42:12 -0700649 defaultRoutingHandler.shutdown();
Charles Chan2b078ae2015-10-14 11:24:40 -0700650
Saurav Das261c3002017-06-13 15:35:54 -0700651 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800652 vlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700653 portNextObjStore.destroy();
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700654 dummyVlanIdStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700655 tunnelStore.destroy();
656 policyStore.destroy();
Pier Luigi35dab3f2018-01-25 16:16:02 +0100657
658 mcastHandler.terminate();
sangho80f11cb2015-04-01 13:05:26 -0700659 log.info("Stopped");
660 }
661
Charles Chan873661e2017-11-30 15:37:50 -0800662 @Modified
663 private void modified(ComponentContext context) {
664 Dictionary<?, ?> properties = context.getProperties();
665 if (properties == null) {
666 return;
667 }
668
Ray Milkey48d10c02018-10-24 10:04:03 -0700669 String strActiveProbing = Tools.get(properties, PROP_ACTIVE_PROBING);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700670 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProbing);
Charles Chan873661e2017-11-30 15:37:50 -0800671 if (expectActiveProbing != activeProbing) {
672 activeProbing = expectActiveProbing;
673 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
674 }
Saurav Dasec683dc2018-04-27 18:42:30 -0700675
Ray Milkeye96d0de2018-11-02 17:12:21 -0700676
677 String strSymmetricProbing = Tools.get(properties, PROP_SYMMETRIC_PROBING);
678 boolean expectSymmetricProbing = Boolean.parseBoolean(strSymmetricProbing);
Mayank Tiwarif7942402018-10-29 18:27:35 -0400679 if (expectSymmetricProbing != symmetricProbing) {
680 symmetricProbing = expectSymmetricProbing;
681 log.info("{} symmetric probing", symmetricProbing ? "Enabling" : "Disabling");
682 }
683
Ray Milkey48d10c02018-10-24 10:04:03 -0700684 String strSingleHomedDown = Tools.get(properties, PROP_SINGLE_HOMED_DOWN);
Saurav Dasec683dc2018-04-27 18:42:30 -0700685 boolean expectSingleHomedDown = Boolean.parseBoolean(strSingleHomedDown);
686 if (expectSingleHomedDown != singleHomedDown) {
687 singleHomedDown = expectSingleHomedDown;
688 log.info("{} downing of single homed hosts for lost uplinks",
689 singleHomedDown ? "Enabling" : "Disabling");
690 if (singleHomedDown && linkHandler != null) {
691 hostService.getHosts().forEach(host -> host.locations()
692 .forEach(loc -> {
693 if (interfaceService.isConfigured(loc)) {
694 linkHandler.checkUplinksForHost(loc);
695 }
696 }));
697 } else {
698 log.warn("Disabling singleHomedDown does not re-enable already "
699 + "downed ports for single-homed hosts");
700 }
701 }
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700702
Ray Milkey48d10c02018-10-24 10:04:03 -0700703 String strRespondToUnknownHosts = Tools.get(properties, PROP_RESPOND_TO_UNKNOWN_HOSTS);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700704 boolean expectRespondToUnknownHosts = Boolean.parseBoolean(strRespondToUnknownHosts);
705 if (expectRespondToUnknownHosts != respondToUnknownHosts) {
706 respondToUnknownHosts = expectRespondToUnknownHosts;
707 log.info("{} responding to ARPs/NDPs from unknown hosts", respondToUnknownHosts ? "Enabling" : "Disabling");
708 }
Charles Chan4eb73bb2018-07-19 14:55:31 -0700709
Ray Milkey48d10c02018-10-24 10:04:03 -0700710 String strRouteDoubleTaggedHosts = Tools.get(properties, PROP_ROUTE_DOUBLE_TAGGED_HOSTS);
Charles Chanfbca55e2018-07-24 16:40:35 -0700711 boolean expectRouteDoubleTaggedHosts = Boolean.parseBoolean(strRouteDoubleTaggedHosts);
712 if (expectRouteDoubleTaggedHosts != routeDoubleTaggedHosts) {
713 routeDoubleTaggedHosts = expectRouteDoubleTaggedHosts;
714 log.info("{} routing for double tagged hosts", routeDoubleTaggedHosts ? "Enabling" : "Disabling");
Charles Chan4eb73bb2018-07-19 14:55:31 -0700715
Charles Chanfbca55e2018-07-24 16:40:35 -0700716 if (routeDoubleTaggedHosts) {
Charles Chan4eb73bb2018-07-19 14:55:31 -0700717 hostHandler.populateAllDoubleTaggedHost();
718 } else {
719 hostHandler.revokeAllDoubleTaggedHost();
720 }
721 }
Saurav Das9bf49582018-08-13 15:34:26 -0700722
Ray Milkey48d10c02018-10-24 10:04:03 -0700723 String strDefaultInternalVlan = Tools.get(properties, PROP_DEFAULT_INTERNAL_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700724 int defIntVlan = Integer.parseInt(strDefaultInternalVlan);
725 if (defIntVlan != defaultInternalVlan) {
726 if (canUseVlanId(defIntVlan)) {
727 log.warn("Default internal vlan value changed from {} to {}.. "
728 + "re-programming filtering rules, but NOT any groups already "
729 + "created with the former value", defaultInternalVlan, defIntVlan);
730 VlanId oldDefIntVlan = VlanId.vlanId((short) defaultInternalVlan);
731 defaultInternalVlan = defIntVlan;
732 routingRulePopulator
733 .updateSpecialVlanFilteringRules(true, oldDefIntVlan,
734 VlanId.vlanId((short) defIntVlan));
735 } else {
736 log.warn("Cannot change default internal vlan to unusable "
737 + "value {}", defIntVlan);
738 }
739 }
740
Ray Milkey48d10c02018-10-24 10:04:03 -0700741 String strPwTxpVlan = Tools.get(properties, PROP_PW_TRANSPORT_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700742 int pwTxpVlan = Integer.parseInt(strPwTxpVlan);
743 if (pwTxpVlan != pwTransportVlan) {
744 if (canUseVlanId(pwTxpVlan)) {
745 log.warn("Pseudowire transport vlan value changed from {} to {}.. "
746 + "re-programming filtering rules, but NOT any groups already "
747 + "created with the former value", pwTransportVlan,
748 pwTxpVlan);
749 VlanId oldPwTxpVlan = VlanId.vlanId((short) pwTransportVlan);
750 pwTransportVlan = pwTxpVlan;
751 routingRulePopulator
752 .updateSpecialVlanFilteringRules(false, oldPwTxpVlan,
753 VlanId.vlanId((short) pwTxpVlan));
754 } else {
755 log.warn("Cannot change pseudowire transport vlan to unusable "
756 + "value {}", pwTxpVlan);
757 }
758 }
759
psneha86e60d32019-03-26 06:31:41 -0400760 String strRouteSimplification = Tools.get(properties, PROP_ROUTE_SIMPLIFICATION);
761 boolean expectRouteSimplification = Boolean.parseBoolean(strRouteSimplification);
762 if (expectRouteSimplification != routeSimplification) {
763 routeSimplification = expectRouteSimplification;
764 log.info("{} route simplification", routeSimplification ? "Enabling" : "Disabling");
765 }
766
Saurav Das9bf49582018-08-13 15:34:26 -0700767 }
768
769 /**
770 * Returns true if given vlan id is not being used in the system currently,
771 * either as one of the default system wide vlans or as one of the
772 * configured interface vlans.
773 *
774 * @param vlanId given vlan id
775 * @return true if vlan is not currently in use
776 */
777 public boolean canUseVlanId(int vlanId) {
778 if (vlanId >= 4095 || vlanId <= 1) {
779 log.error("Vlan id {} value is not in valid range 2 <--> 4094",
780 vlanId);
781 return false;
782 }
783
784 VlanId vid = VlanId.vlanId((short) vlanId);
785 if (getDefaultInternalVlan().equals(vid) || getPwTransportVlan().equals(vid)) {
786 log.warn("Vlan id {} value is already in use system-wide. "
787 + "DefaultInternalVlan:{} PwTransportVlan:{} ", vlanId,
788 getDefaultInternalVlan(), getPwTransportVlan());
789 return false;
790 }
791
792 if (interfaceService.inUse(vid)) {
793 log.warn("Vlan id {} value is already in use on a configured "
794 + "interface in the system", vlanId);
795 return false;
796 }
797 return true;
798 }
799
800 /**
801 * Returns the VlanId assigned internally by default to unconfigured ports.
802 *
803 * @return the default internal vlan id
804 */
805 public VlanId getDefaultInternalVlan() {
806 return VlanId.vlanId((short) defaultInternalVlan);
807 }
808
809 /**
810 * Returns the Vlan id used to transport pseudowire traffic across the
811 * network.
812 *
813 * @return the pseudowire transport vlan id
814 */
815 public VlanId getPwTransportVlan() {
816 return VlanId.vlanId((short) pwTransportVlan);
Charles Chan873661e2017-11-30 15:37:50 -0800817 }
818
sangho27462c62015-05-14 00:39:53 -0700819 @Override
820 public List<Tunnel> getTunnels() {
821 return tunnelHandler.getTunnels();
822 }
823
824 @Override
sanghobd812f82015-06-29 14:58:47 -0700825 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
826 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700827 }
828
829 @Override
sanghobd812f82015-06-29 14:58:47 -0700830 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700831 for (Policy policy: policyHandler.getPolicies()) {
832 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
833 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
834 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
835 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700836 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700837 }
838 }
839 }
sanghobd812f82015-06-29 14:58:47 -0700840 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700841 }
842
843 @Override
sanghobd812f82015-06-29 14:58:47 -0700844 public PolicyHandler.Result removePolicy(Policy policy) {
845 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700846 }
847
848 @Override
sanghobd812f82015-06-29 14:58:47 -0700849 public PolicyHandler.Result createPolicy(Policy policy) {
850 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700851 }
852
853 @Override
854 public List<Policy> getPolicies() {
855 return policyHandler.getPolicies();
856 }
857
Saurav Das07c74602016-04-27 18:35:50 -0700858 @Override
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700859 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
860 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700861 }
862
863 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800864 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700865 return l2TunnelHandler.getL2Tunnels();
866 }
867
868 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800869 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700870 return l2TunnelHandler.getL2Policies();
871 }
872
Saurav Dasdebcf882018-04-06 20:16:01 -0700873 @Override
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700874 @Deprecated
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700875 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
876
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700877 // get both added and pending pseudowires
878 List<L2TunnelDescription> pseudowires = new ArrayList<>();
879 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
880 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700881 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700882
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700883 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
884
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700885 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
886 L2TunnelHandler.Result res;
887 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
888 res = addPseudowire(pw);
889 if (res != L2TunnelHandler.Result.SUCCESS) {
890 log.error("Pseudowire with id {} can not be instantiated !", res);
891 retRes = res;
892 }
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700893 }
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700894
895 return retRes;
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700896 }
897
898 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800899 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700900 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700901 }
902
903 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800904 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700905 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700906 }
907
908 @Override
Saurav Das07c74602016-04-27 18:35:50 -0700909 public void rerouteNetwork() {
910 cfgListener.configureNetwork();
Saurav Das07c74602016-04-27 18:35:50 -0700911 }
912
Charles Chand7844e52016-10-20 17:02:44 -0700913 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800914 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
915 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart61e24e12017-11-30 18:23:42 -0800916 deviceConfiguration.getRouters().forEach(device ->
917 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chand7844e52016-10-20 17:02:44 -0700918 return deviceSubnetMap;
919 }
920
Saurav Das62ae6792017-05-15 15:34:25 -0700921
922 @Override
923 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
924 if (defaultRoutingHandler != null) {
925 return defaultRoutingHandler.getCurrentEmcpSpgMap();
926 } else {
927 return null;
928 }
929 }
930
931 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700932 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDstNextObjStore() {
Saurav Das261c3002017-06-13 15:35:54 -0700933 if (dsNextObjStore != null) {
934 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700935 } else {
936 return ImmutableMap.of();
937 }
938 }
939
Saurav Dasfbe74572017-08-03 18:30:35 -0700940 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700941 public ImmutableMap<VlanNextObjectiveStoreKey, Integer> getVlanNextObjStore() {
942 if (vlanNextObjStore != null) {
943 return ImmutableMap.copyOf(vlanNextObjStore.entrySet());
944 } else {
945 return ImmutableMap.of();
946 }
947 }
948
949 @Override
950 public ImmutableMap<PortNextObjectiveStoreKey, Integer> getPortNextObjStore() {
951 if (portNextObjStore != null) {
952 return ImmutableMap.copyOf(portNextObjStore.entrySet());
953 } else {
954 return ImmutableMap.of();
955 }
956 }
957
958 @Override
959 public ImmutableMap<String, NextObjective> getPwInitNext() {
960 if (l2TunnelHandler != null) {
961 return l2TunnelHandler.getInitNext();
962 } else {
963 return ImmutableMap.of();
964 }
965 }
966
967 @Override
968 public ImmutableMap<String, NextObjective> getPwTermNext() {
969 if (l2TunnelHandler != null) {
970 return l2TunnelHandler.getTermNext();
971 } else {
972 return ImmutableMap.of();
973 }
974 }
975
976 @Override
977 public void invalidateNextObj(int nextId) {
978 if (dsNextObjStore != null) {
979 dsNextObjStore.entrySet().forEach(e -> {
980 if (e.getValue().nextId() == nextId) {
981 dsNextObjStore.remove(e.getKey());
982 }
983 });
984 }
985 if (vlanNextObjStore != null) {
986 vlanNextObjStore.entrySet().forEach(e -> {
987 if (e.getValue() == nextId) {
988 vlanNextObjStore.remove(e.getKey());
989 }
990 });
991 }
992 if (portNextObjStore != null) {
993 portNextObjStore.entrySet().forEach(e -> {
994 if (e.getValue() == nextId) {
995 portNextObjStore.remove(e.getKey());
996 }
997 });
998 }
999 if (mcastHandler != null) {
1000 mcastHandler.removeNextId(nextId);
1001 }
1002 if (l2TunnelHandler != null) {
1003 l2TunnelHandler.removeNextId(nextId);
1004 }
1005 if (xconnectService != null) {
1006 xconnectService.removeNextId(nextId);
1007 }
1008 }
1009
1010 @Override
Saurav Dasfbe74572017-08-03 18:30:35 -07001011 public void verifyGroups(DeviceId id) {
1012 DefaultGroupHandler gh = groupHandlerMap.get(id);
1013 if (gh != null) {
1014 gh.triggerBucketCorrector();
1015 }
1016 }
1017
Saurav Das6430f412018-01-25 09:49:01 -08001018 @Override
1019 public ImmutableMap<Link, Boolean> getSeenLinks() {
1020 return linkHandler.getSeenLinks();
1021 }
1022
1023 @Override
1024 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
1025 return linkHandler.getDownedPorts();
1026 }
1027
Pier Luigi0f9635b2018-01-15 18:06:43 +01001028 @Override
1029 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
Charles Chan0b1dd7e2018-08-19 19:21:46 -07001030 return mcastHandler.getNextIds(mcastIp);
Pier Luigi0f9635b2018-01-15 18:06:43 +01001031 }
1032
1033 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +01001034 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigi0f9635b2018-01-15 18:06:43 +01001035 return mcastHandler.getMcastRoles(mcastIp);
1036 }
1037
1038 @Override
Piere99511d2018-04-19 16:47:06 +02001039 public Map<McastRoleStoreKey, McastRole> getMcastRoles(IpAddress mcastIp, ConnectPoint sourcecp) {
1040 return mcastHandler.getMcastRoles(mcastIp, sourcecp);
1041 }
1042
1043 @Override
Pier Luigi0f9635b2018-01-15 18:06:43 +01001044 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
1045 return mcastHandler.getMcastPaths(mcastIp);
1046 }
1047
Pierdb27b8d2018-04-17 16:29:56 +02001048 @Override
Pier71c55772018-04-17 17:25:22 +02001049 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
1050 ConnectPoint sourcecp) {
1051 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
1052 }
1053
1054 @Override
Pierdb27b8d2018-04-17 16:29:56 +02001055 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
1056 return mcastHandler.getMcastLeaders(mcastIp);
1057 }
1058
Charles Chanb13e0702018-04-17 18:56:53 -07001059 @Override
1060 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
1061 return defaultRoutingHandler == null ? ImmutableMap.of() :
1062 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
1063 }
1064
1065 @Override
1066 public Map<DeviceId, Boolean> getShouldProgramCache() {
1067 return defaultRoutingHandler == null ? ImmutableMap.of() :
1068 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
1069 }
1070
Charles Chan8d316332018-06-19 20:31:57 -07001071 @Override
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301072 public boolean shouldProgram(DeviceId deviceId) {
1073 return defaultRoutingHandler.shouldProgram(deviceId);
1074 }
1075
1076 @Override
Ray Milkeyb85de082017-04-05 09:42:04 -07001077 public ApplicationId appId() {
1078 return appId;
1079 }
1080
1081 /**
1082 * Returns the device configuration.
1083 *
1084 * @return device configuration
1085 */
1086 public DeviceConfiguration deviceConfiguration() {
1087 return deviceConfiguration;
1088 }
1089
1090 /**
Saurav Das261c3002017-06-13 15:35:54 -07001091 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -07001092 * Used to keep track on MPLS group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001093 *
1094 * @return next objective ID store
1095 */
Saurav Das261c3002017-06-13 15:35:54 -07001096 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
1097 dsNextObjStore() {
1098 return dsNextObjStore;
Ray Milkeyb85de082017-04-05 09:42:04 -07001099 }
1100
1101 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001102 * Per device next objective ID store with (device id + vlanid) as key.
1103 * Used to keep track on L2 flood group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001104 *
1105 * @return vlan next object store
1106 */
1107 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
1108 return vlanNextObjStore;
1109 }
1110
1111 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001112 * Per device next objective ID store with (device id + port + treatment + meta) as key.
1113 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001114 *
1115 * @return port next object store.
1116 */
1117 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
1118 return portNextObjStore;
1119 }
1120
1121 /**
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001122 * Per port dummy VLAN ID store with (connect point + ip address) as key.
1123 * Used to keep track on dummy VLAN ID allocation.
1124 *
1125 * @return dummy vlan id store.
1126 */
1127 public EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId> dummyVlanIdStore() {
1128 return dummyVlanIdStore;
1129 }
1130
1131 /**
Saurav Das261c3002017-06-13 15:35:54 -07001132 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
1133 * labeled packets should be programmed or not.
Pier Ventre7a78de22016-10-31 15:00:01 -07001134 *
1135 * @return MPLS-ECMP value
1136 */
1137 public boolean getMplsEcmp() {
1138 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
1139 .getConfig(this.appId, SegmentRoutingAppConfig.class);
1140 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
1141 }
1142
1143 /**
sangho80f1f892015-05-19 11:57:42 -07001144 * Returns the tunnel object with the tunnel ID.
1145 *
1146 * @param tunnelId Tunnel ID
1147 * @return Tunnel reference
1148 */
sangho27462c62015-05-14 00:39:53 -07001149 public Tunnel getTunnel(String tunnelId) {
1150 return tunnelHandler.getTunnel(tunnelId);
1151 }
1152
Charles Chan8d316332018-06-19 20:31:57 -07001153 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +01001154 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chan098ca202018-05-01 11:50:20 -07001155 VlanId untaggedVlanId = interfaceService.getUntaggedVlanId(connectPoint);
1156 VlanId nativeVlanId = interfaceService.getNativeVlanId(connectPoint);
Charles Chand9265a32017-06-16 15:19:24 -07001157 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
1158 }
1159
Charles Chan8d316332018-06-19 20:31:57 -07001160 @Override
1161 public Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001162 SegmentRoutingDeviceConfig deviceConfig =
1163 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1164 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
1165 }
Charles Chan8d316332018-06-19 20:31:57 -07001166
1167 @Override
Saurav Dasec683dc2018-04-27 18:42:30 -07001168 public Optional<PortNumber> getPairLocalPort(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001169 SegmentRoutingDeviceConfig deviceConfig =
1170 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1171 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
1172 }
1173
1174 /**
Charles Chanf0ae41e2017-08-23 13:55:39 -07001175 * Returns locations of given resolved route.
1176 *
1177 * @param resolvedRoute resolved route
1178 * @return locations of nexthop. Might be empty if next hop is not found
1179 */
1180 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
1181 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
1182 return Optional.ofNullable(hostService.getHost(hostId))
1183 .map(Host::locations).orElse(Sets.newHashSet())
1184 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
1185 }
1186
1187 /**
Charles Chan90772a72017-02-08 15:52:08 -08001188 * Returns vlan port map of given device.
1189 *
1190 * @param deviceId device id
1191 * @return vlan-port multimap
1192 */
1193 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
1194 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
1195
1196 interfaceService.getInterfaces().stream()
1197 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
1198 .forEach(intf -> {
1199 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan3ed34d82017-06-22 18:03:14 -07001200 intf.vlanTagged().forEach(vlanTagged ->
1201 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
1202 );
Charles Chan90772a72017-02-08 15:52:08 -08001203 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
1204 });
1205 vlanPortMap.removeAll(VlanId.NONE);
1206
1207 return vlanPortMap;
1208 }
1209
1210 /**
Charles Chan10b0fb72017-02-02 16:20:42 -08001211 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -08001212 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -07001213 *
1214 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -08001215 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -08001216 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -07001217 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001218 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -07001219 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -08001220 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
1221 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -07001222 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -08001223 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -08001224 + "device {} not found", deviceId);
1225 return -1;
1226 }
1227 }
1228
1229 /**
1230 * Returns the next objective ID for the given portNumber, given the treatment.
1231 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -07001232 * would result in different objectives. If the next object does not exist,
1233 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -08001234 *
1235 * @param deviceId Device ID
1236 * @param portNum port number on device for which NextObjective is queried
1237 * @param treatment the actions to apply on the packets (should include outport)
1238 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -07001239 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -07001240 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -08001241 */
1242 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
1243 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -07001244 TrafficSelector meta,
1245 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -08001246 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1247 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -07001248 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -08001249 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -08001250 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
1251 + " not found", deviceId);
1252 return -1;
1253 }
1254 }
1255
Saurav Das62ae6792017-05-15 15:34:25 -07001256 /**
1257 * Returns the group handler object for the specified device id.
1258 *
1259 * @param devId the device identifier
1260 * @return the groupHandler object for the device id, or null if not found
1261 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001262 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Das62ae6792017-05-15 15:34:25 -07001263 return groupHandlerMap.get(devId);
1264 }
1265
1266 /**
Saurav Dasfbe74572017-08-03 18:30:35 -07001267 * Returns the default routing handler object.
1268 *
1269 * @return the default routing handler object
1270 */
1271 public DefaultRoutingHandler getRoutingHandler() {
1272 return defaultRoutingHandler;
1273 }
1274
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001275 /**
1276 * Returns new dummy VLAN ID.
1277 * Dummy VLAN ID should be unique in each connect point.
1278 *
1279 * @param cp connect point
1280 * @param ipAddress IP address
1281 * @return new dummy VLAN ID. Returns VlanId.NONE if no VLAN ID is available.
1282 */
1283 public synchronized VlanId allocateDummyVlanId(ConnectPoint cp, IpAddress ipAddress) {
1284 Set<VlanId> usedVlanId = Sets.union(getVlanPortMap(cp.deviceId()).keySet(),
1285 dummyVlanIdStore.entrySet().stream().filter(entry ->
1286 (entry.getKey()).connectPoint().equals(cp))
1287 .map(Map.Entry::getValue)
1288 .collect(Collectors.toSet()));
1289
1290 VlanId dummyVlanId = IntStream.range(MIN_DUMMY_VLAN_ID, MAX_DUMMY_VLAN_ID).mapToObj(
1291 i -> VlanId.vlanId((short) (i & 0xFFFF))
1292 ).filter(vlanId -> !usedVlanId.contains(vlanId)).findFirst().orElse(VlanId.NONE);
1293
1294 if (!dummyVlanId.equals(VlanId.NONE)) {
1295 this.dummyVlanIdStore.put(new DummyVlanIdStoreKey(cp, ipAddress), dummyVlanId);
1296 log.debug("Dummy VLAN ID {} is allocated to {}, {}", dummyVlanId, cp, ipAddress);
1297 } else {
1298 log.error("Failed to allocate dummy VLAN ID for {}, {}", cp, ipAddress);
1299 }
1300 return dummyVlanId;
1301 }
1302
1303
sangho80f11cb2015-04-01 13:05:26 -07001304 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -07001305 @Override
1306 public void process(PacketContext context) {
Charles Chan77cdde42018-05-08 21:35:50 -07001307 packetExecutor.execute(() -> processPacketInternal(context));
1308 }
sangho80f11cb2015-04-01 13:05:26 -07001309
Charles Chan77cdde42018-05-08 21:35:50 -07001310 private void processPacketInternal(PacketContext context) {
sangho80f11cb2015-04-01 13:05:26 -07001311 if (context.isHandled()) {
1312 return;
1313 }
1314
1315 InboundPacket pkt = context.inPacket();
1316 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -08001317
1318 if (ethernet == null) {
1319 return;
1320 }
1321
Saurav Das261c3002017-06-13 15:35:54 -07001322 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1323 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001324 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -07001325 log.warn("Received unexpected ARP packet on {}",
1326 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -07001327 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001328 return;
sangho80f11cb2015-04-01 13:05:26 -07001329 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -08001330 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1331 //ipHandler.addToPacketBuffer(ipv4Packet);
1332 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1333 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001334 } else {
Charles Chand041ad82017-01-13 17:20:44 -08001335 // NOTE: We don't support IP learning at this moment so this
1336 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001337 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001338 }
Pier Ventreb6a7f342016-11-26 21:05:22 -08001339 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1340 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -08001341 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -08001342 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -08001343 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1344 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1345 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1346 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1347 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1348 } else {
Saurav Das62ae6792017-05-15 15:34:25 -07001349 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -07001350 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -08001351 }
1352 } else {
1353 // NOTE: We don't support IP learning at this moment so this
1354 // is not necessary. Also it causes duplication of DHCPv6 packets.
1355 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1356 }
sangho80f11cb2015-04-01 13:05:26 -07001357 }
1358 }
1359 }
1360
sangho80f11cb2015-04-01 13:05:26 -07001361 private class InternalEventHandler implements Runnable {
Charles Chan52003922018-04-05 16:31:15 -07001362 private Event event;
1363
1364 InternalEventHandler(Event event) {
1365 this.event = event;
1366 }
1367
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001368 @Override
sangho80f11cb2015-04-01 13:05:26 -07001369 public void run() {
Charles Chan52003922018-04-05 16:31:15 -07001370 try {
1371 // TODO We should also change SR routing and PW to listen to TopologyEvents
1372 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1373 event.type() == LinkEvent.Type.LINK_UPDATED) {
1374 linkHandler.processLinkAdded((Link) event.subject());
1375 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1376 linkHandler.processLinkRemoved((Link) event.subject());
1377 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1378 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1379 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1380 DeviceId deviceId = ((Device) event.subject()).id();
1381 if (deviceService.isAvailable(deviceId)) {
1382 log.info("** DEVICE UP Processing device event {} "
1383 + "for available device {}",
1384 event.type(), ((Device) event.subject()).id());
1385 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001386 } else {
Charles Chanb8f3af52018-08-29 21:21:51 -07001387 if (event.type() == DeviceEvent.Type.DEVICE_ADDED) {
1388 // Note: For p4 devices, the device will be added but unavailable at the beginning.
1389 // The device will later on being marked as available once the pipeline is pushed
1390 // to the device.
1391 log.info("** DEVICE ADDED but unavailable. Ignore");
1392 return;
1393 }
Charles Chan52003922018-04-05 16:31:15 -07001394 log.info(" ** DEVICE DOWN Processing device event {}"
1395 + " for unavailable device {}",
1396 event.type(), ((Device) event.subject()).id());
1397 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001398 }
Charles Chan52003922018-04-05 16:31:15 -07001399 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1400 // typically these calls come when device is added first time
1401 // so port filtering rules are handled at the device_added event.
1402 // port added calls represent all ports on the device,
1403 // enabled or not.
1404 log.trace("** PORT ADDED {}/{} -> {}",
1405 ((DeviceEvent) event).subject().id(),
1406 ((DeviceEvent) event).port().number(),
1407 event.type());
1408 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1409 // these calls happen for every subsequent event
1410 // ports enabled, disabled, switch goes away, comes back
1411 log.info("** PORT UPDATED {}/{} -> {}",
1412 event.subject(),
1413 ((DeviceEvent) event).port(),
1414 event.type());
Saurav Dasec683dc2018-04-27 18:42:30 -07001415 processPortUpdatedInternal(((Device) event.subject()),
Charles Chan52003922018-04-05 16:31:15 -07001416 ((DeviceEvent) event).port());
1417 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1418 // Process topology event, needed for all modules relying on
1419 // topology service for path computation
1420 TopologyEvent topologyEvent = (TopologyEvent) event;
1421 log.info("Processing topology event {}, topology age {}, reasons {}",
1422 event.type(), topologyEvent.subject().time(),
1423 topologyEvent.reasons().size());
1424 topologyHandler.processTopologyChange(topologyEvent.reasons());
1425 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1426 hostHandler.processHostAddedEvent((HostEvent) event);
1427 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1428 hostHandler.processHostMovedEvent((HostEvent) event);
1429 routeHandler.processHostMovedEvent((HostEvent) event);
1430 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1431 hostHandler.processHostRemovedEvent((HostEvent) event);
1432 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1433 hostHandler.processHostUpdatedEvent((HostEvent) event);
1434 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1435 routeHandler.processRouteAdded((RouteEvent) event);
1436 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1437 routeHandler.processRouteUpdated((RouteEvent) event);
1438 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1439 routeHandler.processRouteRemoved((RouteEvent) event);
1440 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1441 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1442 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1443 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1444 event.type() == McastEvent.Type.SINKS_ADDED ||
1445 event.type() == McastEvent.Type.SINKS_REMOVED ||
1446 event.type() == McastEvent.Type.ROUTE_ADDED ||
1447 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1448 mcastHandler.processMcastEvent((McastEvent) event);
1449 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1450 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1451 Class configClass = netcfgEvent.configClass();
1452 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1453 appCfgHandler.processAppConfigAdded(netcfgEvent);
1454 log.info("App config event .. configuring network");
1455 cfgListener.configureNetwork();
1456 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1457 log.info("Segment Routing Device Config added for {}", event.subject());
1458 cfgListener.configureNetwork();
1459 } else if (configClass.equals(XConnectConfig.class)) {
1460 xConnectHandler.processXConnectConfigAdded(netcfgEvent);
1461 } else if (configClass.equals(InterfaceConfig.class)) {
1462 log.info("Interface Config added for {}", event.subject());
1463 cfgListener.configureNetwork();
1464 } else {
1465 log.error("Unhandled config class: {}", configClass);
1466 }
1467 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1468 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1469 Class configClass = netcfgEvent.configClass();
1470 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1471 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1472 log.info("App config event .. configuring network");
1473 cfgListener.configureNetwork();
1474 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1475 log.info("Segment Routing Device Config updated for {}", event.subject());
1476 createOrUpdateDeviceConfiguration();
1477 } else if (configClass.equals(XConnectConfig.class)) {
1478 xConnectHandler.processXConnectConfigUpdated(netcfgEvent);
1479 } else if (configClass.equals(InterfaceConfig.class)) {
1480 log.info("Interface Config updated for {}", event.subject());
1481 createOrUpdateDeviceConfiguration();
1482 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1483 (InterfaceConfig) netcfgEvent.prevConfig().get());
1484 } else {
1485 log.error("Unhandled config class: {}", configClass);
1486 }
1487 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1488 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1489 Class configClass = netcfgEvent.configClass();
1490 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1491 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1492 log.info("App config event .. configuring network");
1493 cfgListener.configureNetwork();
1494 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1495 // TODO Handle sr device config removal
1496 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
1497 } else if (configClass.equals(XConnectConfig.class)) {
1498 xConnectHandler.processXConnectConfigRemoved(netcfgEvent);
1499 } else if (configClass.equals(InterfaceConfig.class)) {
1500 // TODO Handle interface removal
1501 log.info("InterfaceConfig removal is not handled in current implementation");
1502 } else {
1503 log.error("Unhandled config class: {}", configClass);
1504 }
Saurav Das00e553b2018-04-21 17:19:48 -07001505 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1506 MastershipEvent me = (MastershipEvent) event;
1507 DeviceId deviceId = me.subject();
1508 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1509 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1510 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1511 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1512 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1513 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan52003922018-04-05 16:31:15 -07001514 } else {
1515 log.warn("Unhandled event type: {}", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001516 }
Charles Chan52003922018-04-05 16:31:15 -07001517 } catch (Exception e) {
1518 log.error("SegmentRouting event handler thread thrown an exception: {}",
1519 e.getMessage(), e);
sangho80f11cb2015-04-01 13:05:26 -07001520 }
sangho80f11cb2015-04-01 13:05:26 -07001521 }
1522 }
1523
Saurav Dase6c448a2018-01-18 12:07:33 -08001524 void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001525 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001526
1527 // NOTE: Punt ARP/NDP even when the device is not configured.
1528 // Host learning without network config is required for CORD config generator.
1529 routingRulePopulator.populateIpPunts(device.id());
1530 routingRulePopulator.populateArpNdpPunts(device.id());
1531
Charles Chan319d1a22015-11-03 10:42:14 -08001532 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001533 log.warn("Device configuration unavailable. Device {} will be "
1534 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001535 return;
1536 }
Charles Chan72779502016-04-23 17:36:10 -07001537 processDeviceAddedInternal(device.id());
1538 }
1539
1540 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001541 // Irrespective of whether the local is a MASTER or not for this device,
1542 // we need to create a SR-group-handler instance. This is because in a
1543 // multi-instance setup, any instance can initiate forwarding/next-objectives
1544 // for any switch (even if this instance is a SLAVE or not even connected
1545 // to the switch). To handle this, a default-group-handler instance is necessary
1546 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001547 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1548 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001549 DefaultGroupHandler groupHandler;
1550 try {
1551 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001552 createGroupHandler(deviceId,
1553 appId,
1554 deviceConfiguration,
1555 linkService,
1556 flowObjectiveService,
1557 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001558 } catch (DeviceConfigNotFoundException e) {
1559 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1560 return;
1561 }
Saurav Das2b6a00f2017-12-05 15:00:23 -08001562 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan72779502016-04-23 17:36:10 -07001563 deviceId);
1564 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001565 }
Saurav Dasb149be12016-06-07 10:08:06 -07001566
Charles Chan72779502016-04-23 17:36:10 -07001567 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001568 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -07001569 xConnectHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001570 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001571 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001572 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001573 }
Charles Chan82ab1932016-01-30 23:22:37 -08001574
Charles Chandebfea32016-10-24 14:52:01 -07001575 appCfgHandler.init(deviceId);
Charles Chan074fae62018-04-29 18:11:37 -07001576 hostEventExecutor.execute(() -> hostHandler.init(deviceId));
1577 routeEventExecutor.execute(() -> routeHandler.init(deviceId));
sangho80f11cb2015-04-01 13:05:26 -07001578 }
1579
Saurav Dasc3604f12016-03-23 11:22:49 -07001580 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001581 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001582 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan873661e2017-11-30 15:37:50 -08001583 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan10b0fb72017-02-02 16:20:42 -08001584 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001585 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001586 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001587 portNextObjStore.entrySet().stream()
1588 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001589 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Dase6c448a2018-01-18 12:07:33 -08001590 linkHandler.processDeviceRemoved(device);
Charles Chan17d75d82017-06-15 00:44:51 -07001591
Saurav Dasfbe74572017-08-03 18:30:35 -07001592 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1593 if (gh != null) {
1594 gh.shutdown();
1595 }
Saurav Das62ae6792017-05-15 15:34:25 -07001596 // Note that a switch going down is associated with all of its links
1597 // going down as well, but it is treated as a single switch down event
Saurav Dasdebcf882018-04-06 20:16:01 -07001598 // while the link-downs are ignored. We cannot rely on the ordering of
1599 // events - i.e we cannot expect all link-downs to come before the
1600 // switch down - so we purge all seen-links for the switch before
1601 // handling route-path changes for the switch-down
Saurav Das62ae6792017-05-15 15:34:25 -07001602 defaultRoutingHandler
Saurav Dasdc7f2752018-03-18 21:28:15 -07001603 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Das6430f412018-01-25 09:49:01 -08001604 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan82f19972016-05-17 13:13:55 -07001605 xConnectHandler.removeDevice(device.id());
Saurav Das97241862018-02-14 14:14:54 -08001606
1607 // Cleanup all internal groupHandler stores for this device. Should be
1608 // done after all rerouting or rehashing has been completed
1609 groupHandlerMap.entrySet()
1610 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001611 }
1612
Saurav Das49368392018-04-23 18:42:12 -07001613 /**
1614 * Purge the destinationSet nextObjective store of entries with this device
1615 * as key. Erases app-level knowledge of hashed groups in this device.
1616 *
1617 * @param devId the device identifier
1618 */
Saurav Das00e553b2018-04-21 17:19:48 -07001619 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Das49368392018-04-23 18:42:12 -07001620 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Das00e553b2018-04-21 17:19:48 -07001621 dsNextObjStore.entrySet().stream()
1622 .filter(entry -> entry.getKey().deviceId().equals(devId))
1623 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1624 }
1625
Saurav Dasec683dc2018-04-27 18:42:30 -07001626 private void processPortUpdatedInternal(Device device, Port port) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001627 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1628 log.warn("Device configuration uploading. Not handling port event for"
1629 + "dev: {} port: {}", device.id(), port.number());
1630 return;
1631 }
Saurav Dasf9332192017-02-18 14:05:44 -08001632
Saurav Dasec683dc2018-04-27 18:42:30 -07001633 if (interfaceService.isConfigured(new ConnectPoint(device.id(), port.number()))) {
1634 lastEdgePortEvent = Instant.now();
1635 }
1636
Saurav Dasf9332192017-02-18 14:05:44 -08001637 if (!mastershipService.isLocalMaster(device.id())) {
1638 log.debug("Not master for dev:{} .. not handling port updated event"
1639 + "for port {}", device.id(), port.number());
1640 return;
1641 }
Saurav Dasec683dc2018-04-27 18:42:30 -07001642 processPortUpdated(device.id(), port);
1643 }
Saurav Dasf9332192017-02-18 14:05:44 -08001644
Saurav Dasec683dc2018-04-27 18:42:30 -07001645 /**
1646 * Adds or remove filtering rules for the given switchport. If switchport is
1647 * an edge facing port, additionally handles host probing and broadcast
1648 * rules. Must be called by local master of device.
1649 *
1650 * @param deviceId the device identifier
1651 * @param port the port to update
1652 */
1653 void processPortUpdated(DeviceId deviceId, Port port) {
Saurav Dasf9332192017-02-18 14:05:44 -08001654 // first we handle filtering rules associated with the port
1655 if (port.isEnabled()) {
1656 log.info("Switchport {}/{} enabled..programming filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001657 deviceId, port.number());
1658 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001659 } else {
1660 log.info("Switchport {}/{} disabled..removing filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001661 deviceId, port.number());
1662 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001663 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001664
1665 // portUpdated calls are for ports that have gone down or up. For switch
1666 // to switch ports, link-events should take care of any re-routing or
1667 // group editing necessary for port up/down. Here we only process edge ports
1668 // that are already configured.
Saurav Dasec683dc2018-04-27 18:42:30 -07001669 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
Charles Chan098ca202018-05-01 11:50:20 -07001670 VlanId untaggedVlan = interfaceService.getUntaggedVlanId(cp);
1671 VlanId nativeVlan = interfaceService.getNativeVlanId(cp);
1672 Set<VlanId> taggedVlans = interfaceService.getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001673
Saurav Das3fb28272017-03-04 16:08:47 -08001674 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001675 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasec683dc2018-04-27 18:42:30 -07001676 + "dev/port: {}/{}", deviceId, port.number());
Saurav Dasf0f592d2016-11-18 15:21:57 -08001677 return;
1678 }
Saurav Das3fb28272017-03-04 16:08:47 -08001679 if (untaggedVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001680 processEdgePort(deviceId, port, untaggedVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001681 }
1682 if (nativeVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001683 processEdgePort(deviceId, port, nativeVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001684 }
1685 if (!taggedVlans.isEmpty()) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001686 taggedVlans.forEach(tag -> processEdgePort(deviceId, port, tag, false));
Saurav Das3fb28272017-03-04 16:08:47 -08001687 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001688 }
1689
Saurav Dasec683dc2018-04-27 18:42:30 -07001690 private void processEdgePort(DeviceId deviceId, Port port, VlanId vlanId,
Saurav Das3fb28272017-03-04 16:08:47 -08001691 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001692 boolean portUp = port.isEnabled();
1693 if (portUp) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001694 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001695 port.number(), vlanId);
Charles Chan074fae62018-04-29 18:11:37 -07001696 hostEventExecutor.execute(() -> hostHandler.processPortUp(new ConnectPoint(deviceId, port.number())));
Saurav Dasf0f592d2016-11-18 15:21:57 -08001697 } else {
Saurav Dasec683dc2018-04-27 18:42:30 -07001698 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001699 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001700 }
1701
Saurav Dasec683dc2018-04-27 18:42:30 -07001702 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001703 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001704 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001705 } else {
1706 log.warn("Group handler not found for dev:{}. Not handling edge port"
Saurav Dasec683dc2018-04-27 18:42:30 -07001707 + " {} event for port:{}", deviceId,
Saurav Dasf0f592d2016-11-18 15:21:57 -08001708 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001709 }
1710 }
sangho27462c62015-05-14 00:39:53 -07001711
Charles Chan8ca5a122017-10-20 16:06:55 -07001712 private void createOrUpdateDeviceConfiguration() {
1713 if (deviceConfiguration == null) {
Saurav Dase321cff2018-02-09 17:26:45 -08001714 log.info("Creating new DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001715 deviceConfiguration = new DeviceConfiguration(this);
1716 } else {
Saurav Dase321cff2018-02-09 17:26:45 -08001717 log.info("Updating DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001718 deviceConfiguration.updateConfig();
1719 }
1720 }
1721
Charles Chanfbcb8812018-04-18 18:41:05 -07001722 private void createOrUpdateDefaultRoutingHandler() {
1723 if (defaultRoutingHandler == null) {
1724 log.info("Creating new DefaultRoutingHandler");
1725 defaultRoutingHandler = new DefaultRoutingHandler(this);
1726 } else {
1727 log.info("Updating DefaultRoutingHandler");
1728 defaultRoutingHandler.update(this);
1729 }
1730 }
1731
Pier Ventreb6a7f342016-11-26 21:05:22 -08001732 /**
1733 * Registers the given connect point with the NRS, this is necessary
1734 * to receive the NDP and ARP packets from the NRS.
1735 *
1736 * @param portToRegister connect point to register
1737 */
1738 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001739 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001740 portToRegister,
1741 neighbourHandler,
1742 appId
1743 );
1744 }
1745
Charles Chan72f556a2015-10-05 17:50:33 -07001746 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001747 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001748 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001749
Charles Chanb7f75ac2016-01-11 18:28:54 -08001750 /**
1751 * Constructs the internal network config listener.
1752 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001753 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001754 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001755 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001756 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001757 }
1758
Charles Chanb7f75ac2016-01-11 18:28:54 -08001759 /**
1760 * Reads network config and initializes related data structure accordingly.
1761 */
Charles Chan873661e2017-11-30 15:37:50 -08001762 void configureNetwork() {
Saurav Dase321cff2018-02-09 17:26:45 -08001763 log.info("Configuring network ...");
Andrea Campanella060cad82018-04-17 12:09:34 +02001764
1765 // Setting handling of network configuration events completable future
1766 // The completable future is needed because of the async behaviour of the configureNetwork,
1767 // listener registration and event arrival
1768 // Enables us to buffer the events and execute them when the configure network is done.
Ray Milkey521e4392018-11-16 15:15:14 -08001769 synchronized (networkConfigCompletionLock) {
1770 networkConfigCompletion = new CompletableFuture<>();
Andrea Campanella060cad82018-04-17 12:09:34 +02001771
Ray Milkey521e4392018-11-16 15:15:14 -08001772 // add a small delay to absorb multiple network config added notifications
1773 if (!programmingScheduled.get()) {
1774 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1775 programmingScheduled.set(true);
1776 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1777 }
1778
1779 createOrUpdateDeviceConfiguration();
1780
1781 arpHandler = new ArpHandler(srManager);
1782 icmpHandler = new IcmpHandler(srManager);
1783 ipHandler = new IpHandler(srManager);
1784 routingRulePopulator = new RoutingRulePopulator(srManager);
1785 createOrUpdateDefaultRoutingHandler();
1786
1787 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1788 groupHandlerMap, tunnelStore);
1789 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1790 flowObjectiveService,
1791 tunnelHandler, policyStore);
1792 networkConfigCompletion.complete(true);
Andrea Campanella060cad82018-04-17 12:09:34 +02001793 }
1794
Charles Chan72779502016-04-23 17:36:10 -07001795 mcastHandler.init();
Andrea Campanella060cad82018-04-17 12:09:34 +02001796
Charles Chane7c61022015-10-07 14:21:45 -07001797 }
1798
Charles Chan72f556a2015-10-05 17:50:33 -07001799 @Override
1800 public void event(NetworkConfigEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001801 if (mainEventExecutor == null) {
1802 return;
1803 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001804 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
1805 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1806 switch (event.type()) {
1807 case CONFIG_ADDED:
1808 case CONFIG_UPDATED:
1809 case CONFIG_REMOVED:
1810 log.trace("Schedule Network Config event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001811 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1812 mainEventExecutor.execute(new InternalEventHandler(event));
1813 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001814 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001815 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001816 break;
1817 default:
1818 break;
Charles Chan72f556a2015-10-05 17:50:33 -07001819 }
1820 }
Saurav Das261c3002017-06-13 15:35:54 -07001821
Charles Chanc4a68c32018-01-03 16:26:32 -08001822 @Override
1823 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase321cff2018-02-09 17:26:45 -08001824 if (event.type() == CONFIG_REGISTERED ||
1825 event.type() == CONFIG_UNREGISTERED) {
1826 log.debug("Ignore event {} due to type mismatch", event);
1827 return false;
Charles Chanc4a68c32018-01-03 16:26:32 -08001828 }
Saurav Dase321cff2018-02-09 17:26:45 -08001829
1830 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1831 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1832 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -08001833 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase321cff2018-02-09 17:26:45 -08001834 log.debug("Ignore event {} due to class mismatch", event);
1835 return false;
1836 }
1837
1838 return true;
Charles Chanc4a68c32018-01-03 16:26:32 -08001839 }
1840
Saurav Das261c3002017-06-13 15:35:54 -07001841 private final class ConfigChange implements Runnable {
1842 @Override
1843 public void run() {
1844 programmingScheduled.set(false);
Saurav Dase321cff2018-02-09 17:26:45 -08001845 log.info("Reacting to config changes after buffer delay");
Saurav Das261c3002017-06-13 15:35:54 -07001846 for (Device device : deviceService.getDevices()) {
1847 processDeviceAdded(device);
1848 }
1849 defaultRoutingHandler.startPopulationProcess();
1850 }
1851 }
Charles Chan72f556a2015-10-05 17:50:33 -07001852 }
Charles Chanf4586112015-11-09 16:37:23 -08001853
Charles Chan3f4aca62018-03-21 16:57:47 -07001854 private class InternalLinkListener implements LinkListener {
1855 @Override
1856 public void event(LinkEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001857 if (mainEventExecutor == null) {
1858 return;
1859 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001860 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1861 event.type() == LinkEvent.Type.LINK_UPDATED ||
1862 event.type() == LinkEvent.Type.LINK_REMOVED) {
1863 log.trace("Schedule Link event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001864 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1865 mainEventExecutor.execute(new InternalEventHandler(event));
1866 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001867 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001868 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001869 }
1870 }
1871 }
1872
1873 private class InternalDeviceListener implements DeviceListener {
1874 @Override
1875 public void event(DeviceEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001876 if (mainEventExecutor == null) {
1877 return;
1878 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001879 switch (event.type()) {
1880 case DEVICE_ADDED:
1881 case PORT_UPDATED:
1882 case PORT_ADDED:
1883 case DEVICE_UPDATED:
1884 case DEVICE_AVAILABILITY_CHANGED:
1885 log.trace("Schedule Device event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001886 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1887 mainEventExecutor.execute(new InternalEventHandler(event));
1888 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001889 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001890 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001891 break;
1892 default:
1893 }
1894 }
1895 }
1896
1897 private class InternalTopologyListener implements TopologyListener {
1898 @Override
1899 public void event(TopologyEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001900 if (mainEventExecutor == null) {
1901 return;
1902 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001903 switch (event.type()) {
1904 case TOPOLOGY_CHANGED:
1905 log.trace("Schedule Topology event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001906 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1907 mainEventExecutor.execute(new InternalEventHandler(event));
1908 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001909 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001910 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001911 break;
1912 default:
1913 }
1914 }
1915 }
1916
Charles Chanf4586112015-11-09 16:37:23 -08001917 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001918 @Override
1919 public void event(HostEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001920 if (hostEventExecutor == null) {
1921 return;
1922 }
Charles Chanf4586112015-11-09 16:37:23 -08001923 switch (event.type()) {
1924 case HOST_ADDED:
Charles Chanf4586112015-11-09 16:37:23 -08001925 case HOST_MOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001926 case HOST_REMOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001927 case HOST_UPDATED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001928 log.trace("Schedule Host event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001929 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chanf4586112015-11-09 16:37:23 -08001930 break;
1931 default:
1932 log.warn("Unsupported host event type: {}", event.type());
1933 break;
1934 }
1935 }
1936 }
1937
Charles Chanc91c8782016-03-30 17:54:24 -07001938 private class InternalMcastListener implements McastListener {
1939 @Override
1940 public void event(McastEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001941 if (mcastEventExecutor == null) {
1942 return;
1943 }
Charles Chanc91c8782016-03-30 17:54:24 -07001944 switch (event.type()) {
Pier1f87aca2018-03-14 16:47:32 -07001945 case SOURCES_ADDED:
1946 case SOURCES_REMOVED:
1947 case SINKS_ADDED:
1948 case SINKS_REMOVED:
Charles Chanc91c8782016-03-30 17:54:24 -07001949 case ROUTE_REMOVED:
Pierdb27b8d2018-04-17 16:29:56 +02001950 case ROUTE_ADDED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001951 log.trace("Schedule Mcast event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001952 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi6786b922018-02-02 16:19:11 +01001953 break;
Charles Chanc91c8782016-03-30 17:54:24 -07001954 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07001955 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chanc91c8782016-03-30 17:54:24 -07001956 break;
1957 }
1958 }
1959 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001960
Charles Chandebfea32016-10-24 14:52:01 -07001961 private class InternalRouteEventListener implements RouteListener {
1962 @Override
1963 public void event(RouteEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001964 if (routeEventExecutor == null) {
1965 return;
1966 }
Charles Chandebfea32016-10-24 14:52:01 -07001967 switch (event.type()) {
1968 case ROUTE_ADDED:
Charles Chandebfea32016-10-24 14:52:01 -07001969 case ROUTE_UPDATED:
Charles Chandebfea32016-10-24 14:52:01 -07001970 case ROUTE_REMOVED:
Charles Chan910be6a2017-08-23 14:46:43 -07001971 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001972 log.trace("Schedule Route event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001973 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan910be6a2017-08-23 14:46:43 -07001974 break;
Charles Chandebfea32016-10-24 14:52:01 -07001975 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07001976 log.warn("Unsupported route event type: {}", event.type());
Charles Chandebfea32016-10-24 14:52:01 -07001977 break;
1978 }
1979 }
1980 }
Saurav Das62ae6792017-05-15 15:34:25 -07001981
Charles Chanfbcb8812018-04-18 18:41:05 -07001982 private class InternalMastershipListener implements MastershipListener {
1983 @Override
1984 public void event(MastershipEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001985 if (mainEventExecutor == null) {
1986 return;
1987 }
Charles Chanfbcb8812018-04-18 18:41:05 -07001988 switch (event.type()) {
Saurav Das00e553b2018-04-21 17:19:48 -07001989 case MASTER_CHANGED:
1990 log.debug("Mastership event: {}/{}", event.subject(),
1991 event.roleInfo());
1992 mainEventExecutor.execute(new InternalEventHandler(event));
1993 break;
1994 case BACKUPS_CHANGED:
1995 case SUSPENDED:
1996 default:
1997 log.debug("Mastership event type {} not handled", event.type());
1998 break;
Charles Chanfbcb8812018-04-18 18:41:05 -07001999 }
2000 }
2001 }
2002
Saurav Das00e553b2018-04-21 17:19:48 -07002003 class InternalClusterListener implements ClusterEventListener {
2004 private Instant lastClusterEvent = Instant.EPOCH;
2005
2006 long timeSinceLastClusterEvent() {
2007 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
2008 }
2009
2010 @Override
2011 public void event(ClusterEvent event) {
2012 switch (event.type()) {
2013 case INSTANCE_ACTIVATED:
2014 case INSTANCE_ADDED:
2015 case INSTANCE_READY:
2016 log.debug("Cluster event {} ignored", event.type());
2017 break;
2018 case INSTANCE_DEACTIVATED:
2019 case INSTANCE_REMOVED:
2020 log.info("** Cluster event {}", event.type());
2021 lastClusterEvent = Instant.now();
2022 break;
2023 default:
2024 break;
2025 }
2026
2027 }
2028
2029 }
2030
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002031 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
2032 try {
2033 Set<Interface> intfs = conf.getInterfaces();
2034 Set<Interface> prevIntfs = prevConf.getInterfaces();
2035
2036 // Now we only handle one interface config at each port.
2037 if (intfs.size() != 1 || prevIntfs.size() != 1) {
2038 log.warn("Interface update aborted - one at a time is allowed, " +
2039 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
2040 return;
2041 }
2042
Andrea Campanella060cad82018-04-17 12:09:34 +02002043 //The system is in an incoherent state, abort
2044 if (defaultRoutingHandler == null) {
2045 log.warn("Interface update aborted, defaultRoutingHandler is null");
2046 return;
2047 }
2048
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002049 Interface intf = intfs.stream().findFirst().get();
2050 Interface prevIntf = prevIntfs.stream().findFirst().get();
2051
2052 DeviceId deviceId = intf.connectPoint().deviceId();
2053 PortNumber portNum = intf.connectPoint().port();
2054
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002055 removeSubnetConfig(prevIntf.connectPoint(),
2056 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
2057 new HashSet<>(intf.ipAddressesList())));
2058
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002059 if (!prevIntf.vlanNative().equals(VlanId.NONE)
2060 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
2061 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
2062 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
2063 // Update filtering objective and L2IG group bucket
2064 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
2065 } else {
2066 // RemoveVlanNative
2067 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
2068 }
2069 }
2070
2071 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
2072 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2073 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2074 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
2075 // Update filtering objective and L2IG group bucket
2076 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
2077 } else {
2078 // RemoveVlanUntagged
2079 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
2080 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002081 }
2082
2083 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2084 // RemoveVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002085 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
2086 .filter(i -> !intf.vlanUntagged().equals(i))
2087 .filter(i -> !intf.vlanNative().equals(i))
2088 .forEach(vlanId -> updateVlanConfigInternal(
2089 deviceId, portNum, vlanId, false, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002090 }
2091
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002092 if (!intf.vlanNative().equals(VlanId.NONE)
2093 && !prevIntf.vlanNative().equals(intf.vlanNative())
2094 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2095 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
2096 // Update filtering objective and L2IG group bucket
2097 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
2098 } else {
2099 // AddVlanNative
2100 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
2101 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002102 }
2103
2104 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2105 // AddVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002106 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
2107 .filter(i -> !prevIntf.vlanUntagged().equals(i))
2108 .filter(i -> !prevIntf.vlanNative().equals(i))
2109 .forEach(vlanId -> updateVlanConfigInternal(
2110 deviceId, portNum, vlanId, false, true)
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002111 );
2112 }
2113
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002114 if (!intf.vlanUntagged().equals(VlanId.NONE)
2115 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2116 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
2117 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
2118 // Update filtering objective and L2IG group bucket
2119 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
2120 } else {
2121 // AddVlanUntagged
2122 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
2123 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002124 }
2125 addSubnetConfig(prevIntf.connectPoint(),
2126 Sets.difference(new HashSet<>(intf.ipAddressesList()),
2127 new HashSet<>(prevIntf.ipAddressesList())));
2128 } catch (ConfigException e) {
2129 log.error("Error in configuration");
2130 }
2131 }
2132
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002133 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
2134 VlanId vlanId, boolean pushVlan) {
2135 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2136 if (grpHandler == null) {
2137 log.warn("Failed to retrieve group handler for device {}", deviceId);
2138 return;
2139 }
2140
2141 // Update filtering objective for a single port
2142 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
2143 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
2144
2145 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
2146 // Update L2IG bucket of the port
2147 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
2148 } else {
2149 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2150 }
2151 }
2152
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002153 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
2154 VlanId vlanId, boolean pushVlan, boolean install) {
2155 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2156 if (grpHandler == null) {
2157 log.warn("Failed to retrieve group handler for device {}", deviceId);
2158 return;
2159 }
2160
2161 // Update filtering objective for a single port
2162 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
2163
2164 // Update filtering objective for multicast ingress port
2165 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
2166
2167 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
2168
2169 if (nextId != -1 && !install) {
2170 // Update next objective for a single port as an output port
2171 // Remove a single port from L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002172 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002173 // Remove L2 Bridging rule and L3 Unicast rule to the host
Charles Chan074fae62018-04-29 18:11:37 -07002174 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2175 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002176 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
2177 // only if there is no port configured on that VLAN ID
2178 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
2179 // Remove broadcast forwarding rule for the VLAN
2180 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2181 // Remove L2FG for VLAN
2182 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
2183 } else {
2184 // Remove L2IG of the port
2185 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
2186 }
2187 } else if (install) {
2188 if (nextId != -1) {
2189 // Add a single port to L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002190 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002191 } else {
2192 // Create L2FG for VLAN
2193 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
2194 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2195 }
Charles Chan074fae62018-04-29 18:11:37 -07002196 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2197 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002198 } else {
2199 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2200 }
2201 }
2202
2203 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2204 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2205 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2206
2207 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2208 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2209 .filter(intf -> !intf.connectPoint().equals(cp))
2210 .flatMap(intf -> intf.ipAddressesList().stream())
2211 .collect(Collectors.toSet());
2212 // 1. Partial subnet population
2213 // Remove routing rules for removed subnet from previous configuration,
2214 // which does not also exist in other interfaces in the same device
2215 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2216 .map(InterfaceIpAddress::subnetAddress)
2217 .collect(Collectors.toSet());
2218
2219 defaultRoutingHandler.revokeSubnet(
2220 ipPrefixSet.stream()
2221 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2222 .collect(Collectors.toSet()));
2223
2224 // 2. Interface IP punts
2225 // Remove IP punts for old Intf address
2226 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2227 .map(InterfaceIpAddress::ipAddress)
2228 .collect(Collectors.toSet());
2229 ipAddressSet.stream()
2230 .map(InterfaceIpAddress::ipAddress)
2231 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2232 .forEach(interfaceIpAddress ->
2233 routingRulePopulator.revokeSingleIpPunts(
2234 cp.deviceId(), interfaceIpAddress));
2235
2236 // 3. Host unicast routing rule
2237 // Remove unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002238 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002239 }
2240
2241 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2242 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2243 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2244
2245 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2246 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2247 .filter(intf -> !intf.connectPoint().equals(cp))
2248 .flatMap(intf -> intf.ipAddressesList().stream())
2249 .collect(Collectors.toSet());
2250 // 1. Partial subnet population
2251 // Add routing rules for newly added subnet, which does not also exist in
2252 // other interfaces in the same device
2253 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2254 .map(InterfaceIpAddress::subnetAddress)
2255 .collect(Collectors.toSet());
2256
2257 defaultRoutingHandler.populateSubnet(
2258 Collections.singleton(cp),
2259 ipPrefixSet.stream()
2260 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2261 .collect(Collectors.toSet()));
2262
2263 // 2. Interface IP punts
2264 // Add IP punts for new Intf address
2265 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2266 .map(InterfaceIpAddress::ipAddress)
2267 .collect(Collectors.toSet());
2268 ipAddressSet.stream()
2269 .map(InterfaceIpAddress::ipAddress)
2270 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2271 .forEach(interfaceIpAddress ->
2272 routingRulePopulator.populateSingleIpPunts(
2273 cp.deviceId(), interfaceIpAddress));
2274
2275 // 3. Host unicast routing rule
2276 // Add unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002277 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002278 }
sangho80f11cb2015-04-01 13:05:26 -07002279}