blob: c75de3cc24e08091ee2fbd1b8f7110641533e6f9 [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;
Charles Chan39b75522018-04-21 00:44:29 -0700311 private List<Event> queuedEvents = new CopyOnWriteArrayList<>();
sangho80f11cb2015-04-01 13:05:26 -0700312
Charles Chan52003922018-04-05 16:31:15 -0700313 // Handles device, link, topology and network config events
Charles Chanfbcb8812018-04-18 18:41:05 -0700314 private ScheduledExecutorService mainEventExecutor;
sangho80f11cb2015-04-01 13:05:26 -0700315
Charles Chanfbcb8812018-04-18 18:41:05 -0700316 // Handles host, route and mcast events respectively
317 private ScheduledExecutorService hostEventExecutor;
318 private ScheduledExecutorService routeEventExecutor;
319 private ScheduledExecutorService mcastEventExecutor;
Charles Chan77cdde42018-05-08 21:35:50 -0700320 private ExecutorService packetExecutor;
Charles Chan52003922018-04-05 16:31:15 -0700321
322 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chanb7f75ac2016-01-11 18:28:54 -0800323 /**
Saurav Das261c3002017-06-13 15:35:54 -0700324 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700325 * Used to keep track on MPLS group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800326 */
Charles Chan873661e2017-11-30 15:37:50 -0800327 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das261c3002017-06-13 15:35:54 -0700328 dsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800329 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700330 * Per device next objective ID store with (device id + vlanid) as key.
331 * Used to keep track on L2 flood group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800332 */
Charles Chan873661e2017-11-30 15:37:50 -0800333 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800334 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800335 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700336 * Per device next objective ID store with (device id + port + treatment + meta) as key.
337 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800338 */
Charles Chan873661e2017-11-30 15:37:50 -0800339 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800340 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800341
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700342 /**
343 * Per port dummy VLAN ID store with (connect point + ip address) as key.
344 * Used to keep track on dummy VLAN ID allocation.
345 */
346 private EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId>
347 dummyVlanIdStore = null;
348
Saurav Das2d94d312015-11-24 23:21:05 -0800349 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
350 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700351
Saurav Das261c3002017-06-13 15:35:54 -0700352 private AtomicBoolean programmingScheduled = new AtomicBoolean();
353
Charles Chanc91c8782016-03-30 17:54:24 -0700354 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700355 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
356 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700357 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700358 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800359 public SegmentRoutingDeviceConfig createConfig() {
360 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700361 }
362 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800363
Charles Chanc91c8782016-03-30 17:54:24 -0700364 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700365 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
366 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700367 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800368 @Override
369 public SegmentRoutingAppConfig createConfig() {
370 return new SegmentRoutingAppConfig();
371 }
372 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800373
Charles Chan82f19972016-05-17 13:13:55 -0700374 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
375 new ConfigFactory<ApplicationId, XConnectConfig>(
376 SubjectFactories.APP_SUBJECT_FACTORY,
377 XConnectConfig.class, "xconnect") {
378 @Override
379 public XConnectConfig createConfig() {
380 return new XConnectConfig();
381 }
382 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800383
Charles Chanc91c8782016-03-30 17:54:24 -0700384 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700385 new ConfigFactory<ApplicationId, McastConfig>(
386 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700387 McastConfig.class, "multicast") {
388 @Override
389 public McastConfig createConfig() {
390 return new McastConfig();
391 }
392 };
393
Charles Chan1963f4f2016-02-18 14:22:42 -0800394 /**
395 * Segment Routing App ID.
396 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800397 public static final String APP_NAME = "org.onosproject.segmentrouting";
Saurav Das7c305372015-10-28 12:39:42 -0700398
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700399 /**
400 * Minumum and maximum value of dummy VLAN ID to be allocated.
401 */
402 public static final int MIN_DUMMY_VLAN_ID = 2;
403 public static final int MAX_DUMMY_VLAN_ID = 4093;
404
Saurav Dasec683dc2018-04-27 18:42:30 -0700405 Instant lastEdgePortEvent = Instant.EPOCH;
406
sangho80f11cb2015-04-01 13:05:26 -0700407 @Activate
Charles Chan873661e2017-11-30 15:37:50 -0800408 protected void activate(ComponentContext context) {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800409 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700410
Charles Chanfbcb8812018-04-18 18:41:05 -0700411 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-main", "%d", log));
412 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-host", "%d", log));
413 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-route", "%d", log));
414 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-mcast", "%d", log));
Charles Chan77cdde42018-05-08 21:35:50 -0700415 packetExecutor = Executors.newSingleThreadExecutor(groupedThreads("sr-packet", "%d", log));
Charles Chanfbcb8812018-04-18 18:41:05 -0700416
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700417 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700418 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700419 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700420 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700421 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700422 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700423 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700424 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700425 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700426
Charles Chan10b0fb72017-02-02 16:20:42 -0800427 log.debug("Creating EC map vlannextobjectivestore");
428 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
429 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
430 vlanNextObjStore = vlanNextObjMapBuilder
431 .withName("vlannextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700432 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700433 .withTimestampProvider((k, v) -> new WallClockTimestamp())
434 .build();
435
Saurav Das2d94d312015-11-24 23:21:05 -0800436 log.debug("Creating EC map subnetnextobjectivestore");
437 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
438 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
439 portNextObjStore = portNextObjMapBuilder
440 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700441 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800442 .withTimestampProvider((k, v) -> new WallClockTimestamp())
443 .build();
444
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700445 EventuallyConsistentMapBuilder<DummyVlanIdStoreKey, VlanId>
446 dummyVlanIdMapBuilder = storageService.eventuallyConsistentMapBuilder();
447 dummyVlanIdStore = dummyVlanIdMapBuilder
448 .withName("dummyvlanidstore")
449 .withSerializer(createSerializer())
450 .withTimestampProvider((k, v) -> new WallClockTimestamp())
451 .build();
452
sangho4a5c42a2015-05-20 22:16:38 -0700453 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
454 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700455 tunnelStore = tunnelMapBuilder
456 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700457 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700458 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700459 .build();
460
461 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
462 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700463 policyStore = policyMapBuilder
464 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700465 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700466 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700467 .build();
468
Saurav Dasc3604f12016-03-23 11:22:49 -0700469 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800470 "purgeOnDisconnection", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700471 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800472 "purgeOnDisconnection", "true");
Pier Luigib9632ba2017-01-12 18:14:58 -0800473 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
474 "requestInterceptsEnabled", "false");
Charles Chanc1909e12017-08-08 15:13:37 -0700475 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800476 "requestInterceptsEnabled", "false");
Charles Chan9597f8d2017-07-24 15:56:10 -0700477 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800478 "arpEnabled", "false");
Pier Luigi0ebeb312017-02-02 22:31:34 -0800479 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
480 "greedyLearningIpv6", "true");
Charles Chancf8ea472017-02-28 15:15:17 -0800481 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
482 "forceUnprovision", "true");
Charles Chanc7b73c72017-08-10 16:57:28 -0700483 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan4c95c0d2017-07-20 16:16:25 -0700484 "distributed", "true");
Charles Chan804772f2017-08-14 11:42:11 -0700485 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
486 "multihomingEnabled", "true");
Charles Chan0c9c19f2017-11-29 19:54:20 -0800487 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
488 "staleLinkAge", "15000");
489 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
490 "allowDuplicateIps", "false");
Yi Tsenga7da8d82018-08-19 03:09:54 +0800491 // For P4 switches
492 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Charles Chan02298512018-08-30 17:30:45 -0700493 "fallbackFlowPollFrequency", "4");
Yi Tsenga7da8d82018-08-19 03:09:54 +0800494 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Charles Chan02298512018-08-30 17:30:45 -0700495 "fallbackGroupPollFrequency", "3");
Charles Chan873661e2017-11-30 15:37:50 -0800496 compCfgService.registerProperties(getClass());
497 modified(context);
Saurav Dasc3604f12016-03-23 11:22:49 -0700498
Charles Chan2b078ae2015-10-14 11:24:40 -0700499 processor = new InternalPacketProcessor();
500 linkListener = new InternalLinkListener();
501 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700502 appCfgHandler = new AppConfigHandler(this);
503 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700504 mcastHandler = new McastHandler(this);
505 hostHandler = new HostHandler(this);
Saurav Dase6c448a2018-01-18 12:07:33 -0800506 linkHandler = new LinkHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700507 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800508 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800509 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigid8a15162018-02-15 16:33:08 +0100510 topologyHandler = new TopologyHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700511
Charles Chand6d25332016-02-26 22:19:52 -0800512 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700513 cfgService.registerConfigFactory(deviceConfigFactory);
514 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700515 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700516 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase321cff2018-02-09 17:26:45 -0800517 log.info("Configuring network before adding listeners");
Andrea Campanella060cad82018-04-17 12:09:34 +0200518
Charles Chan6961f222018-01-04 14:26:07 -0800519 cfgListener.configureNetwork();
520
Charles Chan82ab1932016-01-30 23:22:37 -0800521 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700522 packetService.addProcessor(processor, PacketProcessor.director(2));
523 linkService.addListener(linkListener);
524 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700525 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700526 routeService.addListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100527 topologyService.addListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700528 mastershipService.addListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700529 clusterService.addListener(clusterListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700530
Charles Chanc12c3d02018-03-09 15:53:44 -0800531 linkHandler.init();
Andreas Pantelopoulos75eef062018-01-11 07:53:48 -0800532 l2TunnelHandler.init();
533
Andrea Campanella060cad82018-04-17 12:09:34 +0200534 networkConfigCompletion.whenComplete((value, ex) -> {
535 //setting to null for easier fall through
536 networkConfigCompletion = null;
537 //process all queued events
Charles Chan39b75522018-04-21 00:44:29 -0700538 queuedEvents.forEach(event -> {
Andrea Campanella060cad82018-04-17 12:09:34 +0200539 mainEventExecutor.execute(new InternalEventHandler(event));
540 });
541 });
542
sangho80f11cb2015-04-01 13:05:26 -0700543 log.info("Started");
544 }
545
Saurav Dase6c448a2018-01-18 12:07:33 -0800546 KryoNamespace.Builder createSerializer() {
Jonathan Hart54541d12016-04-12 15:39:44 -0700547 return new KryoNamespace.Builder()
548 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700549 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800550 VlanNextObjectiveStoreKey.class,
551 DestinationSet.class,
Andreas Pantelopoulos92998c92018-05-29 13:11:14 -0700552 DestinationSet.DestinationSetType.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800553 NextNeighbors.class,
554 Tunnel.class,
555 DefaultTunnel.class,
556 Policy.class,
557 TunnelPolicy.class,
558 Policy.Type.class,
559 PortNextObjectiveStoreKey.class,
560 XConnectStoreKey.class,
561 L2Tunnel.class,
562 L2TunnelPolicy.class,
563 DefaultL2Tunnel.class,
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700564 DefaultL2TunnelPolicy.class,
565 DummyVlanIdStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700566 );
567 }
568
sangho80f11cb2015-04-01 13:05:26 -0700569 @Deactivate
570 protected void deactivate() {
Charles Chanfbcb8812018-04-18 18:41:05 -0700571 mainEventExecutor.shutdown();
572 hostEventExecutor.shutdown();
573 routeEventExecutor.shutdown();
574 mcastEventExecutor.shutdown();
Charles Chan77cdde42018-05-08 21:35:50 -0700575 packetExecutor.shutdown();
Charles Chanfbcb8812018-04-18 18:41:05 -0700576
Ray Milkeyaee4d3e2018-05-11 09:59:19 -0700577 mainEventExecutor = null;
578 hostEventExecutor = null;
579 routeEventExecutor = null;
580 mcastEventExecutor = null;
581 packetExecutor = null;
582
Charles Chan72f556a2015-10-05 17:50:33 -0700583 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700584 cfgService.unregisterConfigFactory(deviceConfigFactory);
585 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700586 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700587 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan873661e2017-11-30 15:37:50 -0800588 compCfgService.unregisterProperties(getClass(), false);
Charles Chan72f556a2015-10-05 17:50:33 -0700589
Charles Chanfd48c222017-06-19 00:43:31 -0700590 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700591 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700592 linkService.removeListener(linkListener);
593 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700594 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700595 routeService.removeListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100596 topologyService.removeListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700597 mastershipService.removeListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700598 clusterService.removeListener(clusterListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700599
Charles Chan2e71ef32017-02-23 15:44:08 -0800600 neighbourResolutionService.unregisterNeighbourHandlers(appId);
601
sangho80f11cb2015-04-01 13:05:26 -0700602 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700603 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700604 deviceListener = null;
Charles Chan05bb1702018-04-19 13:10:01 -0700605 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chan2b078ae2015-10-14 11:24:40 -0700606 groupHandlerMap.clear();
Saurav Das49368392018-04-23 18:42:12 -0700607 defaultRoutingHandler.shutdown();
Charles Chan2b078ae2015-10-14 11:24:40 -0700608
Saurav Das261c3002017-06-13 15:35:54 -0700609 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800610 vlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700611 portNextObjStore.destroy();
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700612 dummyVlanIdStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700613 tunnelStore.destroy();
614 policyStore.destroy();
Pier Luigi35dab3f2018-01-25 16:16:02 +0100615
616 mcastHandler.terminate();
sangho80f11cb2015-04-01 13:05:26 -0700617 log.info("Stopped");
618 }
619
Charles Chan873661e2017-11-30 15:37:50 -0800620 @Modified
621 private void modified(ComponentContext context) {
622 Dictionary<?, ?> properties = context.getProperties();
623 if (properties == null) {
624 return;
625 }
626
Ray Milkey48d10c02018-10-24 10:04:03 -0700627 String strActiveProbing = Tools.get(properties, PROP_ACTIVE_PROBING);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700628 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProbing);
Charles Chan873661e2017-11-30 15:37:50 -0800629 if (expectActiveProbing != activeProbing) {
630 activeProbing = expectActiveProbing;
631 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
632 }
Saurav Dasec683dc2018-04-27 18:42:30 -0700633
Ray Milkeye96d0de2018-11-02 17:12:21 -0700634
635 String strSymmetricProbing = Tools.get(properties, PROP_SYMMETRIC_PROBING);
636 boolean expectSymmetricProbing = Boolean.parseBoolean(strSymmetricProbing);
Mayank Tiwarif7942402018-10-29 18:27:35 -0400637 if (expectSymmetricProbing != symmetricProbing) {
638 symmetricProbing = expectSymmetricProbing;
639 log.info("{} symmetric probing", symmetricProbing ? "Enabling" : "Disabling");
640 }
641
Ray Milkey48d10c02018-10-24 10:04:03 -0700642 String strSingleHomedDown = Tools.get(properties, PROP_SINGLE_HOMED_DOWN);
Saurav Dasec683dc2018-04-27 18:42:30 -0700643 boolean expectSingleHomedDown = Boolean.parseBoolean(strSingleHomedDown);
644 if (expectSingleHomedDown != singleHomedDown) {
645 singleHomedDown = expectSingleHomedDown;
646 log.info("{} downing of single homed hosts for lost uplinks",
647 singleHomedDown ? "Enabling" : "Disabling");
648 if (singleHomedDown && linkHandler != null) {
649 hostService.getHosts().forEach(host -> host.locations()
650 .forEach(loc -> {
651 if (interfaceService.isConfigured(loc)) {
652 linkHandler.checkUplinksForHost(loc);
653 }
654 }));
655 } else {
656 log.warn("Disabling singleHomedDown does not re-enable already "
657 + "downed ports for single-homed hosts");
658 }
659 }
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700660
Ray Milkey48d10c02018-10-24 10:04:03 -0700661 String strRespondToUnknownHosts = Tools.get(properties, PROP_RESPOND_TO_UNKNOWN_HOSTS);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700662 boolean expectRespondToUnknownHosts = Boolean.parseBoolean(strRespondToUnknownHosts);
663 if (expectRespondToUnknownHosts != respondToUnknownHosts) {
664 respondToUnknownHosts = expectRespondToUnknownHosts;
665 log.info("{} responding to ARPs/NDPs from unknown hosts", respondToUnknownHosts ? "Enabling" : "Disabling");
666 }
Charles Chan4eb73bb2018-07-19 14:55:31 -0700667
Ray Milkey48d10c02018-10-24 10:04:03 -0700668 String strRouteDoubleTaggedHosts = Tools.get(properties, PROP_ROUTE_DOUBLE_TAGGED_HOSTS);
Charles Chanfbca55e2018-07-24 16:40:35 -0700669 boolean expectRouteDoubleTaggedHosts = Boolean.parseBoolean(strRouteDoubleTaggedHosts);
670 if (expectRouteDoubleTaggedHosts != routeDoubleTaggedHosts) {
671 routeDoubleTaggedHosts = expectRouteDoubleTaggedHosts;
672 log.info("{} routing for double tagged hosts", routeDoubleTaggedHosts ? "Enabling" : "Disabling");
Charles Chan4eb73bb2018-07-19 14:55:31 -0700673
Charles Chanfbca55e2018-07-24 16:40:35 -0700674 if (routeDoubleTaggedHosts) {
Charles Chan4eb73bb2018-07-19 14:55:31 -0700675 hostHandler.populateAllDoubleTaggedHost();
676 } else {
677 hostHandler.revokeAllDoubleTaggedHost();
678 }
679 }
Saurav Das9bf49582018-08-13 15:34:26 -0700680
Ray Milkey48d10c02018-10-24 10:04:03 -0700681 String strDefaultInternalVlan = Tools.get(properties, PROP_DEFAULT_INTERNAL_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700682 int defIntVlan = Integer.parseInt(strDefaultInternalVlan);
683 if (defIntVlan != defaultInternalVlan) {
684 if (canUseVlanId(defIntVlan)) {
685 log.warn("Default internal vlan value changed from {} to {}.. "
686 + "re-programming filtering rules, but NOT any groups already "
687 + "created with the former value", defaultInternalVlan, defIntVlan);
688 VlanId oldDefIntVlan = VlanId.vlanId((short) defaultInternalVlan);
689 defaultInternalVlan = defIntVlan;
690 routingRulePopulator
691 .updateSpecialVlanFilteringRules(true, oldDefIntVlan,
692 VlanId.vlanId((short) defIntVlan));
693 } else {
694 log.warn("Cannot change default internal vlan to unusable "
695 + "value {}", defIntVlan);
696 }
697 }
698
Ray Milkey48d10c02018-10-24 10:04:03 -0700699 String strPwTxpVlan = Tools.get(properties, PROP_PW_TRANSPORT_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700700 int pwTxpVlan = Integer.parseInt(strPwTxpVlan);
701 if (pwTxpVlan != pwTransportVlan) {
702 if (canUseVlanId(pwTxpVlan)) {
703 log.warn("Pseudowire transport vlan value changed from {} to {}.. "
704 + "re-programming filtering rules, but NOT any groups already "
705 + "created with the former value", pwTransportVlan,
706 pwTxpVlan);
707 VlanId oldPwTxpVlan = VlanId.vlanId((short) pwTransportVlan);
708 pwTransportVlan = pwTxpVlan;
709 routingRulePopulator
710 .updateSpecialVlanFilteringRules(false, oldPwTxpVlan,
711 VlanId.vlanId((short) pwTxpVlan));
712 } else {
713 log.warn("Cannot change pseudowire transport vlan to unusable "
714 + "value {}", pwTxpVlan);
715 }
716 }
717
718 }
719
720 /**
721 * Returns true if given vlan id is not being used in the system currently,
722 * either as one of the default system wide vlans or as one of the
723 * configured interface vlans.
724 *
725 * @param vlanId given vlan id
726 * @return true if vlan is not currently in use
727 */
728 public boolean canUseVlanId(int vlanId) {
729 if (vlanId >= 4095 || vlanId <= 1) {
730 log.error("Vlan id {} value is not in valid range 2 <--> 4094",
731 vlanId);
732 return false;
733 }
734
735 VlanId vid = VlanId.vlanId((short) vlanId);
736 if (getDefaultInternalVlan().equals(vid) || getPwTransportVlan().equals(vid)) {
737 log.warn("Vlan id {} value is already in use system-wide. "
738 + "DefaultInternalVlan:{} PwTransportVlan:{} ", vlanId,
739 getDefaultInternalVlan(), getPwTransportVlan());
740 return false;
741 }
742
743 if (interfaceService.inUse(vid)) {
744 log.warn("Vlan id {} value is already in use on a configured "
745 + "interface in the system", vlanId);
746 return false;
747 }
748 return true;
749 }
750
751 /**
752 * Returns the VlanId assigned internally by default to unconfigured ports.
753 *
754 * @return the default internal vlan id
755 */
756 public VlanId getDefaultInternalVlan() {
757 return VlanId.vlanId((short) defaultInternalVlan);
758 }
759
760 /**
761 * Returns the Vlan id used to transport pseudowire traffic across the
762 * network.
763 *
764 * @return the pseudowire transport vlan id
765 */
766 public VlanId getPwTransportVlan() {
767 return VlanId.vlanId((short) pwTransportVlan);
Charles Chan873661e2017-11-30 15:37:50 -0800768 }
769
sangho27462c62015-05-14 00:39:53 -0700770 @Override
771 public List<Tunnel> getTunnels() {
772 return tunnelHandler.getTunnels();
773 }
774
775 @Override
sanghobd812f82015-06-29 14:58:47 -0700776 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
777 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700778 }
779
780 @Override
sanghobd812f82015-06-29 14:58:47 -0700781 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700782 for (Policy policy: policyHandler.getPolicies()) {
783 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
784 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
785 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
786 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700787 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700788 }
789 }
790 }
sanghobd812f82015-06-29 14:58:47 -0700791 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700792 }
793
794 @Override
sanghobd812f82015-06-29 14:58:47 -0700795 public PolicyHandler.Result removePolicy(Policy policy) {
796 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700797 }
798
799 @Override
sanghobd812f82015-06-29 14:58:47 -0700800 public PolicyHandler.Result createPolicy(Policy policy) {
801 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700802 }
803
804 @Override
805 public List<Policy> getPolicies() {
806 return policyHandler.getPolicies();
807 }
808
Saurav Das07c74602016-04-27 18:35:50 -0700809 @Override
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700810 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
811 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700812 }
813
814 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800815 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700816 return l2TunnelHandler.getL2Tunnels();
817 }
818
819 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800820 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700821 return l2TunnelHandler.getL2Policies();
822 }
823
Saurav Dasdebcf882018-04-06 20:16:01 -0700824 @Override
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700825 @Deprecated
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700826 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
827
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700828 // get both added and pending pseudowires
829 List<L2TunnelDescription> pseudowires = new ArrayList<>();
830 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
831 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700832 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700833
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700834 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
835
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700836 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
837 L2TunnelHandler.Result res;
838 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
839 res = addPseudowire(pw);
840 if (res != L2TunnelHandler.Result.SUCCESS) {
841 log.error("Pseudowire with id {} can not be instantiated !", res);
842 retRes = res;
843 }
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700844 }
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700845
846 return retRes;
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700847 }
848
849 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800850 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700851 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700852 }
853
854 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800855 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700856 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700857 }
858
859 @Override
Saurav Das07c74602016-04-27 18:35:50 -0700860 public void rerouteNetwork() {
861 cfgListener.configureNetwork();
Saurav Das07c74602016-04-27 18:35:50 -0700862 }
863
Charles Chand7844e52016-10-20 17:02:44 -0700864 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800865 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
866 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart61e24e12017-11-30 18:23:42 -0800867 deviceConfiguration.getRouters().forEach(device ->
868 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chand7844e52016-10-20 17:02:44 -0700869 return deviceSubnetMap;
870 }
871
Saurav Das62ae6792017-05-15 15:34:25 -0700872
873 @Override
874 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
875 if (defaultRoutingHandler != null) {
876 return defaultRoutingHandler.getCurrentEmcpSpgMap();
877 } else {
878 return null;
879 }
880 }
881
882 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700883 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDstNextObjStore() {
Saurav Das261c3002017-06-13 15:35:54 -0700884 if (dsNextObjStore != null) {
885 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700886 } else {
887 return ImmutableMap.of();
888 }
889 }
890
Saurav Dasfbe74572017-08-03 18:30:35 -0700891 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700892 public ImmutableMap<VlanNextObjectiveStoreKey, Integer> getVlanNextObjStore() {
893 if (vlanNextObjStore != null) {
894 return ImmutableMap.copyOf(vlanNextObjStore.entrySet());
895 } else {
896 return ImmutableMap.of();
897 }
898 }
899
900 @Override
901 public ImmutableMap<PortNextObjectiveStoreKey, Integer> getPortNextObjStore() {
902 if (portNextObjStore != null) {
903 return ImmutableMap.copyOf(portNextObjStore.entrySet());
904 } else {
905 return ImmutableMap.of();
906 }
907 }
908
909 @Override
910 public ImmutableMap<String, NextObjective> getPwInitNext() {
911 if (l2TunnelHandler != null) {
912 return l2TunnelHandler.getInitNext();
913 } else {
914 return ImmutableMap.of();
915 }
916 }
917
918 @Override
919 public ImmutableMap<String, NextObjective> getPwTermNext() {
920 if (l2TunnelHandler != null) {
921 return l2TunnelHandler.getTermNext();
922 } else {
923 return ImmutableMap.of();
924 }
925 }
926
927 @Override
928 public void invalidateNextObj(int nextId) {
929 if (dsNextObjStore != null) {
930 dsNextObjStore.entrySet().forEach(e -> {
931 if (e.getValue().nextId() == nextId) {
932 dsNextObjStore.remove(e.getKey());
933 }
934 });
935 }
936 if (vlanNextObjStore != null) {
937 vlanNextObjStore.entrySet().forEach(e -> {
938 if (e.getValue() == nextId) {
939 vlanNextObjStore.remove(e.getKey());
940 }
941 });
942 }
943 if (portNextObjStore != null) {
944 portNextObjStore.entrySet().forEach(e -> {
945 if (e.getValue() == nextId) {
946 portNextObjStore.remove(e.getKey());
947 }
948 });
949 }
950 if (mcastHandler != null) {
951 mcastHandler.removeNextId(nextId);
952 }
953 if (l2TunnelHandler != null) {
954 l2TunnelHandler.removeNextId(nextId);
955 }
956 if (xconnectService != null) {
957 xconnectService.removeNextId(nextId);
958 }
959 }
960
961 @Override
Saurav Dasfbe74572017-08-03 18:30:35 -0700962 public void verifyGroups(DeviceId id) {
963 DefaultGroupHandler gh = groupHandlerMap.get(id);
964 if (gh != null) {
965 gh.triggerBucketCorrector();
966 }
967 }
968
Saurav Das6430f412018-01-25 09:49:01 -0800969 @Override
970 public ImmutableMap<Link, Boolean> getSeenLinks() {
971 return linkHandler.getSeenLinks();
972 }
973
974 @Override
975 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
976 return linkHandler.getDownedPorts();
977 }
978
Pier Luigi0f9635b2018-01-15 18:06:43 +0100979 @Override
980 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700981 return mcastHandler.getNextIds(mcastIp);
Pier Luigi0f9635b2018-01-15 18:06:43 +0100982 }
983
984 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +0100985 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigi0f9635b2018-01-15 18:06:43 +0100986 return mcastHandler.getMcastRoles(mcastIp);
987 }
988
989 @Override
Piere99511d2018-04-19 16:47:06 +0200990 public Map<McastRoleStoreKey, McastRole> getMcastRoles(IpAddress mcastIp, ConnectPoint sourcecp) {
991 return mcastHandler.getMcastRoles(mcastIp, sourcecp);
992 }
993
994 @Override
Pier Luigi0f9635b2018-01-15 18:06:43 +0100995 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
996 return mcastHandler.getMcastPaths(mcastIp);
997 }
998
Pierdb27b8d2018-04-17 16:29:56 +0200999 @Override
Pier71c55772018-04-17 17:25:22 +02001000 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
1001 ConnectPoint sourcecp) {
1002 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
1003 }
1004
1005 @Override
Pierdb27b8d2018-04-17 16:29:56 +02001006 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
1007 return mcastHandler.getMcastLeaders(mcastIp);
1008 }
1009
Charles Chanb13e0702018-04-17 18:56:53 -07001010 @Override
1011 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
1012 return defaultRoutingHandler == null ? ImmutableMap.of() :
1013 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
1014 }
1015
1016 @Override
1017 public Map<DeviceId, Boolean> getShouldProgramCache() {
1018 return defaultRoutingHandler == null ? ImmutableMap.of() :
1019 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
1020 }
1021
Charles Chan8d316332018-06-19 20:31:57 -07001022 @Override
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301023 public boolean shouldProgram(DeviceId deviceId) {
1024 return defaultRoutingHandler.shouldProgram(deviceId);
1025 }
1026
1027 @Override
Ray Milkeyb85de082017-04-05 09:42:04 -07001028 public ApplicationId appId() {
1029 return appId;
1030 }
1031
1032 /**
1033 * Returns the device configuration.
1034 *
1035 * @return device configuration
1036 */
1037 public DeviceConfiguration deviceConfiguration() {
1038 return deviceConfiguration;
1039 }
1040
1041 /**
Saurav Das261c3002017-06-13 15:35:54 -07001042 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -07001043 * Used to keep track on MPLS group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001044 *
1045 * @return next objective ID store
1046 */
Saurav Das261c3002017-06-13 15:35:54 -07001047 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
1048 dsNextObjStore() {
1049 return dsNextObjStore;
Ray Milkeyb85de082017-04-05 09:42:04 -07001050 }
1051
1052 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001053 * Per device next objective ID store with (device id + vlanid) as key.
1054 * Used to keep track on L2 flood group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001055 *
1056 * @return vlan next object store
1057 */
1058 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
1059 return vlanNextObjStore;
1060 }
1061
1062 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001063 * Per device next objective ID store with (device id + port + treatment + meta) as key.
1064 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001065 *
1066 * @return port next object store.
1067 */
1068 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
1069 return portNextObjStore;
1070 }
1071
1072 /**
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001073 * Per port dummy VLAN ID store with (connect point + ip address) as key.
1074 * Used to keep track on dummy VLAN ID allocation.
1075 *
1076 * @return dummy vlan id store.
1077 */
1078 public EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId> dummyVlanIdStore() {
1079 return dummyVlanIdStore;
1080 }
1081
1082 /**
Saurav Das261c3002017-06-13 15:35:54 -07001083 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
1084 * labeled packets should be programmed or not.
Pier Ventre7a78de22016-10-31 15:00:01 -07001085 *
1086 * @return MPLS-ECMP value
1087 */
1088 public boolean getMplsEcmp() {
1089 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
1090 .getConfig(this.appId, SegmentRoutingAppConfig.class);
1091 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
1092 }
1093
1094 /**
sangho80f1f892015-05-19 11:57:42 -07001095 * Returns the tunnel object with the tunnel ID.
1096 *
1097 * @param tunnelId Tunnel ID
1098 * @return Tunnel reference
1099 */
sangho27462c62015-05-14 00:39:53 -07001100 public Tunnel getTunnel(String tunnelId) {
1101 return tunnelHandler.getTunnel(tunnelId);
1102 }
1103
Charles Chan8d316332018-06-19 20:31:57 -07001104 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +01001105 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chan098ca202018-05-01 11:50:20 -07001106 VlanId untaggedVlanId = interfaceService.getUntaggedVlanId(connectPoint);
1107 VlanId nativeVlanId = interfaceService.getNativeVlanId(connectPoint);
Charles Chand9265a32017-06-16 15:19:24 -07001108 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
1109 }
1110
Charles Chan8d316332018-06-19 20:31:57 -07001111 @Override
1112 public Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001113 SegmentRoutingDeviceConfig deviceConfig =
1114 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1115 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
1116 }
Charles Chan8d316332018-06-19 20:31:57 -07001117
1118 @Override
Saurav Dasec683dc2018-04-27 18:42:30 -07001119 public Optional<PortNumber> getPairLocalPort(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001120 SegmentRoutingDeviceConfig deviceConfig =
1121 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1122 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
1123 }
1124
1125 /**
Charles Chanf0ae41e2017-08-23 13:55:39 -07001126 * Returns locations of given resolved route.
1127 *
1128 * @param resolvedRoute resolved route
1129 * @return locations of nexthop. Might be empty if next hop is not found
1130 */
1131 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
1132 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
1133 return Optional.ofNullable(hostService.getHost(hostId))
1134 .map(Host::locations).orElse(Sets.newHashSet())
1135 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
1136 }
1137
1138 /**
Charles Chan90772a72017-02-08 15:52:08 -08001139 * Returns vlan port map of given device.
1140 *
1141 * @param deviceId device id
1142 * @return vlan-port multimap
1143 */
1144 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
1145 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
1146
1147 interfaceService.getInterfaces().stream()
1148 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
1149 .forEach(intf -> {
1150 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan3ed34d82017-06-22 18:03:14 -07001151 intf.vlanTagged().forEach(vlanTagged ->
1152 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
1153 );
Charles Chan90772a72017-02-08 15:52:08 -08001154 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
1155 });
1156 vlanPortMap.removeAll(VlanId.NONE);
1157
1158 return vlanPortMap;
1159 }
1160
1161 /**
Charles Chan10b0fb72017-02-02 16:20:42 -08001162 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -08001163 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -07001164 *
1165 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -08001166 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -08001167 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -07001168 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001169 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -07001170 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -08001171 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
1172 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -07001173 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -08001174 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -08001175 + "device {} not found", deviceId);
1176 return -1;
1177 }
1178 }
1179
1180 /**
1181 * Returns the next objective ID for the given portNumber, given the treatment.
1182 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -07001183 * would result in different objectives. If the next object does not exist,
1184 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -08001185 *
1186 * @param deviceId Device ID
1187 * @param portNum port number on device for which NextObjective is queried
1188 * @param treatment the actions to apply on the packets (should include outport)
1189 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -07001190 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -07001191 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -08001192 */
1193 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
1194 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -07001195 TrafficSelector meta,
1196 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -08001197 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1198 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -07001199 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -08001200 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -08001201 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
1202 + " not found", deviceId);
1203 return -1;
1204 }
1205 }
1206
Saurav Das62ae6792017-05-15 15:34:25 -07001207 /**
1208 * Returns the group handler object for the specified device id.
1209 *
1210 * @param devId the device identifier
1211 * @return the groupHandler object for the device id, or null if not found
1212 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001213 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Das62ae6792017-05-15 15:34:25 -07001214 return groupHandlerMap.get(devId);
1215 }
1216
1217 /**
Saurav Dasfbe74572017-08-03 18:30:35 -07001218 * Returns the default routing handler object.
1219 *
1220 * @return the default routing handler object
1221 */
1222 public DefaultRoutingHandler getRoutingHandler() {
1223 return defaultRoutingHandler;
1224 }
1225
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001226 /**
1227 * Returns new dummy VLAN ID.
1228 * Dummy VLAN ID should be unique in each connect point.
1229 *
1230 * @param cp connect point
1231 * @param ipAddress IP address
1232 * @return new dummy VLAN ID. Returns VlanId.NONE if no VLAN ID is available.
1233 */
1234 public synchronized VlanId allocateDummyVlanId(ConnectPoint cp, IpAddress ipAddress) {
1235 Set<VlanId> usedVlanId = Sets.union(getVlanPortMap(cp.deviceId()).keySet(),
1236 dummyVlanIdStore.entrySet().stream().filter(entry ->
1237 (entry.getKey()).connectPoint().equals(cp))
1238 .map(Map.Entry::getValue)
1239 .collect(Collectors.toSet()));
1240
1241 VlanId dummyVlanId = IntStream.range(MIN_DUMMY_VLAN_ID, MAX_DUMMY_VLAN_ID).mapToObj(
1242 i -> VlanId.vlanId((short) (i & 0xFFFF))
1243 ).filter(vlanId -> !usedVlanId.contains(vlanId)).findFirst().orElse(VlanId.NONE);
1244
1245 if (!dummyVlanId.equals(VlanId.NONE)) {
1246 this.dummyVlanIdStore.put(new DummyVlanIdStoreKey(cp, ipAddress), dummyVlanId);
1247 log.debug("Dummy VLAN ID {} is allocated to {}, {}", dummyVlanId, cp, ipAddress);
1248 } else {
1249 log.error("Failed to allocate dummy VLAN ID for {}, {}", cp, ipAddress);
1250 }
1251 return dummyVlanId;
1252 }
1253
1254
sangho80f11cb2015-04-01 13:05:26 -07001255 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -07001256 @Override
1257 public void process(PacketContext context) {
Charles Chan77cdde42018-05-08 21:35:50 -07001258 packetExecutor.execute(() -> processPacketInternal(context));
1259 }
sangho80f11cb2015-04-01 13:05:26 -07001260
Charles Chan77cdde42018-05-08 21:35:50 -07001261 private void processPacketInternal(PacketContext context) {
sangho80f11cb2015-04-01 13:05:26 -07001262 if (context.isHandled()) {
1263 return;
1264 }
1265
1266 InboundPacket pkt = context.inPacket();
1267 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -08001268
1269 if (ethernet == null) {
1270 return;
1271 }
1272
Saurav Das261c3002017-06-13 15:35:54 -07001273 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1274 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001275 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -07001276 log.warn("Received unexpected ARP packet on {}",
1277 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -07001278 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001279 return;
sangho80f11cb2015-04-01 13:05:26 -07001280 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -08001281 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1282 //ipHandler.addToPacketBuffer(ipv4Packet);
1283 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1284 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001285 } else {
Charles Chand041ad82017-01-13 17:20:44 -08001286 // NOTE: We don't support IP learning at this moment so this
1287 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001288 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001289 }
Pier Ventreb6a7f342016-11-26 21:05:22 -08001290 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1291 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -08001292 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -08001293 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -08001294 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1295 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1296 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1297 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1298 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1299 } else {
Saurav Das62ae6792017-05-15 15:34:25 -07001300 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -07001301 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -08001302 }
1303 } else {
1304 // NOTE: We don't support IP learning at this moment so this
1305 // is not necessary. Also it causes duplication of DHCPv6 packets.
1306 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1307 }
sangho80f11cb2015-04-01 13:05:26 -07001308 }
1309 }
1310 }
1311
sangho80f11cb2015-04-01 13:05:26 -07001312 private class InternalEventHandler implements Runnable {
Charles Chan52003922018-04-05 16:31:15 -07001313 private Event event;
1314
1315 InternalEventHandler(Event event) {
1316 this.event = event;
1317 }
1318
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001319 @Override
sangho80f11cb2015-04-01 13:05:26 -07001320 public void run() {
Charles Chan52003922018-04-05 16:31:15 -07001321 try {
1322 // TODO We should also change SR routing and PW to listen to TopologyEvents
1323 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1324 event.type() == LinkEvent.Type.LINK_UPDATED) {
1325 linkHandler.processLinkAdded((Link) event.subject());
1326 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1327 linkHandler.processLinkRemoved((Link) event.subject());
1328 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1329 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1330 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1331 DeviceId deviceId = ((Device) event.subject()).id();
1332 if (deviceService.isAvailable(deviceId)) {
1333 log.info("** DEVICE UP Processing device event {} "
1334 + "for available device {}",
1335 event.type(), ((Device) event.subject()).id());
1336 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001337 } else {
Charles Chanb8f3af52018-08-29 21:21:51 -07001338 if (event.type() == DeviceEvent.Type.DEVICE_ADDED) {
1339 // Note: For p4 devices, the device will be added but unavailable at the beginning.
1340 // The device will later on being marked as available once the pipeline is pushed
1341 // to the device.
1342 log.info("** DEVICE ADDED but unavailable. Ignore");
1343 return;
1344 }
Charles Chan52003922018-04-05 16:31:15 -07001345 log.info(" ** DEVICE DOWN Processing device event {}"
1346 + " for unavailable device {}",
1347 event.type(), ((Device) event.subject()).id());
1348 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001349 }
Charles Chan52003922018-04-05 16:31:15 -07001350 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1351 // typically these calls come when device is added first time
1352 // so port filtering rules are handled at the device_added event.
1353 // port added calls represent all ports on the device,
1354 // enabled or not.
1355 log.trace("** PORT ADDED {}/{} -> {}",
1356 ((DeviceEvent) event).subject().id(),
1357 ((DeviceEvent) event).port().number(),
1358 event.type());
1359 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1360 // these calls happen for every subsequent event
1361 // ports enabled, disabled, switch goes away, comes back
1362 log.info("** PORT UPDATED {}/{} -> {}",
1363 event.subject(),
1364 ((DeviceEvent) event).port(),
1365 event.type());
Saurav Dasec683dc2018-04-27 18:42:30 -07001366 processPortUpdatedInternal(((Device) event.subject()),
Charles Chan52003922018-04-05 16:31:15 -07001367 ((DeviceEvent) event).port());
1368 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1369 // Process topology event, needed for all modules relying on
1370 // topology service for path computation
1371 TopologyEvent topologyEvent = (TopologyEvent) event;
1372 log.info("Processing topology event {}, topology age {}, reasons {}",
1373 event.type(), topologyEvent.subject().time(),
1374 topologyEvent.reasons().size());
1375 topologyHandler.processTopologyChange(topologyEvent.reasons());
1376 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1377 hostHandler.processHostAddedEvent((HostEvent) event);
1378 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1379 hostHandler.processHostMovedEvent((HostEvent) event);
1380 routeHandler.processHostMovedEvent((HostEvent) event);
1381 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1382 hostHandler.processHostRemovedEvent((HostEvent) event);
1383 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1384 hostHandler.processHostUpdatedEvent((HostEvent) event);
1385 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1386 routeHandler.processRouteAdded((RouteEvent) event);
1387 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1388 routeHandler.processRouteUpdated((RouteEvent) event);
1389 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1390 routeHandler.processRouteRemoved((RouteEvent) event);
1391 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1392 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1393 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1394 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1395 event.type() == McastEvent.Type.SINKS_ADDED ||
1396 event.type() == McastEvent.Type.SINKS_REMOVED ||
1397 event.type() == McastEvent.Type.ROUTE_ADDED ||
1398 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1399 mcastHandler.processMcastEvent((McastEvent) event);
1400 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1401 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1402 Class configClass = netcfgEvent.configClass();
1403 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1404 appCfgHandler.processAppConfigAdded(netcfgEvent);
1405 log.info("App config event .. configuring network");
1406 cfgListener.configureNetwork();
1407 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1408 log.info("Segment Routing Device Config added for {}", event.subject());
1409 cfgListener.configureNetwork();
1410 } else if (configClass.equals(XConnectConfig.class)) {
1411 xConnectHandler.processXConnectConfigAdded(netcfgEvent);
1412 } else if (configClass.equals(InterfaceConfig.class)) {
1413 log.info("Interface Config added for {}", event.subject());
1414 cfgListener.configureNetwork();
1415 } else {
1416 log.error("Unhandled config class: {}", configClass);
1417 }
1418 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1419 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1420 Class configClass = netcfgEvent.configClass();
1421 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1422 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1423 log.info("App config event .. configuring network");
1424 cfgListener.configureNetwork();
1425 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1426 log.info("Segment Routing Device Config updated for {}", event.subject());
1427 createOrUpdateDeviceConfiguration();
1428 } else if (configClass.equals(XConnectConfig.class)) {
1429 xConnectHandler.processXConnectConfigUpdated(netcfgEvent);
1430 } else if (configClass.equals(InterfaceConfig.class)) {
1431 log.info("Interface Config updated for {}", event.subject());
1432 createOrUpdateDeviceConfiguration();
1433 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1434 (InterfaceConfig) netcfgEvent.prevConfig().get());
1435 } else {
1436 log.error("Unhandled config class: {}", configClass);
1437 }
1438 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1439 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1440 Class configClass = netcfgEvent.configClass();
1441 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1442 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1443 log.info("App config event .. configuring network");
1444 cfgListener.configureNetwork();
1445 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1446 // TODO Handle sr device config removal
1447 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
1448 } else if (configClass.equals(XConnectConfig.class)) {
1449 xConnectHandler.processXConnectConfigRemoved(netcfgEvent);
1450 } else if (configClass.equals(InterfaceConfig.class)) {
1451 // TODO Handle interface removal
1452 log.info("InterfaceConfig removal is not handled in current implementation");
1453 } else {
1454 log.error("Unhandled config class: {}", configClass);
1455 }
Saurav Das00e553b2018-04-21 17:19:48 -07001456 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1457 MastershipEvent me = (MastershipEvent) event;
1458 DeviceId deviceId = me.subject();
1459 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1460 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1461 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1462 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1463 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1464 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan52003922018-04-05 16:31:15 -07001465 } else {
1466 log.warn("Unhandled event type: {}", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001467 }
Charles Chan52003922018-04-05 16:31:15 -07001468 } catch (Exception e) {
1469 log.error("SegmentRouting event handler thread thrown an exception: {}",
1470 e.getMessage(), e);
sangho80f11cb2015-04-01 13:05:26 -07001471 }
sangho80f11cb2015-04-01 13:05:26 -07001472 }
1473 }
1474
Saurav Dase6c448a2018-01-18 12:07:33 -08001475 void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001476 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001477
1478 // NOTE: Punt ARP/NDP even when the device is not configured.
1479 // Host learning without network config is required for CORD config generator.
1480 routingRulePopulator.populateIpPunts(device.id());
1481 routingRulePopulator.populateArpNdpPunts(device.id());
1482
Charles Chan319d1a22015-11-03 10:42:14 -08001483 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001484 log.warn("Device configuration unavailable. Device {} will be "
1485 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001486 return;
1487 }
Charles Chan72779502016-04-23 17:36:10 -07001488 processDeviceAddedInternal(device.id());
1489 }
1490
1491 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001492 // Irrespective of whether the local is a MASTER or not for this device,
1493 // we need to create a SR-group-handler instance. This is because in a
1494 // multi-instance setup, any instance can initiate forwarding/next-objectives
1495 // for any switch (even if this instance is a SLAVE or not even connected
1496 // to the switch). To handle this, a default-group-handler instance is necessary
1497 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001498 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1499 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001500 DefaultGroupHandler groupHandler;
1501 try {
1502 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001503 createGroupHandler(deviceId,
1504 appId,
1505 deviceConfiguration,
1506 linkService,
1507 flowObjectiveService,
1508 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001509 } catch (DeviceConfigNotFoundException e) {
1510 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1511 return;
1512 }
Saurav Das2b6a00f2017-12-05 15:00:23 -08001513 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan72779502016-04-23 17:36:10 -07001514 deviceId);
1515 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001516 }
Saurav Dasb149be12016-06-07 10:08:06 -07001517
Charles Chan72779502016-04-23 17:36:10 -07001518 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001519 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -07001520 xConnectHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001521 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001522 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001523 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001524 }
Charles Chan82ab1932016-01-30 23:22:37 -08001525
Charles Chandebfea32016-10-24 14:52:01 -07001526 appCfgHandler.init(deviceId);
Charles Chan074fae62018-04-29 18:11:37 -07001527 hostEventExecutor.execute(() -> hostHandler.init(deviceId));
1528 routeEventExecutor.execute(() -> routeHandler.init(deviceId));
sangho80f11cb2015-04-01 13:05:26 -07001529 }
1530
Saurav Dasc3604f12016-03-23 11:22:49 -07001531 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001532 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001533 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan873661e2017-11-30 15:37:50 -08001534 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan10b0fb72017-02-02 16:20:42 -08001535 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001536 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001537 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001538 portNextObjStore.entrySet().stream()
1539 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001540 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Dase6c448a2018-01-18 12:07:33 -08001541 linkHandler.processDeviceRemoved(device);
Charles Chan17d75d82017-06-15 00:44:51 -07001542
Saurav Dasfbe74572017-08-03 18:30:35 -07001543 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1544 if (gh != null) {
1545 gh.shutdown();
1546 }
Saurav Das62ae6792017-05-15 15:34:25 -07001547 // Note that a switch going down is associated with all of its links
1548 // going down as well, but it is treated as a single switch down event
Saurav Dasdebcf882018-04-06 20:16:01 -07001549 // while the link-downs are ignored. We cannot rely on the ordering of
1550 // events - i.e we cannot expect all link-downs to come before the
1551 // switch down - so we purge all seen-links for the switch before
1552 // handling route-path changes for the switch-down
Saurav Das62ae6792017-05-15 15:34:25 -07001553 defaultRoutingHandler
Saurav Dasdc7f2752018-03-18 21:28:15 -07001554 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Das6430f412018-01-25 09:49:01 -08001555 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan82f19972016-05-17 13:13:55 -07001556 xConnectHandler.removeDevice(device.id());
Saurav Das97241862018-02-14 14:14:54 -08001557
1558 // Cleanup all internal groupHandler stores for this device. Should be
1559 // done after all rerouting or rehashing has been completed
1560 groupHandlerMap.entrySet()
1561 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001562 }
1563
Saurav Das49368392018-04-23 18:42:12 -07001564 /**
1565 * Purge the destinationSet nextObjective store of entries with this device
1566 * as key. Erases app-level knowledge of hashed groups in this device.
1567 *
1568 * @param devId the device identifier
1569 */
Saurav Das00e553b2018-04-21 17:19:48 -07001570 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Das49368392018-04-23 18:42:12 -07001571 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Das00e553b2018-04-21 17:19:48 -07001572 dsNextObjStore.entrySet().stream()
1573 .filter(entry -> entry.getKey().deviceId().equals(devId))
1574 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1575 }
1576
Saurav Dasec683dc2018-04-27 18:42:30 -07001577 private void processPortUpdatedInternal(Device device, Port port) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001578 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1579 log.warn("Device configuration uploading. Not handling port event for"
1580 + "dev: {} port: {}", device.id(), port.number());
1581 return;
1582 }
Saurav Dasf9332192017-02-18 14:05:44 -08001583
Saurav Dasec683dc2018-04-27 18:42:30 -07001584 if (interfaceService.isConfigured(new ConnectPoint(device.id(), port.number()))) {
1585 lastEdgePortEvent = Instant.now();
1586 }
1587
Saurav Dasf9332192017-02-18 14:05:44 -08001588 if (!mastershipService.isLocalMaster(device.id())) {
1589 log.debug("Not master for dev:{} .. not handling port updated event"
1590 + "for port {}", device.id(), port.number());
1591 return;
1592 }
Saurav Dasec683dc2018-04-27 18:42:30 -07001593 processPortUpdated(device.id(), port);
1594 }
Saurav Dasf9332192017-02-18 14:05:44 -08001595
Saurav Dasec683dc2018-04-27 18:42:30 -07001596 /**
1597 * Adds or remove filtering rules for the given switchport. If switchport is
1598 * an edge facing port, additionally handles host probing and broadcast
1599 * rules. Must be called by local master of device.
1600 *
1601 * @param deviceId the device identifier
1602 * @param port the port to update
1603 */
1604 void processPortUpdated(DeviceId deviceId, Port port) {
Saurav Dasf9332192017-02-18 14:05:44 -08001605 // first we handle filtering rules associated with the port
1606 if (port.isEnabled()) {
1607 log.info("Switchport {}/{} enabled..programming filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001608 deviceId, port.number());
1609 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001610 } else {
1611 log.info("Switchport {}/{} disabled..removing filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001612 deviceId, port.number());
1613 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001614 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001615
1616 // portUpdated calls are for ports that have gone down or up. For switch
1617 // to switch ports, link-events should take care of any re-routing or
1618 // group editing necessary for port up/down. Here we only process edge ports
1619 // that are already configured.
Saurav Dasec683dc2018-04-27 18:42:30 -07001620 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
Charles Chan098ca202018-05-01 11:50:20 -07001621 VlanId untaggedVlan = interfaceService.getUntaggedVlanId(cp);
1622 VlanId nativeVlan = interfaceService.getNativeVlanId(cp);
1623 Set<VlanId> taggedVlans = interfaceService.getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001624
Saurav Das3fb28272017-03-04 16:08:47 -08001625 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001626 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasec683dc2018-04-27 18:42:30 -07001627 + "dev/port: {}/{}", deviceId, port.number());
Saurav Dasf0f592d2016-11-18 15:21:57 -08001628 return;
1629 }
Saurav Das3fb28272017-03-04 16:08:47 -08001630 if (untaggedVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001631 processEdgePort(deviceId, port, untaggedVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001632 }
1633 if (nativeVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001634 processEdgePort(deviceId, port, nativeVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001635 }
1636 if (!taggedVlans.isEmpty()) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001637 taggedVlans.forEach(tag -> processEdgePort(deviceId, port, tag, false));
Saurav Das3fb28272017-03-04 16:08:47 -08001638 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001639 }
1640
Saurav Dasec683dc2018-04-27 18:42:30 -07001641 private void processEdgePort(DeviceId deviceId, Port port, VlanId vlanId,
Saurav Das3fb28272017-03-04 16:08:47 -08001642 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001643 boolean portUp = port.isEnabled();
1644 if (portUp) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001645 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001646 port.number(), vlanId);
Charles Chan074fae62018-04-29 18:11:37 -07001647 hostEventExecutor.execute(() -> hostHandler.processPortUp(new ConnectPoint(deviceId, port.number())));
Saurav Dasf0f592d2016-11-18 15:21:57 -08001648 } else {
Saurav Dasec683dc2018-04-27 18:42:30 -07001649 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001650 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001651 }
1652
Saurav Dasec683dc2018-04-27 18:42:30 -07001653 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001654 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001655 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001656 } else {
1657 log.warn("Group handler not found for dev:{}. Not handling edge port"
Saurav Dasec683dc2018-04-27 18:42:30 -07001658 + " {} event for port:{}", deviceId,
Saurav Dasf0f592d2016-11-18 15:21:57 -08001659 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001660 }
1661 }
sangho27462c62015-05-14 00:39:53 -07001662
Charles Chan8ca5a122017-10-20 16:06:55 -07001663 private void createOrUpdateDeviceConfiguration() {
1664 if (deviceConfiguration == null) {
Saurav Dase321cff2018-02-09 17:26:45 -08001665 log.info("Creating new DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001666 deviceConfiguration = new DeviceConfiguration(this);
1667 } else {
Saurav Dase321cff2018-02-09 17:26:45 -08001668 log.info("Updating DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001669 deviceConfiguration.updateConfig();
1670 }
1671 }
1672
Charles Chanfbcb8812018-04-18 18:41:05 -07001673 private void createOrUpdateDefaultRoutingHandler() {
1674 if (defaultRoutingHandler == null) {
1675 log.info("Creating new DefaultRoutingHandler");
1676 defaultRoutingHandler = new DefaultRoutingHandler(this);
1677 } else {
1678 log.info("Updating DefaultRoutingHandler");
1679 defaultRoutingHandler.update(this);
1680 }
1681 }
1682
Pier Ventreb6a7f342016-11-26 21:05:22 -08001683 /**
1684 * Registers the given connect point with the NRS, this is necessary
1685 * to receive the NDP and ARP packets from the NRS.
1686 *
1687 * @param portToRegister connect point to register
1688 */
1689 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001690 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001691 portToRegister,
1692 neighbourHandler,
1693 appId
1694 );
1695 }
1696
Charles Chan72f556a2015-10-05 17:50:33 -07001697 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001698 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001699 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001700
Charles Chanb7f75ac2016-01-11 18:28:54 -08001701 /**
1702 * Constructs the internal network config listener.
1703 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001704 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001705 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001706 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001707 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001708 }
1709
Charles Chanb7f75ac2016-01-11 18:28:54 -08001710 /**
1711 * Reads network config and initializes related data structure accordingly.
1712 */
Charles Chan873661e2017-11-30 15:37:50 -08001713 void configureNetwork() {
Saurav Dase321cff2018-02-09 17:26:45 -08001714 log.info("Configuring network ...");
Andrea Campanella060cad82018-04-17 12:09:34 +02001715
1716 // Setting handling of network configuration events completable future
1717 // The completable future is needed because of the async behaviour of the configureNetwork,
1718 // listener registration and event arrival
1719 // Enables us to buffer the events and execute them when the configure network is done.
1720 networkConfigCompletion = new CompletableFuture<>();
1721
1722 // add a small delay to absorb multiple network config added notifications
1723 if (!programmingScheduled.get()) {
1724 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1725 programmingScheduled.set(true);
1726 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1727 }
1728
Charles Chan8ca5a122017-10-20 16:06:55 -07001729 createOrUpdateDeviceConfiguration();
Charles Chane7c61022015-10-07 14:21:45 -07001730
Charles Chan46fdfaf2016-11-09 20:51:44 -08001731 arpHandler = new ArpHandler(srManager);
1732 icmpHandler = new IcmpHandler(srManager);
1733 ipHandler = new IpHandler(srManager);
1734 routingRulePopulator = new RoutingRulePopulator(srManager);
Charles Chanfbcb8812018-04-18 18:41:05 -07001735 createOrUpdateDefaultRoutingHandler();
Charles Chane7c61022015-10-07 14:21:45 -07001736
1737 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1738 groupHandlerMap, tunnelStore);
1739 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1740 flowObjectiveService,
1741 tunnelHandler, policyStore);
Andrea Campanella060cad82018-04-17 12:09:34 +02001742 networkConfigCompletion.complete(true);
1743
Charles Chan72779502016-04-23 17:36:10 -07001744 mcastHandler.init();
Andrea Campanella060cad82018-04-17 12:09:34 +02001745
Charles Chane7c61022015-10-07 14:21:45 -07001746 }
1747
Charles Chan72f556a2015-10-05 17:50:33 -07001748 @Override
1749 public void event(NetworkConfigEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001750 if (mainEventExecutor == null) {
1751 return;
1752 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001753 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
1754 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1755 switch (event.type()) {
1756 case CONFIG_ADDED:
1757 case CONFIG_UPDATED:
1758 case CONFIG_REMOVED:
1759 log.trace("Schedule Network Config event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001760 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1761 mainEventExecutor.execute(new InternalEventHandler(event));
1762 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001763 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001764 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001765 break;
1766 default:
1767 break;
Charles Chan72f556a2015-10-05 17:50:33 -07001768 }
1769 }
Saurav Das261c3002017-06-13 15:35:54 -07001770
Charles Chanc4a68c32018-01-03 16:26:32 -08001771 @Override
1772 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase321cff2018-02-09 17:26:45 -08001773 if (event.type() == CONFIG_REGISTERED ||
1774 event.type() == CONFIG_UNREGISTERED) {
1775 log.debug("Ignore event {} due to type mismatch", event);
1776 return false;
Charles Chanc4a68c32018-01-03 16:26:32 -08001777 }
Saurav Dase321cff2018-02-09 17:26:45 -08001778
1779 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1780 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1781 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -08001782 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase321cff2018-02-09 17:26:45 -08001783 log.debug("Ignore event {} due to class mismatch", event);
1784 return false;
1785 }
1786
1787 return true;
Charles Chanc4a68c32018-01-03 16:26:32 -08001788 }
1789
Saurav Das261c3002017-06-13 15:35:54 -07001790 private final class ConfigChange implements Runnable {
1791 @Override
1792 public void run() {
1793 programmingScheduled.set(false);
Saurav Dase321cff2018-02-09 17:26:45 -08001794 log.info("Reacting to config changes after buffer delay");
Saurav Das261c3002017-06-13 15:35:54 -07001795 for (Device device : deviceService.getDevices()) {
1796 processDeviceAdded(device);
1797 }
1798 defaultRoutingHandler.startPopulationProcess();
1799 }
1800 }
Charles Chan72f556a2015-10-05 17:50:33 -07001801 }
Charles Chanf4586112015-11-09 16:37:23 -08001802
Charles Chan3f4aca62018-03-21 16:57:47 -07001803 private class InternalLinkListener implements LinkListener {
1804 @Override
1805 public void event(LinkEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001806 if (mainEventExecutor == null) {
1807 return;
1808 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001809 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1810 event.type() == LinkEvent.Type.LINK_UPDATED ||
1811 event.type() == LinkEvent.Type.LINK_REMOVED) {
1812 log.trace("Schedule Link event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001813 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1814 mainEventExecutor.execute(new InternalEventHandler(event));
1815 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001816 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001817 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001818 }
1819 }
1820 }
1821
1822 private class InternalDeviceListener implements DeviceListener {
1823 @Override
1824 public void event(DeviceEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001825 if (mainEventExecutor == null) {
1826 return;
1827 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001828 switch (event.type()) {
1829 case DEVICE_ADDED:
1830 case PORT_UPDATED:
1831 case PORT_ADDED:
1832 case DEVICE_UPDATED:
1833 case DEVICE_AVAILABILITY_CHANGED:
1834 log.trace("Schedule Device event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001835 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1836 mainEventExecutor.execute(new InternalEventHandler(event));
1837 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001838 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001839 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001840 break;
1841 default:
1842 }
1843 }
1844 }
1845
1846 private class InternalTopologyListener implements TopologyListener {
1847 @Override
1848 public void event(TopologyEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001849 if (mainEventExecutor == null) {
1850 return;
1851 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001852 switch (event.type()) {
1853 case TOPOLOGY_CHANGED:
1854 log.trace("Schedule Topology event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001855 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1856 mainEventExecutor.execute(new InternalEventHandler(event));
1857 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001858 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001859 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001860 break;
1861 default:
1862 }
1863 }
1864 }
1865
Charles Chanf4586112015-11-09 16:37:23 -08001866 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001867 @Override
1868 public void event(HostEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001869 if (hostEventExecutor == null) {
1870 return;
1871 }
Charles Chanf4586112015-11-09 16:37:23 -08001872 switch (event.type()) {
1873 case HOST_ADDED:
Charles Chanf4586112015-11-09 16:37:23 -08001874 case HOST_MOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001875 case HOST_REMOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001876 case HOST_UPDATED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001877 log.trace("Schedule Host event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001878 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chanf4586112015-11-09 16:37:23 -08001879 break;
1880 default:
1881 log.warn("Unsupported host event type: {}", event.type());
1882 break;
1883 }
1884 }
1885 }
1886
Charles Chanc91c8782016-03-30 17:54:24 -07001887 private class InternalMcastListener implements McastListener {
1888 @Override
1889 public void event(McastEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001890 if (mcastEventExecutor == null) {
1891 return;
1892 }
Charles Chanc91c8782016-03-30 17:54:24 -07001893 switch (event.type()) {
Pier1f87aca2018-03-14 16:47:32 -07001894 case SOURCES_ADDED:
1895 case SOURCES_REMOVED:
1896 case SINKS_ADDED:
1897 case SINKS_REMOVED:
Charles Chanc91c8782016-03-30 17:54:24 -07001898 case ROUTE_REMOVED:
Pierdb27b8d2018-04-17 16:29:56 +02001899 case ROUTE_ADDED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001900 log.trace("Schedule Mcast event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001901 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi6786b922018-02-02 16:19:11 +01001902 break;
Charles Chanc91c8782016-03-30 17:54:24 -07001903 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07001904 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chanc91c8782016-03-30 17:54:24 -07001905 break;
1906 }
1907 }
1908 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001909
Charles Chandebfea32016-10-24 14:52:01 -07001910 private class InternalRouteEventListener implements RouteListener {
1911 @Override
1912 public void event(RouteEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001913 if (routeEventExecutor == null) {
1914 return;
1915 }
Charles Chandebfea32016-10-24 14:52:01 -07001916 switch (event.type()) {
1917 case ROUTE_ADDED:
Charles Chandebfea32016-10-24 14:52:01 -07001918 case ROUTE_UPDATED:
Charles Chandebfea32016-10-24 14:52:01 -07001919 case ROUTE_REMOVED:
Charles Chan910be6a2017-08-23 14:46:43 -07001920 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001921 log.trace("Schedule Route event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001922 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan910be6a2017-08-23 14:46:43 -07001923 break;
Charles Chandebfea32016-10-24 14:52:01 -07001924 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07001925 log.warn("Unsupported route event type: {}", event.type());
Charles Chandebfea32016-10-24 14:52:01 -07001926 break;
1927 }
1928 }
1929 }
Saurav Das62ae6792017-05-15 15:34:25 -07001930
Charles Chanfbcb8812018-04-18 18:41:05 -07001931 private class InternalMastershipListener implements MastershipListener {
1932 @Override
1933 public void event(MastershipEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001934 if (mainEventExecutor == null) {
1935 return;
1936 }
Charles Chanfbcb8812018-04-18 18:41:05 -07001937 switch (event.type()) {
Saurav Das00e553b2018-04-21 17:19:48 -07001938 case MASTER_CHANGED:
1939 log.debug("Mastership event: {}/{}", event.subject(),
1940 event.roleInfo());
1941 mainEventExecutor.execute(new InternalEventHandler(event));
1942 break;
1943 case BACKUPS_CHANGED:
1944 case SUSPENDED:
1945 default:
1946 log.debug("Mastership event type {} not handled", event.type());
1947 break;
Charles Chanfbcb8812018-04-18 18:41:05 -07001948 }
1949 }
1950 }
1951
Saurav Das00e553b2018-04-21 17:19:48 -07001952 class InternalClusterListener implements ClusterEventListener {
1953 private Instant lastClusterEvent = Instant.EPOCH;
1954
1955 long timeSinceLastClusterEvent() {
1956 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
1957 }
1958
1959 @Override
1960 public void event(ClusterEvent event) {
1961 switch (event.type()) {
1962 case INSTANCE_ACTIVATED:
1963 case INSTANCE_ADDED:
1964 case INSTANCE_READY:
1965 log.debug("Cluster event {} ignored", event.type());
1966 break;
1967 case INSTANCE_DEACTIVATED:
1968 case INSTANCE_REMOVED:
1969 log.info("** Cluster event {}", event.type());
1970 lastClusterEvent = Instant.now();
1971 break;
1972 default:
1973 break;
1974 }
1975
1976 }
1977
1978 }
1979
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001980 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1981 try {
1982 Set<Interface> intfs = conf.getInterfaces();
1983 Set<Interface> prevIntfs = prevConf.getInterfaces();
1984
1985 // Now we only handle one interface config at each port.
1986 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1987 log.warn("Interface update aborted - one at a time is allowed, " +
1988 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1989 return;
1990 }
1991
Andrea Campanella060cad82018-04-17 12:09:34 +02001992 //The system is in an incoherent state, abort
1993 if (defaultRoutingHandler == null) {
1994 log.warn("Interface update aborted, defaultRoutingHandler is null");
1995 return;
1996 }
1997
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001998 Interface intf = intfs.stream().findFirst().get();
1999 Interface prevIntf = prevIntfs.stream().findFirst().get();
2000
2001 DeviceId deviceId = intf.connectPoint().deviceId();
2002 PortNumber portNum = intf.connectPoint().port();
2003
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002004 removeSubnetConfig(prevIntf.connectPoint(),
2005 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
2006 new HashSet<>(intf.ipAddressesList())));
2007
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002008 if (!prevIntf.vlanNative().equals(VlanId.NONE)
2009 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
2010 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
2011 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
2012 // Update filtering objective and L2IG group bucket
2013 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
2014 } else {
2015 // RemoveVlanNative
2016 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
2017 }
2018 }
2019
2020 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
2021 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2022 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2023 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
2024 // Update filtering objective and L2IG group bucket
2025 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
2026 } else {
2027 // RemoveVlanUntagged
2028 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
2029 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002030 }
2031
2032 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2033 // RemoveVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002034 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
2035 .filter(i -> !intf.vlanUntagged().equals(i))
2036 .filter(i -> !intf.vlanNative().equals(i))
2037 .forEach(vlanId -> updateVlanConfigInternal(
2038 deviceId, portNum, vlanId, false, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002039 }
2040
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002041 if (!intf.vlanNative().equals(VlanId.NONE)
2042 && !prevIntf.vlanNative().equals(intf.vlanNative())
2043 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2044 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
2045 // Update filtering objective and L2IG group bucket
2046 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
2047 } else {
2048 // AddVlanNative
2049 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
2050 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002051 }
2052
2053 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2054 // AddVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002055 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
2056 .filter(i -> !prevIntf.vlanUntagged().equals(i))
2057 .filter(i -> !prevIntf.vlanNative().equals(i))
2058 .forEach(vlanId -> updateVlanConfigInternal(
2059 deviceId, portNum, vlanId, false, true)
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002060 );
2061 }
2062
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002063 if (!intf.vlanUntagged().equals(VlanId.NONE)
2064 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2065 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
2066 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
2067 // Update filtering objective and L2IG group bucket
2068 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
2069 } else {
2070 // AddVlanUntagged
2071 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
2072 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002073 }
2074 addSubnetConfig(prevIntf.connectPoint(),
2075 Sets.difference(new HashSet<>(intf.ipAddressesList()),
2076 new HashSet<>(prevIntf.ipAddressesList())));
2077 } catch (ConfigException e) {
2078 log.error("Error in configuration");
2079 }
2080 }
2081
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002082 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
2083 VlanId vlanId, boolean pushVlan) {
2084 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2085 if (grpHandler == null) {
2086 log.warn("Failed to retrieve group handler for device {}", deviceId);
2087 return;
2088 }
2089
2090 // Update filtering objective for a single port
2091 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
2092 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
2093
2094 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
2095 // Update L2IG bucket of the port
2096 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
2097 } else {
2098 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2099 }
2100 }
2101
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002102 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
2103 VlanId vlanId, boolean pushVlan, boolean install) {
2104 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2105 if (grpHandler == null) {
2106 log.warn("Failed to retrieve group handler for device {}", deviceId);
2107 return;
2108 }
2109
2110 // Update filtering objective for a single port
2111 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
2112
2113 // Update filtering objective for multicast ingress port
2114 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
2115
2116 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
2117
2118 if (nextId != -1 && !install) {
2119 // Update next objective for a single port as an output port
2120 // Remove a single port from L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002121 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002122 // Remove L2 Bridging rule and L3 Unicast rule to the host
Charles Chan074fae62018-04-29 18:11:37 -07002123 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2124 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002125 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
2126 // only if there is no port configured on that VLAN ID
2127 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
2128 // Remove broadcast forwarding rule for the VLAN
2129 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2130 // Remove L2FG for VLAN
2131 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
2132 } else {
2133 // Remove L2IG of the port
2134 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
2135 }
2136 } else if (install) {
2137 if (nextId != -1) {
2138 // Add a single port to L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002139 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002140 } else {
2141 // Create L2FG for VLAN
2142 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
2143 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2144 }
Charles Chan074fae62018-04-29 18:11:37 -07002145 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2146 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002147 } else {
2148 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2149 }
2150 }
2151
2152 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2153 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2154 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2155
2156 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2157 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2158 .filter(intf -> !intf.connectPoint().equals(cp))
2159 .flatMap(intf -> intf.ipAddressesList().stream())
2160 .collect(Collectors.toSet());
2161 // 1. Partial subnet population
2162 // Remove routing rules for removed subnet from previous configuration,
2163 // which does not also exist in other interfaces in the same device
2164 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2165 .map(InterfaceIpAddress::subnetAddress)
2166 .collect(Collectors.toSet());
2167
2168 defaultRoutingHandler.revokeSubnet(
2169 ipPrefixSet.stream()
2170 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2171 .collect(Collectors.toSet()));
2172
2173 // 2. Interface IP punts
2174 // Remove IP punts for old Intf address
2175 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2176 .map(InterfaceIpAddress::ipAddress)
2177 .collect(Collectors.toSet());
2178 ipAddressSet.stream()
2179 .map(InterfaceIpAddress::ipAddress)
2180 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2181 .forEach(interfaceIpAddress ->
2182 routingRulePopulator.revokeSingleIpPunts(
2183 cp.deviceId(), interfaceIpAddress));
2184
2185 // 3. Host unicast routing rule
2186 // Remove unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002187 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002188 }
2189
2190 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2191 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2192 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2193
2194 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2195 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2196 .filter(intf -> !intf.connectPoint().equals(cp))
2197 .flatMap(intf -> intf.ipAddressesList().stream())
2198 .collect(Collectors.toSet());
2199 // 1. Partial subnet population
2200 // Add routing rules for newly added subnet, which does not also exist in
2201 // other interfaces in the same device
2202 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2203 .map(InterfaceIpAddress::subnetAddress)
2204 .collect(Collectors.toSet());
2205
2206 defaultRoutingHandler.populateSubnet(
2207 Collections.singleton(cp),
2208 ipPrefixSet.stream()
2209 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2210 .collect(Collectors.toSet()));
2211
2212 // 2. Interface IP punts
2213 // Add IP punts for new Intf address
2214 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2215 .map(InterfaceIpAddress::ipAddress)
2216 .collect(Collectors.toSet());
2217 ipAddressSet.stream()
2218 .map(InterfaceIpAddress::ipAddress)
2219 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2220 .forEach(interfaceIpAddress ->
2221 routingRulePopulator.populateSingleIpPunts(
2222 cp.deviceId(), interfaceIpAddress));
2223
2224 // 3. Host unicast routing rule
2225 // Add unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002226 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002227 }
sangho80f11cb2015-04-01 13:05:26 -07002228}