blob: 0ab5ef0a39124aff514393ec2711d3b9d2912b77 [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 Chanb8e10c82015-10-14 11:24:40 -0700499 groupHandlerMap.clear();
500
Saurav Das7bcbe702017-06-13 15:35:54 -0700501 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800502 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700503 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700504 tunnelStore.destroy();
505 policyStore.destroy();
Pier Luigib72201b2018-01-25 16:16:02 +0100506
507 mcastHandler.terminate();
sanghob35a6192015-04-01 13:05:26 -0700508 log.info("Stopped");
509 }
510
Charles Chan47933752017-11-30 15:37:50 -0800511 @Modified
512 private void modified(ComponentContext context) {
513 Dictionary<?, ?> properties = context.getProperties();
514 if (properties == null) {
515 return;
516 }
517
518 String strActiveProving = Tools.get(properties, "activeProbing");
519 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
520
521 if (expectActiveProbing != activeProbing) {
522 activeProbing = expectActiveProbing;
523 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
524 }
525 }
526
sangho1e575652015-05-14 00:39:53 -0700527 @Override
528 public List<Tunnel> getTunnels() {
529 return tunnelHandler.getTunnels();
530 }
531
532 @Override
sangho71abe1b2015-06-29 14:58:47 -0700533 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
534 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700535 }
536
537 @Override
sangho71abe1b2015-06-29 14:58:47 -0700538 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700539 for (Policy policy: policyHandler.getPolicies()) {
540 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
541 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
542 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
543 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700544 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700545 }
546 }
547 }
sangho71abe1b2015-06-29 14:58:47 -0700548 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700549 }
550
551 @Override
sangho71abe1b2015-06-29 14:58:47 -0700552 public PolicyHandler.Result removePolicy(Policy policy) {
553 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700554 }
555
556 @Override
sangho71abe1b2015-06-29 14:58:47 -0700557 public PolicyHandler.Result createPolicy(Policy policy) {
558 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700559 }
560
561 @Override
562 public List<Policy> getPolicies() {
563 return policyHandler.getPolicies();
564 }
565
Saurav Das59232cf2016-04-27 18:35:50 -0700566 @Override
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700567 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
568 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700569 }
570
571 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800572 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700573 return l2TunnelHandler.getL2Tunnels();
574 }
575
576 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800577 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700578 return l2TunnelHandler.getL2Policies();
579 }
580
Saurav Das5a356042018-04-06 20:16:01 -0700581 @Override
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700582 @Deprecated
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700583 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
584
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700585 // get both added and pending pseudowires
586 List<L2TunnelDescription> pseudowires = new ArrayList<>();
587 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
588 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700589 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700590
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700591 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
592
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700593 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
594 L2TunnelHandler.Result res;
595 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
596 res = addPseudowire(pw);
597 if (res != L2TunnelHandler.Result.SUCCESS) {
598 log.error("Pseudowire with id {} can not be instantiated !", res);
599 retRes = res;
600 }
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700601 }
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700602
603 return retRes;
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700604 }
605
606 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800607 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700608 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700609 }
610
611 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800612 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700613 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700614 }
615
616 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700617 public void rerouteNetwork() {
618 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700619 }
620
Charles Chanc81c45b2016-10-20 17:02:44 -0700621 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800622 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
623 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800624 deviceConfiguration.getRouters().forEach(device ->
625 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700626 return deviceSubnetMap;
627 }
628
Saurav Dasc88d4662017-05-15 15:34:25 -0700629
630 @Override
631 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
632 if (defaultRoutingHandler != null) {
633 return defaultRoutingHandler.getCurrentEmcpSpgMap();
634 } else {
635 return null;
636 }
637 }
638
639 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700640 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
641 if (dsNextObjStore != null) {
642 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700643 } else {
644 return ImmutableMap.of();
645 }
646 }
647
Saurav Dasceccf242017-08-03 18:30:35 -0700648 @Override
649 public void verifyGroups(DeviceId id) {
650 DefaultGroupHandler gh = groupHandlerMap.get(id);
651 if (gh != null) {
652 gh.triggerBucketCorrector();
653 }
654 }
655
Saurav Dasc568c342018-01-25 09:49:01 -0800656 @Override
657 public ImmutableMap<Link, Boolean> getSeenLinks() {
658 return linkHandler.getSeenLinks();
659 }
660
661 @Override
662 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
663 return linkHandler.getDownedPorts();
664 }
665
Pier Luigib29144d2018-01-15 18:06:43 +0100666 @Override
667 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
668 return mcastHandler.getMcastNextIds(mcastIp);
669 }
670
671 @Override
Pier Luigi96fe0772018-02-28 12:10:50 +0100672 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigib29144d2018-01-15 18:06:43 +0100673 return mcastHandler.getMcastRoles(mcastIp);
674 }
675
676 @Override
677 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
678 return mcastHandler.getMcastPaths(mcastIp);
679 }
680
Pier96f63cb2018-04-17 16:29:56 +0200681 @Override
Pierb1fe7382018-04-17 17:25:22 +0200682 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
683 ConnectPoint sourcecp) {
684 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
685 }
686
687 @Override
Pier96f63cb2018-04-17 16:29:56 +0200688 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
689 return mcastHandler.getMcastLeaders(mcastIp);
690 }
691
Charles Chan8bc75ee2018-04-17 18:56:53 -0700692 @Override
693 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
694 return defaultRoutingHandler == null ? ImmutableMap.of() :
695 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
696 }
697
698 @Override
699 public Map<DeviceId, Boolean> getShouldProgramCache() {
700 return defaultRoutingHandler == null ? ImmutableMap.of() :
701 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
702 }
703
sanghof9d0bf12015-05-19 11:57:42 -0700704 /**
Ray Milkeye4afdb52017-04-05 09:42:04 -0700705 * Extracts the application ID from the manager.
706 *
707 * @return application ID
708 */
709 public ApplicationId appId() {
710 return appId;
711 }
712
713 /**
714 * Returns the device configuration.
715 *
716 * @return device configuration
717 */
718 public DeviceConfiguration deviceConfiguration() {
719 return deviceConfiguration;
720 }
721
722 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700723 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700724 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700725 *
726 * @return next objective ID store
727 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700728 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
729 dsNextObjStore() {
730 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700731 }
732
733 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700734 * Per device next objective ID store with (device id + vlanid) as key.
735 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700736 *
737 * @return vlan next object store
738 */
739 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
740 return vlanNextObjStore;
741 }
742
743 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700744 * Per device next objective ID store with (device id + port + treatment + meta) as key.
745 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700746 *
747 * @return port next object store.
748 */
749 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
750 return portNextObjStore;
751 }
752
753 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700754 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
755 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700756 *
757 * @return MPLS-ECMP value
758 */
759 public boolean getMplsEcmp() {
760 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
761 .getConfig(this.appId, SegmentRoutingAppConfig.class);
762 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
763 }
764
765 /**
sanghof9d0bf12015-05-19 11:57:42 -0700766 * Returns the tunnel object with the tunnel ID.
767 *
768 * @param tunnelId Tunnel ID
769 * @return Tunnel reference
770 */
sangho1e575652015-05-14 00:39:53 -0700771 public Tunnel getTunnel(String tunnelId) {
772 return tunnelHandler.getTunnel(tunnelId);
773 }
774
Charles Chan7ffd81f2017-02-08 15:52:08 -0800775 // TODO Consider moving these to InterfaceService
sanghob35a6192015-04-01 13:05:26 -0700776 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800777 * Returns untagged VLAN configured on given connect point.
Charles Chan7ffd81f2017-02-08 15:52:08 -0800778 * <p>
779 * Only returns the first match if there are multiple untagged VLAN configured
780 * on the connect point.
sanghob35a6192015-04-01 13:05:26 -0700781 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800782 * @param connectPoint connect point
783 * @return untagged VLAN or null if not configured
sanghob35a6192015-04-01 13:05:26 -0700784 */
Charles Chan65238242017-06-22 18:03:14 -0700785 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800786 return interfaceService.getInterfacesByPort(connectPoint).stream()
787 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
788 .map(Interface::vlanUntagged)
789 .findFirst().orElse(null);
sanghob35a6192015-04-01 13:05:26 -0700790 }
791
sangho1e575652015-05-14 00:39:53 -0700792 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800793 * Returns tagged VLAN configured on given connect point.
794 * <p>
795 * Returns all matches if there are multiple tagged VLAN configured
796 * on the connect point.
797 *
798 * @param connectPoint connect point
799 * @return tagged VLAN or empty set if not configured
800 */
Charles Chan65238242017-06-22 18:03:14 -0700801 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800802 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
803 return interfaces.stream()
804 .map(Interface::vlanTagged)
Charles Chan65238242017-06-22 18:03:14 -0700805 .flatMap(Set::stream)
Charles Chan7ffd81f2017-02-08 15:52:08 -0800806 .collect(Collectors.toSet());
807 }
808
809 /**
810 * Returns native VLAN configured on given connect point.
811 * <p>
812 * Only returns the first match if there are multiple native VLAN configured
813 * on the connect point.
814 *
815 * @param connectPoint connect point
816 * @return native VLAN or null if not configured
817 */
Charles Chan65238242017-06-22 18:03:14 -0700818 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800819 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
820 return interfaces.stream()
821 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
822 .map(Interface::vlanNative)
823 .findFirst()
824 .orElse(null);
825 }
826
827 /**
Charles Chanf9a52702017-06-16 15:19:24 -0700828 * Returns internal VLAN for untagged hosts on given connect point.
829 * <p>
830 * The internal VLAN is either vlan-untagged for an access port,
831 * or vlan-native for a trunk port.
832 *
833 * @param connectPoint connect point
834 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
835 */
Pier Luigi96fe0772018-02-28 12:10:50 +0100836 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chanf9a52702017-06-16 15:19:24 -0700837 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
838 VlanId nativeVlanId = getNativeVlanId(connectPoint);
839 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
840 }
841
842 /**
Charles Chan65238242017-06-22 18:03:14 -0700843 * Returns optional pair device ID of given device.
844 *
845 * @param deviceId device ID
846 * @return optional pair device ID. Might be empty if pair device is not configured
847 */
848 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
849 SegmentRoutingDeviceConfig deviceConfig =
850 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
851 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
852 }
853 /**
854 * Returns optional pair device local port of given device.
855 *
856 * @param deviceId device ID
857 * @return optional pair device ID. Might be empty if pair device is not configured
858 */
859 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
860 SegmentRoutingDeviceConfig deviceConfig =
861 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
862 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
863 }
864
865 /**
Charles Chan9640c812017-08-23 13:55:39 -0700866 * Returns locations of given resolved route.
867 *
868 * @param resolvedRoute resolved route
869 * @return locations of nexthop. Might be empty if next hop is not found
870 */
871 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
872 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
873 return Optional.ofNullable(hostService.getHost(hostId))
874 .map(Host::locations).orElse(Sets.newHashSet())
875 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
876 }
877
878 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800879 * Returns vlan port map of given device.
880 *
881 * @param deviceId device id
882 * @return vlan-port multimap
883 */
884 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
885 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
886
887 interfaceService.getInterfaces().stream()
888 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
889 .forEach(intf -> {
890 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -0700891 intf.vlanTagged().forEach(vlanTagged ->
892 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
893 );
Charles Chan7ffd81f2017-02-08 15:52:08 -0800894 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
895 });
896 vlanPortMap.removeAll(VlanId.NONE);
897
898 return vlanPortMap;
899 }
900
901 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800902 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800903 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700904 *
905 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800906 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800907 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700908 */
Charles Chan65238242017-06-22 18:03:14 -0700909 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700910 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800911 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
912 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700913 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800914 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800915 + "device {} not found", deviceId);
916 return -1;
917 }
918 }
919
920 /**
921 * Returns the next objective ID for the given portNumber, given the treatment.
922 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700923 * would result in different objectives. If the next object does not exist,
924 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800925 *
926 * @param deviceId Device ID
927 * @param portNum port number on device for which NextObjective is queried
928 * @param treatment the actions to apply on the packets (should include outport)
929 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700930 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700931 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800932 */
933 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
934 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700935 TrafficSelector meta,
936 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -0800937 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
938 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -0700939 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -0800940 } else {
Charles Chane849c192016-01-11 18:28:54 -0800941 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
942 + " not found", deviceId);
943 return -1;
944 }
945 }
946
Saurav Dasc88d4662017-05-15 15:34:25 -0700947 /**
948 * Returns the group handler object for the specified device id.
949 *
950 * @param devId the device identifier
951 * @return the groupHandler object for the device id, or null if not found
952 */
Charles Chan65238242017-06-22 18:03:14 -0700953 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700954 return groupHandlerMap.get(devId);
955 }
956
957 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700958 * Returns the default routing handler object.
959 *
960 * @return the default routing handler object
961 */
962 public DefaultRoutingHandler getRoutingHandler() {
963 return defaultRoutingHandler;
964 }
965
sanghob35a6192015-04-01 13:05:26 -0700966 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700967 @Override
968 public void process(PacketContext context) {
969
970 if (context.isHandled()) {
971 return;
972 }
973
974 InboundPacket pkt = context.inPacket();
975 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800976
977 if (ethernet == null) {
978 return;
979 }
980
Saurav Das7bcbe702017-06-13 15:35:54 -0700981 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
982 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800983 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700984 log.warn("Received unexpected ARP packet on {}",
985 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -0700986 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800987 return;
sanghob35a6192015-04-01 13:05:26 -0700988 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800989 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
990 //ipHandler.addToPacketBuffer(ipv4Packet);
991 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
992 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700993 } else {
Charles Chan50035632017-01-13 17:20:44 -0800994 // NOTE: We don't support IP learning at this moment so this
995 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800996 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700997 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800998 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
999 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -08001000 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -08001001 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -08001002 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1003 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1004 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1005 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1006 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1007 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -07001008 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -07001009 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -08001010 }
1011 } else {
1012 // NOTE: We don't support IP learning at this moment so this
1013 // is not necessary. Also it causes duplication of DHCPv6 packets.
1014 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1015 }
sanghob35a6192015-04-01 13:05:26 -07001016 }
1017 }
1018 }
1019
sanghob35a6192015-04-01 13:05:26 -07001020 private class InternalEventHandler implements Runnable {
Charles Chan9b7217c2018-04-05 16:31:15 -07001021 private Event event;
1022
1023 InternalEventHandler(Event event) {
1024 this.event = event;
1025 }
1026
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001027 @Override
sanghob35a6192015-04-01 13:05:26 -07001028 public void run() {
Charles Chan9b7217c2018-04-05 16:31:15 -07001029 try {
1030 // TODO We should also change SR routing and PW to listen to TopologyEvents
1031 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1032 event.type() == LinkEvent.Type.LINK_UPDATED) {
1033 linkHandler.processLinkAdded((Link) event.subject());
1034 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1035 linkHandler.processLinkRemoved((Link) event.subject());
1036 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1037 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1038 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1039 DeviceId deviceId = ((Device) event.subject()).id();
1040 if (deviceService.isAvailable(deviceId)) {
1041 log.info("** DEVICE UP Processing device event {} "
1042 + "for available device {}",
1043 event.type(), ((Device) event.subject()).id());
1044 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001045 } else {
Charles Chan9b7217c2018-04-05 16:31:15 -07001046 log.info(" ** DEVICE DOWN Processing device event {}"
1047 + " for unavailable device {}",
1048 event.type(), ((Device) event.subject()).id());
1049 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001050 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001051 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1052 // typically these calls come when device is added first time
1053 // so port filtering rules are handled at the device_added event.
1054 // port added calls represent all ports on the device,
1055 // enabled or not.
1056 log.trace("** PORT ADDED {}/{} -> {}",
1057 ((DeviceEvent) event).subject().id(),
1058 ((DeviceEvent) event).port().number(),
1059 event.type());
1060 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1061 // these calls happen for every subsequent event
1062 // ports enabled, disabled, switch goes away, comes back
1063 log.info("** PORT UPDATED {}/{} -> {}",
1064 event.subject(),
1065 ((DeviceEvent) event).port(),
1066 event.type());
1067 processPortUpdated(((Device) event.subject()),
1068 ((DeviceEvent) event).port());
1069 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1070 // Process topology event, needed for all modules relying on
1071 // topology service for path computation
1072 TopologyEvent topologyEvent = (TopologyEvent) event;
1073 log.info("Processing topology event {}, topology age {}, reasons {}",
1074 event.type(), topologyEvent.subject().time(),
1075 topologyEvent.reasons().size());
1076 topologyHandler.processTopologyChange(topologyEvent.reasons());
1077 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1078 hostHandler.processHostAddedEvent((HostEvent) event);
1079 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1080 hostHandler.processHostMovedEvent((HostEvent) event);
1081 routeHandler.processHostMovedEvent((HostEvent) event);
1082 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1083 hostHandler.processHostRemovedEvent((HostEvent) event);
1084 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1085 hostHandler.processHostUpdatedEvent((HostEvent) event);
1086 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1087 routeHandler.processRouteAdded((RouteEvent) event);
1088 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1089 routeHandler.processRouteUpdated((RouteEvent) event);
1090 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1091 routeHandler.processRouteRemoved((RouteEvent) event);
1092 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1093 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1094 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1095 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1096 event.type() == McastEvent.Type.SINKS_ADDED ||
1097 event.type() == McastEvent.Type.SINKS_REMOVED ||
1098 event.type() == McastEvent.Type.ROUTE_ADDED ||
1099 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1100 mcastHandler.processMcastEvent((McastEvent) event);
1101 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1102 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1103 Class configClass = netcfgEvent.configClass();
1104 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1105 appCfgHandler.processAppConfigAdded(netcfgEvent);
1106 log.info("App config event .. configuring network");
1107 cfgListener.configureNetwork();
1108 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1109 log.info("Segment Routing Device Config added for {}", event.subject());
1110 cfgListener.configureNetwork();
1111 } else if (configClass.equals(XConnectConfig.class)) {
1112 xConnectHandler.processXConnectConfigAdded(netcfgEvent);
1113 } else if (configClass.equals(InterfaceConfig.class)) {
1114 log.info("Interface Config added for {}", event.subject());
1115 cfgListener.configureNetwork();
1116 } else {
1117 log.error("Unhandled config class: {}", configClass);
1118 }
1119 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1120 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1121 Class configClass = netcfgEvent.configClass();
1122 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1123 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1124 log.info("App config event .. configuring network");
1125 cfgListener.configureNetwork();
1126 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1127 log.info("Segment Routing Device Config updated for {}", event.subject());
1128 createOrUpdateDeviceConfiguration();
1129 } else if (configClass.equals(XConnectConfig.class)) {
1130 xConnectHandler.processXConnectConfigUpdated(netcfgEvent);
1131 } else if (configClass.equals(InterfaceConfig.class)) {
1132 log.info("Interface Config updated for {}", event.subject());
1133 createOrUpdateDeviceConfiguration();
1134 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1135 (InterfaceConfig) netcfgEvent.prevConfig().get());
1136 } else {
1137 log.error("Unhandled config class: {}", configClass);
1138 }
1139 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1140 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1141 Class configClass = netcfgEvent.configClass();
1142 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1143 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1144 log.info("App config event .. configuring network");
1145 cfgListener.configureNetwork();
1146 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1147 // TODO Handle sr device config removal
1148 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
1149 } else if (configClass.equals(XConnectConfig.class)) {
1150 xConnectHandler.processXConnectConfigRemoved(netcfgEvent);
1151 } else if (configClass.equals(InterfaceConfig.class)) {
1152 // TODO Handle interface removal
1153 log.info("InterfaceConfig removal is not handled in current implementation");
1154 } else {
1155 log.error("Unhandled config class: {}", configClass);
1156 }
1157 } else {
1158 log.warn("Unhandled event type: {}", event.type());
sanghob35a6192015-04-01 13:05:26 -07001159 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001160 } catch (Exception e) {
1161 log.error("SegmentRouting event handler thread thrown an exception: {}",
1162 e.getMessage(), e);
sanghob35a6192015-04-01 13:05:26 -07001163 }
sanghob35a6192015-04-01 13:05:26 -07001164 }
1165 }
1166
Saurav Das45f48152018-01-18 12:07:33 -08001167 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001168 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001169
1170 // NOTE: Punt ARP/NDP even when the device is not configured.
1171 // Host learning without network config is required for CORD config generator.
1172 routingRulePopulator.populateIpPunts(device.id());
1173 routingRulePopulator.populateArpNdpPunts(device.id());
1174
Charles Chan0b4e6182015-11-03 10:42:14 -08001175 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001176 log.warn("Device configuration unavailable. Device {} will be "
1177 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001178 return;
1179 }
Charles Chan2199c302016-04-23 17:36:10 -07001180 processDeviceAddedInternal(device.id());
1181 }
1182
1183 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001184 // Irrespective of whether the local is a MASTER or not for this device,
1185 // we need to create a SR-group-handler instance. This is because in a
1186 // multi-instance setup, any instance can initiate forwarding/next-objectives
1187 // for any switch (even if this instance is a SLAVE or not even connected
1188 // to the switch). To handle this, a default-group-handler instance is necessary
1189 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001190 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1191 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001192 DefaultGroupHandler groupHandler;
1193 try {
1194 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001195 createGroupHandler(deviceId,
1196 appId,
1197 deviceConfiguration,
1198 linkService,
1199 flowObjectiveService,
1200 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001201 } catch (DeviceConfigNotFoundException e) {
1202 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1203 return;
1204 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001205 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001206 deviceId);
1207 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001208 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001209
Charles Chan2199c302016-04-23 17:36:10 -07001210 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001211 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001212 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001213 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001214 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001215 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001216 }
Charles Chan5270ed02016-01-30 23:22:37 -08001217
Charles Chan03a73e02016-10-24 14:52:01 -07001218 appCfgHandler.init(deviceId);
Charles Chan2ff1bac2018-03-29 16:03:41 -07001219 hostHandler.init(deviceId);
Charles Chan03a73e02016-10-24 14:52:01 -07001220 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001221 }
1222
Saurav Das80980c72016-03-23 11:22:49 -07001223 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001224 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001225 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan47933752017-11-30 15:37:50 -08001226 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001227 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001228 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001229 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001230 portNextObjStore.entrySet().stream()
1231 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001232 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Das45f48152018-01-18 12:07:33 -08001233 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001234
Saurav Dasceccf242017-08-03 18:30:35 -07001235 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1236 if (gh != null) {
1237 gh.shutdown();
1238 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001239 // Note that a switch going down is associated with all of its links
1240 // going down as well, but it is treated as a single switch down event
Saurav Das5a356042018-04-06 20:16:01 -07001241 // while the link-downs are ignored. We cannot rely on the ordering of
1242 // events - i.e we cannot expect all link-downs to come before the
1243 // switch down - so we purge all seen-links for the switch before
1244 // handling route-path changes for the switch-down
Saurav Dasc88d4662017-05-15 15:34:25 -07001245 defaultRoutingHandler
Saurav Das604ab3a2018-03-18 21:28:15 -07001246 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Dasc568c342018-01-25 09:49:01 -08001247 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001248 xConnectHandler.removeDevice(device.id());
Saurav Dasa4020382018-02-14 14:14:54 -08001249
1250 // Cleanup all internal groupHandler stores for this device. Should be
1251 // done after all rerouting or rehashing has been completed
1252 groupHandlerMap.entrySet()
1253 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Das80980c72016-03-23 11:22:49 -07001254 }
1255
Saurav Das1a129a02016-11-18 15:21:57 -08001256 private void processPortUpdated(Device device, Port port) {
1257 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1258 log.warn("Device configuration uploading. Not handling port event for"
1259 + "dev: {} port: {}", device.id(), port.number());
1260 return;
1261 }
Saurav Das018605f2017-02-18 14:05:44 -08001262
1263 if (!mastershipService.isLocalMaster(device.id())) {
1264 log.debug("Not master for dev:{} .. not handling port updated event"
1265 + "for port {}", device.id(), port.number());
1266 return;
1267 }
1268
1269 // first we handle filtering rules associated with the port
1270 if (port.isEnabled()) {
1271 log.info("Switchport {}/{} enabled..programming filters",
1272 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001273 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001274 } else {
1275 log.info("Switchport {}/{} disabled..removing filters",
1276 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001277 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001278 }
Saurav Das1a129a02016-11-18 15:21:57 -08001279
1280 // portUpdated calls are for ports that have gone down or up. For switch
1281 // to switch ports, link-events should take care of any re-routing or
1282 // group editing necessary for port up/down. Here we only process edge ports
1283 // that are already configured.
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001284 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1285 VlanId untaggedVlan = getUntaggedVlanId(cp);
1286 VlanId nativeVlan = getNativeVlanId(cp);
1287 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001288
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001289 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001290 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das1a129a02016-11-18 15:21:57 -08001291 + "dev/port: {}/{}", device.id(), port.number());
1292 return;
1293 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001294 if (untaggedVlan != null) {
1295 processEdgePort(device, port, untaggedVlan, true);
1296 }
1297 if (nativeVlan != null) {
1298 processEdgePort(device, port, nativeVlan, true);
1299 }
1300 if (!taggedVlans.isEmpty()) {
1301 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1302 }
Saurav Das1a129a02016-11-18 15:21:57 -08001303 }
1304
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001305 private void processEdgePort(Device device, Port port, VlanId vlanId,
1306 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001307 boolean portUp = port.isEnabled();
1308 if (portUp) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001309 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001310 port.number(), vlanId);
Charles Chan47933752017-11-30 15:37:50 -08001311 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Das1a129a02016-11-18 15:21:57 -08001312 } else {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001313 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001314 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001315 }
1316
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001317 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001318 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001319 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001320 } else {
1321 log.warn("Group handler not found for dev:{}. Not handling edge port"
1322 + " {} event for port:{}", device.id(),
1323 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001324 }
1325 }
sangho1e575652015-05-14 00:39:53 -07001326
Charles Chan27fe1a52017-10-20 16:06:55 -07001327 private void createOrUpdateDeviceConfiguration() {
1328 if (deviceConfiguration == null) {
Saurav Dase7f51012018-02-09 17:26:45 -08001329 log.info("Creating new DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001330 deviceConfiguration = new DeviceConfiguration(this);
1331 } else {
Saurav Dase7f51012018-02-09 17:26:45 -08001332 log.info("Updating DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001333 deviceConfiguration.updateConfig();
1334 }
1335 }
1336
Charles Chan50bb6ef2018-04-18 18:41:05 -07001337 private void createOrUpdateDefaultRoutingHandler() {
1338 if (defaultRoutingHandler == null) {
1339 log.info("Creating new DefaultRoutingHandler");
1340 defaultRoutingHandler = new DefaultRoutingHandler(this);
1341 } else {
1342 log.info("Updating DefaultRoutingHandler");
1343 defaultRoutingHandler.update(this);
1344 }
1345 }
1346
Pier Ventre10bd8d12016-11-26 21:05:22 -08001347 /**
1348 * Registers the given connect point with the NRS, this is necessary
1349 * to receive the NDP and ARP packets from the NRS.
1350 *
1351 * @param portToRegister connect point to register
1352 */
1353 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001354 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001355 portToRegister,
1356 neighbourHandler,
1357 appId
1358 );
1359 }
1360
Charles Chand6832882015-10-05 17:50:33 -07001361 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001362 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001363 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001364
Charles Chane849c192016-01-11 18:28:54 -08001365 /**
1366 * Constructs the internal network config listener.
1367 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001368 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001369 */
Charles Chan65238242017-06-22 18:03:14 -07001370 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001371 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001372 }
1373
Charles Chane849c192016-01-11 18:28:54 -08001374 /**
1375 * Reads network config and initializes related data structure accordingly.
1376 */
Charles Chan47933752017-11-30 15:37:50 -08001377 void configureNetwork() {
Saurav Dase7f51012018-02-09 17:26:45 -08001378 log.info("Configuring network ...");
Charles Chan27fe1a52017-10-20 16:06:55 -07001379 createOrUpdateDeviceConfiguration();
Charles Chan4636be02015-10-07 14:21:45 -07001380
Charles Chan2c15aca2016-11-09 20:51:44 -08001381 arpHandler = new ArpHandler(srManager);
1382 icmpHandler = new IcmpHandler(srManager);
1383 ipHandler = new IpHandler(srManager);
1384 routingRulePopulator = new RoutingRulePopulator(srManager);
Charles Chan50bb6ef2018-04-18 18:41:05 -07001385 createOrUpdateDefaultRoutingHandler();
Charles Chan4636be02015-10-07 14:21:45 -07001386
1387 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1388 groupHandlerMap, tunnelStore);
1389 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1390 flowObjectiveService,
1391 tunnelHandler, policyStore);
Saurav Das7bcbe702017-06-13 15:35:54 -07001392 // add a small delay to absorb multiple network config added notifications
1393 if (!programmingScheduled.get()) {
Saurav Dase7f51012018-02-09 17:26:45 -08001394 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
Saurav Das7bcbe702017-06-13 15:35:54 -07001395 programmingScheduled.set(true);
Charles Chan9b7217c2018-04-05 16:31:15 -07001396 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
Charles Chan4636be02015-10-07 14:21:45 -07001397 }
Charles Chan2199c302016-04-23 17:36:10 -07001398 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001399 }
1400
Charles Chand6832882015-10-05 17:50:33 -07001401 @Override
1402 public void event(NetworkConfigEvent event) {
Charles Chan7a888e82018-03-21 16:57:47 -07001403 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
1404 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1405 switch (event.type()) {
1406 case CONFIG_ADDED:
1407 case CONFIG_UPDATED:
1408 case CONFIG_REMOVED:
1409 log.trace("Schedule Network Config event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001410 mainEventExecutor.execute(new InternalEventHandler(event));
Charles Chan7a888e82018-03-21 16:57:47 -07001411 break;
1412 default:
1413 break;
Charles Chand6832882015-10-05 17:50:33 -07001414 }
1415 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001416
Charles Chan50443e82018-01-03 16:26:32 -08001417 @Override
1418 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase7f51012018-02-09 17:26:45 -08001419 if (event.type() == CONFIG_REGISTERED ||
1420 event.type() == CONFIG_UNREGISTERED) {
1421 log.debug("Ignore event {} due to type mismatch", event);
1422 return false;
Charles Chan50443e82018-01-03 16:26:32 -08001423 }
Saurav Dase7f51012018-02-09 17:26:45 -08001424
1425 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1426 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1427 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscd339592018-02-23 14:18:00 -08001428 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase7f51012018-02-09 17:26:45 -08001429 log.debug("Ignore event {} due to class mismatch", event);
1430 return false;
1431 }
1432
1433 return true;
Charles Chan50443e82018-01-03 16:26:32 -08001434 }
1435
Saurav Das7bcbe702017-06-13 15:35:54 -07001436 private final class ConfigChange implements Runnable {
1437 @Override
1438 public void run() {
1439 programmingScheduled.set(false);
Saurav Dase7f51012018-02-09 17:26:45 -08001440 log.info("Reacting to config changes after buffer delay");
Saurav Das7bcbe702017-06-13 15:35:54 -07001441 for (Device device : deviceService.getDevices()) {
1442 processDeviceAdded(device);
1443 }
1444 defaultRoutingHandler.startPopulationProcess();
1445 }
1446 }
Charles Chand6832882015-10-05 17:50:33 -07001447 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001448
Charles Chan7a888e82018-03-21 16:57:47 -07001449 private class InternalLinkListener implements LinkListener {
1450 @Override
1451 public void event(LinkEvent event) {
1452 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1453 event.type() == LinkEvent.Type.LINK_UPDATED ||
1454 event.type() == LinkEvent.Type.LINK_REMOVED) {
1455 log.trace("Schedule Link event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001456 mainEventExecutor.execute(new InternalEventHandler(event));
Charles Chan7a888e82018-03-21 16:57:47 -07001457 }
1458 }
1459 }
1460
1461 private class InternalDeviceListener implements DeviceListener {
1462 @Override
1463 public void event(DeviceEvent event) {
1464 switch (event.type()) {
1465 case DEVICE_ADDED:
1466 case PORT_UPDATED:
1467 case PORT_ADDED:
1468 case DEVICE_UPDATED:
1469 case DEVICE_AVAILABILITY_CHANGED:
1470 log.trace("Schedule Device event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001471 mainEventExecutor.execute(new InternalEventHandler(event));
Charles Chan7a888e82018-03-21 16:57:47 -07001472 break;
1473 default:
1474 }
1475 }
1476 }
1477
1478 private class InternalTopologyListener implements TopologyListener {
1479 @Override
1480 public void event(TopologyEvent event) {
1481 switch (event.type()) {
1482 case TOPOLOGY_CHANGED:
1483 log.trace("Schedule Topology event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001484 mainEventExecutor.execute(new InternalEventHandler(event));
Charles Chan7a888e82018-03-21 16:57:47 -07001485 break;
1486 default:
1487 }
1488 }
1489 }
1490
Charles Chan68aa62d2015-11-09 16:37:23 -08001491 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001492 @Override
1493 public void event(HostEvent event) {
Charles Chan68aa62d2015-11-09 16:37:23 -08001494 switch (event.type()) {
1495 case HOST_ADDED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001496 case HOST_MOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001497 case HOST_REMOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001498 case HOST_UPDATED:
Charles Chan7a888e82018-03-21 16:57:47 -07001499 log.trace("Schedule Host event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001500 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chan68aa62d2015-11-09 16:37:23 -08001501 break;
1502 default:
1503 log.warn("Unsupported host event type: {}", event.type());
1504 break;
1505 }
1506 }
1507 }
1508
Charles Chand55e84d2016-03-30 17:54:24 -07001509 private class InternalMcastListener implements McastListener {
1510 @Override
1511 public void event(McastEvent event) {
1512 switch (event.type()) {
Pier3ee24552018-03-14 16:47:32 -07001513 case SOURCES_ADDED:
1514 case SOURCES_REMOVED:
1515 case SINKS_ADDED:
1516 case SINKS_REMOVED:
Charles Chand55e84d2016-03-30 17:54:24 -07001517 case ROUTE_REMOVED:
Pier96f63cb2018-04-17 16:29:56 +02001518 case ROUTE_ADDED:
Charles Chan7a888e82018-03-21 16:57:47 -07001519 log.trace("Schedule Mcast event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001520 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi9930da52018-02-02 16:19:11 +01001521 break;
Charles Chand55e84d2016-03-30 17:54:24 -07001522 default:
Charles Chan7a888e82018-03-21 16:57:47 -07001523 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chand55e84d2016-03-30 17:54:24 -07001524 break;
1525 }
1526 }
1527 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001528
Charles Chan03a73e02016-10-24 14:52:01 -07001529 private class InternalRouteEventListener implements RouteListener {
1530 @Override
1531 public void event(RouteEvent event) {
1532 switch (event.type()) {
1533 case ROUTE_ADDED:
Charles Chan03a73e02016-10-24 14:52:01 -07001534 case ROUTE_UPDATED:
Charles Chan03a73e02016-10-24 14:52:01 -07001535 case ROUTE_REMOVED:
Charles Chan2fde6d42017-08-23 14:46:43 -07001536 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan7a888e82018-03-21 16:57:47 -07001537 log.trace("Schedule Route event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001538 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan2fde6d42017-08-23 14:46:43 -07001539 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001540 default:
Charles Chan7a888e82018-03-21 16:57:47 -07001541 log.warn("Unsupported route event type: {}", event.type());
Charles Chan03a73e02016-10-24 14:52:01 -07001542 break;
1543 }
1544 }
1545 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001546
Charles Chan50bb6ef2018-04-18 18:41:05 -07001547 private class InternalMastershipListener implements MastershipListener {
1548 @Override
1549 public void event(MastershipEvent event) {
1550 DeviceId deviceId = event.subject();
1551 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1552
1553 switch (event.type()) {
1554 case MASTER_CHANGED:
1555 log.info("Invalidating shouldProgram cache for {}/pair={} due to mastership change",
1556 deviceId, pairDeviceId);
1557 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1558 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1559 break;
1560 case BACKUPS_CHANGED:
1561 case SUSPENDED:
1562 default:
1563 break;
1564 }
1565 }
1566 }
1567
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001568 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1569 try {
1570 Set<Interface> intfs = conf.getInterfaces();
1571 Set<Interface> prevIntfs = prevConf.getInterfaces();
1572
1573 // Now we only handle one interface config at each port.
1574 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1575 log.warn("Interface update aborted - one at a time is allowed, " +
1576 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1577 return;
1578 }
1579
1580 Interface intf = intfs.stream().findFirst().get();
1581 Interface prevIntf = prevIntfs.stream().findFirst().get();
1582
1583 DeviceId deviceId = intf.connectPoint().deviceId();
1584 PortNumber portNum = intf.connectPoint().port();
1585
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001586 removeSubnetConfig(prevIntf.connectPoint(),
1587 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1588 new HashSet<>(intf.ipAddressesList())));
1589
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001590 if (!prevIntf.vlanNative().equals(VlanId.NONE)
1591 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
1592 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1593 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
1594 // Update filtering objective and L2IG group bucket
1595 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
1596 } else {
1597 // RemoveVlanNative
1598 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1599 }
1600 }
1601
1602 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
1603 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1604 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1605 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
1606 // Update filtering objective and L2IG group bucket
1607 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
1608 } else {
1609 // RemoveVlanUntagged
1610 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1611 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001612 }
1613
1614 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1615 // RemoveVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001616 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
1617 .filter(i -> !intf.vlanUntagged().equals(i))
1618 .filter(i -> !intf.vlanNative().equals(i))
1619 .forEach(vlanId -> updateVlanConfigInternal(
1620 deviceId, portNum, vlanId, false, false));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001621 }
1622
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001623 if (!intf.vlanNative().equals(VlanId.NONE)
1624 && !prevIntf.vlanNative().equals(intf.vlanNative())
1625 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1626 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
1627 // Update filtering objective and L2IG group bucket
1628 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
1629 } else {
1630 // AddVlanNative
1631 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1632 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001633 }
1634
1635 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1636 // AddVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001637 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
1638 .filter(i -> !prevIntf.vlanUntagged().equals(i))
1639 .filter(i -> !prevIntf.vlanNative().equals(i))
1640 .forEach(vlanId -> updateVlanConfigInternal(
1641 deviceId, portNum, vlanId, false, true)
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001642 );
1643 }
1644
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001645 if (!intf.vlanUntagged().equals(VlanId.NONE)
1646 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1647 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
1648 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
1649 // Update filtering objective and L2IG group bucket
1650 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
1651 } else {
1652 // AddVlanUntagged
1653 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1654 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001655 }
1656 addSubnetConfig(prevIntf.connectPoint(),
1657 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1658 new HashSet<>(prevIntf.ipAddressesList())));
1659 } catch (ConfigException e) {
1660 log.error("Error in configuration");
1661 }
1662 }
1663
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001664 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
1665 VlanId vlanId, boolean pushVlan) {
1666 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1667 if (grpHandler == null) {
1668 log.warn("Failed to retrieve group handler for device {}", deviceId);
1669 return;
1670 }
1671
1672 // Update filtering objective for a single port
1673 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
1674 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
1675
1676 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
1677 // Update L2IG bucket of the port
1678 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
1679 } else {
1680 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1681 }
1682 }
1683
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001684 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1685 VlanId vlanId, boolean pushVlan, boolean install) {
1686 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1687 if (grpHandler == null) {
1688 log.warn("Failed to retrieve group handler for device {}", deviceId);
1689 return;
1690 }
1691
1692 // Update filtering objective for a single port
1693 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1694
1695 // Update filtering objective for multicast ingress port
1696 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1697
1698 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1699
1700 if (nextId != -1 && !install) {
1701 // Update next objective for a single port as an output port
1702 // Remove a single port from L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001703 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001704 // Remove L2 Bridging rule and L3 Unicast rule to the host
1705 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1706 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1707 // only if there is no port configured on that VLAN ID
1708 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1709 // Remove broadcast forwarding rule for the VLAN
1710 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1711 // Remove L2FG for VLAN
1712 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1713 } else {
1714 // Remove L2IG of the port
1715 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1716 }
1717 } else if (install) {
1718 if (nextId != -1) {
1719 // Add a single port to L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001720 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001721 } else {
1722 // Create L2FG for VLAN
1723 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1724 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1725 }
1726 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1727 } else {
1728 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1729 }
1730 }
1731
1732 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1733 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1734 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1735
1736 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1737 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1738 .filter(intf -> !intf.connectPoint().equals(cp))
1739 .flatMap(intf -> intf.ipAddressesList().stream())
1740 .collect(Collectors.toSet());
1741 // 1. Partial subnet population
1742 // Remove routing rules for removed subnet from previous configuration,
1743 // which does not also exist in other interfaces in the same device
1744 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1745 .map(InterfaceIpAddress::subnetAddress)
1746 .collect(Collectors.toSet());
1747
1748 defaultRoutingHandler.revokeSubnet(
1749 ipPrefixSet.stream()
1750 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1751 .collect(Collectors.toSet()));
1752
1753 // 2. Interface IP punts
1754 // Remove IP punts for old Intf address
1755 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1756 .map(InterfaceIpAddress::ipAddress)
1757 .collect(Collectors.toSet());
1758 ipAddressSet.stream()
1759 .map(InterfaceIpAddress::ipAddress)
1760 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1761 .forEach(interfaceIpAddress ->
1762 routingRulePopulator.revokeSingleIpPunts(
1763 cp.deviceId(), interfaceIpAddress));
1764
1765 // 3. Host unicast routing rule
1766 // Remove unicast routing rule
1767 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1768 }
1769
1770 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1771 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1772 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1773
1774 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1775 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1776 .filter(intf -> !intf.connectPoint().equals(cp))
1777 .flatMap(intf -> intf.ipAddressesList().stream())
1778 .collect(Collectors.toSet());
1779 // 1. Partial subnet population
1780 // Add routing rules for newly added subnet, which does not also exist in
1781 // other interfaces in the same device
1782 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1783 .map(InterfaceIpAddress::subnetAddress)
1784 .collect(Collectors.toSet());
1785
1786 defaultRoutingHandler.populateSubnet(
1787 Collections.singleton(cp),
1788 ipPrefixSet.stream()
1789 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1790 .collect(Collectors.toSet()));
1791
1792 // 2. Interface IP punts
1793 // Add IP punts for new Intf address
1794 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1795 .map(InterfaceIpAddress::ipAddress)
1796 .collect(Collectors.toSet());
1797 ipAddressSet.stream()
1798 .map(InterfaceIpAddress::ipAddress)
1799 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1800 .forEach(interfaceIpAddress ->
1801 routingRulePopulator.populateSingleIpPunts(
1802 cp.deviceId(), interfaceIpAddress));
1803
1804 // 3. Host unicast routing rule
1805 // Add unicast routing rule
1806 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1807 }
sanghob35a6192015-04-01 13:05:26 -07001808}