blob: acef076bf08777610f4df7bb9adbdeb12fa66db6 [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;
Ray Milkeye96d0de2018-11-02 17:12:21 -0700168import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SYMMETRIC_PROBING;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700169import static org.onosproject.segmentrouting.OsgiPropertyConstants.PW_TRANSPORT_VLAN_DEFAULT;
170import static org.onosproject.segmentrouting.OsgiPropertyConstants.RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
171import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
172import static org.onosproject.segmentrouting.OsgiPropertyConstants.SINGLE_HOMED_DOWN_DEFAULT;
Ray Milkeye96d0de2018-11-02 17:12:21 -0700173import static org.onosproject.segmentrouting.OsgiPropertyConstants.SYMMETRIC_PROBING_DEFAULT;
Charles Chand6d25332016-02-26 22:19:52 -0800174
Charles Chanb7f75ac2016-01-11 18:28:54 -0800175/**
176 * Segment routing manager.
177 */
Ray Milkey05edbfc2018-10-23 14:23:16 -0700178@Component(
179 immediate = true,
180 service = SegmentRoutingService.class,
181 property = {
182 PROP_ACTIVE_PROBING + ":Boolean=" + ACTIVE_PROBING_DEFAULT,
183 PROP_SINGLE_HOMED_DOWN + ":Boolean=" + SINGLE_HOMED_DOWN_DEFAULT,
184 PROP_RESPOND_TO_UNKNOWN_HOSTS + ":Boolean=" + RESPOND_TO_UNKNOWN_HOSTS_DEFAULT,
185 PROP_ROUTE_DOUBLE_TAGGED_HOSTS + ":Boolean=" + ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT,
186 PROP_DEFAULT_INTERNAL_VLAN + ":Integer=" + DEFAULT_INTERNAL_VLAN_DEFAULT,
187 PROP_PW_TRANSPORT_VLAN + ":Integer=" + PW_TRANSPORT_VLAN_DEFAULT,
Ray Milkeye96d0de2018-11-02 17:12:21 -0700188 PROP_SYMMETRIC_PROBING + ":Boolean=" + SYMMETRIC_PROBING_DEFAULT
Ray Milkey05edbfc2018-10-23 14:23:16 -0700189 }
190)
sangho27462c62015-05-14 00:39:53 -0700191public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700192
Charles Chan46fdfaf2016-11-09 20:51:44 -0800193 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan910be6a2017-08-23 14:46:43 -0700194 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sangho80f11cb2015-04-01 13:05:26 -0700195
Ray Milkey2bd24a92018-08-17 14:54:17 -0700196 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700197 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700198
Ray Milkey2bd24a92018-08-17 14:54:17 -0700199 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventreb6a7f342016-11-26 21:05:22 -0800200 private NeighbourResolutionService neighbourResolutionService;
201
Ray Milkey2bd24a92018-08-17 14:54:17 -0700202 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100203 public CoreService coreService;
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 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700207
Ray Milkey2bd24a92018-08-17 14:54:17 -0700208 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700209 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700210
Ray Milkey2bd24a92018-08-17 14:54:17 -0700211 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chanc6bcdf92018-06-01 16:33:48 -0700212 HostProbingService probingService;
Charles Chan873661e2017-11-30 15:37:50 -0800213
Ray Milkey2bd24a92018-08-17 14:54:17 -0700214 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100215 public DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700216
Ray Milkey2bd24a92018-08-17 14:54:17 -0700217 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Saurav Dase6c448a2018-01-18 12:07:33 -0800218 DeviceAdminService deviceAdminService;
219
Ray Milkey2bd24a92018-08-17 14:54:17 -0700220 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800221 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700222
Ray Milkey2bd24a92018-08-17 14:54:17 -0700223 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100224 public LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700225
Ray Milkey2bd24a92018-08-17 14:54:17 -0700226 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800227 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700228
Ray Milkey2bd24a92018-08-17 14:54:17 -0700229 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800230 public StorageService storageService;
Charles Chandebfea32016-10-24 14:52:01 -0700231
Ray Milkey2bd24a92018-08-17 14:54:17 -0700232 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100233 public MulticastRouteService multicastRouteService;
Charles Chandebfea32016-10-24 14:52:01 -0700234
Ray Milkey2bd24a92018-08-17 14:54:17 -0700235 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan83163812018-01-09 13:45:07 -0800236 public TopologyService topologyService;
Charles Chandebfea32016-10-24 14:52:01 -0700237
Ray Milkey2bd24a92018-08-17 14:54:17 -0700238 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700239 RouteService routeService;
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 NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800243
Ray Milkey2bd24a92018-08-17 14:54:17 -0700244 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800245 public InterfaceService interfaceService;
246
Ray Milkey2bd24a92018-08-17 14:54:17 -0700247 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi580fd8a2018-01-16 10:47:50 +0100248 public ClusterService clusterService;
249
Ray Milkey2bd24a92018-08-17 14:54:17 -0700250 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pierdb27b8d2018-04-17 16:29:56 +0200251 public WorkPartitionService workPartitionService;
Pier Luigi580fd8a2018-01-16 10:47:50 +0100252
Ray Milkey2bd24a92018-08-17 14:54:17 -0700253 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700254 public LeadershipService leadershipService;
255
Ray Milkey2bd24a92018-08-17 14:54:17 -0700256 @Reference(cardinality = ReferenceCardinality.OPTIONAL)
Charles Chan8d316332018-06-19 20:31:57 -0700257 public XconnectService xconnectService;
258
Ray Milkey05edbfc2018-10-23 14:23:16 -0700259 /** Enable active probing to discover dual-homed hosts. */
260 boolean activeProbing = ACTIVE_PROBING_DEFAULT;
Charles Chan873661e2017-11-30 15:37:50 -0800261
Ray Milkeye96d0de2018-11-02 17:12:21 -0700262 /** Enable only send probe on the same port number of the pair device. */
263 boolean symmetricProbing = SYMMETRIC_PROBING_DEFAULT;
Mayank Tiwarif7942402018-10-29 18:27:35 -0400264
Ray Milkey05edbfc2018-10-23 14:23:16 -0700265 /** Enable administratively taking down single-homed hosts. */
266 boolean singleHomedDown = SINGLE_HOMED_DOWN_DEFAULT;
Saurav Dasec683dc2018-04-27 18:42:30 -0700267
Ray Milkey05edbfc2018-10-23 14:23:16 -0700268 /** Enable this to respond to ARP/NDP requests from unknown hosts. */
269 boolean respondToUnknownHosts = RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700270
Ray Milkey05edbfc2018-10-23 14:23:16 -0700271 /** Program flows and groups to pop and route double tagged hosts. */
272 boolean routeDoubleTaggedHosts = ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
Charles Chan4eb73bb2018-07-19 14:55:31 -0700273
Ray Milkey05edbfc2018-10-23 14:23:16 -0700274 /** internal vlan assigned by default to unconfigured ports. */
275 private int defaultInternalVlan = DEFAULT_INTERNAL_VLAN_DEFAULT;
Saurav Das9bf49582018-08-13 15:34:26 -0700276
Ray Milkey05edbfc2018-10-23 14:23:16 -0700277 /** vlan used for transport of pseudowires between switches. */
278 private int pwTransportVlan = PW_TRANSPORT_VLAN_DEFAULT;
Saurav Das9bf49582018-08-13 15:34:26 -0700279
Charles Chandebfea32016-10-24 14:52:01 -0700280 ArpHandler arpHandler = null;
281 IcmpHandler icmpHandler = null;
282 IpHandler ipHandler = null;
283 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700284 ApplicationId appId;
285 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700286
Charles Chandebfea32016-10-24 14:52:01 -0700287 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700288 private TunnelHandler tunnelHandler = null;
289 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700290 private InternalPacketProcessor processor = null;
291 private InternalLinkListener linkListener = null;
292 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700293 private AppConfigHandler appCfgHandler = null;
Pier Luigi69f774d2018-02-28 12:10:50 +0100294 public XConnectHandler xConnectHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800295 McastHandler mcastHandler = null;
296 HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800297 private RouteHandler routeHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800298 LinkHandler linkHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800299 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800300 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigid8a15162018-02-15 16:33:08 +0100301 private TopologyHandler topologyHandler = null;
Charles Chan82ab1932016-01-30 23:22:37 -0800302 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700303 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
304 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700305 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigid8a15162018-02-15 16:33:08 +0100306 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chanfbcb8812018-04-18 18:41:05 -0700307 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
Saurav Das00e553b2018-04-21 17:19:48 -0700308 final InternalClusterListener clusterListener = new InternalClusterListener();
Andrea Campanella060cad82018-04-17 12:09:34 +0200309 //Completable future for network configuration process to buffer config events handling during activation
310 private CompletableFuture<Boolean> networkConfigCompletion = null;
Ray Milkey521e4392018-11-16 15:15:14 -0800311 private final Object networkConfigCompletionLock = new Object();
Charles Chan39b75522018-04-21 00:44:29 -0700312 private List<Event> queuedEvents = new CopyOnWriteArrayList<>();
sangho80f11cb2015-04-01 13:05:26 -0700313
Charles Chan52003922018-04-05 16:31:15 -0700314 // Handles device, link, topology and network config events
Charles Chanfbcb8812018-04-18 18:41:05 -0700315 private ScheduledExecutorService mainEventExecutor;
sangho80f11cb2015-04-01 13:05:26 -0700316
Charles Chanfbcb8812018-04-18 18:41:05 -0700317 // Handles host, route and mcast events respectively
318 private ScheduledExecutorService hostEventExecutor;
319 private ScheduledExecutorService routeEventExecutor;
320 private ScheduledExecutorService mcastEventExecutor;
Charles Chan77cdde42018-05-08 21:35:50 -0700321 private ExecutorService packetExecutor;
Charles Chan52003922018-04-05 16:31:15 -0700322
323 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chanb7f75ac2016-01-11 18:28:54 -0800324 /**
Saurav Das261c3002017-06-13 15:35:54 -0700325 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700326 * Used to keep track on MPLS group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800327 */
Charles Chan873661e2017-11-30 15:37:50 -0800328 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das261c3002017-06-13 15:35:54 -0700329 dsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800330 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700331 * Per device next objective ID store with (device id + vlanid) as key.
332 * Used to keep track on L2 flood group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800333 */
Charles Chan873661e2017-11-30 15:37:50 -0800334 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800335 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800336 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700337 * Per device next objective ID store with (device id + port + treatment + meta) as key.
338 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800339 */
Charles Chan873661e2017-11-30 15:37:50 -0800340 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800341 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800342
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700343 /**
344 * Per port dummy VLAN ID store with (connect point + ip address) as key.
345 * Used to keep track on dummy VLAN ID allocation.
346 */
347 private EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId>
348 dummyVlanIdStore = null;
349
Saurav Das2d94d312015-11-24 23:21:05 -0800350 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
351 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700352
Saurav Das261c3002017-06-13 15:35:54 -0700353 private AtomicBoolean programmingScheduled = new AtomicBoolean();
354
Charles Chanc91c8782016-03-30 17:54:24 -0700355 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700356 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
357 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700358 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700359 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800360 public SegmentRoutingDeviceConfig createConfig() {
361 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700362 }
363 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800364
Charles Chanc91c8782016-03-30 17:54:24 -0700365 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700366 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
367 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700368 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800369 @Override
370 public SegmentRoutingAppConfig createConfig() {
371 return new SegmentRoutingAppConfig();
372 }
373 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800374
Charles Chan82f19972016-05-17 13:13:55 -0700375 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
376 new ConfigFactory<ApplicationId, XConnectConfig>(
377 SubjectFactories.APP_SUBJECT_FACTORY,
378 XConnectConfig.class, "xconnect") {
379 @Override
380 public XConnectConfig createConfig() {
381 return new XConnectConfig();
382 }
383 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800384
Charles Chanc91c8782016-03-30 17:54:24 -0700385 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700386 new ConfigFactory<ApplicationId, McastConfig>(
387 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700388 McastConfig.class, "multicast") {
389 @Override
390 public McastConfig createConfig() {
391 return new McastConfig();
392 }
393 };
394
Charles Chan1963f4f2016-02-18 14:22:42 -0800395 /**
396 * Segment Routing App ID.
397 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800398 public static final String APP_NAME = "org.onosproject.segmentrouting";
Saurav Das7c305372015-10-28 12:39:42 -0700399
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700400 /**
401 * Minumum and maximum value of dummy VLAN ID to be allocated.
402 */
403 public static final int MIN_DUMMY_VLAN_ID = 2;
404 public static final int MAX_DUMMY_VLAN_ID = 4093;
405
Saurav Dasec683dc2018-04-27 18:42:30 -0700406 Instant lastEdgePortEvent = Instant.EPOCH;
407
sangho80f11cb2015-04-01 13:05:26 -0700408 @Activate
Charles Chan873661e2017-11-30 15:37:50 -0800409 protected void activate(ComponentContext context) {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800410 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700411
Charles Chanfbcb8812018-04-18 18:41:05 -0700412 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-main", "%d", log));
413 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-host", "%d", log));
414 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-route", "%d", log));
415 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-mcast", "%d", log));
Charles Chan77cdde42018-05-08 21:35:50 -0700416 packetExecutor = Executors.newSingleThreadExecutor(groupedThreads("sr-packet", "%d", log));
Charles Chanfbcb8812018-04-18 18:41:05 -0700417
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700418 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700419 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700420 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700421 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700422 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700423 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700424 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700425 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700426 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700427
Charles Chan10b0fb72017-02-02 16:20:42 -0800428 log.debug("Creating EC map vlannextobjectivestore");
429 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
430 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
431 vlanNextObjStore = vlanNextObjMapBuilder
432 .withName("vlannextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700433 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700434 .withTimestampProvider((k, v) -> new WallClockTimestamp())
435 .build();
436
Saurav Das2d94d312015-11-24 23:21:05 -0800437 log.debug("Creating EC map subnetnextobjectivestore");
438 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
439 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
440 portNextObjStore = portNextObjMapBuilder
441 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700442 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800443 .withTimestampProvider((k, v) -> new WallClockTimestamp())
444 .build();
445
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700446 EventuallyConsistentMapBuilder<DummyVlanIdStoreKey, VlanId>
447 dummyVlanIdMapBuilder = storageService.eventuallyConsistentMapBuilder();
448 dummyVlanIdStore = dummyVlanIdMapBuilder
449 .withName("dummyvlanidstore")
450 .withSerializer(createSerializer())
451 .withTimestampProvider((k, v) -> new WallClockTimestamp())
452 .build();
453
sangho4a5c42a2015-05-20 22:16:38 -0700454 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
455 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700456 tunnelStore = tunnelMapBuilder
457 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700458 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700459 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700460 .build();
461
462 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
463 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700464 policyStore = policyMapBuilder
465 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700466 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700467 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700468 .build();
469
Saurav Dasc3604f12016-03-23 11:22:49 -0700470 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800471 "purgeOnDisconnection", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700472 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800473 "purgeOnDisconnection", "true");
Pier Luigib9632ba2017-01-12 18:14:58 -0800474 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
475 "requestInterceptsEnabled", "false");
Charles Chanc1909e12017-08-08 15:13:37 -0700476 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800477 "requestInterceptsEnabled", "false");
Charles Chan9597f8d2017-07-24 15:56:10 -0700478 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800479 "arpEnabled", "false");
Pier Luigi0ebeb312017-02-02 22:31:34 -0800480 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
481 "greedyLearningIpv6", "true");
Charles Chancf8ea472017-02-28 15:15:17 -0800482 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
483 "forceUnprovision", "true");
Charles Chanc7b73c72017-08-10 16:57:28 -0700484 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan4c95c0d2017-07-20 16:16:25 -0700485 "distributed", "true");
Charles Chan804772f2017-08-14 11:42:11 -0700486 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
487 "multihomingEnabled", "true");
Charles Chan0c9c19f2017-11-29 19:54:20 -0800488 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
489 "staleLinkAge", "15000");
490 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
491 "allowDuplicateIps", "false");
Yi Tsenga7da8d82018-08-19 03:09:54 +0800492 // For P4 switches
493 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Charles Chan02298512018-08-30 17:30:45 -0700494 "fallbackFlowPollFrequency", "4");
Yi Tsenga7da8d82018-08-19 03:09:54 +0800495 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Charles Chan02298512018-08-30 17:30:45 -0700496 "fallbackGroupPollFrequency", "3");
Charles Chan873661e2017-11-30 15:37:50 -0800497 compCfgService.registerProperties(getClass());
498 modified(context);
Saurav Dasc3604f12016-03-23 11:22:49 -0700499
Charles Chan2b078ae2015-10-14 11:24:40 -0700500 processor = new InternalPacketProcessor();
501 linkListener = new InternalLinkListener();
502 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700503 appCfgHandler = new AppConfigHandler(this);
504 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700505 mcastHandler = new McastHandler(this);
506 hostHandler = new HostHandler(this);
Saurav Dase6c448a2018-01-18 12:07:33 -0800507 linkHandler = new LinkHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700508 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800509 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800510 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigid8a15162018-02-15 16:33:08 +0100511 topologyHandler = new TopologyHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700512
Charles Chand6d25332016-02-26 22:19:52 -0800513 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700514 cfgService.registerConfigFactory(deviceConfigFactory);
515 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700516 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700517 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase321cff2018-02-09 17:26:45 -0800518 log.info("Configuring network before adding listeners");
Andrea Campanella060cad82018-04-17 12:09:34 +0200519
Charles Chan6961f222018-01-04 14:26:07 -0800520 cfgListener.configureNetwork();
521
Charles Chan82ab1932016-01-30 23:22:37 -0800522 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700523 packetService.addProcessor(processor, PacketProcessor.director(2));
524 linkService.addListener(linkListener);
525 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700526 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700527 routeService.addListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100528 topologyService.addListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700529 mastershipService.addListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700530 clusterService.addListener(clusterListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700531
Charles Chanc12c3d02018-03-09 15:53:44 -0800532 linkHandler.init();
Andreas Pantelopoulos75eef062018-01-11 07:53:48 -0800533 l2TunnelHandler.init();
534
Ray Milkey521e4392018-11-16 15:15:14 -0800535 synchronized (networkConfigCompletionLock) {
536 networkConfigCompletion.whenComplete((value, ex) -> {
537 //setting to null for easier fall through
538 networkConfigCompletion = null;
539 //process all queued events
540 queuedEvents.forEach(event -> {
541 mainEventExecutor.execute(new InternalEventHandler(event));
542 });
Andrea Campanella060cad82018-04-17 12:09:34 +0200543 });
Ray Milkey521e4392018-11-16 15:15:14 -0800544 }
Andrea Campanella060cad82018-04-17 12:09:34 +0200545
sangho80f11cb2015-04-01 13:05:26 -0700546 log.info("Started");
547 }
548
Saurav Dase6c448a2018-01-18 12:07:33 -0800549 KryoNamespace.Builder createSerializer() {
Jonathan Hart54541d12016-04-12 15:39:44 -0700550 return new KryoNamespace.Builder()
551 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700552 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800553 VlanNextObjectiveStoreKey.class,
554 DestinationSet.class,
Andreas Pantelopoulos92998c92018-05-29 13:11:14 -0700555 DestinationSet.DestinationSetType.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800556 NextNeighbors.class,
557 Tunnel.class,
558 DefaultTunnel.class,
559 Policy.class,
560 TunnelPolicy.class,
561 Policy.Type.class,
562 PortNextObjectiveStoreKey.class,
563 XConnectStoreKey.class,
564 L2Tunnel.class,
565 L2TunnelPolicy.class,
566 DefaultL2Tunnel.class,
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700567 DefaultL2TunnelPolicy.class,
568 DummyVlanIdStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700569 );
570 }
571
sangho80f11cb2015-04-01 13:05:26 -0700572 @Deactivate
573 protected void deactivate() {
Charles Chanfbcb8812018-04-18 18:41:05 -0700574 mainEventExecutor.shutdown();
575 hostEventExecutor.shutdown();
576 routeEventExecutor.shutdown();
577 mcastEventExecutor.shutdown();
Charles Chan77cdde42018-05-08 21:35:50 -0700578 packetExecutor.shutdown();
Charles Chanfbcb8812018-04-18 18:41:05 -0700579
Ray Milkeyaee4d3e2018-05-11 09:59:19 -0700580 mainEventExecutor = null;
581 hostEventExecutor = null;
582 routeEventExecutor = null;
583 mcastEventExecutor = null;
584 packetExecutor = null;
585
Charles Chan72f556a2015-10-05 17:50:33 -0700586 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700587 cfgService.unregisterConfigFactory(deviceConfigFactory);
588 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700589 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700590 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan873661e2017-11-30 15:37:50 -0800591 compCfgService.unregisterProperties(getClass(), false);
Charles Chan72f556a2015-10-05 17:50:33 -0700592
Charles Chanfd48c222017-06-19 00:43:31 -0700593 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700594 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700595 linkService.removeListener(linkListener);
596 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700597 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700598 routeService.removeListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100599 topologyService.removeListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700600 mastershipService.removeListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700601 clusterService.removeListener(clusterListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700602
Charles Chan2e71ef32017-02-23 15:44:08 -0800603 neighbourResolutionService.unregisterNeighbourHandlers(appId);
604
sangho80f11cb2015-04-01 13:05:26 -0700605 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700606 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700607 deviceListener = null;
Charles Chan05bb1702018-04-19 13:10:01 -0700608 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chan2b078ae2015-10-14 11:24:40 -0700609 groupHandlerMap.clear();
Saurav Das49368392018-04-23 18:42:12 -0700610 defaultRoutingHandler.shutdown();
Charles Chan2b078ae2015-10-14 11:24:40 -0700611
Saurav Das261c3002017-06-13 15:35:54 -0700612 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800613 vlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700614 portNextObjStore.destroy();
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700615 dummyVlanIdStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700616 tunnelStore.destroy();
617 policyStore.destroy();
Pier Luigi35dab3f2018-01-25 16:16:02 +0100618
619 mcastHandler.terminate();
sangho80f11cb2015-04-01 13:05:26 -0700620 log.info("Stopped");
621 }
622
Charles Chan873661e2017-11-30 15:37:50 -0800623 @Modified
624 private void modified(ComponentContext context) {
625 Dictionary<?, ?> properties = context.getProperties();
626 if (properties == null) {
627 return;
628 }
629
Ray Milkey48d10c02018-10-24 10:04:03 -0700630 String strActiveProbing = Tools.get(properties, PROP_ACTIVE_PROBING);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700631 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProbing);
Charles Chan873661e2017-11-30 15:37:50 -0800632 if (expectActiveProbing != activeProbing) {
633 activeProbing = expectActiveProbing;
634 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
635 }
Saurav Dasec683dc2018-04-27 18:42:30 -0700636
Ray Milkeye96d0de2018-11-02 17:12:21 -0700637
638 String strSymmetricProbing = Tools.get(properties, PROP_SYMMETRIC_PROBING);
639 boolean expectSymmetricProbing = Boolean.parseBoolean(strSymmetricProbing);
Mayank Tiwarif7942402018-10-29 18:27:35 -0400640 if (expectSymmetricProbing != symmetricProbing) {
641 symmetricProbing = expectSymmetricProbing;
642 log.info("{} symmetric probing", symmetricProbing ? "Enabling" : "Disabling");
643 }
644
Ray Milkey48d10c02018-10-24 10:04:03 -0700645 String strSingleHomedDown = Tools.get(properties, PROP_SINGLE_HOMED_DOWN);
Saurav Dasec683dc2018-04-27 18:42:30 -0700646 boolean expectSingleHomedDown = Boolean.parseBoolean(strSingleHomedDown);
647 if (expectSingleHomedDown != singleHomedDown) {
648 singleHomedDown = expectSingleHomedDown;
649 log.info("{} downing of single homed hosts for lost uplinks",
650 singleHomedDown ? "Enabling" : "Disabling");
651 if (singleHomedDown && linkHandler != null) {
652 hostService.getHosts().forEach(host -> host.locations()
653 .forEach(loc -> {
654 if (interfaceService.isConfigured(loc)) {
655 linkHandler.checkUplinksForHost(loc);
656 }
657 }));
658 } else {
659 log.warn("Disabling singleHomedDown does not re-enable already "
660 + "downed ports for single-homed hosts");
661 }
662 }
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700663
Ray Milkey48d10c02018-10-24 10:04:03 -0700664 String strRespondToUnknownHosts = Tools.get(properties, PROP_RESPOND_TO_UNKNOWN_HOSTS);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700665 boolean expectRespondToUnknownHosts = Boolean.parseBoolean(strRespondToUnknownHosts);
666 if (expectRespondToUnknownHosts != respondToUnknownHosts) {
667 respondToUnknownHosts = expectRespondToUnknownHosts;
668 log.info("{} responding to ARPs/NDPs from unknown hosts", respondToUnknownHosts ? "Enabling" : "Disabling");
669 }
Charles Chan4eb73bb2018-07-19 14:55:31 -0700670
Ray Milkey48d10c02018-10-24 10:04:03 -0700671 String strRouteDoubleTaggedHosts = Tools.get(properties, PROP_ROUTE_DOUBLE_TAGGED_HOSTS);
Charles Chanfbca55e2018-07-24 16:40:35 -0700672 boolean expectRouteDoubleTaggedHosts = Boolean.parseBoolean(strRouteDoubleTaggedHosts);
673 if (expectRouteDoubleTaggedHosts != routeDoubleTaggedHosts) {
674 routeDoubleTaggedHosts = expectRouteDoubleTaggedHosts;
675 log.info("{} routing for double tagged hosts", routeDoubleTaggedHosts ? "Enabling" : "Disabling");
Charles Chan4eb73bb2018-07-19 14:55:31 -0700676
Charles Chanfbca55e2018-07-24 16:40:35 -0700677 if (routeDoubleTaggedHosts) {
Charles Chan4eb73bb2018-07-19 14:55:31 -0700678 hostHandler.populateAllDoubleTaggedHost();
679 } else {
680 hostHandler.revokeAllDoubleTaggedHost();
681 }
682 }
Saurav Das9bf49582018-08-13 15:34:26 -0700683
Ray Milkey48d10c02018-10-24 10:04:03 -0700684 String strDefaultInternalVlan = Tools.get(properties, PROP_DEFAULT_INTERNAL_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700685 int defIntVlan = Integer.parseInt(strDefaultInternalVlan);
686 if (defIntVlan != defaultInternalVlan) {
687 if (canUseVlanId(defIntVlan)) {
688 log.warn("Default internal vlan value changed from {} to {}.. "
689 + "re-programming filtering rules, but NOT any groups already "
690 + "created with the former value", defaultInternalVlan, defIntVlan);
691 VlanId oldDefIntVlan = VlanId.vlanId((short) defaultInternalVlan);
692 defaultInternalVlan = defIntVlan;
693 routingRulePopulator
694 .updateSpecialVlanFilteringRules(true, oldDefIntVlan,
695 VlanId.vlanId((short) defIntVlan));
696 } else {
697 log.warn("Cannot change default internal vlan to unusable "
698 + "value {}", defIntVlan);
699 }
700 }
701
Ray Milkey48d10c02018-10-24 10:04:03 -0700702 String strPwTxpVlan = Tools.get(properties, PROP_PW_TRANSPORT_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700703 int pwTxpVlan = Integer.parseInt(strPwTxpVlan);
704 if (pwTxpVlan != pwTransportVlan) {
705 if (canUseVlanId(pwTxpVlan)) {
706 log.warn("Pseudowire transport vlan value changed from {} to {}.. "
707 + "re-programming filtering rules, but NOT any groups already "
708 + "created with the former value", pwTransportVlan,
709 pwTxpVlan);
710 VlanId oldPwTxpVlan = VlanId.vlanId((short) pwTransportVlan);
711 pwTransportVlan = pwTxpVlan;
712 routingRulePopulator
713 .updateSpecialVlanFilteringRules(false, oldPwTxpVlan,
714 VlanId.vlanId((short) pwTxpVlan));
715 } else {
716 log.warn("Cannot change pseudowire transport vlan to unusable "
717 + "value {}", pwTxpVlan);
718 }
719 }
720
721 }
722
723 /**
724 * Returns true if given vlan id is not being used in the system currently,
725 * either as one of the default system wide vlans or as one of the
726 * configured interface vlans.
727 *
728 * @param vlanId given vlan id
729 * @return true if vlan is not currently in use
730 */
731 public boolean canUseVlanId(int vlanId) {
732 if (vlanId >= 4095 || vlanId <= 1) {
733 log.error("Vlan id {} value is not in valid range 2 <--> 4094",
734 vlanId);
735 return false;
736 }
737
738 VlanId vid = VlanId.vlanId((short) vlanId);
739 if (getDefaultInternalVlan().equals(vid) || getPwTransportVlan().equals(vid)) {
740 log.warn("Vlan id {} value is already in use system-wide. "
741 + "DefaultInternalVlan:{} PwTransportVlan:{} ", vlanId,
742 getDefaultInternalVlan(), getPwTransportVlan());
743 return false;
744 }
745
746 if (interfaceService.inUse(vid)) {
747 log.warn("Vlan id {} value is already in use on a configured "
748 + "interface in the system", vlanId);
749 return false;
750 }
751 return true;
752 }
753
754 /**
755 * Returns the VlanId assigned internally by default to unconfigured ports.
756 *
757 * @return the default internal vlan id
758 */
759 public VlanId getDefaultInternalVlan() {
760 return VlanId.vlanId((short) defaultInternalVlan);
761 }
762
763 /**
764 * Returns the Vlan id used to transport pseudowire traffic across the
765 * network.
766 *
767 * @return the pseudowire transport vlan id
768 */
769 public VlanId getPwTransportVlan() {
770 return VlanId.vlanId((short) pwTransportVlan);
Charles Chan873661e2017-11-30 15:37:50 -0800771 }
772
sangho27462c62015-05-14 00:39:53 -0700773 @Override
774 public List<Tunnel> getTunnels() {
775 return tunnelHandler.getTunnels();
776 }
777
778 @Override
sanghobd812f82015-06-29 14:58:47 -0700779 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
780 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700781 }
782
783 @Override
sanghobd812f82015-06-29 14:58:47 -0700784 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700785 for (Policy policy: policyHandler.getPolicies()) {
786 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
787 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
788 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
789 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700790 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700791 }
792 }
793 }
sanghobd812f82015-06-29 14:58:47 -0700794 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700795 }
796
797 @Override
sanghobd812f82015-06-29 14:58:47 -0700798 public PolicyHandler.Result removePolicy(Policy policy) {
799 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700800 }
801
802 @Override
sanghobd812f82015-06-29 14:58:47 -0700803 public PolicyHandler.Result createPolicy(Policy policy) {
804 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700805 }
806
807 @Override
808 public List<Policy> getPolicies() {
809 return policyHandler.getPolicies();
810 }
811
Saurav Das07c74602016-04-27 18:35:50 -0700812 @Override
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700813 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
814 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700815 }
816
817 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800818 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700819 return l2TunnelHandler.getL2Tunnels();
820 }
821
822 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800823 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700824 return l2TunnelHandler.getL2Policies();
825 }
826
Saurav Dasdebcf882018-04-06 20:16:01 -0700827 @Override
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700828 @Deprecated
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700829 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
830
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700831 // get both added and pending pseudowires
832 List<L2TunnelDescription> pseudowires = new ArrayList<>();
833 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
834 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700835 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700836
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700837 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
838
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700839 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
840 L2TunnelHandler.Result res;
841 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
842 res = addPseudowire(pw);
843 if (res != L2TunnelHandler.Result.SUCCESS) {
844 log.error("Pseudowire with id {} can not be instantiated !", res);
845 retRes = res;
846 }
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700847 }
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700848
849 return retRes;
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700850 }
851
852 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800853 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700854 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700855 }
856
857 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800858 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700859 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700860 }
861
862 @Override
Saurav Das07c74602016-04-27 18:35:50 -0700863 public void rerouteNetwork() {
864 cfgListener.configureNetwork();
Saurav Das07c74602016-04-27 18:35:50 -0700865 }
866
Charles Chand7844e52016-10-20 17:02:44 -0700867 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800868 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
869 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart61e24e12017-11-30 18:23:42 -0800870 deviceConfiguration.getRouters().forEach(device ->
871 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chand7844e52016-10-20 17:02:44 -0700872 return deviceSubnetMap;
873 }
874
Saurav Das62ae6792017-05-15 15:34:25 -0700875
876 @Override
877 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
878 if (defaultRoutingHandler != null) {
879 return defaultRoutingHandler.getCurrentEmcpSpgMap();
880 } else {
881 return null;
882 }
883 }
884
885 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700886 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDstNextObjStore() {
Saurav Das261c3002017-06-13 15:35:54 -0700887 if (dsNextObjStore != null) {
888 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700889 } else {
890 return ImmutableMap.of();
891 }
892 }
893
Saurav Dasfbe74572017-08-03 18:30:35 -0700894 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700895 public ImmutableMap<VlanNextObjectiveStoreKey, Integer> getVlanNextObjStore() {
896 if (vlanNextObjStore != null) {
897 return ImmutableMap.copyOf(vlanNextObjStore.entrySet());
898 } else {
899 return ImmutableMap.of();
900 }
901 }
902
903 @Override
904 public ImmutableMap<PortNextObjectiveStoreKey, Integer> getPortNextObjStore() {
905 if (portNextObjStore != null) {
906 return ImmutableMap.copyOf(portNextObjStore.entrySet());
907 } else {
908 return ImmutableMap.of();
909 }
910 }
911
912 @Override
913 public ImmutableMap<String, NextObjective> getPwInitNext() {
914 if (l2TunnelHandler != null) {
915 return l2TunnelHandler.getInitNext();
916 } else {
917 return ImmutableMap.of();
918 }
919 }
920
921 @Override
922 public ImmutableMap<String, NextObjective> getPwTermNext() {
923 if (l2TunnelHandler != null) {
924 return l2TunnelHandler.getTermNext();
925 } else {
926 return ImmutableMap.of();
927 }
928 }
929
930 @Override
931 public void invalidateNextObj(int nextId) {
932 if (dsNextObjStore != null) {
933 dsNextObjStore.entrySet().forEach(e -> {
934 if (e.getValue().nextId() == nextId) {
935 dsNextObjStore.remove(e.getKey());
936 }
937 });
938 }
939 if (vlanNextObjStore != null) {
940 vlanNextObjStore.entrySet().forEach(e -> {
941 if (e.getValue() == nextId) {
942 vlanNextObjStore.remove(e.getKey());
943 }
944 });
945 }
946 if (portNextObjStore != null) {
947 portNextObjStore.entrySet().forEach(e -> {
948 if (e.getValue() == nextId) {
949 portNextObjStore.remove(e.getKey());
950 }
951 });
952 }
953 if (mcastHandler != null) {
954 mcastHandler.removeNextId(nextId);
955 }
956 if (l2TunnelHandler != null) {
957 l2TunnelHandler.removeNextId(nextId);
958 }
959 if (xconnectService != null) {
960 xconnectService.removeNextId(nextId);
961 }
962 }
963
964 @Override
Saurav Dasfbe74572017-08-03 18:30:35 -0700965 public void verifyGroups(DeviceId id) {
966 DefaultGroupHandler gh = groupHandlerMap.get(id);
967 if (gh != null) {
968 gh.triggerBucketCorrector();
969 }
970 }
971
Saurav Das6430f412018-01-25 09:49:01 -0800972 @Override
973 public ImmutableMap<Link, Boolean> getSeenLinks() {
974 return linkHandler.getSeenLinks();
975 }
976
977 @Override
978 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
979 return linkHandler.getDownedPorts();
980 }
981
Pier Luigi0f9635b2018-01-15 18:06:43 +0100982 @Override
983 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700984 return mcastHandler.getNextIds(mcastIp);
Pier Luigi0f9635b2018-01-15 18:06:43 +0100985 }
986
987 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +0100988 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigi0f9635b2018-01-15 18:06:43 +0100989 return mcastHandler.getMcastRoles(mcastIp);
990 }
991
992 @Override
Piere99511d2018-04-19 16:47:06 +0200993 public Map<McastRoleStoreKey, McastRole> getMcastRoles(IpAddress mcastIp, ConnectPoint sourcecp) {
994 return mcastHandler.getMcastRoles(mcastIp, sourcecp);
995 }
996
997 @Override
Pier Luigi0f9635b2018-01-15 18:06:43 +0100998 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
999 return mcastHandler.getMcastPaths(mcastIp);
1000 }
1001
Pierdb27b8d2018-04-17 16:29:56 +02001002 @Override
Pier71c55772018-04-17 17:25:22 +02001003 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
1004 ConnectPoint sourcecp) {
1005 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
1006 }
1007
1008 @Override
Pierdb27b8d2018-04-17 16:29:56 +02001009 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
1010 return mcastHandler.getMcastLeaders(mcastIp);
1011 }
1012
Charles Chanb13e0702018-04-17 18:56:53 -07001013 @Override
1014 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
1015 return defaultRoutingHandler == null ? ImmutableMap.of() :
1016 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
1017 }
1018
1019 @Override
1020 public Map<DeviceId, Boolean> getShouldProgramCache() {
1021 return defaultRoutingHandler == null ? ImmutableMap.of() :
1022 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
1023 }
1024
Charles Chan8d316332018-06-19 20:31:57 -07001025 @Override
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301026 public boolean shouldProgram(DeviceId deviceId) {
1027 return defaultRoutingHandler.shouldProgram(deviceId);
1028 }
1029
1030 @Override
Ray Milkeyb85de082017-04-05 09:42:04 -07001031 public ApplicationId appId() {
1032 return appId;
1033 }
1034
1035 /**
1036 * Returns the device configuration.
1037 *
1038 * @return device configuration
1039 */
1040 public DeviceConfiguration deviceConfiguration() {
1041 return deviceConfiguration;
1042 }
1043
1044 /**
Saurav Das261c3002017-06-13 15:35:54 -07001045 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -07001046 * Used to keep track on MPLS group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001047 *
1048 * @return next objective ID store
1049 */
Saurav Das261c3002017-06-13 15:35:54 -07001050 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
1051 dsNextObjStore() {
1052 return dsNextObjStore;
Ray Milkeyb85de082017-04-05 09:42:04 -07001053 }
1054
1055 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001056 * Per device next objective ID store with (device id + vlanid) as key.
1057 * Used to keep track on L2 flood group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001058 *
1059 * @return vlan next object store
1060 */
1061 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
1062 return vlanNextObjStore;
1063 }
1064
1065 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001066 * Per device next objective ID store with (device id + port + treatment + meta) as key.
1067 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001068 *
1069 * @return port next object store.
1070 */
1071 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
1072 return portNextObjStore;
1073 }
1074
1075 /**
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001076 * Per port dummy VLAN ID store with (connect point + ip address) as key.
1077 * Used to keep track on dummy VLAN ID allocation.
1078 *
1079 * @return dummy vlan id store.
1080 */
1081 public EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId> dummyVlanIdStore() {
1082 return dummyVlanIdStore;
1083 }
1084
1085 /**
Saurav Das261c3002017-06-13 15:35:54 -07001086 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
1087 * labeled packets should be programmed or not.
Pier Ventre7a78de22016-10-31 15:00:01 -07001088 *
1089 * @return MPLS-ECMP value
1090 */
1091 public boolean getMplsEcmp() {
1092 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
1093 .getConfig(this.appId, SegmentRoutingAppConfig.class);
1094 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
1095 }
1096
1097 /**
sangho80f1f892015-05-19 11:57:42 -07001098 * Returns the tunnel object with the tunnel ID.
1099 *
1100 * @param tunnelId Tunnel ID
1101 * @return Tunnel reference
1102 */
sangho27462c62015-05-14 00:39:53 -07001103 public Tunnel getTunnel(String tunnelId) {
1104 return tunnelHandler.getTunnel(tunnelId);
1105 }
1106
Charles Chan8d316332018-06-19 20:31:57 -07001107 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +01001108 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chan098ca202018-05-01 11:50:20 -07001109 VlanId untaggedVlanId = interfaceService.getUntaggedVlanId(connectPoint);
1110 VlanId nativeVlanId = interfaceService.getNativeVlanId(connectPoint);
Charles Chand9265a32017-06-16 15:19:24 -07001111 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
1112 }
1113
Charles Chan8d316332018-06-19 20:31:57 -07001114 @Override
1115 public Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001116 SegmentRoutingDeviceConfig deviceConfig =
1117 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1118 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
1119 }
Charles Chan8d316332018-06-19 20:31:57 -07001120
1121 @Override
Saurav Dasec683dc2018-04-27 18:42:30 -07001122 public Optional<PortNumber> getPairLocalPort(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001123 SegmentRoutingDeviceConfig deviceConfig =
1124 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1125 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
1126 }
1127
1128 /**
Charles Chanf0ae41e2017-08-23 13:55:39 -07001129 * Returns locations of given resolved route.
1130 *
1131 * @param resolvedRoute resolved route
1132 * @return locations of nexthop. Might be empty if next hop is not found
1133 */
1134 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
1135 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
1136 return Optional.ofNullable(hostService.getHost(hostId))
1137 .map(Host::locations).orElse(Sets.newHashSet())
1138 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
1139 }
1140
1141 /**
Charles Chan90772a72017-02-08 15:52:08 -08001142 * Returns vlan port map of given device.
1143 *
1144 * @param deviceId device id
1145 * @return vlan-port multimap
1146 */
1147 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
1148 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
1149
1150 interfaceService.getInterfaces().stream()
1151 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
1152 .forEach(intf -> {
1153 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan3ed34d82017-06-22 18:03:14 -07001154 intf.vlanTagged().forEach(vlanTagged ->
1155 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
1156 );
Charles Chan90772a72017-02-08 15:52:08 -08001157 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
1158 });
1159 vlanPortMap.removeAll(VlanId.NONE);
1160
1161 return vlanPortMap;
1162 }
1163
1164 /**
Charles Chan10b0fb72017-02-02 16:20:42 -08001165 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -08001166 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -07001167 *
1168 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -08001169 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -08001170 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -07001171 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001172 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -07001173 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -08001174 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
1175 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -07001176 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -08001177 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -08001178 + "device {} not found", deviceId);
1179 return -1;
1180 }
1181 }
1182
1183 /**
1184 * Returns the next objective ID for the given portNumber, given the treatment.
1185 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -07001186 * would result in different objectives. If the next object does not exist,
1187 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -08001188 *
1189 * @param deviceId Device ID
1190 * @param portNum port number on device for which NextObjective is queried
1191 * @param treatment the actions to apply on the packets (should include outport)
1192 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -07001193 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -07001194 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -08001195 */
1196 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
1197 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -07001198 TrafficSelector meta,
1199 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -08001200 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1201 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -07001202 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -08001203 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -08001204 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
1205 + " not found", deviceId);
1206 return -1;
1207 }
1208 }
1209
Saurav Das62ae6792017-05-15 15:34:25 -07001210 /**
1211 * Returns the group handler object for the specified device id.
1212 *
1213 * @param devId the device identifier
1214 * @return the groupHandler object for the device id, or null if not found
1215 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001216 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Das62ae6792017-05-15 15:34:25 -07001217 return groupHandlerMap.get(devId);
1218 }
1219
1220 /**
Saurav Dasfbe74572017-08-03 18:30:35 -07001221 * Returns the default routing handler object.
1222 *
1223 * @return the default routing handler object
1224 */
1225 public DefaultRoutingHandler getRoutingHandler() {
1226 return defaultRoutingHandler;
1227 }
1228
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001229 /**
1230 * Returns new dummy VLAN ID.
1231 * Dummy VLAN ID should be unique in each connect point.
1232 *
1233 * @param cp connect point
1234 * @param ipAddress IP address
1235 * @return new dummy VLAN ID. Returns VlanId.NONE if no VLAN ID is available.
1236 */
1237 public synchronized VlanId allocateDummyVlanId(ConnectPoint cp, IpAddress ipAddress) {
1238 Set<VlanId> usedVlanId = Sets.union(getVlanPortMap(cp.deviceId()).keySet(),
1239 dummyVlanIdStore.entrySet().stream().filter(entry ->
1240 (entry.getKey()).connectPoint().equals(cp))
1241 .map(Map.Entry::getValue)
1242 .collect(Collectors.toSet()));
1243
1244 VlanId dummyVlanId = IntStream.range(MIN_DUMMY_VLAN_ID, MAX_DUMMY_VLAN_ID).mapToObj(
1245 i -> VlanId.vlanId((short) (i & 0xFFFF))
1246 ).filter(vlanId -> !usedVlanId.contains(vlanId)).findFirst().orElse(VlanId.NONE);
1247
1248 if (!dummyVlanId.equals(VlanId.NONE)) {
1249 this.dummyVlanIdStore.put(new DummyVlanIdStoreKey(cp, ipAddress), dummyVlanId);
1250 log.debug("Dummy VLAN ID {} is allocated to {}, {}", dummyVlanId, cp, ipAddress);
1251 } else {
1252 log.error("Failed to allocate dummy VLAN ID for {}, {}", cp, ipAddress);
1253 }
1254 return dummyVlanId;
1255 }
1256
1257
sangho80f11cb2015-04-01 13:05:26 -07001258 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -07001259 @Override
1260 public void process(PacketContext context) {
Charles Chan77cdde42018-05-08 21:35:50 -07001261 packetExecutor.execute(() -> processPacketInternal(context));
1262 }
sangho80f11cb2015-04-01 13:05:26 -07001263
Charles Chan77cdde42018-05-08 21:35:50 -07001264 private void processPacketInternal(PacketContext context) {
sangho80f11cb2015-04-01 13:05:26 -07001265 if (context.isHandled()) {
1266 return;
1267 }
1268
1269 InboundPacket pkt = context.inPacket();
1270 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -08001271
1272 if (ethernet == null) {
1273 return;
1274 }
1275
Saurav Das261c3002017-06-13 15:35:54 -07001276 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1277 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001278 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -07001279 log.warn("Received unexpected ARP packet on {}",
1280 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -07001281 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001282 return;
sangho80f11cb2015-04-01 13:05:26 -07001283 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -08001284 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1285 //ipHandler.addToPacketBuffer(ipv4Packet);
1286 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1287 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001288 } else {
Charles Chand041ad82017-01-13 17:20:44 -08001289 // NOTE: We don't support IP learning at this moment so this
1290 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001291 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001292 }
Pier Ventreb6a7f342016-11-26 21:05:22 -08001293 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1294 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -08001295 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -08001296 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -08001297 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1298 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1299 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1300 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1301 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1302 } else {
Saurav Das62ae6792017-05-15 15:34:25 -07001303 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -07001304 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -08001305 }
1306 } else {
1307 // NOTE: We don't support IP learning at this moment so this
1308 // is not necessary. Also it causes duplication of DHCPv6 packets.
1309 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1310 }
sangho80f11cb2015-04-01 13:05:26 -07001311 }
1312 }
1313 }
1314
sangho80f11cb2015-04-01 13:05:26 -07001315 private class InternalEventHandler implements Runnable {
Charles Chan52003922018-04-05 16:31:15 -07001316 private Event event;
1317
1318 InternalEventHandler(Event event) {
1319 this.event = event;
1320 }
1321
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001322 @Override
sangho80f11cb2015-04-01 13:05:26 -07001323 public void run() {
Charles Chan52003922018-04-05 16:31:15 -07001324 try {
1325 // TODO We should also change SR routing and PW to listen to TopologyEvents
1326 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1327 event.type() == LinkEvent.Type.LINK_UPDATED) {
1328 linkHandler.processLinkAdded((Link) event.subject());
1329 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1330 linkHandler.processLinkRemoved((Link) event.subject());
1331 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1332 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1333 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1334 DeviceId deviceId = ((Device) event.subject()).id();
1335 if (deviceService.isAvailable(deviceId)) {
1336 log.info("** DEVICE UP Processing device event {} "
1337 + "for available device {}",
1338 event.type(), ((Device) event.subject()).id());
1339 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001340 } else {
Charles Chanb8f3af52018-08-29 21:21:51 -07001341 if (event.type() == DeviceEvent.Type.DEVICE_ADDED) {
1342 // Note: For p4 devices, the device will be added but unavailable at the beginning.
1343 // The device will later on being marked as available once the pipeline is pushed
1344 // to the device.
1345 log.info("** DEVICE ADDED but unavailable. Ignore");
1346 return;
1347 }
Charles Chan52003922018-04-05 16:31:15 -07001348 log.info(" ** DEVICE DOWN Processing device event {}"
1349 + " for unavailable device {}",
1350 event.type(), ((Device) event.subject()).id());
1351 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001352 }
Charles Chan52003922018-04-05 16:31:15 -07001353 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1354 // typically these calls come when device is added first time
1355 // so port filtering rules are handled at the device_added event.
1356 // port added calls represent all ports on the device,
1357 // enabled or not.
1358 log.trace("** PORT ADDED {}/{} -> {}",
1359 ((DeviceEvent) event).subject().id(),
1360 ((DeviceEvent) event).port().number(),
1361 event.type());
1362 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1363 // these calls happen for every subsequent event
1364 // ports enabled, disabled, switch goes away, comes back
1365 log.info("** PORT UPDATED {}/{} -> {}",
1366 event.subject(),
1367 ((DeviceEvent) event).port(),
1368 event.type());
Saurav Dasec683dc2018-04-27 18:42:30 -07001369 processPortUpdatedInternal(((Device) event.subject()),
Charles Chan52003922018-04-05 16:31:15 -07001370 ((DeviceEvent) event).port());
1371 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1372 // Process topology event, needed for all modules relying on
1373 // topology service for path computation
1374 TopologyEvent topologyEvent = (TopologyEvent) event;
1375 log.info("Processing topology event {}, topology age {}, reasons {}",
1376 event.type(), topologyEvent.subject().time(),
1377 topologyEvent.reasons().size());
1378 topologyHandler.processTopologyChange(topologyEvent.reasons());
1379 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1380 hostHandler.processHostAddedEvent((HostEvent) event);
1381 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1382 hostHandler.processHostMovedEvent((HostEvent) event);
1383 routeHandler.processHostMovedEvent((HostEvent) event);
1384 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1385 hostHandler.processHostRemovedEvent((HostEvent) event);
1386 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1387 hostHandler.processHostUpdatedEvent((HostEvent) event);
1388 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1389 routeHandler.processRouteAdded((RouteEvent) event);
1390 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1391 routeHandler.processRouteUpdated((RouteEvent) event);
1392 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1393 routeHandler.processRouteRemoved((RouteEvent) event);
1394 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1395 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1396 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1397 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1398 event.type() == McastEvent.Type.SINKS_ADDED ||
1399 event.type() == McastEvent.Type.SINKS_REMOVED ||
1400 event.type() == McastEvent.Type.ROUTE_ADDED ||
1401 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1402 mcastHandler.processMcastEvent((McastEvent) event);
1403 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1404 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1405 Class configClass = netcfgEvent.configClass();
1406 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1407 appCfgHandler.processAppConfigAdded(netcfgEvent);
1408 log.info("App config event .. configuring network");
1409 cfgListener.configureNetwork();
1410 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1411 log.info("Segment Routing Device Config added for {}", event.subject());
1412 cfgListener.configureNetwork();
1413 } else if (configClass.equals(XConnectConfig.class)) {
1414 xConnectHandler.processXConnectConfigAdded(netcfgEvent);
1415 } else if (configClass.equals(InterfaceConfig.class)) {
1416 log.info("Interface Config added for {}", event.subject());
1417 cfgListener.configureNetwork();
1418 } else {
1419 log.error("Unhandled config class: {}", configClass);
1420 }
1421 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1422 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1423 Class configClass = netcfgEvent.configClass();
1424 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1425 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1426 log.info("App config event .. configuring network");
1427 cfgListener.configureNetwork();
1428 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1429 log.info("Segment Routing Device Config updated for {}", event.subject());
1430 createOrUpdateDeviceConfiguration();
1431 } else if (configClass.equals(XConnectConfig.class)) {
1432 xConnectHandler.processXConnectConfigUpdated(netcfgEvent);
1433 } else if (configClass.equals(InterfaceConfig.class)) {
1434 log.info("Interface Config updated for {}", event.subject());
1435 createOrUpdateDeviceConfiguration();
1436 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1437 (InterfaceConfig) netcfgEvent.prevConfig().get());
1438 } else {
1439 log.error("Unhandled config class: {}", configClass);
1440 }
1441 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1442 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1443 Class configClass = netcfgEvent.configClass();
1444 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1445 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1446 log.info("App config event .. configuring network");
1447 cfgListener.configureNetwork();
1448 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1449 // TODO Handle sr device config removal
1450 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
1451 } else if (configClass.equals(XConnectConfig.class)) {
1452 xConnectHandler.processXConnectConfigRemoved(netcfgEvent);
1453 } else if (configClass.equals(InterfaceConfig.class)) {
1454 // TODO Handle interface removal
1455 log.info("InterfaceConfig removal is not handled in current implementation");
1456 } else {
1457 log.error("Unhandled config class: {}", configClass);
1458 }
Saurav Das00e553b2018-04-21 17:19:48 -07001459 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1460 MastershipEvent me = (MastershipEvent) event;
1461 DeviceId deviceId = me.subject();
1462 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1463 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1464 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1465 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1466 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1467 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan52003922018-04-05 16:31:15 -07001468 } else {
1469 log.warn("Unhandled event type: {}", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001470 }
Charles Chan52003922018-04-05 16:31:15 -07001471 } catch (Exception e) {
1472 log.error("SegmentRouting event handler thread thrown an exception: {}",
1473 e.getMessage(), e);
sangho80f11cb2015-04-01 13:05:26 -07001474 }
sangho80f11cb2015-04-01 13:05:26 -07001475 }
1476 }
1477
Saurav Dase6c448a2018-01-18 12:07:33 -08001478 void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001479 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001480
1481 // NOTE: Punt ARP/NDP even when the device is not configured.
1482 // Host learning without network config is required for CORD config generator.
1483 routingRulePopulator.populateIpPunts(device.id());
1484 routingRulePopulator.populateArpNdpPunts(device.id());
1485
Charles Chan319d1a22015-11-03 10:42:14 -08001486 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001487 log.warn("Device configuration unavailable. Device {} will be "
1488 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001489 return;
1490 }
Charles Chan72779502016-04-23 17:36:10 -07001491 processDeviceAddedInternal(device.id());
1492 }
1493
1494 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001495 // Irrespective of whether the local is a MASTER or not for this device,
1496 // we need to create a SR-group-handler instance. This is because in a
1497 // multi-instance setup, any instance can initiate forwarding/next-objectives
1498 // for any switch (even if this instance is a SLAVE or not even connected
1499 // to the switch). To handle this, a default-group-handler instance is necessary
1500 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001501 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1502 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001503 DefaultGroupHandler groupHandler;
1504 try {
1505 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001506 createGroupHandler(deviceId,
1507 appId,
1508 deviceConfiguration,
1509 linkService,
1510 flowObjectiveService,
1511 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001512 } catch (DeviceConfigNotFoundException e) {
1513 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1514 return;
1515 }
Saurav Das2b6a00f2017-12-05 15:00:23 -08001516 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan72779502016-04-23 17:36:10 -07001517 deviceId);
1518 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001519 }
Saurav Dasb149be12016-06-07 10:08:06 -07001520
Charles Chan72779502016-04-23 17:36:10 -07001521 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001522 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -07001523 xConnectHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001524 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001525 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001526 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001527 }
Charles Chan82ab1932016-01-30 23:22:37 -08001528
Charles Chandebfea32016-10-24 14:52:01 -07001529 appCfgHandler.init(deviceId);
Charles Chan074fae62018-04-29 18:11:37 -07001530 hostEventExecutor.execute(() -> hostHandler.init(deviceId));
1531 routeEventExecutor.execute(() -> routeHandler.init(deviceId));
sangho80f11cb2015-04-01 13:05:26 -07001532 }
1533
Saurav Dasc3604f12016-03-23 11:22:49 -07001534 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001535 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001536 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan873661e2017-11-30 15:37:50 -08001537 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan10b0fb72017-02-02 16:20:42 -08001538 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001539 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001540 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001541 portNextObjStore.entrySet().stream()
1542 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001543 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Dase6c448a2018-01-18 12:07:33 -08001544 linkHandler.processDeviceRemoved(device);
Charles Chan17d75d82017-06-15 00:44:51 -07001545
Saurav Dasfbe74572017-08-03 18:30:35 -07001546 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1547 if (gh != null) {
1548 gh.shutdown();
1549 }
Saurav Das62ae6792017-05-15 15:34:25 -07001550 // Note that a switch going down is associated with all of its links
1551 // going down as well, but it is treated as a single switch down event
Saurav Dasdebcf882018-04-06 20:16:01 -07001552 // while the link-downs are ignored. We cannot rely on the ordering of
1553 // events - i.e we cannot expect all link-downs to come before the
1554 // switch down - so we purge all seen-links for the switch before
1555 // handling route-path changes for the switch-down
Saurav Das62ae6792017-05-15 15:34:25 -07001556 defaultRoutingHandler
Saurav Dasdc7f2752018-03-18 21:28:15 -07001557 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Das6430f412018-01-25 09:49:01 -08001558 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan82f19972016-05-17 13:13:55 -07001559 xConnectHandler.removeDevice(device.id());
Saurav Das97241862018-02-14 14:14:54 -08001560
1561 // Cleanup all internal groupHandler stores for this device. Should be
1562 // done after all rerouting or rehashing has been completed
1563 groupHandlerMap.entrySet()
1564 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001565 }
1566
Saurav Das49368392018-04-23 18:42:12 -07001567 /**
1568 * Purge the destinationSet nextObjective store of entries with this device
1569 * as key. Erases app-level knowledge of hashed groups in this device.
1570 *
1571 * @param devId the device identifier
1572 */
Saurav Das00e553b2018-04-21 17:19:48 -07001573 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Das49368392018-04-23 18:42:12 -07001574 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Das00e553b2018-04-21 17:19:48 -07001575 dsNextObjStore.entrySet().stream()
1576 .filter(entry -> entry.getKey().deviceId().equals(devId))
1577 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1578 }
1579
Saurav Dasec683dc2018-04-27 18:42:30 -07001580 private void processPortUpdatedInternal(Device device, Port port) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001581 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1582 log.warn("Device configuration uploading. Not handling port event for"
1583 + "dev: {} port: {}", device.id(), port.number());
1584 return;
1585 }
Saurav Dasf9332192017-02-18 14:05:44 -08001586
Saurav Dasec683dc2018-04-27 18:42:30 -07001587 if (interfaceService.isConfigured(new ConnectPoint(device.id(), port.number()))) {
1588 lastEdgePortEvent = Instant.now();
1589 }
1590
Saurav Dasf9332192017-02-18 14:05:44 -08001591 if (!mastershipService.isLocalMaster(device.id())) {
1592 log.debug("Not master for dev:{} .. not handling port updated event"
1593 + "for port {}", device.id(), port.number());
1594 return;
1595 }
Saurav Dasec683dc2018-04-27 18:42:30 -07001596 processPortUpdated(device.id(), port);
1597 }
Saurav Dasf9332192017-02-18 14:05:44 -08001598
Saurav Dasec683dc2018-04-27 18:42:30 -07001599 /**
1600 * Adds or remove filtering rules for the given switchport. If switchport is
1601 * an edge facing port, additionally handles host probing and broadcast
1602 * rules. Must be called by local master of device.
1603 *
1604 * @param deviceId the device identifier
1605 * @param port the port to update
1606 */
1607 void processPortUpdated(DeviceId deviceId, Port port) {
Saurav Dasf9332192017-02-18 14:05:44 -08001608 // first we handle filtering rules associated with the port
1609 if (port.isEnabled()) {
1610 log.info("Switchport {}/{} enabled..programming filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001611 deviceId, port.number());
1612 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001613 } else {
1614 log.info("Switchport {}/{} disabled..removing filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001615 deviceId, port.number());
1616 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001617 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001618
1619 // portUpdated calls are for ports that have gone down or up. For switch
1620 // to switch ports, link-events should take care of any re-routing or
1621 // group editing necessary for port up/down. Here we only process edge ports
1622 // that are already configured.
Saurav Dasec683dc2018-04-27 18:42:30 -07001623 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
Charles Chan098ca202018-05-01 11:50:20 -07001624 VlanId untaggedVlan = interfaceService.getUntaggedVlanId(cp);
1625 VlanId nativeVlan = interfaceService.getNativeVlanId(cp);
1626 Set<VlanId> taggedVlans = interfaceService.getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001627
Saurav Das3fb28272017-03-04 16:08:47 -08001628 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001629 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasec683dc2018-04-27 18:42:30 -07001630 + "dev/port: {}/{}", deviceId, port.number());
Saurav Dasf0f592d2016-11-18 15:21:57 -08001631 return;
1632 }
Saurav Das3fb28272017-03-04 16:08:47 -08001633 if (untaggedVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001634 processEdgePort(deviceId, port, untaggedVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001635 }
1636 if (nativeVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001637 processEdgePort(deviceId, port, nativeVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001638 }
1639 if (!taggedVlans.isEmpty()) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001640 taggedVlans.forEach(tag -> processEdgePort(deviceId, port, tag, false));
Saurav Das3fb28272017-03-04 16:08:47 -08001641 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001642 }
1643
Saurav Dasec683dc2018-04-27 18:42:30 -07001644 private void processEdgePort(DeviceId deviceId, Port port, VlanId vlanId,
Saurav Das3fb28272017-03-04 16:08:47 -08001645 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001646 boolean portUp = port.isEnabled();
1647 if (portUp) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001648 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001649 port.number(), vlanId);
Charles Chan074fae62018-04-29 18:11:37 -07001650 hostEventExecutor.execute(() -> hostHandler.processPortUp(new ConnectPoint(deviceId, port.number())));
Saurav Dasf0f592d2016-11-18 15:21:57 -08001651 } else {
Saurav Dasec683dc2018-04-27 18:42:30 -07001652 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001653 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001654 }
1655
Saurav Dasec683dc2018-04-27 18:42:30 -07001656 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001657 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001658 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001659 } else {
1660 log.warn("Group handler not found for dev:{}. Not handling edge port"
Saurav Dasec683dc2018-04-27 18:42:30 -07001661 + " {} event for port:{}", deviceId,
Saurav Dasf0f592d2016-11-18 15:21:57 -08001662 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001663 }
1664 }
sangho27462c62015-05-14 00:39:53 -07001665
Charles Chan8ca5a122017-10-20 16:06:55 -07001666 private void createOrUpdateDeviceConfiguration() {
1667 if (deviceConfiguration == null) {
Saurav Dase321cff2018-02-09 17:26:45 -08001668 log.info("Creating new DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001669 deviceConfiguration = new DeviceConfiguration(this);
1670 } else {
Saurav Dase321cff2018-02-09 17:26:45 -08001671 log.info("Updating DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001672 deviceConfiguration.updateConfig();
1673 }
1674 }
1675
Charles Chanfbcb8812018-04-18 18:41:05 -07001676 private void createOrUpdateDefaultRoutingHandler() {
1677 if (defaultRoutingHandler == null) {
1678 log.info("Creating new DefaultRoutingHandler");
1679 defaultRoutingHandler = new DefaultRoutingHandler(this);
1680 } else {
1681 log.info("Updating DefaultRoutingHandler");
1682 defaultRoutingHandler.update(this);
1683 }
1684 }
1685
Pier Ventreb6a7f342016-11-26 21:05:22 -08001686 /**
1687 * Registers the given connect point with the NRS, this is necessary
1688 * to receive the NDP and ARP packets from the NRS.
1689 *
1690 * @param portToRegister connect point to register
1691 */
1692 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001693 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001694 portToRegister,
1695 neighbourHandler,
1696 appId
1697 );
1698 }
1699
Charles Chan72f556a2015-10-05 17:50:33 -07001700 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001701 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001702 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001703
Charles Chanb7f75ac2016-01-11 18:28:54 -08001704 /**
1705 * Constructs the internal network config listener.
1706 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001707 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001708 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001709 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001710 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001711 }
1712
Charles Chanb7f75ac2016-01-11 18:28:54 -08001713 /**
1714 * Reads network config and initializes related data structure accordingly.
1715 */
Charles Chan873661e2017-11-30 15:37:50 -08001716 void configureNetwork() {
Saurav Dase321cff2018-02-09 17:26:45 -08001717 log.info("Configuring network ...");
Andrea Campanella060cad82018-04-17 12:09:34 +02001718
1719 // Setting handling of network configuration events completable future
1720 // The completable future is needed because of the async behaviour of the configureNetwork,
1721 // listener registration and event arrival
1722 // Enables us to buffer the events and execute them when the configure network is done.
Ray Milkey521e4392018-11-16 15:15:14 -08001723 synchronized (networkConfigCompletionLock) {
1724 networkConfigCompletion = new CompletableFuture<>();
Andrea Campanella060cad82018-04-17 12:09:34 +02001725
Ray Milkey521e4392018-11-16 15:15:14 -08001726 // add a small delay to absorb multiple network config added notifications
1727 if (!programmingScheduled.get()) {
1728 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1729 programmingScheduled.set(true);
1730 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1731 }
1732
1733 createOrUpdateDeviceConfiguration();
1734
1735 arpHandler = new ArpHandler(srManager);
1736 icmpHandler = new IcmpHandler(srManager);
1737 ipHandler = new IpHandler(srManager);
1738 routingRulePopulator = new RoutingRulePopulator(srManager);
1739 createOrUpdateDefaultRoutingHandler();
1740
1741 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1742 groupHandlerMap, tunnelStore);
1743 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1744 flowObjectiveService,
1745 tunnelHandler, policyStore);
1746 networkConfigCompletion.complete(true);
Andrea Campanella060cad82018-04-17 12:09:34 +02001747 }
1748
Charles Chan72779502016-04-23 17:36:10 -07001749 mcastHandler.init();
Andrea Campanella060cad82018-04-17 12:09:34 +02001750
Charles Chane7c61022015-10-07 14:21:45 -07001751 }
1752
Charles Chan72f556a2015-10-05 17:50:33 -07001753 @Override
1754 public void event(NetworkConfigEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001755 if (mainEventExecutor == null) {
1756 return;
1757 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001758 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
1759 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1760 switch (event.type()) {
1761 case CONFIG_ADDED:
1762 case CONFIG_UPDATED:
1763 case CONFIG_REMOVED:
1764 log.trace("Schedule Network Config event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001765 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1766 mainEventExecutor.execute(new InternalEventHandler(event));
1767 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001768 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001769 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001770 break;
1771 default:
1772 break;
Charles Chan72f556a2015-10-05 17:50:33 -07001773 }
1774 }
Saurav Das261c3002017-06-13 15:35:54 -07001775
Charles Chanc4a68c32018-01-03 16:26:32 -08001776 @Override
1777 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase321cff2018-02-09 17:26:45 -08001778 if (event.type() == CONFIG_REGISTERED ||
1779 event.type() == CONFIG_UNREGISTERED) {
1780 log.debug("Ignore event {} due to type mismatch", event);
1781 return false;
Charles Chanc4a68c32018-01-03 16:26:32 -08001782 }
Saurav Dase321cff2018-02-09 17:26:45 -08001783
1784 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1785 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1786 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -08001787 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase321cff2018-02-09 17:26:45 -08001788 log.debug("Ignore event {} due to class mismatch", event);
1789 return false;
1790 }
1791
1792 return true;
Charles Chanc4a68c32018-01-03 16:26:32 -08001793 }
1794
Saurav Das261c3002017-06-13 15:35:54 -07001795 private final class ConfigChange implements Runnable {
1796 @Override
1797 public void run() {
1798 programmingScheduled.set(false);
Saurav Dase321cff2018-02-09 17:26:45 -08001799 log.info("Reacting to config changes after buffer delay");
Saurav Das261c3002017-06-13 15:35:54 -07001800 for (Device device : deviceService.getDevices()) {
1801 processDeviceAdded(device);
1802 }
1803 defaultRoutingHandler.startPopulationProcess();
1804 }
1805 }
Charles Chan72f556a2015-10-05 17:50:33 -07001806 }
Charles Chanf4586112015-11-09 16:37:23 -08001807
Charles Chan3f4aca62018-03-21 16:57:47 -07001808 private class InternalLinkListener implements LinkListener {
1809 @Override
1810 public void event(LinkEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001811 if (mainEventExecutor == null) {
1812 return;
1813 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001814 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1815 event.type() == LinkEvent.Type.LINK_UPDATED ||
1816 event.type() == LinkEvent.Type.LINK_REMOVED) {
1817 log.trace("Schedule Link event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001818 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1819 mainEventExecutor.execute(new InternalEventHandler(event));
1820 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001821 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001822 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001823 }
1824 }
1825 }
1826
1827 private class InternalDeviceListener implements DeviceListener {
1828 @Override
1829 public void event(DeviceEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001830 if (mainEventExecutor == null) {
1831 return;
1832 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001833 switch (event.type()) {
1834 case DEVICE_ADDED:
1835 case PORT_UPDATED:
1836 case PORT_ADDED:
1837 case DEVICE_UPDATED:
1838 case DEVICE_AVAILABILITY_CHANGED:
1839 log.trace("Schedule Device event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001840 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1841 mainEventExecutor.execute(new InternalEventHandler(event));
1842 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001843 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001844 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001845 break;
1846 default:
1847 }
1848 }
1849 }
1850
1851 private class InternalTopologyListener implements TopologyListener {
1852 @Override
1853 public void event(TopologyEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001854 if (mainEventExecutor == null) {
1855 return;
1856 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001857 switch (event.type()) {
1858 case TOPOLOGY_CHANGED:
1859 log.trace("Schedule Topology event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001860 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1861 mainEventExecutor.execute(new InternalEventHandler(event));
1862 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001863 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001864 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001865 break;
1866 default:
1867 }
1868 }
1869 }
1870
Charles Chanf4586112015-11-09 16:37:23 -08001871 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001872 @Override
1873 public void event(HostEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001874 if (hostEventExecutor == null) {
1875 return;
1876 }
Charles Chanf4586112015-11-09 16:37:23 -08001877 switch (event.type()) {
1878 case HOST_ADDED:
Charles Chanf4586112015-11-09 16:37:23 -08001879 case HOST_MOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001880 case HOST_REMOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001881 case HOST_UPDATED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001882 log.trace("Schedule Host event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001883 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chanf4586112015-11-09 16:37:23 -08001884 break;
1885 default:
1886 log.warn("Unsupported host event type: {}", event.type());
1887 break;
1888 }
1889 }
1890 }
1891
Charles Chanc91c8782016-03-30 17:54:24 -07001892 private class InternalMcastListener implements McastListener {
1893 @Override
1894 public void event(McastEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001895 if (mcastEventExecutor == null) {
1896 return;
1897 }
Charles Chanc91c8782016-03-30 17:54:24 -07001898 switch (event.type()) {
Pier1f87aca2018-03-14 16:47:32 -07001899 case SOURCES_ADDED:
1900 case SOURCES_REMOVED:
1901 case SINKS_ADDED:
1902 case SINKS_REMOVED:
Charles Chanc91c8782016-03-30 17:54:24 -07001903 case ROUTE_REMOVED:
Pierdb27b8d2018-04-17 16:29:56 +02001904 case ROUTE_ADDED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001905 log.trace("Schedule Mcast event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001906 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi6786b922018-02-02 16:19:11 +01001907 break;
Charles Chanc91c8782016-03-30 17:54:24 -07001908 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07001909 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chanc91c8782016-03-30 17:54:24 -07001910 break;
1911 }
1912 }
1913 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001914
Charles Chandebfea32016-10-24 14:52:01 -07001915 private class InternalRouteEventListener implements RouteListener {
1916 @Override
1917 public void event(RouteEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001918 if (routeEventExecutor == null) {
1919 return;
1920 }
Charles Chandebfea32016-10-24 14:52:01 -07001921 switch (event.type()) {
1922 case ROUTE_ADDED:
Charles Chandebfea32016-10-24 14:52:01 -07001923 case ROUTE_UPDATED:
Charles Chandebfea32016-10-24 14:52:01 -07001924 case ROUTE_REMOVED:
Charles Chan910be6a2017-08-23 14:46:43 -07001925 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001926 log.trace("Schedule Route event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001927 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan910be6a2017-08-23 14:46:43 -07001928 break;
Charles Chandebfea32016-10-24 14:52:01 -07001929 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07001930 log.warn("Unsupported route event type: {}", event.type());
Charles Chandebfea32016-10-24 14:52:01 -07001931 break;
1932 }
1933 }
1934 }
Saurav Das62ae6792017-05-15 15:34:25 -07001935
Charles Chanfbcb8812018-04-18 18:41:05 -07001936 private class InternalMastershipListener implements MastershipListener {
1937 @Override
1938 public void event(MastershipEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001939 if (mainEventExecutor == null) {
1940 return;
1941 }
Charles Chanfbcb8812018-04-18 18:41:05 -07001942 switch (event.type()) {
Saurav Das00e553b2018-04-21 17:19:48 -07001943 case MASTER_CHANGED:
1944 log.debug("Mastership event: {}/{}", event.subject(),
1945 event.roleInfo());
1946 mainEventExecutor.execute(new InternalEventHandler(event));
1947 break;
1948 case BACKUPS_CHANGED:
1949 case SUSPENDED:
1950 default:
1951 log.debug("Mastership event type {} not handled", event.type());
1952 break;
Charles Chanfbcb8812018-04-18 18:41:05 -07001953 }
1954 }
1955 }
1956
Saurav Das00e553b2018-04-21 17:19:48 -07001957 class InternalClusterListener implements ClusterEventListener {
1958 private Instant lastClusterEvent = Instant.EPOCH;
1959
1960 long timeSinceLastClusterEvent() {
1961 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
1962 }
1963
1964 @Override
1965 public void event(ClusterEvent event) {
1966 switch (event.type()) {
1967 case INSTANCE_ACTIVATED:
1968 case INSTANCE_ADDED:
1969 case INSTANCE_READY:
1970 log.debug("Cluster event {} ignored", event.type());
1971 break;
1972 case INSTANCE_DEACTIVATED:
1973 case INSTANCE_REMOVED:
1974 log.info("** Cluster event {}", event.type());
1975 lastClusterEvent = Instant.now();
1976 break;
1977 default:
1978 break;
1979 }
1980
1981 }
1982
1983 }
1984
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001985 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1986 try {
1987 Set<Interface> intfs = conf.getInterfaces();
1988 Set<Interface> prevIntfs = prevConf.getInterfaces();
1989
1990 // Now we only handle one interface config at each port.
1991 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1992 log.warn("Interface update aborted - one at a time is allowed, " +
1993 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1994 return;
1995 }
1996
Andrea Campanella060cad82018-04-17 12:09:34 +02001997 //The system is in an incoherent state, abort
1998 if (defaultRoutingHandler == null) {
1999 log.warn("Interface update aborted, defaultRoutingHandler is null");
2000 return;
2001 }
2002
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002003 Interface intf = intfs.stream().findFirst().get();
2004 Interface prevIntf = prevIntfs.stream().findFirst().get();
2005
2006 DeviceId deviceId = intf.connectPoint().deviceId();
2007 PortNumber portNum = intf.connectPoint().port();
2008
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002009 removeSubnetConfig(prevIntf.connectPoint(),
2010 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
2011 new HashSet<>(intf.ipAddressesList())));
2012
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002013 if (!prevIntf.vlanNative().equals(VlanId.NONE)
2014 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
2015 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
2016 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
2017 // Update filtering objective and L2IG group bucket
2018 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
2019 } else {
2020 // RemoveVlanNative
2021 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
2022 }
2023 }
2024
2025 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
2026 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2027 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2028 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
2029 // Update filtering objective and L2IG group bucket
2030 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
2031 } else {
2032 // RemoveVlanUntagged
2033 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
2034 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002035 }
2036
2037 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2038 // RemoveVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002039 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
2040 .filter(i -> !intf.vlanUntagged().equals(i))
2041 .filter(i -> !intf.vlanNative().equals(i))
2042 .forEach(vlanId -> updateVlanConfigInternal(
2043 deviceId, portNum, vlanId, false, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002044 }
2045
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002046 if (!intf.vlanNative().equals(VlanId.NONE)
2047 && !prevIntf.vlanNative().equals(intf.vlanNative())
2048 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2049 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
2050 // Update filtering objective and L2IG group bucket
2051 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
2052 } else {
2053 // AddVlanNative
2054 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
2055 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002056 }
2057
2058 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2059 // AddVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002060 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
2061 .filter(i -> !prevIntf.vlanUntagged().equals(i))
2062 .filter(i -> !prevIntf.vlanNative().equals(i))
2063 .forEach(vlanId -> updateVlanConfigInternal(
2064 deviceId, portNum, vlanId, false, true)
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002065 );
2066 }
2067
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002068 if (!intf.vlanUntagged().equals(VlanId.NONE)
2069 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2070 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
2071 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
2072 // Update filtering objective and L2IG group bucket
2073 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
2074 } else {
2075 // AddVlanUntagged
2076 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
2077 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002078 }
2079 addSubnetConfig(prevIntf.connectPoint(),
2080 Sets.difference(new HashSet<>(intf.ipAddressesList()),
2081 new HashSet<>(prevIntf.ipAddressesList())));
2082 } catch (ConfigException e) {
2083 log.error("Error in configuration");
2084 }
2085 }
2086
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002087 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
2088 VlanId vlanId, boolean pushVlan) {
2089 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2090 if (grpHandler == null) {
2091 log.warn("Failed to retrieve group handler for device {}", deviceId);
2092 return;
2093 }
2094
2095 // Update filtering objective for a single port
2096 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
2097 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
2098
2099 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
2100 // Update L2IG bucket of the port
2101 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
2102 } else {
2103 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2104 }
2105 }
2106
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002107 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
2108 VlanId vlanId, boolean pushVlan, boolean install) {
2109 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2110 if (grpHandler == null) {
2111 log.warn("Failed to retrieve group handler for device {}", deviceId);
2112 return;
2113 }
2114
2115 // Update filtering objective for a single port
2116 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
2117
2118 // Update filtering objective for multicast ingress port
2119 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
2120
2121 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
2122
2123 if (nextId != -1 && !install) {
2124 // Update next objective for a single port as an output port
2125 // Remove a single port from L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002126 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002127 // Remove L2 Bridging rule and L3 Unicast rule to the host
Charles Chan074fae62018-04-29 18:11:37 -07002128 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2129 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002130 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
2131 // only if there is no port configured on that VLAN ID
2132 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
2133 // Remove broadcast forwarding rule for the VLAN
2134 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2135 // Remove L2FG for VLAN
2136 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
2137 } else {
2138 // Remove L2IG of the port
2139 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
2140 }
2141 } else if (install) {
2142 if (nextId != -1) {
2143 // Add a single port to L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002144 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002145 } else {
2146 // Create L2FG for VLAN
2147 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
2148 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2149 }
Charles Chan074fae62018-04-29 18:11:37 -07002150 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2151 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002152 } else {
2153 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2154 }
2155 }
2156
2157 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2158 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2159 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2160
2161 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2162 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2163 .filter(intf -> !intf.connectPoint().equals(cp))
2164 .flatMap(intf -> intf.ipAddressesList().stream())
2165 .collect(Collectors.toSet());
2166 // 1. Partial subnet population
2167 // Remove routing rules for removed subnet from previous configuration,
2168 // which does not also exist in other interfaces in the same device
2169 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2170 .map(InterfaceIpAddress::subnetAddress)
2171 .collect(Collectors.toSet());
2172
2173 defaultRoutingHandler.revokeSubnet(
2174 ipPrefixSet.stream()
2175 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2176 .collect(Collectors.toSet()));
2177
2178 // 2. Interface IP punts
2179 // Remove IP punts for old Intf address
2180 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2181 .map(InterfaceIpAddress::ipAddress)
2182 .collect(Collectors.toSet());
2183 ipAddressSet.stream()
2184 .map(InterfaceIpAddress::ipAddress)
2185 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2186 .forEach(interfaceIpAddress ->
2187 routingRulePopulator.revokeSingleIpPunts(
2188 cp.deviceId(), interfaceIpAddress));
2189
2190 // 3. Host unicast routing rule
2191 // Remove unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002192 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002193 }
2194
2195 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2196 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2197 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2198
2199 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2200 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2201 .filter(intf -> !intf.connectPoint().equals(cp))
2202 .flatMap(intf -> intf.ipAddressesList().stream())
2203 .collect(Collectors.toSet());
2204 // 1. Partial subnet population
2205 // Add routing rules for newly added subnet, which does not also exist in
2206 // other interfaces in the same device
2207 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2208 .map(InterfaceIpAddress::subnetAddress)
2209 .collect(Collectors.toSet());
2210
2211 defaultRoutingHandler.populateSubnet(
2212 Collections.singleton(cp),
2213 ipPrefixSet.stream()
2214 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2215 .collect(Collectors.toSet()));
2216
2217 // 2. Interface IP punts
2218 // Add IP punts for new Intf address
2219 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2220 .map(InterfaceIpAddress::ipAddress)
2221 .collect(Collectors.toSet());
2222 ipAddressSet.stream()
2223 .map(InterfaceIpAddress::ipAddress)
2224 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2225 .forEach(interfaceIpAddress ->
2226 routingRulePopulator.populateSingleIpPunts(
2227 cp.deviceId(), interfaceIpAddress));
2228
2229 // 3. Host unicast routing rule
2230 // Add unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002231 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002232 }
sangho80f11cb2015-04-01 13:05:26 -07002233}