blob: 00bfe93c8aa11e32dede7e4b791bf89e0cbc7ec7 [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sanghob35a6192015-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 Hart8ca2bc02017-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 Chan9640c812017-08-23 13:55:39 -070022import com.google.common.collect.Sets;
sanghob35a6192015-04-01 13:05:26 -070023import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
Charles Chan47933752017-11-30 15:37:50 -080026import org.apache.felix.scr.annotations.Modified;
27import org.apache.felix.scr.annotations.Property;
sanghob35a6192015-04-01 13:05:26 -070028import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho1e575652015-05-14 00:39:53 -070030import org.apache.felix.scr.annotations.Service;
sanghob35a6192015-04-01 13:05:26 -070031import org.onlab.packet.Ethernet;
Pier Ventre735b8c82016-12-02 08:16:05 -080032import org.onlab.packet.ICMP6;
Charles Chanc42e84e2015-10-20 16:24:19 -070033import org.onlab.packet.IPv4;
Pier Ventre10bd8d12016-11-26 21:05:22 -080034import org.onlab.packet.IPv6;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070035import org.onlab.packet.IpAddress;
Charles Chanc42e84e2015-10-20 16:24:19 -070036import org.onlab.packet.IpPrefix;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070037import org.onlab.packet.VlanId;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070038import org.onlab.util.KryoNamespace;
Charles Chan47933752017-11-30 15:37:50 -080039import org.onlab.util.Tools;
Saurav Das80980c72016-03-23 11:22:49 -070040import org.onosproject.cfg.ComponentConfigService;
Pier Luigieba73a02018-01-16 10:47:50 +010041import org.onosproject.cluster.ClusterService;
Pier96f63cb2018-04-17 16:29:56 +020042import org.onosproject.cluster.NodeId;
sanghob35a6192015-04-01 13:05:26 -070043import org.onosproject.core.ApplicationId;
44import org.onosproject.core.CoreService;
45import org.onosproject.event.Event;
Charles Chan50bb6ef2018-04-18 18:41:05 -070046import org.onosproject.mastership.MastershipEvent;
47import org.onosproject.mastership.MastershipListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070048import org.onosproject.mastership.MastershipService;
Pier3ee24552018-03-14 16:47:32 -070049import org.onosproject.mcast.api.McastEvent;
50import org.onosproject.mcast.api.McastListener;
51import org.onosproject.mcast.api.MulticastRouteService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080052import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070053import org.onosproject.net.Device;
54import org.onosproject.net.DeviceId;
Charles Chan9640c812017-08-23 13:55:39 -070055import org.onosproject.net.Host;
56import org.onosproject.net.HostId;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070057import org.onosproject.net.Link;
58import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080059import org.onosproject.net.PortNumber;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070060import org.onosproject.net.config.ConfigException;
Charles Chand6832882015-10-05 17:50:33 -070061import org.onosproject.net.config.ConfigFactory;
62import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070063import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070064import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070065import org.onosproject.net.config.basics.InterfaceConfig;
66import org.onosproject.net.config.basics.McastConfig;
Charles Chand6832882015-10-05 17:50:33 -070067import org.onosproject.net.config.basics.SubjectFactories;
Saurav Das45f48152018-01-18 12:07:33 -080068import org.onosproject.net.device.DeviceAdminService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070069import org.onosproject.net.device.DeviceEvent;
70import org.onosproject.net.device.DeviceListener;
71import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080072import org.onosproject.net.flow.TrafficSelector;
73import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070074import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan68aa62d2015-11-09 16:37:23 -080075import org.onosproject.net.host.HostEvent;
76import org.onosproject.net.host.HostListener;
Charles Chan47933752017-11-30 15:37:50 -080077import org.onosproject.net.host.HostLocationProbingService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080078import org.onosproject.net.host.HostService;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070079import org.onosproject.net.host.InterfaceIpAddress;
Pier96f63cb2018-04-17 16:29:56 +020080import org.onosproject.net.intent.WorkPartitionService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070081import org.onosproject.net.intf.Interface;
82import org.onosproject.net.intf.InterfaceService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080083import org.onosproject.net.link.LinkEvent;
84import org.onosproject.net.link.LinkListener;
85import org.onosproject.net.link.LinkService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070086import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080087import org.onosproject.net.packet.InboundPacket;
88import org.onosproject.net.packet.PacketContext;
89import org.onosproject.net.packet.PacketProcessor;
90import org.onosproject.net.packet.PacketService;
Pier Luigi83f919b2018-02-15 16:33:08 +010091import org.onosproject.net.topology.TopologyEvent;
92import org.onosproject.net.topology.TopologyListener;
Charles Chand55e84d2016-03-30 17:54:24 -070093import org.onosproject.net.topology.TopologyService;
Charles Chan9640c812017-08-23 13:55:39 -070094import org.onosproject.routeservice.ResolvedRoute;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070095import org.onosproject.routeservice.RouteEvent;
96import org.onosproject.routeservice.RouteListener;
97import org.onosproject.routeservice.RouteService;
Charles Chand55e84d2016-03-30 17:54:24 -070098import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
99import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventref34966c2016-11-07 16:21:04 -0800100import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700101import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -0700102import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -0700103import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700104import org.onosproject.segmentrouting.grouphandler.DestinationSet;
105import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Pier Luigi96fe0772018-02-28 12:10:50 +0100106import org.onosproject.segmentrouting.mcast.McastHandler;
107import org.onosproject.segmentrouting.mcast.McastRole;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700108import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700109import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800110import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700111import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800112
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800113import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700114import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800115import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800116import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
117
Saurav Das7bcbe702017-06-13 15:35:54 -0700118import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Pier Luigib29144d2018-01-15 18:06:43 +0100119import org.onosproject.segmentrouting.storekey.McastStoreKey;
Charles Chand2990362016-04-18 13:44:03 -0700120import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -0800121import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -0700122import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700123import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700124import org.onosproject.store.service.EventuallyConsistentMap;
125import org.onosproject.store.service.EventuallyConsistentMapBuilder;
126import org.onosproject.store.service.StorageService;
127import org.onosproject.store.service.WallClockTimestamp;
Charles Chan47933752017-11-30 15:37:50 -0800128import org.osgi.service.component.ComponentContext;
sanghob35a6192015-04-01 13:05:26 -0700129import org.slf4j.Logger;
130import org.slf4j.LoggerFactory;
131
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700132import java.util.ArrayList;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800133import java.util.Collections;
Charles Chan47933752017-11-30 15:37:50 -0800134import java.util.Dictionary;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800135import java.util.HashSet;
136import java.util.List;
137import java.util.Map;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800138import java.util.Optional;
139import java.util.Set;
140import java.util.concurrent.ConcurrentHashMap;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800141import java.util.concurrent.Executors;
142import java.util.concurrent.ScheduledExecutorService;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800143import java.util.concurrent.TimeUnit;
144import java.util.concurrent.atomic.AtomicBoolean;
145import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -0700146
Charles Chan3e783d02016-02-26 22:19:52 -0800147import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800148import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700149import static org.onlab.util.Tools.groupedThreads;
Saurav Dase7f51012018-02-09 17:26:45 -0800150import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
151import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Charles Chan3e783d02016-02-26 22:19:52 -0800152
Charles Chane849c192016-01-11 18:28:54 -0800153/**
154 * Segment routing manager.
155 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700156@Service
157@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700158public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700159
Charles Chan2c15aca2016-11-09 20:51:44 -0800160 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan2fde6d42017-08-23 14:46:43 -0700161 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sanghob35a6192015-04-01 13:05:26 -0700162
163 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700164 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700165
166 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800167 private NeighbourResolutionService neighbourResolutionService;
168
169 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100170 public CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700171
172 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700173 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700174
175 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700176 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700177
178 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan47933752017-11-30 15:37:50 -0800179 HostLocationProbingService probingService;
180
181 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100182 public DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700183
184 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Saurav Das45f48152018-01-18 12:07:33 -0800185 DeviceAdminService deviceAdminService;
186
187 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800188 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700189
190 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100191 public LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700192
Charles Chan5270ed02016-01-30 23:22:37 -0800193 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800194 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700195
196 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800197 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700198
199 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100200 public MulticastRouteService multicastRouteService;
Charles Chan03a73e02016-10-24 14:52:01 -0700201
202 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chanf237e1b2018-01-09 13:45:07 -0800203 public TopologyService topologyService;
Charles Chan03a73e02016-10-24 14:52:01 -0700204
205 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700206 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800207
208 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800209 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800210
Saurav Das80980c72016-03-23 11:22:49 -0700211 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800212 public InterfaceService interfaceService;
213
Pier Luigieba73a02018-01-16 10:47:50 +0100214 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
215 public ClusterService clusterService;
216
217 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier96f63cb2018-04-17 16:29:56 +0200218 public WorkPartitionService workPartitionService;
Pier Luigieba73a02018-01-16 10:47:50 +0100219
Charles Chan47933752017-11-30 15:37:50 -0800220 @Property(name = "activeProbing", boolValue = true,
221 label = "Enable active probing to discover dual-homed hosts.")
222 boolean activeProbing = true;
223
Charles Chan03a73e02016-10-24 14:52:01 -0700224 ArpHandler arpHandler = null;
225 IcmpHandler icmpHandler = null;
226 IpHandler ipHandler = null;
227 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700228 ApplicationId appId;
229 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700230
Charles Chan03a73e02016-10-24 14:52:01 -0700231 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700232 private TunnelHandler tunnelHandler = null;
233 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700234 private InternalPacketProcessor processor = null;
235 private InternalLinkListener linkListener = null;
236 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700237 private AppConfigHandler appCfgHandler = null;
Pier Luigi96fe0772018-02-28 12:10:50 +0100238 public XConnectHandler xConnectHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800239 McastHandler mcastHandler = null;
240 HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800241 private RouteHandler routeHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800242 LinkHandler linkHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800243 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800244 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigi83f919b2018-02-15 16:33:08 +0100245 private TopologyHandler topologyHandler = null;
Charles Chan5270ed02016-01-30 23:22:37 -0800246 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700247 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
248 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700249 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigi83f919b2018-02-15 16:33:08 +0100250 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chan50bb6ef2018-04-18 18:41:05 -0700251 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
sanghob35a6192015-04-01 13:05:26 -0700252
Charles Chan9b7217c2018-04-05 16:31:15 -0700253 // Handles device, link, topology and network config events
Charles Chan50bb6ef2018-04-18 18:41:05 -0700254 private ScheduledExecutorService mainEventExecutor;
sanghob35a6192015-04-01 13:05:26 -0700255
Charles Chan50bb6ef2018-04-18 18:41:05 -0700256 // Handles host, route and mcast events respectively
257 private ScheduledExecutorService hostEventExecutor;
258 private ScheduledExecutorService routeEventExecutor;
259 private ScheduledExecutorService mcastEventExecutor;
Charles Chan9b7217c2018-04-05 16:31:15 -0700260
261 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chane849c192016-01-11 18:28:54 -0800262 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700263 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700264 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800265 */
Charles Chan47933752017-11-30 15:37:50 -0800266 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das7bcbe702017-06-13 15:35:54 -0700267 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800268 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700269 * Per device next objective ID store with (device id + vlanid) as key.
270 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800271 */
Charles Chan47933752017-11-30 15:37:50 -0800272 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800273 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800274 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700275 * Per device next objective ID store with (device id + port + treatment + meta) as key.
276 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chane849c192016-01-11 18:28:54 -0800277 */
Charles Chan47933752017-11-30 15:37:50 -0800278 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800279 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800280
Saurav Das4ce45962015-11-24 23:21:05 -0800281 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
282 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700283
Saurav Das7bcbe702017-06-13 15:35:54 -0700284 private AtomicBoolean programmingScheduled = new AtomicBoolean();
285
Charles Chand55e84d2016-03-30 17:54:24 -0700286 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700287 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
288 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700289 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700290 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800291 public SegmentRoutingDeviceConfig createConfig() {
292 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700293 }
294 };
Pier Ventref34966c2016-11-07 16:21:04 -0800295
Charles Chand55e84d2016-03-30 17:54:24 -0700296 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700297 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
298 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700299 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800300 @Override
301 public SegmentRoutingAppConfig createConfig() {
302 return new SegmentRoutingAppConfig();
303 }
304 };
Pier Ventref34966c2016-11-07 16:21:04 -0800305
Charles Chanfc5c7802016-05-17 13:13:55 -0700306 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
307 new ConfigFactory<ApplicationId, XConnectConfig>(
308 SubjectFactories.APP_SUBJECT_FACTORY,
309 XConnectConfig.class, "xconnect") {
310 @Override
311 public XConnectConfig createConfig() {
312 return new XConnectConfig();
313 }
314 };
Pier Ventref34966c2016-11-07 16:21:04 -0800315
Charles Chand55e84d2016-03-30 17:54:24 -0700316 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700317 new ConfigFactory<ApplicationId, McastConfig>(
318 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700319 McastConfig.class, "multicast") {
320 @Override
321 public McastConfig createConfig() {
322 return new McastConfig();
323 }
324 };
325
Charles Chan116188d2016-02-18 14:22:42 -0800326 /**
327 * Segment Routing App ID.
328 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800329 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan59cc16d2017-02-02 16:20:42 -0800330
Charles Chane849c192016-01-11 18:28:54 -0800331 /**
332 * The default VLAN ID assigned to the interfaces without subnet config.
333 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800334 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700335
sanghob35a6192015-04-01 13:05:26 -0700336 @Activate
Charles Chan47933752017-11-30 15:37:50 -0800337 protected void activate(ComponentContext context) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800338 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700339
Charles Chan50bb6ef2018-04-18 18:41:05 -0700340 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-main", "%d", log));
341 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-host", "%d", log));
342 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-route", "%d", log));
343 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-mcast", "%d", log));
344
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700345 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700346 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700347 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700348 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700349 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700350 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700351 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700352 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700353 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700354
Charles Chan59cc16d2017-02-02 16:20:42 -0800355 log.debug("Creating EC map vlannextobjectivestore");
356 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
357 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
358 vlanNextObjStore = vlanNextObjMapBuilder
359 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700360 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700361 .withTimestampProvider((k, v) -> new WallClockTimestamp())
362 .build();
363
Saurav Das4ce45962015-11-24 23:21:05 -0800364 log.debug("Creating EC map subnetnextobjectivestore");
365 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
366 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
367 portNextObjStore = portNextObjMapBuilder
368 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700369 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800370 .withTimestampProvider((k, v) -> new WallClockTimestamp())
371 .build();
372
sangho0b2b6d12015-05-20 22:16:38 -0700373 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
374 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700375 tunnelStore = tunnelMapBuilder
376 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700377 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700378 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700379 .build();
380
381 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
382 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700383 policyStore = policyMapBuilder
384 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700385 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700386 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700387 .build();
388
Saurav Das80980c72016-03-23 11:22:49 -0700389 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800390 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700391 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800392 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800393 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
394 "requestInterceptsEnabled", "false");
Charles Chand3baaba2017-08-08 15:13:37 -0700395 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800396 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700397 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800398 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800399 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
400 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800401 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
402 "forceUnprovision", "true");
Charles Chanef624ed2017-08-10 16:57:28 -0700403 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan73316522017-07-20 16:16:25 -0700404 "distributed", "true");
Charles Chan35a32322017-08-14 11:42:11 -0700405 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
406 "multihomingEnabled", "true");
Charles Chan807d87a2017-11-29 19:54:20 -0800407 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
408 "staleLinkAge", "15000");
409 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
410 "allowDuplicateIps", "false");
Charles Chan47933752017-11-30 15:37:50 -0800411 compCfgService.registerProperties(getClass());
412 modified(context);
Saurav Das80980c72016-03-23 11:22:49 -0700413
Charles Chanb8e10c82015-10-14 11:24:40 -0700414 processor = new InternalPacketProcessor();
415 linkListener = new InternalLinkListener();
416 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700417 appCfgHandler = new AppConfigHandler(this);
418 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700419 mcastHandler = new McastHandler(this);
420 hostHandler = new HostHandler(this);
Saurav Das45f48152018-01-18 12:07:33 -0800421 linkHandler = new LinkHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700422 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800423 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800424 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigi83f919b2018-02-15 16:33:08 +0100425 topologyHandler = new TopologyHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700426
Charles Chan3e783d02016-02-26 22:19:52 -0800427 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700428 cfgService.registerConfigFactory(deviceConfigFactory);
429 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700430 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700431 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase7f51012018-02-09 17:26:45 -0800432 log.info("Configuring network before adding listeners");
Charles Chan132393a2018-01-04 14:26:07 -0800433 cfgListener.configureNetwork();
434
Charles Chan5270ed02016-01-30 23:22:37 -0800435 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700436 packetService.addProcessor(processor, PacketProcessor.director(2));
437 linkService.addListener(linkListener);
438 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700439 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700440 routeService.addListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100441 topologyService.addListener(topologyListener);
Charles Chan50bb6ef2018-04-18 18:41:05 -0700442 mastershipService.addListener(mastershipListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700443
Charles Chanb39777c2018-03-09 15:53:44 -0800444 linkHandler.init();
Andreas Pantelopoulos4a768c02018-01-11 07:53:48 -0800445 l2TunnelHandler.init();
446
sanghob35a6192015-04-01 13:05:26 -0700447 log.info("Started");
448 }
449
Saurav Das45f48152018-01-18 12:07:33 -0800450 KryoNamespace.Builder createSerializer() {
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700451 return new KryoNamespace.Builder()
452 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700453 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800454 VlanNextObjectiveStoreKey.class,
455 DestinationSet.class,
456 NextNeighbors.class,
457 Tunnel.class,
458 DefaultTunnel.class,
459 Policy.class,
460 TunnelPolicy.class,
461 Policy.Type.class,
462 PortNextObjectiveStoreKey.class,
463 XConnectStoreKey.class,
464 L2Tunnel.class,
465 L2TunnelPolicy.class,
466 DefaultL2Tunnel.class,
467 DefaultL2TunnelPolicy.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700468 );
469 }
470
sanghob35a6192015-04-01 13:05:26 -0700471 @Deactivate
472 protected void deactivate() {
Charles Chan50bb6ef2018-04-18 18:41:05 -0700473 mainEventExecutor.shutdown();
474 hostEventExecutor.shutdown();
475 routeEventExecutor.shutdown();
476 mcastEventExecutor.shutdown();
477
Charles Chand6832882015-10-05 17:50:33 -0700478 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700479 cfgService.unregisterConfigFactory(deviceConfigFactory);
480 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700481 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700482 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan47933752017-11-30 15:37:50 -0800483 compCfgService.unregisterProperties(getClass(), false);
Charles Chand6832882015-10-05 17:50:33 -0700484
Charles Chanc7a8a682017-06-19 00:43:31 -0700485 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700486 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700487 linkService.removeListener(linkListener);
488 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700489 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700490 routeService.removeListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100491 topologyService.removeListener(topologyListener);
Charles Chan50bb6ef2018-04-18 18:41:05 -0700492 mastershipService.removeListener(mastershipListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700493
Charles Chan0aa674e2017-02-23 15:44:08 -0800494 neighbourResolutionService.unregisterNeighbourHandlers(appId);
495
sanghob35a6192015-04-01 13:05:26 -0700496 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700497 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700498 deviceListener = null;
Charles Chan6577f612018-04-19 13:10:01 -0700499 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chanb8e10c82015-10-14 11:24:40 -0700500 groupHandlerMap.clear();
501
Saurav Das7bcbe702017-06-13 15:35:54 -0700502 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800503 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700504 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700505 tunnelStore.destroy();
506 policyStore.destroy();
Pier Luigib72201b2018-01-25 16:16:02 +0100507
508 mcastHandler.terminate();
sanghob35a6192015-04-01 13:05:26 -0700509 log.info("Stopped");
510 }
511
Charles Chan47933752017-11-30 15:37:50 -0800512 @Modified
513 private void modified(ComponentContext context) {
514 Dictionary<?, ?> properties = context.getProperties();
515 if (properties == null) {
516 return;
517 }
518
519 String strActiveProving = Tools.get(properties, "activeProbing");
520 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
521
522 if (expectActiveProbing != activeProbing) {
523 activeProbing = expectActiveProbing;
524 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
525 }
526 }
527
sangho1e575652015-05-14 00:39:53 -0700528 @Override
529 public List<Tunnel> getTunnels() {
530 return tunnelHandler.getTunnels();
531 }
532
533 @Override
sangho71abe1b2015-06-29 14:58:47 -0700534 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
535 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700536 }
537
538 @Override
sangho71abe1b2015-06-29 14:58:47 -0700539 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700540 for (Policy policy: policyHandler.getPolicies()) {
541 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
542 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
543 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
544 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700545 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700546 }
547 }
548 }
sangho71abe1b2015-06-29 14:58:47 -0700549 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700550 }
551
552 @Override
sangho71abe1b2015-06-29 14:58:47 -0700553 public PolicyHandler.Result removePolicy(Policy policy) {
554 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700555 }
556
557 @Override
sangho71abe1b2015-06-29 14:58:47 -0700558 public PolicyHandler.Result createPolicy(Policy policy) {
559 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700560 }
561
562 @Override
563 public List<Policy> getPolicies() {
564 return policyHandler.getPolicies();
565 }
566
Saurav Das59232cf2016-04-27 18:35:50 -0700567 @Override
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700568 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
569 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700570 }
571
572 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800573 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700574 return l2TunnelHandler.getL2Tunnels();
575 }
576
577 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800578 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700579 return l2TunnelHandler.getL2Policies();
580 }
581
Saurav Das5a356042018-04-06 20:16:01 -0700582 @Override
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700583 @Deprecated
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700584 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
585
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700586 // get both added and pending pseudowires
587 List<L2TunnelDescription> pseudowires = new ArrayList<>();
588 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
589 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700590 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700591
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700592 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
593
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700594 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
595 L2TunnelHandler.Result res;
596 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
597 res = addPseudowire(pw);
598 if (res != L2TunnelHandler.Result.SUCCESS) {
599 log.error("Pseudowire with id {} can not be instantiated !", res);
600 retRes = res;
601 }
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700602 }
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700603
604 return retRes;
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700605 }
606
607 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800608 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700609 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700610 }
611
612 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800613 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700614 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700615 }
616
617 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700618 public void rerouteNetwork() {
619 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700620 }
621
Charles Chanc81c45b2016-10-20 17:02:44 -0700622 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800623 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
624 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800625 deviceConfiguration.getRouters().forEach(device ->
626 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700627 return deviceSubnetMap;
628 }
629
Saurav Dasc88d4662017-05-15 15:34:25 -0700630
631 @Override
632 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
633 if (defaultRoutingHandler != null) {
634 return defaultRoutingHandler.getCurrentEmcpSpgMap();
635 } else {
636 return null;
637 }
638 }
639
640 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700641 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
642 if (dsNextObjStore != null) {
643 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700644 } else {
645 return ImmutableMap.of();
646 }
647 }
648
Saurav Dasceccf242017-08-03 18:30:35 -0700649 @Override
650 public void verifyGroups(DeviceId id) {
651 DefaultGroupHandler gh = groupHandlerMap.get(id);
652 if (gh != null) {
653 gh.triggerBucketCorrector();
654 }
655 }
656
Saurav Dasc568c342018-01-25 09:49:01 -0800657 @Override
658 public ImmutableMap<Link, Boolean> getSeenLinks() {
659 return linkHandler.getSeenLinks();
660 }
661
662 @Override
663 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
664 return linkHandler.getDownedPorts();
665 }
666
Pier Luigib29144d2018-01-15 18:06:43 +0100667 @Override
668 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
669 return mcastHandler.getMcastNextIds(mcastIp);
670 }
671
672 @Override
Pier Luigi96fe0772018-02-28 12:10:50 +0100673 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigib29144d2018-01-15 18:06:43 +0100674 return mcastHandler.getMcastRoles(mcastIp);
675 }
676
677 @Override
678 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
679 return mcastHandler.getMcastPaths(mcastIp);
680 }
681
Pier96f63cb2018-04-17 16:29:56 +0200682 @Override
Pierb1fe7382018-04-17 17:25:22 +0200683 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
684 ConnectPoint sourcecp) {
685 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
686 }
687
688 @Override
Pier96f63cb2018-04-17 16:29:56 +0200689 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
690 return mcastHandler.getMcastLeaders(mcastIp);
691 }
692
Charles Chan8bc75ee2018-04-17 18:56:53 -0700693 @Override
694 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
695 return defaultRoutingHandler == null ? ImmutableMap.of() :
696 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
697 }
698
699 @Override
700 public Map<DeviceId, Boolean> getShouldProgramCache() {
701 return defaultRoutingHandler == null ? ImmutableMap.of() :
702 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
703 }
704
sanghof9d0bf12015-05-19 11:57:42 -0700705 /**
Ray Milkeye4afdb52017-04-05 09:42:04 -0700706 * Extracts the application ID from the manager.
707 *
708 * @return application ID
709 */
710 public ApplicationId appId() {
711 return appId;
712 }
713
714 /**
715 * Returns the device configuration.
716 *
717 * @return device configuration
718 */
719 public DeviceConfiguration deviceConfiguration() {
720 return deviceConfiguration;
721 }
722
723 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700724 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700725 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700726 *
727 * @return next objective ID store
728 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700729 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
730 dsNextObjStore() {
731 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700732 }
733
734 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700735 * Per device next objective ID store with (device id + vlanid) as key.
736 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700737 *
738 * @return vlan next object store
739 */
740 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
741 return vlanNextObjStore;
742 }
743
744 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700745 * Per device next objective ID store with (device id + port + treatment + meta) as key.
746 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700747 *
748 * @return port next object store.
749 */
750 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
751 return portNextObjStore;
752 }
753
754 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700755 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
756 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700757 *
758 * @return MPLS-ECMP value
759 */
760 public boolean getMplsEcmp() {
761 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
762 .getConfig(this.appId, SegmentRoutingAppConfig.class);
763 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
764 }
765
766 /**
sanghof9d0bf12015-05-19 11:57:42 -0700767 * Returns the tunnel object with the tunnel ID.
768 *
769 * @param tunnelId Tunnel ID
770 * @return Tunnel reference
771 */
sangho1e575652015-05-14 00:39:53 -0700772 public Tunnel getTunnel(String tunnelId) {
773 return tunnelHandler.getTunnel(tunnelId);
774 }
775
Charles Chan7ffd81f2017-02-08 15:52:08 -0800776 // TODO Consider moving these to InterfaceService
sanghob35a6192015-04-01 13:05:26 -0700777 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800778 * Returns untagged VLAN configured on given connect point.
Charles Chan7ffd81f2017-02-08 15:52:08 -0800779 * <p>
780 * Only returns the first match if there are multiple untagged VLAN configured
781 * on the connect point.
sanghob35a6192015-04-01 13:05:26 -0700782 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800783 * @param connectPoint connect point
784 * @return untagged VLAN or null if not configured
sanghob35a6192015-04-01 13:05:26 -0700785 */
Charles Chan65238242017-06-22 18:03:14 -0700786 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800787 return interfaceService.getInterfacesByPort(connectPoint).stream()
788 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
789 .map(Interface::vlanUntagged)
790 .findFirst().orElse(null);
sanghob35a6192015-04-01 13:05:26 -0700791 }
792
sangho1e575652015-05-14 00:39:53 -0700793 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800794 * Returns tagged VLAN configured on given connect point.
795 * <p>
796 * Returns all matches if there are multiple tagged VLAN configured
797 * on the connect point.
798 *
799 * @param connectPoint connect point
800 * @return tagged VLAN or empty set if not configured
801 */
Charles Chan65238242017-06-22 18:03:14 -0700802 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800803 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
804 return interfaces.stream()
805 .map(Interface::vlanTagged)
Charles Chan65238242017-06-22 18:03:14 -0700806 .flatMap(Set::stream)
Charles Chan7ffd81f2017-02-08 15:52:08 -0800807 .collect(Collectors.toSet());
808 }
809
810 /**
811 * Returns native VLAN configured on given connect point.
812 * <p>
813 * Only returns the first match if there are multiple native VLAN configured
814 * on the connect point.
815 *
816 * @param connectPoint connect point
817 * @return native VLAN or null if not configured
818 */
Charles Chan65238242017-06-22 18:03:14 -0700819 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800820 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
821 return interfaces.stream()
822 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
823 .map(Interface::vlanNative)
824 .findFirst()
825 .orElse(null);
826 }
827
828 /**
Charles Chanf9a52702017-06-16 15:19:24 -0700829 * Returns internal VLAN for untagged hosts on given connect point.
830 * <p>
831 * The internal VLAN is either vlan-untagged for an access port,
832 * or vlan-native for a trunk port.
833 *
834 * @param connectPoint connect point
835 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
836 */
Pier Luigi96fe0772018-02-28 12:10:50 +0100837 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chanf9a52702017-06-16 15:19:24 -0700838 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
839 VlanId nativeVlanId = getNativeVlanId(connectPoint);
840 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
841 }
842
843 /**
Charles Chan65238242017-06-22 18:03:14 -0700844 * Returns optional pair device ID of given device.
845 *
846 * @param deviceId device ID
847 * @return optional pair device ID. Might be empty if pair device is not configured
848 */
849 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
850 SegmentRoutingDeviceConfig deviceConfig =
851 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
852 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
853 }
854 /**
855 * Returns optional pair device local port of given device.
856 *
857 * @param deviceId device ID
858 * @return optional pair device ID. Might be empty if pair device is not configured
859 */
860 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
861 SegmentRoutingDeviceConfig deviceConfig =
862 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
863 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
864 }
865
866 /**
Charles Chan9640c812017-08-23 13:55:39 -0700867 * Returns locations of given resolved route.
868 *
869 * @param resolvedRoute resolved route
870 * @return locations of nexthop. Might be empty if next hop is not found
871 */
872 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
873 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
874 return Optional.ofNullable(hostService.getHost(hostId))
875 .map(Host::locations).orElse(Sets.newHashSet())
876 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
877 }
878
879 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800880 * Returns vlan port map of given device.
881 *
882 * @param deviceId device id
883 * @return vlan-port multimap
884 */
885 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
886 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
887
888 interfaceService.getInterfaces().stream()
889 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
890 .forEach(intf -> {
891 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -0700892 intf.vlanTagged().forEach(vlanTagged ->
893 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
894 );
Charles Chan7ffd81f2017-02-08 15:52:08 -0800895 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
896 });
897 vlanPortMap.removeAll(VlanId.NONE);
898
899 return vlanPortMap;
900 }
901
902 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800903 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800904 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700905 *
906 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800907 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800908 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700909 */
Charles Chan65238242017-06-22 18:03:14 -0700910 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700911 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800912 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
913 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700914 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800915 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800916 + "device {} not found", deviceId);
917 return -1;
918 }
919 }
920
921 /**
922 * Returns the next objective ID for the given portNumber, given the treatment.
923 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700924 * would result in different objectives. If the next object does not exist,
925 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800926 *
927 * @param deviceId Device ID
928 * @param portNum port number on device for which NextObjective is queried
929 * @param treatment the actions to apply on the packets (should include outport)
930 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700931 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700932 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800933 */
934 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
935 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700936 TrafficSelector meta,
937 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -0800938 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
939 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -0700940 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -0800941 } else {
Charles Chane849c192016-01-11 18:28:54 -0800942 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
943 + " not found", deviceId);
944 return -1;
945 }
946 }
947
Saurav Dasc88d4662017-05-15 15:34:25 -0700948 /**
949 * Returns the group handler object for the specified device id.
950 *
951 * @param devId the device identifier
952 * @return the groupHandler object for the device id, or null if not found
953 */
Charles Chan65238242017-06-22 18:03:14 -0700954 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700955 return groupHandlerMap.get(devId);
956 }
957
958 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700959 * Returns the default routing handler object.
960 *
961 * @return the default routing handler object
962 */
963 public DefaultRoutingHandler getRoutingHandler() {
964 return defaultRoutingHandler;
965 }
966
sanghob35a6192015-04-01 13:05:26 -0700967 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700968 @Override
969 public void process(PacketContext context) {
970
971 if (context.isHandled()) {
972 return;
973 }
974
975 InboundPacket pkt = context.inPacket();
976 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800977
978 if (ethernet == null) {
979 return;
980 }
981
Saurav Das7bcbe702017-06-13 15:35:54 -0700982 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
983 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800984 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700985 log.warn("Received unexpected ARP packet on {}",
986 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -0700987 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800988 return;
sanghob35a6192015-04-01 13:05:26 -0700989 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800990 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
991 //ipHandler.addToPacketBuffer(ipv4Packet);
992 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
993 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700994 } else {
Charles Chan50035632017-01-13 17:20:44 -0800995 // NOTE: We don't support IP learning at this moment so this
996 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800997 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700998 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800999 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1000 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -08001001 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -08001002 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -08001003 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1004 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1005 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1006 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1007 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1008 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -07001009 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -07001010 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -08001011 }
1012 } else {
1013 // NOTE: We don't support IP learning at this moment so this
1014 // is not necessary. Also it causes duplication of DHCPv6 packets.
1015 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1016 }
sanghob35a6192015-04-01 13:05:26 -07001017 }
1018 }
1019 }
1020
sanghob35a6192015-04-01 13:05:26 -07001021 private class InternalEventHandler implements Runnable {
Charles Chan9b7217c2018-04-05 16:31:15 -07001022 private Event event;
1023
1024 InternalEventHandler(Event event) {
1025 this.event = event;
1026 }
1027
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001028 @Override
sanghob35a6192015-04-01 13:05:26 -07001029 public void run() {
Charles Chan9b7217c2018-04-05 16:31:15 -07001030 try {
1031 // TODO We should also change SR routing and PW to listen to TopologyEvents
1032 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1033 event.type() == LinkEvent.Type.LINK_UPDATED) {
1034 linkHandler.processLinkAdded((Link) event.subject());
1035 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1036 linkHandler.processLinkRemoved((Link) event.subject());
1037 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1038 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1039 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1040 DeviceId deviceId = ((Device) event.subject()).id();
1041 if (deviceService.isAvailable(deviceId)) {
1042 log.info("** DEVICE UP Processing device event {} "
1043 + "for available device {}",
1044 event.type(), ((Device) event.subject()).id());
1045 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001046 } else {
Charles Chan9b7217c2018-04-05 16:31:15 -07001047 log.info(" ** DEVICE DOWN Processing device event {}"
1048 + " for unavailable device {}",
1049 event.type(), ((Device) event.subject()).id());
1050 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001051 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001052 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1053 // typically these calls come when device is added first time
1054 // so port filtering rules are handled at the device_added event.
1055 // port added calls represent all ports on the device,
1056 // enabled or not.
1057 log.trace("** PORT ADDED {}/{} -> {}",
1058 ((DeviceEvent) event).subject().id(),
1059 ((DeviceEvent) event).port().number(),
1060 event.type());
1061 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1062 // these calls happen for every subsequent event
1063 // ports enabled, disabled, switch goes away, comes back
1064 log.info("** PORT UPDATED {}/{} -> {}",
1065 event.subject(),
1066 ((DeviceEvent) event).port(),
1067 event.type());
1068 processPortUpdated(((Device) event.subject()),
1069 ((DeviceEvent) event).port());
1070 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1071 // Process topology event, needed for all modules relying on
1072 // topology service for path computation
1073 TopologyEvent topologyEvent = (TopologyEvent) event;
1074 log.info("Processing topology event {}, topology age {}, reasons {}",
1075 event.type(), topologyEvent.subject().time(),
1076 topologyEvent.reasons().size());
1077 topologyHandler.processTopologyChange(topologyEvent.reasons());
1078 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1079 hostHandler.processHostAddedEvent((HostEvent) event);
1080 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1081 hostHandler.processHostMovedEvent((HostEvent) event);
1082 routeHandler.processHostMovedEvent((HostEvent) event);
1083 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1084 hostHandler.processHostRemovedEvent((HostEvent) event);
1085 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1086 hostHandler.processHostUpdatedEvent((HostEvent) event);
1087 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1088 routeHandler.processRouteAdded((RouteEvent) event);
1089 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1090 routeHandler.processRouteUpdated((RouteEvent) event);
1091 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1092 routeHandler.processRouteRemoved((RouteEvent) event);
1093 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1094 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1095 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1096 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1097 event.type() == McastEvent.Type.SINKS_ADDED ||
1098 event.type() == McastEvent.Type.SINKS_REMOVED ||
1099 event.type() == McastEvent.Type.ROUTE_ADDED ||
1100 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1101 mcastHandler.processMcastEvent((McastEvent) event);
1102 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1103 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1104 Class configClass = netcfgEvent.configClass();
1105 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1106 appCfgHandler.processAppConfigAdded(netcfgEvent);
1107 log.info("App config event .. configuring network");
1108 cfgListener.configureNetwork();
1109 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1110 log.info("Segment Routing Device Config added for {}", event.subject());
1111 cfgListener.configureNetwork();
1112 } else if (configClass.equals(XConnectConfig.class)) {
1113 xConnectHandler.processXConnectConfigAdded(netcfgEvent);
1114 } else if (configClass.equals(InterfaceConfig.class)) {
1115 log.info("Interface Config added for {}", event.subject());
1116 cfgListener.configureNetwork();
1117 } else {
1118 log.error("Unhandled config class: {}", configClass);
1119 }
1120 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1121 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1122 Class configClass = netcfgEvent.configClass();
1123 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1124 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1125 log.info("App config event .. configuring network");
1126 cfgListener.configureNetwork();
1127 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1128 log.info("Segment Routing Device Config updated for {}", event.subject());
1129 createOrUpdateDeviceConfiguration();
1130 } else if (configClass.equals(XConnectConfig.class)) {
1131 xConnectHandler.processXConnectConfigUpdated(netcfgEvent);
1132 } else if (configClass.equals(InterfaceConfig.class)) {
1133 log.info("Interface Config updated for {}", event.subject());
1134 createOrUpdateDeviceConfiguration();
1135 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1136 (InterfaceConfig) netcfgEvent.prevConfig().get());
1137 } else {
1138 log.error("Unhandled config class: {}", configClass);
1139 }
1140 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1141 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1142 Class configClass = netcfgEvent.configClass();
1143 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1144 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1145 log.info("App config event .. configuring network");
1146 cfgListener.configureNetwork();
1147 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1148 // TODO Handle sr device config removal
1149 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
1150 } else if (configClass.equals(XConnectConfig.class)) {
1151 xConnectHandler.processXConnectConfigRemoved(netcfgEvent);
1152 } else if (configClass.equals(InterfaceConfig.class)) {
1153 // TODO Handle interface removal
1154 log.info("InterfaceConfig removal is not handled in current implementation");
1155 } else {
1156 log.error("Unhandled config class: {}", configClass);
1157 }
1158 } else {
1159 log.warn("Unhandled event type: {}", event.type());
sanghob35a6192015-04-01 13:05:26 -07001160 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001161 } catch (Exception e) {
1162 log.error("SegmentRouting event handler thread thrown an exception: {}",
1163 e.getMessage(), e);
sanghob35a6192015-04-01 13:05:26 -07001164 }
sanghob35a6192015-04-01 13:05:26 -07001165 }
1166 }
1167
Saurav Das45f48152018-01-18 12:07:33 -08001168 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001169 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001170
1171 // NOTE: Punt ARP/NDP even when the device is not configured.
1172 // Host learning without network config is required for CORD config generator.
1173 routingRulePopulator.populateIpPunts(device.id());
1174 routingRulePopulator.populateArpNdpPunts(device.id());
1175
Charles Chan0b4e6182015-11-03 10:42:14 -08001176 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001177 log.warn("Device configuration unavailable. Device {} will be "
1178 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001179 return;
1180 }
Charles Chan2199c302016-04-23 17:36:10 -07001181 processDeviceAddedInternal(device.id());
1182 }
1183
1184 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001185 // Irrespective of whether the local is a MASTER or not for this device,
1186 // we need to create a SR-group-handler instance. This is because in a
1187 // multi-instance setup, any instance can initiate forwarding/next-objectives
1188 // for any switch (even if this instance is a SLAVE or not even connected
1189 // to the switch). To handle this, a default-group-handler instance is necessary
1190 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001191 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1192 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001193 DefaultGroupHandler groupHandler;
1194 try {
1195 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001196 createGroupHandler(deviceId,
1197 appId,
1198 deviceConfiguration,
1199 linkService,
1200 flowObjectiveService,
1201 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001202 } catch (DeviceConfigNotFoundException e) {
1203 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1204 return;
1205 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001206 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001207 deviceId);
1208 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001209 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001210
Charles Chan2199c302016-04-23 17:36:10 -07001211 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001212 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001213 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001214 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001215 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001216 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001217 }
Charles Chan5270ed02016-01-30 23:22:37 -08001218
Charles Chan03a73e02016-10-24 14:52:01 -07001219 appCfgHandler.init(deviceId);
Charles Chan2ff1bac2018-03-29 16:03:41 -07001220 hostHandler.init(deviceId);
Charles Chan03a73e02016-10-24 14:52:01 -07001221 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001222 }
1223
Saurav Das80980c72016-03-23 11:22:49 -07001224 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001225 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001226 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan47933752017-11-30 15:37:50 -08001227 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001228 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001229 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001230 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001231 portNextObjStore.entrySet().stream()
1232 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001233 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Das45f48152018-01-18 12:07:33 -08001234 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001235
Saurav Dasceccf242017-08-03 18:30:35 -07001236 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1237 if (gh != null) {
1238 gh.shutdown();
1239 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001240 // Note that a switch going down is associated with all of its links
1241 // going down as well, but it is treated as a single switch down event
Saurav Das5a356042018-04-06 20:16:01 -07001242 // while the link-downs are ignored. We cannot rely on the ordering of
1243 // events - i.e we cannot expect all link-downs to come before the
1244 // switch down - so we purge all seen-links for the switch before
1245 // handling route-path changes for the switch-down
Saurav Dasc88d4662017-05-15 15:34:25 -07001246 defaultRoutingHandler
Saurav Das604ab3a2018-03-18 21:28:15 -07001247 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Dasc568c342018-01-25 09:49:01 -08001248 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001249 xConnectHandler.removeDevice(device.id());
Saurav Dasa4020382018-02-14 14:14:54 -08001250
1251 // Cleanup all internal groupHandler stores for this device. Should be
1252 // done after all rerouting or rehashing has been completed
1253 groupHandlerMap.entrySet()
1254 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Das80980c72016-03-23 11:22:49 -07001255 }
1256
Saurav Das1a129a02016-11-18 15:21:57 -08001257 private void processPortUpdated(Device device, Port port) {
1258 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1259 log.warn("Device configuration uploading. Not handling port event for"
1260 + "dev: {} port: {}", device.id(), port.number());
1261 return;
1262 }
Saurav Das018605f2017-02-18 14:05:44 -08001263
1264 if (!mastershipService.isLocalMaster(device.id())) {
1265 log.debug("Not master for dev:{} .. not handling port updated event"
1266 + "for port {}", device.id(), port.number());
1267 return;
1268 }
1269
1270 // first we handle filtering rules associated with the port
1271 if (port.isEnabled()) {
1272 log.info("Switchport {}/{} enabled..programming filters",
1273 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001274 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001275 } else {
1276 log.info("Switchport {}/{} disabled..removing filters",
1277 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001278 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001279 }
Saurav Das1a129a02016-11-18 15:21:57 -08001280
1281 // portUpdated calls are for ports that have gone down or up. For switch
1282 // to switch ports, link-events should take care of any re-routing or
1283 // group editing necessary for port up/down. Here we only process edge ports
1284 // that are already configured.
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001285 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1286 VlanId untaggedVlan = getUntaggedVlanId(cp);
1287 VlanId nativeVlan = getNativeVlanId(cp);
1288 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001289
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001290 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001291 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das1a129a02016-11-18 15:21:57 -08001292 + "dev/port: {}/{}", device.id(), port.number());
1293 return;
1294 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001295 if (untaggedVlan != null) {
1296 processEdgePort(device, port, untaggedVlan, true);
1297 }
1298 if (nativeVlan != null) {
1299 processEdgePort(device, port, nativeVlan, true);
1300 }
1301 if (!taggedVlans.isEmpty()) {
1302 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1303 }
Saurav Das1a129a02016-11-18 15:21:57 -08001304 }
1305
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001306 private void processEdgePort(Device device, Port port, VlanId vlanId,
1307 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001308 boolean portUp = port.isEnabled();
1309 if (portUp) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001310 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001311 port.number(), vlanId);
Charles Chan47933752017-11-30 15:37:50 -08001312 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Das1a129a02016-11-18 15:21:57 -08001313 } else {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001314 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001315 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001316 }
1317
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001318 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001319 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001320 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001321 } else {
1322 log.warn("Group handler not found for dev:{}. Not handling edge port"
1323 + " {} event for port:{}", device.id(),
1324 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001325 }
1326 }
sangho1e575652015-05-14 00:39:53 -07001327
Charles Chan27fe1a52017-10-20 16:06:55 -07001328 private void createOrUpdateDeviceConfiguration() {
1329 if (deviceConfiguration == null) {
Saurav Dase7f51012018-02-09 17:26:45 -08001330 log.info("Creating new DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001331 deviceConfiguration = new DeviceConfiguration(this);
1332 } else {
Saurav Dase7f51012018-02-09 17:26:45 -08001333 log.info("Updating DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001334 deviceConfiguration.updateConfig();
1335 }
1336 }
1337
Charles Chan50bb6ef2018-04-18 18:41:05 -07001338 private void createOrUpdateDefaultRoutingHandler() {
1339 if (defaultRoutingHandler == null) {
1340 log.info("Creating new DefaultRoutingHandler");
1341 defaultRoutingHandler = new DefaultRoutingHandler(this);
1342 } else {
1343 log.info("Updating DefaultRoutingHandler");
1344 defaultRoutingHandler.update(this);
1345 }
1346 }
1347
Pier Ventre10bd8d12016-11-26 21:05:22 -08001348 /**
1349 * Registers the given connect point with the NRS, this is necessary
1350 * to receive the NDP and ARP packets from the NRS.
1351 *
1352 * @param portToRegister connect point to register
1353 */
1354 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001355 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001356 portToRegister,
1357 neighbourHandler,
1358 appId
1359 );
1360 }
1361
Charles Chand6832882015-10-05 17:50:33 -07001362 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001363 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001364 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001365
Charles Chane849c192016-01-11 18:28:54 -08001366 /**
1367 * Constructs the internal network config listener.
1368 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001369 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001370 */
Charles Chan65238242017-06-22 18:03:14 -07001371 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001372 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001373 }
1374
Charles Chane849c192016-01-11 18:28:54 -08001375 /**
1376 * Reads network config and initializes related data structure accordingly.
1377 */
Charles Chan47933752017-11-30 15:37:50 -08001378 void configureNetwork() {
Saurav Dase7f51012018-02-09 17:26:45 -08001379 log.info("Configuring network ...");
Charles Chan27fe1a52017-10-20 16:06:55 -07001380 createOrUpdateDeviceConfiguration();
Charles Chan4636be02015-10-07 14:21:45 -07001381
Charles Chan2c15aca2016-11-09 20:51:44 -08001382 arpHandler = new ArpHandler(srManager);
1383 icmpHandler = new IcmpHandler(srManager);
1384 ipHandler = new IpHandler(srManager);
1385 routingRulePopulator = new RoutingRulePopulator(srManager);
Charles Chan50bb6ef2018-04-18 18:41:05 -07001386 createOrUpdateDefaultRoutingHandler();
Charles Chan4636be02015-10-07 14:21:45 -07001387
1388 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1389 groupHandlerMap, tunnelStore);
1390 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1391 flowObjectiveService,
1392 tunnelHandler, policyStore);
Saurav Das7bcbe702017-06-13 15:35:54 -07001393 // add a small delay to absorb multiple network config added notifications
1394 if (!programmingScheduled.get()) {
Saurav Dase7f51012018-02-09 17:26:45 -08001395 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
Saurav Das7bcbe702017-06-13 15:35:54 -07001396 programmingScheduled.set(true);
Charles Chan9b7217c2018-04-05 16:31:15 -07001397 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
Charles Chan4636be02015-10-07 14:21:45 -07001398 }
Charles Chan2199c302016-04-23 17:36:10 -07001399 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001400 }
1401
Charles Chand6832882015-10-05 17:50:33 -07001402 @Override
1403 public void event(NetworkConfigEvent event) {
Charles Chan7a888e82018-03-21 16:57:47 -07001404 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
1405 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1406 switch (event.type()) {
1407 case CONFIG_ADDED:
1408 case CONFIG_UPDATED:
1409 case CONFIG_REMOVED:
1410 log.trace("Schedule Network Config event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001411 mainEventExecutor.execute(new InternalEventHandler(event));
Charles Chan7a888e82018-03-21 16:57:47 -07001412 break;
1413 default:
1414 break;
Charles Chand6832882015-10-05 17:50:33 -07001415 }
1416 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001417
Charles Chan50443e82018-01-03 16:26:32 -08001418 @Override
1419 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase7f51012018-02-09 17:26:45 -08001420 if (event.type() == CONFIG_REGISTERED ||
1421 event.type() == CONFIG_UNREGISTERED) {
1422 log.debug("Ignore event {} due to type mismatch", event);
1423 return false;
Charles Chan50443e82018-01-03 16:26:32 -08001424 }
Saurav Dase7f51012018-02-09 17:26:45 -08001425
1426 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1427 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1428 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscd339592018-02-23 14:18:00 -08001429 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase7f51012018-02-09 17:26:45 -08001430 log.debug("Ignore event {} due to class mismatch", event);
1431 return false;
1432 }
1433
1434 return true;
Charles Chan50443e82018-01-03 16:26:32 -08001435 }
1436
Saurav Das7bcbe702017-06-13 15:35:54 -07001437 private final class ConfigChange implements Runnable {
1438 @Override
1439 public void run() {
1440 programmingScheduled.set(false);
Saurav Dase7f51012018-02-09 17:26:45 -08001441 log.info("Reacting to config changes after buffer delay");
Saurav Das7bcbe702017-06-13 15:35:54 -07001442 for (Device device : deviceService.getDevices()) {
1443 processDeviceAdded(device);
1444 }
1445 defaultRoutingHandler.startPopulationProcess();
1446 }
1447 }
Charles Chand6832882015-10-05 17:50:33 -07001448 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001449
Charles Chan7a888e82018-03-21 16:57:47 -07001450 private class InternalLinkListener implements LinkListener {
1451 @Override
1452 public void event(LinkEvent event) {
1453 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1454 event.type() == LinkEvent.Type.LINK_UPDATED ||
1455 event.type() == LinkEvent.Type.LINK_REMOVED) {
1456 log.trace("Schedule Link event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001457 mainEventExecutor.execute(new InternalEventHandler(event));
Charles Chan7a888e82018-03-21 16:57:47 -07001458 }
1459 }
1460 }
1461
1462 private class InternalDeviceListener implements DeviceListener {
1463 @Override
1464 public void event(DeviceEvent event) {
1465 switch (event.type()) {
1466 case DEVICE_ADDED:
1467 case PORT_UPDATED:
1468 case PORT_ADDED:
1469 case DEVICE_UPDATED:
1470 case DEVICE_AVAILABILITY_CHANGED:
1471 log.trace("Schedule Device event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001472 mainEventExecutor.execute(new InternalEventHandler(event));
Charles Chan7a888e82018-03-21 16:57:47 -07001473 break;
1474 default:
1475 }
1476 }
1477 }
1478
1479 private class InternalTopologyListener implements TopologyListener {
1480 @Override
1481 public void event(TopologyEvent event) {
1482 switch (event.type()) {
1483 case TOPOLOGY_CHANGED:
1484 log.trace("Schedule Topology event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001485 mainEventExecutor.execute(new InternalEventHandler(event));
Charles Chan7a888e82018-03-21 16:57:47 -07001486 break;
1487 default:
1488 }
1489 }
1490 }
1491
Charles Chan68aa62d2015-11-09 16:37:23 -08001492 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001493 @Override
1494 public void event(HostEvent event) {
Charles Chan68aa62d2015-11-09 16:37:23 -08001495 switch (event.type()) {
1496 case HOST_ADDED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001497 case HOST_MOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001498 case HOST_REMOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001499 case HOST_UPDATED:
Charles Chan7a888e82018-03-21 16:57:47 -07001500 log.trace("Schedule Host event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001501 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chan68aa62d2015-11-09 16:37:23 -08001502 break;
1503 default:
1504 log.warn("Unsupported host event type: {}", event.type());
1505 break;
1506 }
1507 }
1508 }
1509
Charles Chand55e84d2016-03-30 17:54:24 -07001510 private class InternalMcastListener implements McastListener {
1511 @Override
1512 public void event(McastEvent event) {
1513 switch (event.type()) {
Pier3ee24552018-03-14 16:47:32 -07001514 case SOURCES_ADDED:
1515 case SOURCES_REMOVED:
1516 case SINKS_ADDED:
1517 case SINKS_REMOVED:
Charles Chand55e84d2016-03-30 17:54:24 -07001518 case ROUTE_REMOVED:
Pier96f63cb2018-04-17 16:29:56 +02001519 case ROUTE_ADDED:
Charles Chan7a888e82018-03-21 16:57:47 -07001520 log.trace("Schedule Mcast event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001521 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi9930da52018-02-02 16:19:11 +01001522 break;
Charles Chand55e84d2016-03-30 17:54:24 -07001523 default:
Charles Chan7a888e82018-03-21 16:57:47 -07001524 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chand55e84d2016-03-30 17:54:24 -07001525 break;
1526 }
1527 }
1528 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001529
Charles Chan03a73e02016-10-24 14:52:01 -07001530 private class InternalRouteEventListener implements RouteListener {
1531 @Override
1532 public void event(RouteEvent event) {
1533 switch (event.type()) {
1534 case ROUTE_ADDED:
Charles Chan03a73e02016-10-24 14:52:01 -07001535 case ROUTE_UPDATED:
Charles Chan03a73e02016-10-24 14:52:01 -07001536 case ROUTE_REMOVED:
Charles Chan2fde6d42017-08-23 14:46:43 -07001537 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan7a888e82018-03-21 16:57:47 -07001538 log.trace("Schedule Route event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001539 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan2fde6d42017-08-23 14:46:43 -07001540 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001541 default:
Charles Chan7a888e82018-03-21 16:57:47 -07001542 log.warn("Unsupported route event type: {}", event.type());
Charles Chan03a73e02016-10-24 14:52:01 -07001543 break;
1544 }
1545 }
1546 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001547
Charles Chan50bb6ef2018-04-18 18:41:05 -07001548 private class InternalMastershipListener implements MastershipListener {
1549 @Override
1550 public void event(MastershipEvent event) {
1551 DeviceId deviceId = event.subject();
1552 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1553
1554 switch (event.type()) {
1555 case MASTER_CHANGED:
1556 log.info("Invalidating shouldProgram cache for {}/pair={} due to mastership change",
1557 deviceId, pairDeviceId);
1558 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1559 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1560 break;
1561 case BACKUPS_CHANGED:
1562 case SUSPENDED:
1563 default:
1564 break;
1565 }
1566 }
1567 }
1568
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001569 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1570 try {
1571 Set<Interface> intfs = conf.getInterfaces();
1572 Set<Interface> prevIntfs = prevConf.getInterfaces();
1573
1574 // Now we only handle one interface config at each port.
1575 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1576 log.warn("Interface update aborted - one at a time is allowed, " +
1577 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1578 return;
1579 }
1580
1581 Interface intf = intfs.stream().findFirst().get();
1582 Interface prevIntf = prevIntfs.stream().findFirst().get();
1583
1584 DeviceId deviceId = intf.connectPoint().deviceId();
1585 PortNumber portNum = intf.connectPoint().port();
1586
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001587 removeSubnetConfig(prevIntf.connectPoint(),
1588 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1589 new HashSet<>(intf.ipAddressesList())));
1590
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001591 if (!prevIntf.vlanNative().equals(VlanId.NONE)
1592 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
1593 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1594 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
1595 // Update filtering objective and L2IG group bucket
1596 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
1597 } else {
1598 // RemoveVlanNative
1599 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1600 }
1601 }
1602
1603 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
1604 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1605 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1606 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
1607 // Update filtering objective and L2IG group bucket
1608 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
1609 } else {
1610 // RemoveVlanUntagged
1611 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1612 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001613 }
1614
1615 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1616 // RemoveVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001617 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
1618 .filter(i -> !intf.vlanUntagged().equals(i))
1619 .filter(i -> !intf.vlanNative().equals(i))
1620 .forEach(vlanId -> updateVlanConfigInternal(
1621 deviceId, portNum, vlanId, false, false));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001622 }
1623
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001624 if (!intf.vlanNative().equals(VlanId.NONE)
1625 && !prevIntf.vlanNative().equals(intf.vlanNative())
1626 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1627 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
1628 // Update filtering objective and L2IG group bucket
1629 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
1630 } else {
1631 // AddVlanNative
1632 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1633 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001634 }
1635
1636 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1637 // AddVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001638 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
1639 .filter(i -> !prevIntf.vlanUntagged().equals(i))
1640 .filter(i -> !prevIntf.vlanNative().equals(i))
1641 .forEach(vlanId -> updateVlanConfigInternal(
1642 deviceId, portNum, vlanId, false, true)
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001643 );
1644 }
1645
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001646 if (!intf.vlanUntagged().equals(VlanId.NONE)
1647 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1648 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
1649 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
1650 // Update filtering objective and L2IG group bucket
1651 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
1652 } else {
1653 // AddVlanUntagged
1654 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1655 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001656 }
1657 addSubnetConfig(prevIntf.connectPoint(),
1658 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1659 new HashSet<>(prevIntf.ipAddressesList())));
1660 } catch (ConfigException e) {
1661 log.error("Error in configuration");
1662 }
1663 }
1664
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001665 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
1666 VlanId vlanId, boolean pushVlan) {
1667 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1668 if (grpHandler == null) {
1669 log.warn("Failed to retrieve group handler for device {}", deviceId);
1670 return;
1671 }
1672
1673 // Update filtering objective for a single port
1674 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
1675 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
1676
1677 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
1678 // Update L2IG bucket of the port
1679 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
1680 } else {
1681 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1682 }
1683 }
1684
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001685 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1686 VlanId vlanId, boolean pushVlan, boolean install) {
1687 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1688 if (grpHandler == null) {
1689 log.warn("Failed to retrieve group handler for device {}", deviceId);
1690 return;
1691 }
1692
1693 // Update filtering objective for a single port
1694 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1695
1696 // Update filtering objective for multicast ingress port
1697 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1698
1699 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1700
1701 if (nextId != -1 && !install) {
1702 // Update next objective for a single port as an output port
1703 // Remove a single port from L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001704 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001705 // Remove L2 Bridging rule and L3 Unicast rule to the host
1706 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1707 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1708 // only if there is no port configured on that VLAN ID
1709 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1710 // Remove broadcast forwarding rule for the VLAN
1711 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1712 // Remove L2FG for VLAN
1713 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1714 } else {
1715 // Remove L2IG of the port
1716 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1717 }
1718 } else if (install) {
1719 if (nextId != -1) {
1720 // Add a single port to L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001721 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001722 } else {
1723 // Create L2FG for VLAN
1724 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1725 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1726 }
1727 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1728 } else {
1729 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1730 }
1731 }
1732
1733 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1734 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1735 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1736
1737 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1738 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1739 .filter(intf -> !intf.connectPoint().equals(cp))
1740 .flatMap(intf -> intf.ipAddressesList().stream())
1741 .collect(Collectors.toSet());
1742 // 1. Partial subnet population
1743 // Remove routing rules for removed subnet from previous configuration,
1744 // which does not also exist in other interfaces in the same device
1745 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1746 .map(InterfaceIpAddress::subnetAddress)
1747 .collect(Collectors.toSet());
1748
1749 defaultRoutingHandler.revokeSubnet(
1750 ipPrefixSet.stream()
1751 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1752 .collect(Collectors.toSet()));
1753
1754 // 2. Interface IP punts
1755 // Remove IP punts for old Intf address
1756 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1757 .map(InterfaceIpAddress::ipAddress)
1758 .collect(Collectors.toSet());
1759 ipAddressSet.stream()
1760 .map(InterfaceIpAddress::ipAddress)
1761 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1762 .forEach(interfaceIpAddress ->
1763 routingRulePopulator.revokeSingleIpPunts(
1764 cp.deviceId(), interfaceIpAddress));
1765
1766 // 3. Host unicast routing rule
1767 // Remove unicast routing rule
1768 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1769 }
1770
1771 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1772 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1773 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1774
1775 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1776 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1777 .filter(intf -> !intf.connectPoint().equals(cp))
1778 .flatMap(intf -> intf.ipAddressesList().stream())
1779 .collect(Collectors.toSet());
1780 // 1. Partial subnet population
1781 // Add routing rules for newly added subnet, which does not also exist in
1782 // other interfaces in the same device
1783 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1784 .map(InterfaceIpAddress::subnetAddress)
1785 .collect(Collectors.toSet());
1786
1787 defaultRoutingHandler.populateSubnet(
1788 Collections.singleton(cp),
1789 ipPrefixSet.stream()
1790 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1791 .collect(Collectors.toSet()));
1792
1793 // 2. Interface IP punts
1794 // Add IP punts for new Intf address
1795 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1796 .map(InterfaceIpAddress::ipAddress)
1797 .collect(Collectors.toSet());
1798 ipAddressSet.stream()
1799 .map(InterfaceIpAddress::ipAddress)
1800 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1801 .forEach(interfaceIpAddress ->
1802 routingRulePopulator.populateSingleIpPunts(
1803 cp.deviceId(), interfaceIpAddress));
1804
1805 // 3. Host unicast routing rule
1806 // Add unicast routing rule
1807 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1808 }
sanghob35a6192015-04-01 13:05:26 -07001809}