blob: 17e733c22ba70b09945bcb70ed478af57d141337 [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;
sangho80f11cb2015-04-01 13:05:26 -0700132import org.slf4j.Logger;
133import org.slf4j.LoggerFactory;
134
Saurav Das00e553b2018-04-21 17:19:48 -0700135import java.time.Instant;
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700136import java.util.ArrayList;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800137import java.util.Collections;
Charles Chan873661e2017-11-30 15:37:50 -0800138import java.util.Dictionary;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800139import java.util.HashSet;
140import java.util.List;
141import java.util.Map;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800142import java.util.Optional;
143import java.util.Set;
Andrea Campanella060cad82018-04-17 12:09:34 +0200144import java.util.concurrent.CompletableFuture;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800145import java.util.concurrent.ConcurrentHashMap;
Andrea Campanella060cad82018-04-17 12:09:34 +0200146import java.util.concurrent.CopyOnWriteArrayList;
Charles Chan77cdde42018-05-08 21:35:50 -0700147import java.util.concurrent.ExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800148import java.util.concurrent.Executors;
149import java.util.concurrent.ScheduledExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800150import java.util.concurrent.TimeUnit;
151import java.util.concurrent.atomic.AtomicBoolean;
152import java.util.stream.Collectors;
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700153import java.util.stream.IntStream;
sangho80f11cb2015-04-01 13:05:26 -0700154
Charles Chand6d25332016-02-26 22:19:52 -0800155import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800156import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700157import static org.onlab.util.Tools.groupedThreads;
Saurav Dase321cff2018-02-09 17:26:45 -0800158import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
159import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700160import static org.onosproject.segmentrouting.OsgiPropertyConstants.ACTIVE_PROBING_DEFAULT;
161import static org.onosproject.segmentrouting.OsgiPropertyConstants.DEFAULT_INTERNAL_VLAN_DEFAULT;
162import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ACTIVE_PROBING;
163import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_DEFAULT_INTERNAL_VLAN;
164import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_PW_TRANSPORT_VLAN;
165import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_RESPOND_TO_UNKNOWN_HOSTS;
166import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_DOUBLE_TAGGED_HOSTS;
167import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SINGLE_HOMED_DOWN;
168import static org.onosproject.segmentrouting.OsgiPropertyConstants.PW_TRANSPORT_VLAN_DEFAULT;
169import static org.onosproject.segmentrouting.OsgiPropertyConstants.RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
170import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
171import static org.onosproject.segmentrouting.OsgiPropertyConstants.SINGLE_HOMED_DOWN_DEFAULT;
Charles Chand6d25332016-02-26 22:19:52 -0800172
Charles Chanb7f75ac2016-01-11 18:28:54 -0800173/**
174 * Segment routing manager.
175 */
Ray Milkey05edbfc2018-10-23 14:23:16 -0700176@Component(
177 immediate = true,
178 service = SegmentRoutingService.class,
179 property = {
180 PROP_ACTIVE_PROBING + ":Boolean=" + ACTIVE_PROBING_DEFAULT,
181 PROP_SINGLE_HOMED_DOWN + ":Boolean=" + SINGLE_HOMED_DOWN_DEFAULT,
182 PROP_RESPOND_TO_UNKNOWN_HOSTS + ":Boolean=" + RESPOND_TO_UNKNOWN_HOSTS_DEFAULT,
183 PROP_ROUTE_DOUBLE_TAGGED_HOSTS + ":Boolean=" + ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT,
184 PROP_DEFAULT_INTERNAL_VLAN + ":Integer=" + DEFAULT_INTERNAL_VLAN_DEFAULT,
185 PROP_PW_TRANSPORT_VLAN + ":Integer=" + PW_TRANSPORT_VLAN_DEFAULT,
186 }
187)
sangho27462c62015-05-14 00:39:53 -0700188public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700189
Charles Chan46fdfaf2016-11-09 20:51:44 -0800190 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan910be6a2017-08-23 14:46:43 -0700191 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sangho80f11cb2015-04-01 13:05:26 -0700192
Ray Milkey2bd24a92018-08-17 14:54:17 -0700193 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700194 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700195
Ray Milkey2bd24a92018-08-17 14:54:17 -0700196 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventreb6a7f342016-11-26 21:05:22 -0800197 private NeighbourResolutionService neighbourResolutionService;
198
Ray Milkey2bd24a92018-08-17 14:54:17 -0700199 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100200 public CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700201
Ray Milkey2bd24a92018-08-17 14:54:17 -0700202 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700203 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700204
Ray Milkey2bd24a92018-08-17 14:54:17 -0700205 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700206 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700207
Ray Milkey2bd24a92018-08-17 14:54:17 -0700208 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chanc6bcdf92018-06-01 16:33:48 -0700209 HostProbingService probingService;
Charles Chan873661e2017-11-30 15:37:50 -0800210
Ray Milkey2bd24a92018-08-17 14:54:17 -0700211 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100212 public DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700213
Ray Milkey2bd24a92018-08-17 14:54:17 -0700214 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Saurav Dase6c448a2018-01-18 12:07:33 -0800215 DeviceAdminService deviceAdminService;
216
Ray Milkey2bd24a92018-08-17 14:54:17 -0700217 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800218 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700219
Ray Milkey2bd24a92018-08-17 14:54:17 -0700220 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100221 public LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700222
Ray Milkey2bd24a92018-08-17 14:54:17 -0700223 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800224 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700225
Ray Milkey2bd24a92018-08-17 14:54:17 -0700226 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800227 public StorageService storageService;
Charles Chandebfea32016-10-24 14:52:01 -0700228
Ray Milkey2bd24a92018-08-17 14:54:17 -0700229 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100230 public MulticastRouteService multicastRouteService;
Charles Chandebfea32016-10-24 14:52:01 -0700231
Ray Milkey2bd24a92018-08-17 14:54:17 -0700232 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan83163812018-01-09 13:45:07 -0800233 public TopologyService topologyService;
Charles Chandebfea32016-10-24 14:52:01 -0700234
Ray Milkey2bd24a92018-08-17 14:54:17 -0700235 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700236 RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800237
Ray Milkey2bd24a92018-08-17 14:54:17 -0700238 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800239 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800240
Ray Milkey2bd24a92018-08-17 14:54:17 -0700241 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800242 public InterfaceService interfaceService;
243
Ray Milkey2bd24a92018-08-17 14:54:17 -0700244 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi580fd8a2018-01-16 10:47:50 +0100245 public ClusterService clusterService;
246
Ray Milkey2bd24a92018-08-17 14:54:17 -0700247 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pierdb27b8d2018-04-17 16:29:56 +0200248 public WorkPartitionService workPartitionService;
Pier Luigi580fd8a2018-01-16 10:47:50 +0100249
Ray Milkey2bd24a92018-08-17 14:54:17 -0700250 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700251 public LeadershipService leadershipService;
252
Ray Milkey2bd24a92018-08-17 14:54:17 -0700253 @Reference(cardinality = ReferenceCardinality.OPTIONAL)
Charles Chan8d316332018-06-19 20:31:57 -0700254 public XconnectService xconnectService;
255
Ray Milkey05edbfc2018-10-23 14:23:16 -0700256 /** Enable active probing to discover dual-homed hosts. */
257 boolean activeProbing = ACTIVE_PROBING_DEFAULT;
Charles Chan873661e2017-11-30 15:37:50 -0800258
Ray Milkey05edbfc2018-10-23 14:23:16 -0700259 /** Enable administratively taking down single-homed hosts. */
260 boolean singleHomedDown = SINGLE_HOMED_DOWN_DEFAULT;
Saurav Dasec683dc2018-04-27 18:42:30 -0700261
Ray Milkey05edbfc2018-10-23 14:23:16 -0700262 /** Enable this to respond to ARP/NDP requests from unknown hosts. */
263 boolean respondToUnknownHosts = RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700264
Ray Milkey05edbfc2018-10-23 14:23:16 -0700265 /** Program flows and groups to pop and route double tagged hosts. */
266 boolean routeDoubleTaggedHosts = ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
Charles Chan4eb73bb2018-07-19 14:55:31 -0700267
Ray Milkey05edbfc2018-10-23 14:23:16 -0700268 /** internal vlan assigned by default to unconfigured ports. */
269 private int defaultInternalVlan = DEFAULT_INTERNAL_VLAN_DEFAULT;
Saurav Das9bf49582018-08-13 15:34:26 -0700270
Ray Milkey05edbfc2018-10-23 14:23:16 -0700271 /** vlan used for transport of pseudowires between switches. */
272 private int pwTransportVlan = PW_TRANSPORT_VLAN_DEFAULT;
Saurav Das9bf49582018-08-13 15:34:26 -0700273
Charles Chandebfea32016-10-24 14:52:01 -0700274 ArpHandler arpHandler = null;
275 IcmpHandler icmpHandler = null;
276 IpHandler ipHandler = null;
277 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700278 ApplicationId appId;
279 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700280
Charles Chandebfea32016-10-24 14:52:01 -0700281 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700282 private TunnelHandler tunnelHandler = null;
283 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700284 private InternalPacketProcessor processor = null;
285 private InternalLinkListener linkListener = null;
286 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700287 private AppConfigHandler appCfgHandler = null;
Pier Luigi69f774d2018-02-28 12:10:50 +0100288 public XConnectHandler xConnectHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800289 McastHandler mcastHandler = null;
290 HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800291 private RouteHandler routeHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800292 LinkHandler linkHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800293 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800294 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigid8a15162018-02-15 16:33:08 +0100295 private TopologyHandler topologyHandler = null;
Charles Chan82ab1932016-01-30 23:22:37 -0800296 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700297 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
298 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700299 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigid8a15162018-02-15 16:33:08 +0100300 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chanfbcb8812018-04-18 18:41:05 -0700301 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
Saurav Das00e553b2018-04-21 17:19:48 -0700302 final InternalClusterListener clusterListener = new InternalClusterListener();
Andrea Campanella060cad82018-04-17 12:09:34 +0200303 //Completable future for network configuration process to buffer config events handling during activation
304 private CompletableFuture<Boolean> networkConfigCompletion = null;
Charles Chan39b75522018-04-21 00:44:29 -0700305 private List<Event> queuedEvents = new CopyOnWriteArrayList<>();
sangho80f11cb2015-04-01 13:05:26 -0700306
Charles Chan52003922018-04-05 16:31:15 -0700307 // Handles device, link, topology and network config events
Charles Chanfbcb8812018-04-18 18:41:05 -0700308 private ScheduledExecutorService mainEventExecutor;
sangho80f11cb2015-04-01 13:05:26 -0700309
Charles Chanfbcb8812018-04-18 18:41:05 -0700310 // Handles host, route and mcast events respectively
311 private ScheduledExecutorService hostEventExecutor;
312 private ScheduledExecutorService routeEventExecutor;
313 private ScheduledExecutorService mcastEventExecutor;
Charles Chan77cdde42018-05-08 21:35:50 -0700314 private ExecutorService packetExecutor;
Charles Chan52003922018-04-05 16:31:15 -0700315
316 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chanb7f75ac2016-01-11 18:28:54 -0800317 /**
Saurav Das261c3002017-06-13 15:35:54 -0700318 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700319 * Used to keep track on MPLS group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800320 */
Charles Chan873661e2017-11-30 15:37:50 -0800321 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das261c3002017-06-13 15:35:54 -0700322 dsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800323 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700324 * Per device next objective ID store with (device id + vlanid) as key.
325 * Used to keep track on L2 flood group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800326 */
Charles Chan873661e2017-11-30 15:37:50 -0800327 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800328 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800329 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700330 * Per device next objective ID store with (device id + port + treatment + meta) as key.
331 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800332 */
Charles Chan873661e2017-11-30 15:37:50 -0800333 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800334 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800335
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700336 /**
337 * Per port dummy VLAN ID store with (connect point + ip address) as key.
338 * Used to keep track on dummy VLAN ID allocation.
339 */
340 private EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId>
341 dummyVlanIdStore = null;
342
Saurav Das2d94d312015-11-24 23:21:05 -0800343 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
344 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700345
Saurav Das261c3002017-06-13 15:35:54 -0700346 private AtomicBoolean programmingScheduled = new AtomicBoolean();
347
Charles Chanc91c8782016-03-30 17:54:24 -0700348 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700349 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
350 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700351 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700352 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800353 public SegmentRoutingDeviceConfig createConfig() {
354 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700355 }
356 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800357
Charles Chanc91c8782016-03-30 17:54:24 -0700358 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700359 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
360 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700361 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800362 @Override
363 public SegmentRoutingAppConfig createConfig() {
364 return new SegmentRoutingAppConfig();
365 }
366 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800367
Charles Chan82f19972016-05-17 13:13:55 -0700368 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
369 new ConfigFactory<ApplicationId, XConnectConfig>(
370 SubjectFactories.APP_SUBJECT_FACTORY,
371 XConnectConfig.class, "xconnect") {
372 @Override
373 public XConnectConfig createConfig() {
374 return new XConnectConfig();
375 }
376 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800377
Charles Chanc91c8782016-03-30 17:54:24 -0700378 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700379 new ConfigFactory<ApplicationId, McastConfig>(
380 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700381 McastConfig.class, "multicast") {
382 @Override
383 public McastConfig createConfig() {
384 return new McastConfig();
385 }
386 };
387
Charles Chan1963f4f2016-02-18 14:22:42 -0800388 /**
389 * Segment Routing App ID.
390 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800391 public static final String APP_NAME = "org.onosproject.segmentrouting";
Saurav Das7c305372015-10-28 12:39:42 -0700392
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700393 /**
394 * Minumum and maximum value of dummy VLAN ID to be allocated.
395 */
396 public static final int MIN_DUMMY_VLAN_ID = 2;
397 public static final int MAX_DUMMY_VLAN_ID = 4093;
398
Saurav Dasec683dc2018-04-27 18:42:30 -0700399 Instant lastEdgePortEvent = Instant.EPOCH;
400
sangho80f11cb2015-04-01 13:05:26 -0700401 @Activate
Charles Chan873661e2017-11-30 15:37:50 -0800402 protected void activate(ComponentContext context) {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800403 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700404
Charles Chanfbcb8812018-04-18 18:41:05 -0700405 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-main", "%d", log));
406 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-host", "%d", log));
407 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-route", "%d", log));
408 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-mcast", "%d", log));
Charles Chan77cdde42018-05-08 21:35:50 -0700409 packetExecutor = Executors.newSingleThreadExecutor(groupedThreads("sr-packet", "%d", log));
Charles Chanfbcb8812018-04-18 18:41:05 -0700410
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700411 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700412 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700413 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700414 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700415 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700416 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700417 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700418 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700419 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700420
Charles Chan10b0fb72017-02-02 16:20:42 -0800421 log.debug("Creating EC map vlannextobjectivestore");
422 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
423 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
424 vlanNextObjStore = vlanNextObjMapBuilder
425 .withName("vlannextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700426 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700427 .withTimestampProvider((k, v) -> new WallClockTimestamp())
428 .build();
429
Saurav Das2d94d312015-11-24 23:21:05 -0800430 log.debug("Creating EC map subnetnextobjectivestore");
431 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
432 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
433 portNextObjStore = portNextObjMapBuilder
434 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700435 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800436 .withTimestampProvider((k, v) -> new WallClockTimestamp())
437 .build();
438
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700439 EventuallyConsistentMapBuilder<DummyVlanIdStoreKey, VlanId>
440 dummyVlanIdMapBuilder = storageService.eventuallyConsistentMapBuilder();
441 dummyVlanIdStore = dummyVlanIdMapBuilder
442 .withName("dummyvlanidstore")
443 .withSerializer(createSerializer())
444 .withTimestampProvider((k, v) -> new WallClockTimestamp())
445 .build();
446
sangho4a5c42a2015-05-20 22:16:38 -0700447 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
448 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700449 tunnelStore = tunnelMapBuilder
450 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700451 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700452 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700453 .build();
454
455 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
456 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700457 policyStore = policyMapBuilder
458 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700459 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700460 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700461 .build();
462
Saurav Dasc3604f12016-03-23 11:22:49 -0700463 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800464 "purgeOnDisconnection", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700465 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800466 "purgeOnDisconnection", "true");
Pier Luigib9632ba2017-01-12 18:14:58 -0800467 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
468 "requestInterceptsEnabled", "false");
Charles Chanc1909e12017-08-08 15:13:37 -0700469 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800470 "requestInterceptsEnabled", "false");
Charles Chan9597f8d2017-07-24 15:56:10 -0700471 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800472 "arpEnabled", "false");
Pier Luigi0ebeb312017-02-02 22:31:34 -0800473 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
474 "greedyLearningIpv6", "true");
Charles Chancf8ea472017-02-28 15:15:17 -0800475 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
476 "forceUnprovision", "true");
Charles Chanc7b73c72017-08-10 16:57:28 -0700477 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan4c95c0d2017-07-20 16:16:25 -0700478 "distributed", "true");
Charles Chan804772f2017-08-14 11:42:11 -0700479 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
480 "multihomingEnabled", "true");
Charles Chan0c9c19f2017-11-29 19:54:20 -0800481 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
482 "staleLinkAge", "15000");
483 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
484 "allowDuplicateIps", "false");
Yi Tsenga7da8d82018-08-19 03:09:54 +0800485 // For P4 switches
486 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Charles Chan02298512018-08-30 17:30:45 -0700487 "fallbackFlowPollFrequency", "4");
Yi Tsenga7da8d82018-08-19 03:09:54 +0800488 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Charles Chan02298512018-08-30 17:30:45 -0700489 "fallbackGroupPollFrequency", "3");
Charles Chan873661e2017-11-30 15:37:50 -0800490 compCfgService.registerProperties(getClass());
491 modified(context);
Saurav Dasc3604f12016-03-23 11:22:49 -0700492
Charles Chan2b078ae2015-10-14 11:24:40 -0700493 processor = new InternalPacketProcessor();
494 linkListener = new InternalLinkListener();
495 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700496 appCfgHandler = new AppConfigHandler(this);
497 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700498 mcastHandler = new McastHandler(this);
499 hostHandler = new HostHandler(this);
Saurav Dase6c448a2018-01-18 12:07:33 -0800500 linkHandler = new LinkHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700501 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800502 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800503 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigid8a15162018-02-15 16:33:08 +0100504 topologyHandler = new TopologyHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700505
Charles Chand6d25332016-02-26 22:19:52 -0800506 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700507 cfgService.registerConfigFactory(deviceConfigFactory);
508 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700509 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700510 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase321cff2018-02-09 17:26:45 -0800511 log.info("Configuring network before adding listeners");
Andrea Campanella060cad82018-04-17 12:09:34 +0200512
Charles Chan6961f222018-01-04 14:26:07 -0800513 cfgListener.configureNetwork();
514
Charles Chan82ab1932016-01-30 23:22:37 -0800515 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700516 packetService.addProcessor(processor, PacketProcessor.director(2));
517 linkService.addListener(linkListener);
518 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700519 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700520 routeService.addListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100521 topologyService.addListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700522 mastershipService.addListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700523 clusterService.addListener(clusterListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700524
Charles Chanc12c3d02018-03-09 15:53:44 -0800525 linkHandler.init();
Andreas Pantelopoulos75eef062018-01-11 07:53:48 -0800526 l2TunnelHandler.init();
527
Andrea Campanella060cad82018-04-17 12:09:34 +0200528 networkConfigCompletion.whenComplete((value, ex) -> {
529 //setting to null for easier fall through
530 networkConfigCompletion = null;
531 //process all queued events
Charles Chan39b75522018-04-21 00:44:29 -0700532 queuedEvents.forEach(event -> {
Andrea Campanella060cad82018-04-17 12:09:34 +0200533 mainEventExecutor.execute(new InternalEventHandler(event));
534 });
535 });
536
sangho80f11cb2015-04-01 13:05:26 -0700537 log.info("Started");
538 }
539
Saurav Dase6c448a2018-01-18 12:07:33 -0800540 KryoNamespace.Builder createSerializer() {
Jonathan Hart54541d12016-04-12 15:39:44 -0700541 return new KryoNamespace.Builder()
542 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700543 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800544 VlanNextObjectiveStoreKey.class,
545 DestinationSet.class,
Andreas Pantelopoulos92998c92018-05-29 13:11:14 -0700546 DestinationSet.DestinationSetType.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800547 NextNeighbors.class,
548 Tunnel.class,
549 DefaultTunnel.class,
550 Policy.class,
551 TunnelPolicy.class,
552 Policy.Type.class,
553 PortNextObjectiveStoreKey.class,
554 XConnectStoreKey.class,
555 L2Tunnel.class,
556 L2TunnelPolicy.class,
557 DefaultL2Tunnel.class,
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700558 DefaultL2TunnelPolicy.class,
559 DummyVlanIdStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700560 );
561 }
562
sangho80f11cb2015-04-01 13:05:26 -0700563 @Deactivate
564 protected void deactivate() {
Charles Chanfbcb8812018-04-18 18:41:05 -0700565 mainEventExecutor.shutdown();
566 hostEventExecutor.shutdown();
567 routeEventExecutor.shutdown();
568 mcastEventExecutor.shutdown();
Charles Chan77cdde42018-05-08 21:35:50 -0700569 packetExecutor.shutdown();
Charles Chanfbcb8812018-04-18 18:41:05 -0700570
Ray Milkeyaee4d3e2018-05-11 09:59:19 -0700571 mainEventExecutor = null;
572 hostEventExecutor = null;
573 routeEventExecutor = null;
574 mcastEventExecutor = null;
575 packetExecutor = null;
576
Charles Chan72f556a2015-10-05 17:50:33 -0700577 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700578 cfgService.unregisterConfigFactory(deviceConfigFactory);
579 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700580 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700581 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan873661e2017-11-30 15:37:50 -0800582 compCfgService.unregisterProperties(getClass(), false);
Charles Chan72f556a2015-10-05 17:50:33 -0700583
Charles Chanfd48c222017-06-19 00:43:31 -0700584 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700585 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700586 linkService.removeListener(linkListener);
587 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700588 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700589 routeService.removeListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100590 topologyService.removeListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700591 mastershipService.removeListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700592 clusterService.removeListener(clusterListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700593
Charles Chan2e71ef32017-02-23 15:44:08 -0800594 neighbourResolutionService.unregisterNeighbourHandlers(appId);
595
sangho80f11cb2015-04-01 13:05:26 -0700596 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700597 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700598 deviceListener = null;
Charles Chan05bb1702018-04-19 13:10:01 -0700599 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chan2b078ae2015-10-14 11:24:40 -0700600 groupHandlerMap.clear();
Saurav Das49368392018-04-23 18:42:12 -0700601 defaultRoutingHandler.shutdown();
Charles Chan2b078ae2015-10-14 11:24:40 -0700602
Saurav Das261c3002017-06-13 15:35:54 -0700603 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800604 vlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700605 portNextObjStore.destroy();
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700606 dummyVlanIdStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700607 tunnelStore.destroy();
608 policyStore.destroy();
Pier Luigi35dab3f2018-01-25 16:16:02 +0100609
610 mcastHandler.terminate();
sangho80f11cb2015-04-01 13:05:26 -0700611 log.info("Stopped");
612 }
613
Charles Chan873661e2017-11-30 15:37:50 -0800614 @Modified
615 private void modified(ComponentContext context) {
616 Dictionary<?, ?> properties = context.getProperties();
617 if (properties == null) {
618 return;
619 }
620
Ray Milkey48d10c02018-10-24 10:04:03 -0700621 String strActiveProbing = Tools.get(properties, PROP_ACTIVE_PROBING);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700622 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProbing);
Charles Chan873661e2017-11-30 15:37:50 -0800623 if (expectActiveProbing != activeProbing) {
624 activeProbing = expectActiveProbing;
625 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
626 }
Saurav Dasec683dc2018-04-27 18:42:30 -0700627
Ray Milkey48d10c02018-10-24 10:04:03 -0700628 String strSingleHomedDown = Tools.get(properties, PROP_SINGLE_HOMED_DOWN);
Saurav Dasec683dc2018-04-27 18:42:30 -0700629 boolean expectSingleHomedDown = Boolean.parseBoolean(strSingleHomedDown);
630 if (expectSingleHomedDown != singleHomedDown) {
631 singleHomedDown = expectSingleHomedDown;
632 log.info("{} downing of single homed hosts for lost uplinks",
633 singleHomedDown ? "Enabling" : "Disabling");
634 if (singleHomedDown && linkHandler != null) {
635 hostService.getHosts().forEach(host -> host.locations()
636 .forEach(loc -> {
637 if (interfaceService.isConfigured(loc)) {
638 linkHandler.checkUplinksForHost(loc);
639 }
640 }));
641 } else {
642 log.warn("Disabling singleHomedDown does not re-enable already "
643 + "downed ports for single-homed hosts");
644 }
645 }
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700646
Ray Milkey48d10c02018-10-24 10:04:03 -0700647 String strRespondToUnknownHosts = Tools.get(properties, PROP_RESPOND_TO_UNKNOWN_HOSTS);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700648 boolean expectRespondToUnknownHosts = Boolean.parseBoolean(strRespondToUnknownHosts);
649 if (expectRespondToUnknownHosts != respondToUnknownHosts) {
650 respondToUnknownHosts = expectRespondToUnknownHosts;
651 log.info("{} responding to ARPs/NDPs from unknown hosts", respondToUnknownHosts ? "Enabling" : "Disabling");
652 }
Charles Chan4eb73bb2018-07-19 14:55:31 -0700653
Ray Milkey48d10c02018-10-24 10:04:03 -0700654 String strRouteDoubleTaggedHosts = Tools.get(properties, PROP_ROUTE_DOUBLE_TAGGED_HOSTS);
Charles Chanfbca55e2018-07-24 16:40:35 -0700655 boolean expectRouteDoubleTaggedHosts = Boolean.parseBoolean(strRouteDoubleTaggedHosts);
656 if (expectRouteDoubleTaggedHosts != routeDoubleTaggedHosts) {
657 routeDoubleTaggedHosts = expectRouteDoubleTaggedHosts;
658 log.info("{} routing for double tagged hosts", routeDoubleTaggedHosts ? "Enabling" : "Disabling");
Charles Chan4eb73bb2018-07-19 14:55:31 -0700659
Charles Chanfbca55e2018-07-24 16:40:35 -0700660 if (routeDoubleTaggedHosts) {
Charles Chan4eb73bb2018-07-19 14:55:31 -0700661 hostHandler.populateAllDoubleTaggedHost();
662 } else {
663 hostHandler.revokeAllDoubleTaggedHost();
664 }
665 }
Saurav Das9bf49582018-08-13 15:34:26 -0700666
Ray Milkey48d10c02018-10-24 10:04:03 -0700667 String strDefaultInternalVlan = Tools.get(properties, PROP_DEFAULT_INTERNAL_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700668 int defIntVlan = Integer.parseInt(strDefaultInternalVlan);
669 if (defIntVlan != defaultInternalVlan) {
670 if (canUseVlanId(defIntVlan)) {
671 log.warn("Default internal vlan value changed from {} to {}.. "
672 + "re-programming filtering rules, but NOT any groups already "
673 + "created with the former value", defaultInternalVlan, defIntVlan);
674 VlanId oldDefIntVlan = VlanId.vlanId((short) defaultInternalVlan);
675 defaultInternalVlan = defIntVlan;
676 routingRulePopulator
677 .updateSpecialVlanFilteringRules(true, oldDefIntVlan,
678 VlanId.vlanId((short) defIntVlan));
679 } else {
680 log.warn("Cannot change default internal vlan to unusable "
681 + "value {}", defIntVlan);
682 }
683 }
684
Ray Milkey48d10c02018-10-24 10:04:03 -0700685 String strPwTxpVlan = Tools.get(properties, PROP_PW_TRANSPORT_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700686 int pwTxpVlan = Integer.parseInt(strPwTxpVlan);
687 if (pwTxpVlan != pwTransportVlan) {
688 if (canUseVlanId(pwTxpVlan)) {
689 log.warn("Pseudowire transport vlan value changed from {} to {}.. "
690 + "re-programming filtering rules, but NOT any groups already "
691 + "created with the former value", pwTransportVlan,
692 pwTxpVlan);
693 VlanId oldPwTxpVlan = VlanId.vlanId((short) pwTransportVlan);
694 pwTransportVlan = pwTxpVlan;
695 routingRulePopulator
696 .updateSpecialVlanFilteringRules(false, oldPwTxpVlan,
697 VlanId.vlanId((short) pwTxpVlan));
698 } else {
699 log.warn("Cannot change pseudowire transport vlan to unusable "
700 + "value {}", pwTxpVlan);
701 }
702 }
703
704 }
705
706 /**
707 * Returns true if given vlan id is not being used in the system currently,
708 * either as one of the default system wide vlans or as one of the
709 * configured interface vlans.
710 *
711 * @param vlanId given vlan id
712 * @return true if vlan is not currently in use
713 */
714 public boolean canUseVlanId(int vlanId) {
715 if (vlanId >= 4095 || vlanId <= 1) {
716 log.error("Vlan id {} value is not in valid range 2 <--> 4094",
717 vlanId);
718 return false;
719 }
720
721 VlanId vid = VlanId.vlanId((short) vlanId);
722 if (getDefaultInternalVlan().equals(vid) || getPwTransportVlan().equals(vid)) {
723 log.warn("Vlan id {} value is already in use system-wide. "
724 + "DefaultInternalVlan:{} PwTransportVlan:{} ", vlanId,
725 getDefaultInternalVlan(), getPwTransportVlan());
726 return false;
727 }
728
729 if (interfaceService.inUse(vid)) {
730 log.warn("Vlan id {} value is already in use on a configured "
731 + "interface in the system", vlanId);
732 return false;
733 }
734 return true;
735 }
736
737 /**
738 * Returns the VlanId assigned internally by default to unconfigured ports.
739 *
740 * @return the default internal vlan id
741 */
742 public VlanId getDefaultInternalVlan() {
743 return VlanId.vlanId((short) defaultInternalVlan);
744 }
745
746 /**
747 * Returns the Vlan id used to transport pseudowire traffic across the
748 * network.
749 *
750 * @return the pseudowire transport vlan id
751 */
752 public VlanId getPwTransportVlan() {
753 return VlanId.vlanId((short) pwTransportVlan);
Charles Chan873661e2017-11-30 15:37:50 -0800754 }
755
sangho27462c62015-05-14 00:39:53 -0700756 @Override
757 public List<Tunnel> getTunnels() {
758 return tunnelHandler.getTunnels();
759 }
760
761 @Override
sanghobd812f82015-06-29 14:58:47 -0700762 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
763 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700764 }
765
766 @Override
sanghobd812f82015-06-29 14:58:47 -0700767 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700768 for (Policy policy: policyHandler.getPolicies()) {
769 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
770 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
771 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
772 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700773 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700774 }
775 }
776 }
sanghobd812f82015-06-29 14:58:47 -0700777 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700778 }
779
780 @Override
sanghobd812f82015-06-29 14:58:47 -0700781 public PolicyHandler.Result removePolicy(Policy policy) {
782 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700783 }
784
785 @Override
sanghobd812f82015-06-29 14:58:47 -0700786 public PolicyHandler.Result createPolicy(Policy policy) {
787 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700788 }
789
790 @Override
791 public List<Policy> getPolicies() {
792 return policyHandler.getPolicies();
793 }
794
Saurav Das07c74602016-04-27 18:35:50 -0700795 @Override
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700796 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
797 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700798 }
799
800 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800801 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700802 return l2TunnelHandler.getL2Tunnels();
803 }
804
805 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800806 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700807 return l2TunnelHandler.getL2Policies();
808 }
809
Saurav Dasdebcf882018-04-06 20:16:01 -0700810 @Override
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700811 @Deprecated
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700812 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
813
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700814 // get both added and pending pseudowires
815 List<L2TunnelDescription> pseudowires = new ArrayList<>();
816 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
817 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700818 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700819
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700820 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
821
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700822 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
823 L2TunnelHandler.Result res;
824 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
825 res = addPseudowire(pw);
826 if (res != L2TunnelHandler.Result.SUCCESS) {
827 log.error("Pseudowire with id {} can not be instantiated !", res);
828 retRes = res;
829 }
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700830 }
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700831
832 return retRes;
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700833 }
834
835 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800836 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700837 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700838 }
839
840 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800841 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700842 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700843 }
844
845 @Override
Saurav Das07c74602016-04-27 18:35:50 -0700846 public void rerouteNetwork() {
847 cfgListener.configureNetwork();
Saurav Das07c74602016-04-27 18:35:50 -0700848 }
849
Charles Chand7844e52016-10-20 17:02:44 -0700850 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800851 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
852 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart61e24e12017-11-30 18:23:42 -0800853 deviceConfiguration.getRouters().forEach(device ->
854 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chand7844e52016-10-20 17:02:44 -0700855 return deviceSubnetMap;
856 }
857
Saurav Das62ae6792017-05-15 15:34:25 -0700858
859 @Override
860 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
861 if (defaultRoutingHandler != null) {
862 return defaultRoutingHandler.getCurrentEmcpSpgMap();
863 } else {
864 return null;
865 }
866 }
867
868 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700869 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDstNextObjStore() {
Saurav Das261c3002017-06-13 15:35:54 -0700870 if (dsNextObjStore != null) {
871 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700872 } else {
873 return ImmutableMap.of();
874 }
875 }
876
Saurav Dasfbe74572017-08-03 18:30:35 -0700877 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700878 public ImmutableMap<VlanNextObjectiveStoreKey, Integer> getVlanNextObjStore() {
879 if (vlanNextObjStore != null) {
880 return ImmutableMap.copyOf(vlanNextObjStore.entrySet());
881 } else {
882 return ImmutableMap.of();
883 }
884 }
885
886 @Override
887 public ImmutableMap<PortNextObjectiveStoreKey, Integer> getPortNextObjStore() {
888 if (portNextObjStore != null) {
889 return ImmutableMap.copyOf(portNextObjStore.entrySet());
890 } else {
891 return ImmutableMap.of();
892 }
893 }
894
895 @Override
896 public ImmutableMap<String, NextObjective> getPwInitNext() {
897 if (l2TunnelHandler != null) {
898 return l2TunnelHandler.getInitNext();
899 } else {
900 return ImmutableMap.of();
901 }
902 }
903
904 @Override
905 public ImmutableMap<String, NextObjective> getPwTermNext() {
906 if (l2TunnelHandler != null) {
907 return l2TunnelHandler.getTermNext();
908 } else {
909 return ImmutableMap.of();
910 }
911 }
912
913 @Override
914 public void invalidateNextObj(int nextId) {
915 if (dsNextObjStore != null) {
916 dsNextObjStore.entrySet().forEach(e -> {
917 if (e.getValue().nextId() == nextId) {
918 dsNextObjStore.remove(e.getKey());
919 }
920 });
921 }
922 if (vlanNextObjStore != null) {
923 vlanNextObjStore.entrySet().forEach(e -> {
924 if (e.getValue() == nextId) {
925 vlanNextObjStore.remove(e.getKey());
926 }
927 });
928 }
929 if (portNextObjStore != null) {
930 portNextObjStore.entrySet().forEach(e -> {
931 if (e.getValue() == nextId) {
932 portNextObjStore.remove(e.getKey());
933 }
934 });
935 }
936 if (mcastHandler != null) {
937 mcastHandler.removeNextId(nextId);
938 }
939 if (l2TunnelHandler != null) {
940 l2TunnelHandler.removeNextId(nextId);
941 }
942 if (xconnectService != null) {
943 xconnectService.removeNextId(nextId);
944 }
945 }
946
947 @Override
Saurav Dasfbe74572017-08-03 18:30:35 -0700948 public void verifyGroups(DeviceId id) {
949 DefaultGroupHandler gh = groupHandlerMap.get(id);
950 if (gh != null) {
951 gh.triggerBucketCorrector();
952 }
953 }
954
Saurav Das6430f412018-01-25 09:49:01 -0800955 @Override
956 public ImmutableMap<Link, Boolean> getSeenLinks() {
957 return linkHandler.getSeenLinks();
958 }
959
960 @Override
961 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
962 return linkHandler.getDownedPorts();
963 }
964
Pier Luigi0f9635b2018-01-15 18:06:43 +0100965 @Override
966 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700967 return mcastHandler.getNextIds(mcastIp);
Pier Luigi0f9635b2018-01-15 18:06:43 +0100968 }
969
970 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +0100971 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigi0f9635b2018-01-15 18:06:43 +0100972 return mcastHandler.getMcastRoles(mcastIp);
973 }
974
975 @Override
Piere99511d2018-04-19 16:47:06 +0200976 public Map<McastRoleStoreKey, McastRole> getMcastRoles(IpAddress mcastIp, ConnectPoint sourcecp) {
977 return mcastHandler.getMcastRoles(mcastIp, sourcecp);
978 }
979
980 @Override
Pier Luigi0f9635b2018-01-15 18:06:43 +0100981 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
982 return mcastHandler.getMcastPaths(mcastIp);
983 }
984
Pierdb27b8d2018-04-17 16:29:56 +0200985 @Override
Pier71c55772018-04-17 17:25:22 +0200986 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
987 ConnectPoint sourcecp) {
988 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
989 }
990
991 @Override
Pierdb27b8d2018-04-17 16:29:56 +0200992 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
993 return mcastHandler.getMcastLeaders(mcastIp);
994 }
995
Charles Chanb13e0702018-04-17 18:56:53 -0700996 @Override
997 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
998 return defaultRoutingHandler == null ? ImmutableMap.of() :
999 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
1000 }
1001
1002 @Override
1003 public Map<DeviceId, Boolean> getShouldProgramCache() {
1004 return defaultRoutingHandler == null ? ImmutableMap.of() :
1005 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
1006 }
1007
Charles Chan8d316332018-06-19 20:31:57 -07001008 @Override
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301009 public boolean shouldProgram(DeviceId deviceId) {
1010 return defaultRoutingHandler.shouldProgram(deviceId);
1011 }
1012
1013 @Override
Ray Milkeyb85de082017-04-05 09:42:04 -07001014 public ApplicationId appId() {
1015 return appId;
1016 }
1017
1018 /**
1019 * Returns the device configuration.
1020 *
1021 * @return device configuration
1022 */
1023 public DeviceConfiguration deviceConfiguration() {
1024 return deviceConfiguration;
1025 }
1026
1027 /**
Saurav Das261c3002017-06-13 15:35:54 -07001028 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -07001029 * Used to keep track on MPLS group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001030 *
1031 * @return next objective ID store
1032 */
Saurav Das261c3002017-06-13 15:35:54 -07001033 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
1034 dsNextObjStore() {
1035 return dsNextObjStore;
Ray Milkeyb85de082017-04-05 09:42:04 -07001036 }
1037
1038 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001039 * Per device next objective ID store with (device id + vlanid) as key.
1040 * Used to keep track on L2 flood group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001041 *
1042 * @return vlan next object store
1043 */
1044 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
1045 return vlanNextObjStore;
1046 }
1047
1048 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001049 * Per device next objective ID store with (device id + port + treatment + meta) as key.
1050 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001051 *
1052 * @return port next object store.
1053 */
1054 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
1055 return portNextObjStore;
1056 }
1057
1058 /**
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001059 * Per port dummy VLAN ID store with (connect point + ip address) as key.
1060 * Used to keep track on dummy VLAN ID allocation.
1061 *
1062 * @return dummy vlan id store.
1063 */
1064 public EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId> dummyVlanIdStore() {
1065 return dummyVlanIdStore;
1066 }
1067
1068 /**
Saurav Das261c3002017-06-13 15:35:54 -07001069 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
1070 * labeled packets should be programmed or not.
Pier Ventre7a78de22016-10-31 15:00:01 -07001071 *
1072 * @return MPLS-ECMP value
1073 */
1074 public boolean getMplsEcmp() {
1075 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
1076 .getConfig(this.appId, SegmentRoutingAppConfig.class);
1077 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
1078 }
1079
1080 /**
sangho80f1f892015-05-19 11:57:42 -07001081 * Returns the tunnel object with the tunnel ID.
1082 *
1083 * @param tunnelId Tunnel ID
1084 * @return Tunnel reference
1085 */
sangho27462c62015-05-14 00:39:53 -07001086 public Tunnel getTunnel(String tunnelId) {
1087 return tunnelHandler.getTunnel(tunnelId);
1088 }
1089
Charles Chan8d316332018-06-19 20:31:57 -07001090 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +01001091 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chan098ca202018-05-01 11:50:20 -07001092 VlanId untaggedVlanId = interfaceService.getUntaggedVlanId(connectPoint);
1093 VlanId nativeVlanId = interfaceService.getNativeVlanId(connectPoint);
Charles Chand9265a32017-06-16 15:19:24 -07001094 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
1095 }
1096
Charles Chan8d316332018-06-19 20:31:57 -07001097 @Override
1098 public Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001099 SegmentRoutingDeviceConfig deviceConfig =
1100 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1101 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
1102 }
Charles Chan8d316332018-06-19 20:31:57 -07001103
1104 @Override
Saurav Dasec683dc2018-04-27 18:42:30 -07001105 public Optional<PortNumber> getPairLocalPort(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001106 SegmentRoutingDeviceConfig deviceConfig =
1107 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1108 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
1109 }
1110
1111 /**
Charles Chanf0ae41e2017-08-23 13:55:39 -07001112 * Returns locations of given resolved route.
1113 *
1114 * @param resolvedRoute resolved route
1115 * @return locations of nexthop. Might be empty if next hop is not found
1116 */
1117 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
1118 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
1119 return Optional.ofNullable(hostService.getHost(hostId))
1120 .map(Host::locations).orElse(Sets.newHashSet())
1121 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
1122 }
1123
1124 /**
Charles Chan90772a72017-02-08 15:52:08 -08001125 * Returns vlan port map of given device.
1126 *
1127 * @param deviceId device id
1128 * @return vlan-port multimap
1129 */
1130 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
1131 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
1132
1133 interfaceService.getInterfaces().stream()
1134 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
1135 .forEach(intf -> {
1136 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan3ed34d82017-06-22 18:03:14 -07001137 intf.vlanTagged().forEach(vlanTagged ->
1138 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
1139 );
Charles Chan90772a72017-02-08 15:52:08 -08001140 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
1141 });
1142 vlanPortMap.removeAll(VlanId.NONE);
1143
1144 return vlanPortMap;
1145 }
1146
1147 /**
Charles Chan10b0fb72017-02-02 16:20:42 -08001148 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -08001149 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -07001150 *
1151 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -08001152 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -08001153 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -07001154 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001155 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -07001156 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -08001157 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
1158 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -07001159 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -08001160 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -08001161 + "device {} not found", deviceId);
1162 return -1;
1163 }
1164 }
1165
1166 /**
1167 * Returns the next objective ID for the given portNumber, given the treatment.
1168 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -07001169 * would result in different objectives. If the next object does not exist,
1170 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -08001171 *
1172 * @param deviceId Device ID
1173 * @param portNum port number on device for which NextObjective is queried
1174 * @param treatment the actions to apply on the packets (should include outport)
1175 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -07001176 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -07001177 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -08001178 */
1179 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
1180 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -07001181 TrafficSelector meta,
1182 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -08001183 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1184 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -07001185 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -08001186 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -08001187 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
1188 + " not found", deviceId);
1189 return -1;
1190 }
1191 }
1192
Saurav Das62ae6792017-05-15 15:34:25 -07001193 /**
1194 * Returns the group handler object for the specified device id.
1195 *
1196 * @param devId the device identifier
1197 * @return the groupHandler object for the device id, or null if not found
1198 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001199 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Das62ae6792017-05-15 15:34:25 -07001200 return groupHandlerMap.get(devId);
1201 }
1202
1203 /**
Saurav Dasfbe74572017-08-03 18:30:35 -07001204 * Returns the default routing handler object.
1205 *
1206 * @return the default routing handler object
1207 */
1208 public DefaultRoutingHandler getRoutingHandler() {
1209 return defaultRoutingHandler;
1210 }
1211
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001212 /**
1213 * Returns new dummy VLAN ID.
1214 * Dummy VLAN ID should be unique in each connect point.
1215 *
1216 * @param cp connect point
1217 * @param ipAddress IP address
1218 * @return new dummy VLAN ID. Returns VlanId.NONE if no VLAN ID is available.
1219 */
1220 public synchronized VlanId allocateDummyVlanId(ConnectPoint cp, IpAddress ipAddress) {
1221 Set<VlanId> usedVlanId = Sets.union(getVlanPortMap(cp.deviceId()).keySet(),
1222 dummyVlanIdStore.entrySet().stream().filter(entry ->
1223 (entry.getKey()).connectPoint().equals(cp))
1224 .map(Map.Entry::getValue)
1225 .collect(Collectors.toSet()));
1226
1227 VlanId dummyVlanId = IntStream.range(MIN_DUMMY_VLAN_ID, MAX_DUMMY_VLAN_ID).mapToObj(
1228 i -> VlanId.vlanId((short) (i & 0xFFFF))
1229 ).filter(vlanId -> !usedVlanId.contains(vlanId)).findFirst().orElse(VlanId.NONE);
1230
1231 if (!dummyVlanId.equals(VlanId.NONE)) {
1232 this.dummyVlanIdStore.put(new DummyVlanIdStoreKey(cp, ipAddress), dummyVlanId);
1233 log.debug("Dummy VLAN ID {} is allocated to {}, {}", dummyVlanId, cp, ipAddress);
1234 } else {
1235 log.error("Failed to allocate dummy VLAN ID for {}, {}", cp, ipAddress);
1236 }
1237 return dummyVlanId;
1238 }
1239
1240
sangho80f11cb2015-04-01 13:05:26 -07001241 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -07001242 @Override
1243 public void process(PacketContext context) {
Charles Chan77cdde42018-05-08 21:35:50 -07001244 packetExecutor.execute(() -> processPacketInternal(context));
1245 }
sangho80f11cb2015-04-01 13:05:26 -07001246
Charles Chan77cdde42018-05-08 21:35:50 -07001247 private void processPacketInternal(PacketContext context) {
sangho80f11cb2015-04-01 13:05:26 -07001248 if (context.isHandled()) {
1249 return;
1250 }
1251
1252 InboundPacket pkt = context.inPacket();
1253 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -08001254
1255 if (ethernet == null) {
1256 return;
1257 }
1258
Saurav Das261c3002017-06-13 15:35:54 -07001259 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1260 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001261 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -07001262 log.warn("Received unexpected ARP packet on {}",
1263 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -07001264 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001265 return;
sangho80f11cb2015-04-01 13:05:26 -07001266 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -08001267 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1268 //ipHandler.addToPacketBuffer(ipv4Packet);
1269 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1270 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001271 } else {
Charles Chand041ad82017-01-13 17:20:44 -08001272 // NOTE: We don't support IP learning at this moment so this
1273 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001274 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001275 }
Pier Ventreb6a7f342016-11-26 21:05:22 -08001276 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1277 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -08001278 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -08001279 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -08001280 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1281 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1282 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1283 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1284 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1285 } else {
Saurav Das62ae6792017-05-15 15:34:25 -07001286 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -07001287 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -08001288 }
1289 } else {
1290 // NOTE: We don't support IP learning at this moment so this
1291 // is not necessary. Also it causes duplication of DHCPv6 packets.
1292 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1293 }
sangho80f11cb2015-04-01 13:05:26 -07001294 }
1295 }
1296 }
1297
sangho80f11cb2015-04-01 13:05:26 -07001298 private class InternalEventHandler implements Runnable {
Charles Chan52003922018-04-05 16:31:15 -07001299 private Event event;
1300
1301 InternalEventHandler(Event event) {
1302 this.event = event;
1303 }
1304
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001305 @Override
sangho80f11cb2015-04-01 13:05:26 -07001306 public void run() {
Charles Chan52003922018-04-05 16:31:15 -07001307 try {
1308 // TODO We should also change SR routing and PW to listen to TopologyEvents
1309 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1310 event.type() == LinkEvent.Type.LINK_UPDATED) {
1311 linkHandler.processLinkAdded((Link) event.subject());
1312 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1313 linkHandler.processLinkRemoved((Link) event.subject());
1314 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1315 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1316 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1317 DeviceId deviceId = ((Device) event.subject()).id();
1318 if (deviceService.isAvailable(deviceId)) {
1319 log.info("** DEVICE UP Processing device event {} "
1320 + "for available device {}",
1321 event.type(), ((Device) event.subject()).id());
1322 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001323 } else {
Charles Chanb8f3af52018-08-29 21:21:51 -07001324 if (event.type() == DeviceEvent.Type.DEVICE_ADDED) {
1325 // Note: For p4 devices, the device will be added but unavailable at the beginning.
1326 // The device will later on being marked as available once the pipeline is pushed
1327 // to the device.
1328 log.info("** DEVICE ADDED but unavailable. Ignore");
1329 return;
1330 }
Charles Chan52003922018-04-05 16:31:15 -07001331 log.info(" ** DEVICE DOWN Processing device event {}"
1332 + " for unavailable device {}",
1333 event.type(), ((Device) event.subject()).id());
1334 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001335 }
Charles Chan52003922018-04-05 16:31:15 -07001336 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1337 // typically these calls come when device is added first time
1338 // so port filtering rules are handled at the device_added event.
1339 // port added calls represent all ports on the device,
1340 // enabled or not.
1341 log.trace("** PORT ADDED {}/{} -> {}",
1342 ((DeviceEvent) event).subject().id(),
1343 ((DeviceEvent) event).port().number(),
1344 event.type());
1345 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1346 // these calls happen for every subsequent event
1347 // ports enabled, disabled, switch goes away, comes back
1348 log.info("** PORT UPDATED {}/{} -> {}",
1349 event.subject(),
1350 ((DeviceEvent) event).port(),
1351 event.type());
Saurav Dasec683dc2018-04-27 18:42:30 -07001352 processPortUpdatedInternal(((Device) event.subject()),
Charles Chan52003922018-04-05 16:31:15 -07001353 ((DeviceEvent) event).port());
1354 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1355 // Process topology event, needed for all modules relying on
1356 // topology service for path computation
1357 TopologyEvent topologyEvent = (TopologyEvent) event;
1358 log.info("Processing topology event {}, topology age {}, reasons {}",
1359 event.type(), topologyEvent.subject().time(),
1360 topologyEvent.reasons().size());
1361 topologyHandler.processTopologyChange(topologyEvent.reasons());
1362 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1363 hostHandler.processHostAddedEvent((HostEvent) event);
1364 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1365 hostHandler.processHostMovedEvent((HostEvent) event);
1366 routeHandler.processHostMovedEvent((HostEvent) event);
1367 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1368 hostHandler.processHostRemovedEvent((HostEvent) event);
1369 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1370 hostHandler.processHostUpdatedEvent((HostEvent) event);
1371 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1372 routeHandler.processRouteAdded((RouteEvent) event);
1373 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1374 routeHandler.processRouteUpdated((RouteEvent) event);
1375 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1376 routeHandler.processRouteRemoved((RouteEvent) event);
1377 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1378 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1379 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1380 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1381 event.type() == McastEvent.Type.SINKS_ADDED ||
1382 event.type() == McastEvent.Type.SINKS_REMOVED ||
1383 event.type() == McastEvent.Type.ROUTE_ADDED ||
1384 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1385 mcastHandler.processMcastEvent((McastEvent) event);
1386 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1387 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1388 Class configClass = netcfgEvent.configClass();
1389 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1390 appCfgHandler.processAppConfigAdded(netcfgEvent);
1391 log.info("App config event .. configuring network");
1392 cfgListener.configureNetwork();
1393 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1394 log.info("Segment Routing Device Config added for {}", event.subject());
1395 cfgListener.configureNetwork();
1396 } else if (configClass.equals(XConnectConfig.class)) {
1397 xConnectHandler.processXConnectConfigAdded(netcfgEvent);
1398 } else if (configClass.equals(InterfaceConfig.class)) {
1399 log.info("Interface Config added for {}", event.subject());
1400 cfgListener.configureNetwork();
1401 } else {
1402 log.error("Unhandled config class: {}", configClass);
1403 }
1404 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1405 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1406 Class configClass = netcfgEvent.configClass();
1407 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1408 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1409 log.info("App config event .. configuring network");
1410 cfgListener.configureNetwork();
1411 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1412 log.info("Segment Routing Device Config updated for {}", event.subject());
1413 createOrUpdateDeviceConfiguration();
1414 } else if (configClass.equals(XConnectConfig.class)) {
1415 xConnectHandler.processXConnectConfigUpdated(netcfgEvent);
1416 } else if (configClass.equals(InterfaceConfig.class)) {
1417 log.info("Interface Config updated for {}", event.subject());
1418 createOrUpdateDeviceConfiguration();
1419 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1420 (InterfaceConfig) netcfgEvent.prevConfig().get());
1421 } else {
1422 log.error("Unhandled config class: {}", configClass);
1423 }
1424 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1425 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1426 Class configClass = netcfgEvent.configClass();
1427 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1428 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1429 log.info("App config event .. configuring network");
1430 cfgListener.configureNetwork();
1431 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1432 // TODO Handle sr device config removal
1433 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
1434 } else if (configClass.equals(XConnectConfig.class)) {
1435 xConnectHandler.processXConnectConfigRemoved(netcfgEvent);
1436 } else if (configClass.equals(InterfaceConfig.class)) {
1437 // TODO Handle interface removal
1438 log.info("InterfaceConfig removal is not handled in current implementation");
1439 } else {
1440 log.error("Unhandled config class: {}", configClass);
1441 }
Saurav Das00e553b2018-04-21 17:19:48 -07001442 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1443 MastershipEvent me = (MastershipEvent) event;
1444 DeviceId deviceId = me.subject();
1445 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1446 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1447 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1448 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1449 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1450 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan52003922018-04-05 16:31:15 -07001451 } else {
1452 log.warn("Unhandled event type: {}", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001453 }
Charles Chan52003922018-04-05 16:31:15 -07001454 } catch (Exception e) {
1455 log.error("SegmentRouting event handler thread thrown an exception: {}",
1456 e.getMessage(), e);
sangho80f11cb2015-04-01 13:05:26 -07001457 }
sangho80f11cb2015-04-01 13:05:26 -07001458 }
1459 }
1460
Saurav Dase6c448a2018-01-18 12:07:33 -08001461 void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001462 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001463
1464 // NOTE: Punt ARP/NDP even when the device is not configured.
1465 // Host learning without network config is required for CORD config generator.
1466 routingRulePopulator.populateIpPunts(device.id());
1467 routingRulePopulator.populateArpNdpPunts(device.id());
1468
Charles Chan319d1a22015-11-03 10:42:14 -08001469 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001470 log.warn("Device configuration unavailable. Device {} will be "
1471 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001472 return;
1473 }
Charles Chan72779502016-04-23 17:36:10 -07001474 processDeviceAddedInternal(device.id());
1475 }
1476
1477 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001478 // Irrespective of whether the local is a MASTER or not for this device,
1479 // we need to create a SR-group-handler instance. This is because in a
1480 // multi-instance setup, any instance can initiate forwarding/next-objectives
1481 // for any switch (even if this instance is a SLAVE or not even connected
1482 // to the switch). To handle this, a default-group-handler instance is necessary
1483 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001484 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1485 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001486 DefaultGroupHandler groupHandler;
1487 try {
1488 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001489 createGroupHandler(deviceId,
1490 appId,
1491 deviceConfiguration,
1492 linkService,
1493 flowObjectiveService,
1494 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001495 } catch (DeviceConfigNotFoundException e) {
1496 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1497 return;
1498 }
Saurav Das2b6a00f2017-12-05 15:00:23 -08001499 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan72779502016-04-23 17:36:10 -07001500 deviceId);
1501 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001502 }
Saurav Dasb149be12016-06-07 10:08:06 -07001503
Charles Chan72779502016-04-23 17:36:10 -07001504 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001505 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -07001506 xConnectHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001507 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001508 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001509 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001510 }
Charles Chan82ab1932016-01-30 23:22:37 -08001511
Charles Chandebfea32016-10-24 14:52:01 -07001512 appCfgHandler.init(deviceId);
Charles Chan074fae62018-04-29 18:11:37 -07001513 hostEventExecutor.execute(() -> hostHandler.init(deviceId));
1514 routeEventExecutor.execute(() -> routeHandler.init(deviceId));
sangho80f11cb2015-04-01 13:05:26 -07001515 }
1516
Saurav Dasc3604f12016-03-23 11:22:49 -07001517 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001518 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001519 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan873661e2017-11-30 15:37:50 -08001520 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan10b0fb72017-02-02 16:20:42 -08001521 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001522 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001523 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001524 portNextObjStore.entrySet().stream()
1525 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001526 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Dase6c448a2018-01-18 12:07:33 -08001527 linkHandler.processDeviceRemoved(device);
Charles Chan17d75d82017-06-15 00:44:51 -07001528
Saurav Dasfbe74572017-08-03 18:30:35 -07001529 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1530 if (gh != null) {
1531 gh.shutdown();
1532 }
Saurav Das62ae6792017-05-15 15:34:25 -07001533 // Note that a switch going down is associated with all of its links
1534 // going down as well, but it is treated as a single switch down event
Saurav Dasdebcf882018-04-06 20:16:01 -07001535 // while the link-downs are ignored. We cannot rely on the ordering of
1536 // events - i.e we cannot expect all link-downs to come before the
1537 // switch down - so we purge all seen-links for the switch before
1538 // handling route-path changes for the switch-down
Saurav Das62ae6792017-05-15 15:34:25 -07001539 defaultRoutingHandler
Saurav Dasdc7f2752018-03-18 21:28:15 -07001540 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Das6430f412018-01-25 09:49:01 -08001541 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan82f19972016-05-17 13:13:55 -07001542 xConnectHandler.removeDevice(device.id());
Saurav Das97241862018-02-14 14:14:54 -08001543
1544 // Cleanup all internal groupHandler stores for this device. Should be
1545 // done after all rerouting or rehashing has been completed
1546 groupHandlerMap.entrySet()
1547 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001548 }
1549
Saurav Das49368392018-04-23 18:42:12 -07001550 /**
1551 * Purge the destinationSet nextObjective store of entries with this device
1552 * as key. Erases app-level knowledge of hashed groups in this device.
1553 *
1554 * @param devId the device identifier
1555 */
Saurav Das00e553b2018-04-21 17:19:48 -07001556 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Das49368392018-04-23 18:42:12 -07001557 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Das00e553b2018-04-21 17:19:48 -07001558 dsNextObjStore.entrySet().stream()
1559 .filter(entry -> entry.getKey().deviceId().equals(devId))
1560 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1561 }
1562
Saurav Dasec683dc2018-04-27 18:42:30 -07001563 private void processPortUpdatedInternal(Device device, Port port) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001564 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1565 log.warn("Device configuration uploading. Not handling port event for"
1566 + "dev: {} port: {}", device.id(), port.number());
1567 return;
1568 }
Saurav Dasf9332192017-02-18 14:05:44 -08001569
Saurav Dasec683dc2018-04-27 18:42:30 -07001570 if (interfaceService.isConfigured(new ConnectPoint(device.id(), port.number()))) {
1571 lastEdgePortEvent = Instant.now();
1572 }
1573
Saurav Dasf9332192017-02-18 14:05:44 -08001574 if (!mastershipService.isLocalMaster(device.id())) {
1575 log.debug("Not master for dev:{} .. not handling port updated event"
1576 + "for port {}", device.id(), port.number());
1577 return;
1578 }
Saurav Dasec683dc2018-04-27 18:42:30 -07001579 processPortUpdated(device.id(), port);
1580 }
Saurav Dasf9332192017-02-18 14:05:44 -08001581
Saurav Dasec683dc2018-04-27 18:42:30 -07001582 /**
1583 * Adds or remove filtering rules for the given switchport. If switchport is
1584 * an edge facing port, additionally handles host probing and broadcast
1585 * rules. Must be called by local master of device.
1586 *
1587 * @param deviceId the device identifier
1588 * @param port the port to update
1589 */
1590 void processPortUpdated(DeviceId deviceId, Port port) {
Saurav Dasf9332192017-02-18 14:05:44 -08001591 // first we handle filtering rules associated with the port
1592 if (port.isEnabled()) {
1593 log.info("Switchport {}/{} enabled..programming filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001594 deviceId, port.number());
1595 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001596 } else {
1597 log.info("Switchport {}/{} disabled..removing filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001598 deviceId, port.number());
1599 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001600 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001601
1602 // portUpdated calls are for ports that have gone down or up. For switch
1603 // to switch ports, link-events should take care of any re-routing or
1604 // group editing necessary for port up/down. Here we only process edge ports
1605 // that are already configured.
Saurav Dasec683dc2018-04-27 18:42:30 -07001606 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
Charles Chan098ca202018-05-01 11:50:20 -07001607 VlanId untaggedVlan = interfaceService.getUntaggedVlanId(cp);
1608 VlanId nativeVlan = interfaceService.getNativeVlanId(cp);
1609 Set<VlanId> taggedVlans = interfaceService.getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001610
Saurav Das3fb28272017-03-04 16:08:47 -08001611 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001612 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasec683dc2018-04-27 18:42:30 -07001613 + "dev/port: {}/{}", deviceId, port.number());
Saurav Dasf0f592d2016-11-18 15:21:57 -08001614 return;
1615 }
Saurav Das3fb28272017-03-04 16:08:47 -08001616 if (untaggedVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001617 processEdgePort(deviceId, port, untaggedVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001618 }
1619 if (nativeVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001620 processEdgePort(deviceId, port, nativeVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001621 }
1622 if (!taggedVlans.isEmpty()) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001623 taggedVlans.forEach(tag -> processEdgePort(deviceId, port, tag, false));
Saurav Das3fb28272017-03-04 16:08:47 -08001624 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001625 }
1626
Saurav Dasec683dc2018-04-27 18:42:30 -07001627 private void processEdgePort(DeviceId deviceId, Port port, VlanId vlanId,
Saurav Das3fb28272017-03-04 16:08:47 -08001628 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001629 boolean portUp = port.isEnabled();
1630 if (portUp) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001631 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001632 port.number(), vlanId);
Charles Chan074fae62018-04-29 18:11:37 -07001633 hostEventExecutor.execute(() -> hostHandler.processPortUp(new ConnectPoint(deviceId, port.number())));
Saurav Dasf0f592d2016-11-18 15:21:57 -08001634 } else {
Saurav Dasec683dc2018-04-27 18:42:30 -07001635 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001636 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001637 }
1638
Saurav Dasec683dc2018-04-27 18:42:30 -07001639 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001640 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001641 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001642 } else {
1643 log.warn("Group handler not found for dev:{}. Not handling edge port"
Saurav Dasec683dc2018-04-27 18:42:30 -07001644 + " {} event for port:{}", deviceId,
Saurav Dasf0f592d2016-11-18 15:21:57 -08001645 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001646 }
1647 }
sangho27462c62015-05-14 00:39:53 -07001648
Charles Chan8ca5a122017-10-20 16:06:55 -07001649 private void createOrUpdateDeviceConfiguration() {
1650 if (deviceConfiguration == null) {
Saurav Dase321cff2018-02-09 17:26:45 -08001651 log.info("Creating new DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001652 deviceConfiguration = new DeviceConfiguration(this);
1653 } else {
Saurav Dase321cff2018-02-09 17:26:45 -08001654 log.info("Updating DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001655 deviceConfiguration.updateConfig();
1656 }
1657 }
1658
Charles Chanfbcb8812018-04-18 18:41:05 -07001659 private void createOrUpdateDefaultRoutingHandler() {
1660 if (defaultRoutingHandler == null) {
1661 log.info("Creating new DefaultRoutingHandler");
1662 defaultRoutingHandler = new DefaultRoutingHandler(this);
1663 } else {
1664 log.info("Updating DefaultRoutingHandler");
1665 defaultRoutingHandler.update(this);
1666 }
1667 }
1668
Pier Ventreb6a7f342016-11-26 21:05:22 -08001669 /**
1670 * Registers the given connect point with the NRS, this is necessary
1671 * to receive the NDP and ARP packets from the NRS.
1672 *
1673 * @param portToRegister connect point to register
1674 */
1675 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001676 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001677 portToRegister,
1678 neighbourHandler,
1679 appId
1680 );
1681 }
1682
Charles Chan72f556a2015-10-05 17:50:33 -07001683 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001684 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001685 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001686
Charles Chanb7f75ac2016-01-11 18:28:54 -08001687 /**
1688 * Constructs the internal network config listener.
1689 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001690 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001691 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001692 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001693 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001694 }
1695
Charles Chanb7f75ac2016-01-11 18:28:54 -08001696 /**
1697 * Reads network config and initializes related data structure accordingly.
1698 */
Charles Chan873661e2017-11-30 15:37:50 -08001699 void configureNetwork() {
Saurav Dase321cff2018-02-09 17:26:45 -08001700 log.info("Configuring network ...");
Andrea Campanella060cad82018-04-17 12:09:34 +02001701
1702 // Setting handling of network configuration events completable future
1703 // The completable future is needed because of the async behaviour of the configureNetwork,
1704 // listener registration and event arrival
1705 // Enables us to buffer the events and execute them when the configure network is done.
1706 networkConfigCompletion = new CompletableFuture<>();
1707
1708 // add a small delay to absorb multiple network config added notifications
1709 if (!programmingScheduled.get()) {
1710 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1711 programmingScheduled.set(true);
1712 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1713 }
1714
Charles Chan8ca5a122017-10-20 16:06:55 -07001715 createOrUpdateDeviceConfiguration();
Charles Chane7c61022015-10-07 14:21:45 -07001716
Charles Chan46fdfaf2016-11-09 20:51:44 -08001717 arpHandler = new ArpHandler(srManager);
1718 icmpHandler = new IcmpHandler(srManager);
1719 ipHandler = new IpHandler(srManager);
1720 routingRulePopulator = new RoutingRulePopulator(srManager);
Charles Chanfbcb8812018-04-18 18:41:05 -07001721 createOrUpdateDefaultRoutingHandler();
Charles Chane7c61022015-10-07 14:21:45 -07001722
1723 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1724 groupHandlerMap, tunnelStore);
1725 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1726 flowObjectiveService,
1727 tunnelHandler, policyStore);
Andrea Campanella060cad82018-04-17 12:09:34 +02001728 networkConfigCompletion.complete(true);
1729
Charles Chan72779502016-04-23 17:36:10 -07001730 mcastHandler.init();
Andrea Campanella060cad82018-04-17 12:09:34 +02001731
Charles Chane7c61022015-10-07 14:21:45 -07001732 }
1733
Charles Chan72f556a2015-10-05 17:50:33 -07001734 @Override
1735 public void event(NetworkConfigEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001736 if (mainEventExecutor == null) {
1737 return;
1738 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001739 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
1740 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1741 switch (event.type()) {
1742 case CONFIG_ADDED:
1743 case CONFIG_UPDATED:
1744 case CONFIG_REMOVED:
1745 log.trace("Schedule Network Config event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001746 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1747 mainEventExecutor.execute(new InternalEventHandler(event));
1748 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001749 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001750 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001751 break;
1752 default:
1753 break;
Charles Chan72f556a2015-10-05 17:50:33 -07001754 }
1755 }
Saurav Das261c3002017-06-13 15:35:54 -07001756
Charles Chanc4a68c32018-01-03 16:26:32 -08001757 @Override
1758 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase321cff2018-02-09 17:26:45 -08001759 if (event.type() == CONFIG_REGISTERED ||
1760 event.type() == CONFIG_UNREGISTERED) {
1761 log.debug("Ignore event {} due to type mismatch", event);
1762 return false;
Charles Chanc4a68c32018-01-03 16:26:32 -08001763 }
Saurav Dase321cff2018-02-09 17:26:45 -08001764
1765 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1766 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1767 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -08001768 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase321cff2018-02-09 17:26:45 -08001769 log.debug("Ignore event {} due to class mismatch", event);
1770 return false;
1771 }
1772
1773 return true;
Charles Chanc4a68c32018-01-03 16:26:32 -08001774 }
1775
Saurav Das261c3002017-06-13 15:35:54 -07001776 private final class ConfigChange implements Runnable {
1777 @Override
1778 public void run() {
1779 programmingScheduled.set(false);
Saurav Dase321cff2018-02-09 17:26:45 -08001780 log.info("Reacting to config changes after buffer delay");
Saurav Das261c3002017-06-13 15:35:54 -07001781 for (Device device : deviceService.getDevices()) {
1782 processDeviceAdded(device);
1783 }
1784 defaultRoutingHandler.startPopulationProcess();
1785 }
1786 }
Charles Chan72f556a2015-10-05 17:50:33 -07001787 }
Charles Chanf4586112015-11-09 16:37:23 -08001788
Charles Chan3f4aca62018-03-21 16:57:47 -07001789 private class InternalLinkListener implements LinkListener {
1790 @Override
1791 public void event(LinkEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001792 if (mainEventExecutor == null) {
1793 return;
1794 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001795 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1796 event.type() == LinkEvent.Type.LINK_UPDATED ||
1797 event.type() == LinkEvent.Type.LINK_REMOVED) {
1798 log.trace("Schedule Link event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001799 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1800 mainEventExecutor.execute(new InternalEventHandler(event));
1801 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001802 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001803 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001804 }
1805 }
1806 }
1807
1808 private class InternalDeviceListener implements DeviceListener {
1809 @Override
1810 public void event(DeviceEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001811 if (mainEventExecutor == null) {
1812 return;
1813 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001814 switch (event.type()) {
1815 case DEVICE_ADDED:
1816 case PORT_UPDATED:
1817 case PORT_ADDED:
1818 case DEVICE_UPDATED:
1819 case DEVICE_AVAILABILITY_CHANGED:
1820 log.trace("Schedule Device event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001821 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1822 mainEventExecutor.execute(new InternalEventHandler(event));
1823 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001824 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001825 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001826 break;
1827 default:
1828 }
1829 }
1830 }
1831
1832 private class InternalTopologyListener implements TopologyListener {
1833 @Override
1834 public void event(TopologyEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001835 if (mainEventExecutor == null) {
1836 return;
1837 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001838 switch (event.type()) {
1839 case TOPOLOGY_CHANGED:
1840 log.trace("Schedule Topology event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001841 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1842 mainEventExecutor.execute(new InternalEventHandler(event));
1843 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001844 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001845 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001846 break;
1847 default:
1848 }
1849 }
1850 }
1851
Charles Chanf4586112015-11-09 16:37:23 -08001852 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001853 @Override
1854 public void event(HostEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001855 if (hostEventExecutor == null) {
1856 return;
1857 }
Charles Chanf4586112015-11-09 16:37:23 -08001858 switch (event.type()) {
1859 case HOST_ADDED:
Charles Chanf4586112015-11-09 16:37:23 -08001860 case HOST_MOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001861 case HOST_REMOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001862 case HOST_UPDATED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001863 log.trace("Schedule Host event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001864 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chanf4586112015-11-09 16:37:23 -08001865 break;
1866 default:
1867 log.warn("Unsupported host event type: {}", event.type());
1868 break;
1869 }
1870 }
1871 }
1872
Charles Chanc91c8782016-03-30 17:54:24 -07001873 private class InternalMcastListener implements McastListener {
1874 @Override
1875 public void event(McastEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001876 if (mcastEventExecutor == null) {
1877 return;
1878 }
Charles Chanc91c8782016-03-30 17:54:24 -07001879 switch (event.type()) {
Pier1f87aca2018-03-14 16:47:32 -07001880 case SOURCES_ADDED:
1881 case SOURCES_REMOVED:
1882 case SINKS_ADDED:
1883 case SINKS_REMOVED:
Charles Chanc91c8782016-03-30 17:54:24 -07001884 case ROUTE_REMOVED:
Pierdb27b8d2018-04-17 16:29:56 +02001885 case ROUTE_ADDED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001886 log.trace("Schedule Mcast event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001887 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi6786b922018-02-02 16:19:11 +01001888 break;
Charles Chanc91c8782016-03-30 17:54:24 -07001889 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07001890 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chanc91c8782016-03-30 17:54:24 -07001891 break;
1892 }
1893 }
1894 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001895
Charles Chandebfea32016-10-24 14:52:01 -07001896 private class InternalRouteEventListener implements RouteListener {
1897 @Override
1898 public void event(RouteEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001899 if (routeEventExecutor == null) {
1900 return;
1901 }
Charles Chandebfea32016-10-24 14:52:01 -07001902 switch (event.type()) {
1903 case ROUTE_ADDED:
Charles Chandebfea32016-10-24 14:52:01 -07001904 case ROUTE_UPDATED:
Charles Chandebfea32016-10-24 14:52:01 -07001905 case ROUTE_REMOVED:
Charles Chan910be6a2017-08-23 14:46:43 -07001906 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001907 log.trace("Schedule Route event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001908 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan910be6a2017-08-23 14:46:43 -07001909 break;
Charles Chandebfea32016-10-24 14:52:01 -07001910 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07001911 log.warn("Unsupported route event type: {}", event.type());
Charles Chandebfea32016-10-24 14:52:01 -07001912 break;
1913 }
1914 }
1915 }
Saurav Das62ae6792017-05-15 15:34:25 -07001916
Charles Chanfbcb8812018-04-18 18:41:05 -07001917 private class InternalMastershipListener implements MastershipListener {
1918 @Override
1919 public void event(MastershipEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001920 if (mainEventExecutor == null) {
1921 return;
1922 }
Charles Chanfbcb8812018-04-18 18:41:05 -07001923 switch (event.type()) {
Saurav Das00e553b2018-04-21 17:19:48 -07001924 case MASTER_CHANGED:
1925 log.debug("Mastership event: {}/{}", event.subject(),
1926 event.roleInfo());
1927 mainEventExecutor.execute(new InternalEventHandler(event));
1928 break;
1929 case BACKUPS_CHANGED:
1930 case SUSPENDED:
1931 default:
1932 log.debug("Mastership event type {} not handled", event.type());
1933 break;
Charles Chanfbcb8812018-04-18 18:41:05 -07001934 }
1935 }
1936 }
1937
Saurav Das00e553b2018-04-21 17:19:48 -07001938 class InternalClusterListener implements ClusterEventListener {
1939 private Instant lastClusterEvent = Instant.EPOCH;
1940
1941 long timeSinceLastClusterEvent() {
1942 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
1943 }
1944
1945 @Override
1946 public void event(ClusterEvent event) {
1947 switch (event.type()) {
1948 case INSTANCE_ACTIVATED:
1949 case INSTANCE_ADDED:
1950 case INSTANCE_READY:
1951 log.debug("Cluster event {} ignored", event.type());
1952 break;
1953 case INSTANCE_DEACTIVATED:
1954 case INSTANCE_REMOVED:
1955 log.info("** Cluster event {}", event.type());
1956 lastClusterEvent = Instant.now();
1957 break;
1958 default:
1959 break;
1960 }
1961
1962 }
1963
1964 }
1965
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001966 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1967 try {
1968 Set<Interface> intfs = conf.getInterfaces();
1969 Set<Interface> prevIntfs = prevConf.getInterfaces();
1970
1971 // Now we only handle one interface config at each port.
1972 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1973 log.warn("Interface update aborted - one at a time is allowed, " +
1974 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1975 return;
1976 }
1977
Andrea Campanella060cad82018-04-17 12:09:34 +02001978 //The system is in an incoherent state, abort
1979 if (defaultRoutingHandler == null) {
1980 log.warn("Interface update aborted, defaultRoutingHandler is null");
1981 return;
1982 }
1983
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001984 Interface intf = intfs.stream().findFirst().get();
1985 Interface prevIntf = prevIntfs.stream().findFirst().get();
1986
1987 DeviceId deviceId = intf.connectPoint().deviceId();
1988 PortNumber portNum = intf.connectPoint().port();
1989
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001990 removeSubnetConfig(prevIntf.connectPoint(),
1991 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1992 new HashSet<>(intf.ipAddressesList())));
1993
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001994 if (!prevIntf.vlanNative().equals(VlanId.NONE)
1995 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
1996 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1997 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
1998 // Update filtering objective and L2IG group bucket
1999 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
2000 } else {
2001 // RemoveVlanNative
2002 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
2003 }
2004 }
2005
2006 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
2007 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2008 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2009 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
2010 // Update filtering objective and L2IG group bucket
2011 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
2012 } else {
2013 // RemoveVlanUntagged
2014 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
2015 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002016 }
2017
2018 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2019 // RemoveVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002020 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
2021 .filter(i -> !intf.vlanUntagged().equals(i))
2022 .filter(i -> !intf.vlanNative().equals(i))
2023 .forEach(vlanId -> updateVlanConfigInternal(
2024 deviceId, portNum, vlanId, false, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002025 }
2026
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002027 if (!intf.vlanNative().equals(VlanId.NONE)
2028 && !prevIntf.vlanNative().equals(intf.vlanNative())
2029 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2030 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
2031 // Update filtering objective and L2IG group bucket
2032 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
2033 } else {
2034 // AddVlanNative
2035 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
2036 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002037 }
2038
2039 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2040 // AddVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002041 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
2042 .filter(i -> !prevIntf.vlanUntagged().equals(i))
2043 .filter(i -> !prevIntf.vlanNative().equals(i))
2044 .forEach(vlanId -> updateVlanConfigInternal(
2045 deviceId, portNum, vlanId, false, true)
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002046 );
2047 }
2048
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002049 if (!intf.vlanUntagged().equals(VlanId.NONE)
2050 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2051 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
2052 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
2053 // Update filtering objective and L2IG group bucket
2054 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
2055 } else {
2056 // AddVlanUntagged
2057 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
2058 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002059 }
2060 addSubnetConfig(prevIntf.connectPoint(),
2061 Sets.difference(new HashSet<>(intf.ipAddressesList()),
2062 new HashSet<>(prevIntf.ipAddressesList())));
2063 } catch (ConfigException e) {
2064 log.error("Error in configuration");
2065 }
2066 }
2067
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002068 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
2069 VlanId vlanId, boolean pushVlan) {
2070 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2071 if (grpHandler == null) {
2072 log.warn("Failed to retrieve group handler for device {}", deviceId);
2073 return;
2074 }
2075
2076 // Update filtering objective for a single port
2077 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
2078 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
2079
2080 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
2081 // Update L2IG bucket of the port
2082 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
2083 } else {
2084 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2085 }
2086 }
2087
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002088 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
2089 VlanId vlanId, boolean pushVlan, boolean install) {
2090 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2091 if (grpHandler == null) {
2092 log.warn("Failed to retrieve group handler for device {}", deviceId);
2093 return;
2094 }
2095
2096 // Update filtering objective for a single port
2097 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
2098
2099 // Update filtering objective for multicast ingress port
2100 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
2101
2102 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
2103
2104 if (nextId != -1 && !install) {
2105 // Update next objective for a single port as an output port
2106 // Remove a single port from L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002107 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002108 // Remove L2 Bridging rule and L3 Unicast rule to the host
Charles Chan074fae62018-04-29 18:11:37 -07002109 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2110 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002111 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
2112 // only if there is no port configured on that VLAN ID
2113 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
2114 // Remove broadcast forwarding rule for the VLAN
2115 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2116 // Remove L2FG for VLAN
2117 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
2118 } else {
2119 // Remove L2IG of the port
2120 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
2121 }
2122 } else if (install) {
2123 if (nextId != -1) {
2124 // Add a single port to L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002125 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002126 } else {
2127 // Create L2FG for VLAN
2128 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
2129 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2130 }
Charles Chan074fae62018-04-29 18:11:37 -07002131 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2132 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002133 } else {
2134 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2135 }
2136 }
2137
2138 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2139 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2140 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2141
2142 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2143 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2144 .filter(intf -> !intf.connectPoint().equals(cp))
2145 .flatMap(intf -> intf.ipAddressesList().stream())
2146 .collect(Collectors.toSet());
2147 // 1. Partial subnet population
2148 // Remove routing rules for removed subnet from previous configuration,
2149 // which does not also exist in other interfaces in the same device
2150 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2151 .map(InterfaceIpAddress::subnetAddress)
2152 .collect(Collectors.toSet());
2153
2154 defaultRoutingHandler.revokeSubnet(
2155 ipPrefixSet.stream()
2156 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2157 .collect(Collectors.toSet()));
2158
2159 // 2. Interface IP punts
2160 // Remove IP punts for old Intf address
2161 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2162 .map(InterfaceIpAddress::ipAddress)
2163 .collect(Collectors.toSet());
2164 ipAddressSet.stream()
2165 .map(InterfaceIpAddress::ipAddress)
2166 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2167 .forEach(interfaceIpAddress ->
2168 routingRulePopulator.revokeSingleIpPunts(
2169 cp.deviceId(), interfaceIpAddress));
2170
2171 // 3. Host unicast routing rule
2172 // Remove unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002173 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002174 }
2175
2176 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2177 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2178 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2179
2180 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2181 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2182 .filter(intf -> !intf.connectPoint().equals(cp))
2183 .flatMap(intf -> intf.ipAddressesList().stream())
2184 .collect(Collectors.toSet());
2185 // 1. Partial subnet population
2186 // Add routing rules for newly added subnet, which does not also exist in
2187 // other interfaces in the same device
2188 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2189 .map(InterfaceIpAddress::subnetAddress)
2190 .collect(Collectors.toSet());
2191
2192 defaultRoutingHandler.populateSubnet(
2193 Collections.singleton(cp),
2194 ipPrefixSet.stream()
2195 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2196 .collect(Collectors.toSet()));
2197
2198 // 2. Interface IP punts
2199 // Add IP punts for new Intf address
2200 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2201 .map(InterfaceIpAddress::ipAddress)
2202 .collect(Collectors.toSet());
2203 ipAddressSet.stream()
2204 .map(InterfaceIpAddress::ipAddress)
2205 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2206 .forEach(interfaceIpAddress ->
2207 routingRulePopulator.populateSingleIpPunts(
2208 cp.deviceId(), interfaceIpAddress));
2209
2210 // 3. Host unicast routing rule
2211 // Add unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002212 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002213 }
sangho80f11cb2015-04-01 13:05:26 -07002214}