blob: eff78e54ddf1c898119aae9d6243a85865fdc87b [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;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070046import org.onosproject.mastership.MastershipService;
Pier3ee24552018-03-14 16:47:32 -070047import org.onosproject.mcast.api.McastEvent;
48import org.onosproject.mcast.api.McastListener;
49import org.onosproject.mcast.api.MulticastRouteService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080050import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070051import org.onosproject.net.Device;
52import org.onosproject.net.DeviceId;
Charles Chan9640c812017-08-23 13:55:39 -070053import org.onosproject.net.Host;
54import org.onosproject.net.HostId;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070055import org.onosproject.net.Link;
56import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080057import org.onosproject.net.PortNumber;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070058import org.onosproject.net.config.ConfigException;
Charles Chand6832882015-10-05 17:50:33 -070059import org.onosproject.net.config.ConfigFactory;
60import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070061import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070062import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070063import org.onosproject.net.config.basics.InterfaceConfig;
64import org.onosproject.net.config.basics.McastConfig;
Charles Chand6832882015-10-05 17:50:33 -070065import org.onosproject.net.config.basics.SubjectFactories;
Saurav Das45f48152018-01-18 12:07:33 -080066import org.onosproject.net.device.DeviceAdminService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070067import org.onosproject.net.device.DeviceEvent;
68import org.onosproject.net.device.DeviceListener;
69import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080070import org.onosproject.net.flow.TrafficSelector;
71import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070072import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan68aa62d2015-11-09 16:37:23 -080073import org.onosproject.net.host.HostEvent;
74import org.onosproject.net.host.HostListener;
Charles Chan47933752017-11-30 15:37:50 -080075import org.onosproject.net.host.HostLocationProbingService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080076import org.onosproject.net.host.HostService;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070077import org.onosproject.net.host.InterfaceIpAddress;
Pier96f63cb2018-04-17 16:29:56 +020078import org.onosproject.net.intent.WorkPartitionService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070079import org.onosproject.net.intf.Interface;
80import org.onosproject.net.intf.InterfaceService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080081import org.onosproject.net.link.LinkEvent;
82import org.onosproject.net.link.LinkListener;
83import org.onosproject.net.link.LinkService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070084import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080085import org.onosproject.net.packet.InboundPacket;
86import org.onosproject.net.packet.PacketContext;
87import org.onosproject.net.packet.PacketProcessor;
88import org.onosproject.net.packet.PacketService;
Pier Luigi83f919b2018-02-15 16:33:08 +010089import org.onosproject.net.topology.TopologyEvent;
90import org.onosproject.net.topology.TopologyListener;
Charles Chand55e84d2016-03-30 17:54:24 -070091import org.onosproject.net.topology.TopologyService;
Charles Chan9640c812017-08-23 13:55:39 -070092import org.onosproject.routeservice.ResolvedRoute;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070093import org.onosproject.routeservice.RouteEvent;
94import org.onosproject.routeservice.RouteListener;
95import org.onosproject.routeservice.RouteService;
Charles Chand55e84d2016-03-30 17:54:24 -070096import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
97import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventref34966c2016-11-07 16:21:04 -080098import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070099import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -0700100import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -0700101import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700102import org.onosproject.segmentrouting.grouphandler.DestinationSet;
103import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Pier Luigi96fe0772018-02-28 12:10:50 +0100104import org.onosproject.segmentrouting.mcast.McastHandler;
105import org.onosproject.segmentrouting.mcast.McastRole;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700106import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700107import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800108import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700109import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800110
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800111import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700112import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800113import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800114import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
115
Saurav Das7bcbe702017-06-13 15:35:54 -0700116import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Pier Luigib29144d2018-01-15 18:06:43 +0100117import org.onosproject.segmentrouting.storekey.McastStoreKey;
Charles Chand2990362016-04-18 13:44:03 -0700118import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -0800119import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -0700120import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700121import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700122import org.onosproject.store.service.EventuallyConsistentMap;
123import org.onosproject.store.service.EventuallyConsistentMapBuilder;
124import org.onosproject.store.service.StorageService;
125import org.onosproject.store.service.WallClockTimestamp;
Charles Chan47933752017-11-30 15:37:50 -0800126import org.osgi.service.component.ComponentContext;
sanghob35a6192015-04-01 13:05:26 -0700127import org.slf4j.Logger;
128import org.slf4j.LoggerFactory;
129
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700130import java.util.ArrayList;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800131import java.util.Collections;
Charles Chan47933752017-11-30 15:37:50 -0800132import java.util.Dictionary;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800133import java.util.HashSet;
134import java.util.List;
135import java.util.Map;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800136import java.util.Optional;
137import java.util.Set;
138import java.util.concurrent.ConcurrentHashMap;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800139import java.util.concurrent.Executors;
140import java.util.concurrent.ScheduledExecutorService;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800141import java.util.concurrent.TimeUnit;
142import java.util.concurrent.atomic.AtomicBoolean;
143import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -0700144
Charles Chan3e783d02016-02-26 22:19:52 -0800145import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800146import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700147import static org.onlab.util.Tools.groupedThreads;
Saurav Dase7f51012018-02-09 17:26:45 -0800148import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
149import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Charles Chan3e783d02016-02-26 22:19:52 -0800150
Charles Chane849c192016-01-11 18:28:54 -0800151/**
152 * Segment routing manager.
153 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700154@Service
155@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700156public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700157
Charles Chan2c15aca2016-11-09 20:51:44 -0800158 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan2fde6d42017-08-23 14:46:43 -0700159 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sanghob35a6192015-04-01 13:05:26 -0700160
161 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700162 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700163
164 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800165 private NeighbourResolutionService neighbourResolutionService;
166
167 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100168 public CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700169
170 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700171 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700172
173 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700174 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700175
176 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan47933752017-11-30 15:37:50 -0800177 HostLocationProbingService probingService;
178
179 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100180 public DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700181
182 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Saurav Das45f48152018-01-18 12:07:33 -0800183 DeviceAdminService deviceAdminService;
184
185 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800186 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700187
188 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100189 public LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700190
Charles Chan5270ed02016-01-30 23:22:37 -0800191 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800192 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700193
194 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800195 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700196
197 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100198 public MulticastRouteService multicastRouteService;
Charles Chan03a73e02016-10-24 14:52:01 -0700199
200 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chanf237e1b2018-01-09 13:45:07 -0800201 public TopologyService topologyService;
Charles Chan03a73e02016-10-24 14:52:01 -0700202
203 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700204 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800205
206 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800207 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800208
Saurav Das80980c72016-03-23 11:22:49 -0700209 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800210 public InterfaceService interfaceService;
211
Pier Luigieba73a02018-01-16 10:47:50 +0100212 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
213 public ClusterService clusterService;
214
215 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier96f63cb2018-04-17 16:29:56 +0200216 public WorkPartitionService workPartitionService;
Pier Luigieba73a02018-01-16 10:47:50 +0100217
Charles Chan47933752017-11-30 15:37:50 -0800218 @Property(name = "activeProbing", boolValue = true,
219 label = "Enable active probing to discover dual-homed hosts.")
220 boolean activeProbing = true;
221
Charles Chan03a73e02016-10-24 14:52:01 -0700222 ArpHandler arpHandler = null;
223 IcmpHandler icmpHandler = null;
224 IpHandler ipHandler = null;
225 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700226 ApplicationId appId;
227 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700228
Charles Chan03a73e02016-10-24 14:52:01 -0700229 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700230 private TunnelHandler tunnelHandler = null;
231 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700232 private InternalPacketProcessor processor = null;
233 private InternalLinkListener linkListener = null;
234 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700235 private AppConfigHandler appCfgHandler = null;
Pier Luigi96fe0772018-02-28 12:10:50 +0100236 public XConnectHandler xConnectHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800237 McastHandler mcastHandler = null;
238 HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800239 private RouteHandler routeHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800240 LinkHandler linkHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800241 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800242 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigi83f919b2018-02-15 16:33:08 +0100243 private TopologyHandler topologyHandler = null;
Charles Chan5270ed02016-01-30 23:22:37 -0800244 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700245 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
246 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700247 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigi83f919b2018-02-15 16:33:08 +0100248 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
sanghob35a6192015-04-01 13:05:26 -0700249
Charles Chan9b7217c2018-04-05 16:31:15 -0700250 // Handles device, link, topology and network config events
251 private ScheduledExecutorService mainEventExecutor = Executors
252 .newScheduledThreadPool(1, groupedThreads("sr-event-main", "%d", log));
sanghob35a6192015-04-01 13:05:26 -0700253
Charles Chan9b7217c2018-04-05 16:31:15 -0700254 // Handles host, route, mcast events
255 private ScheduledExecutorService hostEventExecutor = Executors
256 .newScheduledThreadPool(1, groupedThreads("sr-event-host", "%d", log));
257 private ScheduledExecutorService routeEventExecutor = Executors
258 .newScheduledThreadPool(1, groupedThreads("sr-event-route", "%d", log));
259 private ScheduledExecutorService mcastEventExecutor = Executors
260 .newScheduledThreadPool(1, groupedThreads("sr-event-mcast", "%d", log));
261
262
263 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chane849c192016-01-11 18:28:54 -0800264 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700265 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700266 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800267 */
Charles Chan47933752017-11-30 15:37:50 -0800268 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das7bcbe702017-06-13 15:35:54 -0700269 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800270 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700271 * Per device next objective ID store with (device id + vlanid) as key.
272 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800273 */
Charles Chan47933752017-11-30 15:37:50 -0800274 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800275 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800276 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700277 * Per device next objective ID store with (device id + port + treatment + meta) as key.
278 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chane849c192016-01-11 18:28:54 -0800279 */
Charles Chan47933752017-11-30 15:37:50 -0800280 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800281 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800282
Saurav Das4ce45962015-11-24 23:21:05 -0800283 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
284 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700285
Saurav Das7bcbe702017-06-13 15:35:54 -0700286 private AtomicBoolean programmingScheduled = new AtomicBoolean();
287
Charles Chand55e84d2016-03-30 17:54:24 -0700288 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700289 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
290 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700291 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700292 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800293 public SegmentRoutingDeviceConfig createConfig() {
294 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700295 }
296 };
Pier Ventref34966c2016-11-07 16:21:04 -0800297
Charles Chand55e84d2016-03-30 17:54:24 -0700298 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700299 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
300 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700301 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800302 @Override
303 public SegmentRoutingAppConfig createConfig() {
304 return new SegmentRoutingAppConfig();
305 }
306 };
Pier Ventref34966c2016-11-07 16:21:04 -0800307
Charles Chanfc5c7802016-05-17 13:13:55 -0700308 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
309 new ConfigFactory<ApplicationId, XConnectConfig>(
310 SubjectFactories.APP_SUBJECT_FACTORY,
311 XConnectConfig.class, "xconnect") {
312 @Override
313 public XConnectConfig createConfig() {
314 return new XConnectConfig();
315 }
316 };
Pier Ventref34966c2016-11-07 16:21:04 -0800317
Charles Chand55e84d2016-03-30 17:54:24 -0700318 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700319 new ConfigFactory<ApplicationId, McastConfig>(
320 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700321 McastConfig.class, "multicast") {
322 @Override
323 public McastConfig createConfig() {
324 return new McastConfig();
325 }
326 };
327
Charles Chan65238242017-06-22 18:03:14 -0700328 private static final Object THREAD_SCHED_LOCK = new Object();
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700329 private static int numOfEventsQueued = 0;
330 private static int numOfEventsExecuted = 0;
sanghob35a6192015-04-01 13:05:26 -0700331 private static int numOfHandlerExecution = 0;
332 private static int numOfHandlerScheduled = 0;
333
Charles Chan116188d2016-02-18 14:22:42 -0800334 /**
335 * Segment Routing App ID.
336 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800337 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan59cc16d2017-02-02 16:20:42 -0800338
Charles Chane849c192016-01-11 18:28:54 -0800339 /**
340 * The default VLAN ID assigned to the interfaces without subnet config.
341 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800342 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700343
sanghob35a6192015-04-01 13:05:26 -0700344 @Activate
Charles Chan47933752017-11-30 15:37:50 -0800345 protected void activate(ComponentContext context) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800346 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700347
348 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700349 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700350 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700351 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700352 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700353 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700354 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700355 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700356 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700357
Charles Chan59cc16d2017-02-02 16:20:42 -0800358 log.debug("Creating EC map vlannextobjectivestore");
359 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
360 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
361 vlanNextObjStore = vlanNextObjMapBuilder
362 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700363 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700364 .withTimestampProvider((k, v) -> new WallClockTimestamp())
365 .build();
366
Saurav Das4ce45962015-11-24 23:21:05 -0800367 log.debug("Creating EC map subnetnextobjectivestore");
368 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
369 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
370 portNextObjStore = portNextObjMapBuilder
371 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700372 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800373 .withTimestampProvider((k, v) -> new WallClockTimestamp())
374 .build();
375
sangho0b2b6d12015-05-20 22:16:38 -0700376 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
377 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700378 tunnelStore = tunnelMapBuilder
379 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700380 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700381 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700382 .build();
383
384 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
385 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700386 policyStore = policyMapBuilder
387 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700388 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700389 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700390 .build();
391
Saurav Das80980c72016-03-23 11:22:49 -0700392 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800393 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700394 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800395 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800396 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
397 "requestInterceptsEnabled", "false");
Charles Chand3baaba2017-08-08 15:13:37 -0700398 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800399 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700400 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800401 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800402 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
403 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800404 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
405 "forceUnprovision", "true");
Charles Chanef624ed2017-08-10 16:57:28 -0700406 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan73316522017-07-20 16:16:25 -0700407 "distributed", "true");
Charles Chan35a32322017-08-14 11:42:11 -0700408 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
409 "multihomingEnabled", "true");
Charles Chan807d87a2017-11-29 19:54:20 -0800410 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
411 "staleLinkAge", "15000");
412 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
413 "allowDuplicateIps", "false");
Charles Chan47933752017-11-30 15:37:50 -0800414 compCfgService.registerProperties(getClass());
415 modified(context);
Saurav Das80980c72016-03-23 11:22:49 -0700416
Charles Chanb8e10c82015-10-14 11:24:40 -0700417 processor = new InternalPacketProcessor();
418 linkListener = new InternalLinkListener();
419 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700420 appCfgHandler = new AppConfigHandler(this);
421 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700422 mcastHandler = new McastHandler(this);
423 hostHandler = new HostHandler(this);
Saurav Das45f48152018-01-18 12:07:33 -0800424 linkHandler = new LinkHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700425 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800426 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800427 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigi83f919b2018-02-15 16:33:08 +0100428 topologyHandler = new TopologyHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700429
Charles Chan3e783d02016-02-26 22:19:52 -0800430 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700431 cfgService.registerConfigFactory(deviceConfigFactory);
432 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700433 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700434 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase7f51012018-02-09 17:26:45 -0800435 log.info("Configuring network before adding listeners");
Charles Chan132393a2018-01-04 14:26:07 -0800436 cfgListener.configureNetwork();
437
Charles Chan5270ed02016-01-30 23:22:37 -0800438 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700439 packetService.addProcessor(processor, PacketProcessor.director(2));
440 linkService.addListener(linkListener);
441 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700442 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700443 routeService.addListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100444 topologyService.addListener(topologyListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700445
Charles Chanb39777c2018-03-09 15:53:44 -0800446 linkHandler.init();
Andreas Pantelopoulos4a768c02018-01-11 07:53:48 -0800447 l2TunnelHandler.init();
448
sanghob35a6192015-04-01 13:05:26 -0700449 log.info("Started");
450 }
451
Saurav Das45f48152018-01-18 12:07:33 -0800452 KryoNamespace.Builder createSerializer() {
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700453 return new KryoNamespace.Builder()
454 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700455 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800456 VlanNextObjectiveStoreKey.class,
457 DestinationSet.class,
458 NextNeighbors.class,
459 Tunnel.class,
460 DefaultTunnel.class,
461 Policy.class,
462 TunnelPolicy.class,
463 Policy.Type.class,
464 PortNextObjectiveStoreKey.class,
465 XConnectStoreKey.class,
466 L2Tunnel.class,
467 L2TunnelPolicy.class,
468 DefaultL2Tunnel.class,
469 DefaultL2TunnelPolicy.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700470 );
471 }
472
sanghob35a6192015-04-01 13:05:26 -0700473 @Deactivate
474 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700475 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700476 cfgService.unregisterConfigFactory(deviceConfigFactory);
477 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700478 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700479 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan47933752017-11-30 15:37:50 -0800480 compCfgService.unregisterProperties(getClass(), false);
Charles Chand6832882015-10-05 17:50:33 -0700481
Charles Chanc7a8a682017-06-19 00:43:31 -0700482 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700483 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700484 linkService.removeListener(linkListener);
485 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700486 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700487 routeService.removeListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100488 topologyService.removeListener(topologyListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700489
Charles Chan0aa674e2017-02-23 15:44:08 -0800490 neighbourResolutionService.unregisterNeighbourHandlers(appId);
491
sanghob35a6192015-04-01 13:05:26 -0700492 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700493 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700494 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700495 groupHandlerMap.clear();
496
Saurav Das7bcbe702017-06-13 15:35:54 -0700497 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800498 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700499 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700500 tunnelStore.destroy();
501 policyStore.destroy();
Pier Luigib72201b2018-01-25 16:16:02 +0100502
503 mcastHandler.terminate();
sanghob35a6192015-04-01 13:05:26 -0700504 log.info("Stopped");
505 }
506
Charles Chan47933752017-11-30 15:37:50 -0800507 @Modified
508 private void modified(ComponentContext context) {
509 Dictionary<?, ?> properties = context.getProperties();
510 if (properties == null) {
511 return;
512 }
513
514 String strActiveProving = Tools.get(properties, "activeProbing");
515 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
516
517 if (expectActiveProbing != activeProbing) {
518 activeProbing = expectActiveProbing;
519 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
520 }
521 }
522
sangho1e575652015-05-14 00:39:53 -0700523 @Override
524 public List<Tunnel> getTunnels() {
525 return tunnelHandler.getTunnels();
526 }
527
528 @Override
sangho71abe1b2015-06-29 14:58:47 -0700529 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
530 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700531 }
532
533 @Override
sangho71abe1b2015-06-29 14:58:47 -0700534 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700535 for (Policy policy: policyHandler.getPolicies()) {
536 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
537 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
538 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
539 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700540 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700541 }
542 }
543 }
sangho71abe1b2015-06-29 14:58:47 -0700544 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700545 }
546
547 @Override
sangho71abe1b2015-06-29 14:58:47 -0700548 public PolicyHandler.Result removePolicy(Policy policy) {
549 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700550 }
551
552 @Override
sangho71abe1b2015-06-29 14:58:47 -0700553 public PolicyHandler.Result createPolicy(Policy policy) {
554 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700555 }
556
557 @Override
558 public List<Policy> getPolicies() {
559 return policyHandler.getPolicies();
560 }
561
Saurav Das59232cf2016-04-27 18:35:50 -0700562 @Override
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700563 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
564 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700565 }
566
567 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800568 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700569 return l2TunnelHandler.getL2Tunnels();
570 }
571
572 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800573 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700574 return l2TunnelHandler.getL2Policies();
575 }
576
Saurav Das5a356042018-04-06 20:16:01 -0700577 @Override
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700578 @Deprecated
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700579 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
580
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700581 // get both added and pending pseudowires
582 List<L2TunnelDescription> pseudowires = new ArrayList<>();
583 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
584 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700585 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700586
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700587 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
588
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700589 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
590 L2TunnelHandler.Result res;
591 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
592 res = addPseudowire(pw);
593 if (res != L2TunnelHandler.Result.SUCCESS) {
594 log.error("Pseudowire with id {} can not be instantiated !", res);
595 retRes = res;
596 }
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700597 }
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700598
599 return retRes;
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700600 }
601
602 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800603 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700604 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700605 }
606
607 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800608 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700609 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700610 }
611
612 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700613 public void rerouteNetwork() {
614 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700615 }
616
Charles Chanc81c45b2016-10-20 17:02:44 -0700617 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800618 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
619 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800620 deviceConfiguration.getRouters().forEach(device ->
621 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700622 return deviceSubnetMap;
623 }
624
Saurav Dasc88d4662017-05-15 15:34:25 -0700625
626 @Override
627 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
628 if (defaultRoutingHandler != null) {
629 return defaultRoutingHandler.getCurrentEmcpSpgMap();
630 } else {
631 return null;
632 }
633 }
634
635 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700636 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
637 if (dsNextObjStore != null) {
638 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700639 } else {
640 return ImmutableMap.of();
641 }
642 }
643
Saurav Dasceccf242017-08-03 18:30:35 -0700644 @Override
645 public void verifyGroups(DeviceId id) {
646 DefaultGroupHandler gh = groupHandlerMap.get(id);
647 if (gh != null) {
648 gh.triggerBucketCorrector();
649 }
650 }
651
Saurav Dasc568c342018-01-25 09:49:01 -0800652 @Override
653 public ImmutableMap<Link, Boolean> getSeenLinks() {
654 return linkHandler.getSeenLinks();
655 }
656
657 @Override
658 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
659 return linkHandler.getDownedPorts();
660 }
661
Pier Luigib29144d2018-01-15 18:06:43 +0100662 @Override
663 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
664 return mcastHandler.getMcastNextIds(mcastIp);
665 }
666
667 @Override
Pier Luigi96fe0772018-02-28 12:10:50 +0100668 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigib29144d2018-01-15 18:06:43 +0100669 return mcastHandler.getMcastRoles(mcastIp);
670 }
671
672 @Override
673 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
674 return mcastHandler.getMcastPaths(mcastIp);
675 }
676
Pier96f63cb2018-04-17 16:29:56 +0200677 @Override
678 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
679 return mcastHandler.getMcastLeaders(mcastIp);
680 }
681
sanghof9d0bf12015-05-19 11:57:42 -0700682 /**
Ray Milkeye4afdb52017-04-05 09:42:04 -0700683 * Extracts the application ID from the manager.
684 *
685 * @return application ID
686 */
687 public ApplicationId appId() {
688 return appId;
689 }
690
691 /**
692 * Returns the device configuration.
693 *
694 * @return device configuration
695 */
696 public DeviceConfiguration deviceConfiguration() {
697 return deviceConfiguration;
698 }
699
700 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700701 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700702 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700703 *
704 * @return next objective ID store
705 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700706 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
707 dsNextObjStore() {
708 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700709 }
710
711 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700712 * Per device next objective ID store with (device id + vlanid) as key.
713 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700714 *
715 * @return vlan next object store
716 */
717 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
718 return vlanNextObjStore;
719 }
720
721 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700722 * Per device next objective ID store with (device id + port + treatment + meta) as key.
723 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700724 *
725 * @return port next object store.
726 */
727 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
728 return portNextObjStore;
729 }
730
731 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700732 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
733 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700734 *
735 * @return MPLS-ECMP value
736 */
737 public boolean getMplsEcmp() {
738 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
739 .getConfig(this.appId, SegmentRoutingAppConfig.class);
740 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
741 }
742
743 /**
sanghof9d0bf12015-05-19 11:57:42 -0700744 * Returns the tunnel object with the tunnel ID.
745 *
746 * @param tunnelId Tunnel ID
747 * @return Tunnel reference
748 */
sangho1e575652015-05-14 00:39:53 -0700749 public Tunnel getTunnel(String tunnelId) {
750 return tunnelHandler.getTunnel(tunnelId);
751 }
752
Charles Chan7ffd81f2017-02-08 15:52:08 -0800753 // TODO Consider moving these to InterfaceService
sanghob35a6192015-04-01 13:05:26 -0700754 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800755 * Returns untagged VLAN configured on given connect point.
Charles Chan7ffd81f2017-02-08 15:52:08 -0800756 * <p>
757 * Only returns the first match if there are multiple untagged VLAN configured
758 * on the connect point.
sanghob35a6192015-04-01 13:05:26 -0700759 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800760 * @param connectPoint connect point
761 * @return untagged VLAN or null if not configured
sanghob35a6192015-04-01 13:05:26 -0700762 */
Charles Chan65238242017-06-22 18:03:14 -0700763 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800764 return interfaceService.getInterfacesByPort(connectPoint).stream()
765 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
766 .map(Interface::vlanUntagged)
767 .findFirst().orElse(null);
sanghob35a6192015-04-01 13:05:26 -0700768 }
769
sangho1e575652015-05-14 00:39:53 -0700770 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800771 * Returns tagged VLAN configured on given connect point.
772 * <p>
773 * Returns all matches if there are multiple tagged VLAN configured
774 * on the connect point.
775 *
776 * @param connectPoint connect point
777 * @return tagged VLAN or empty set if not configured
778 */
Charles Chan65238242017-06-22 18:03:14 -0700779 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800780 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
781 return interfaces.stream()
782 .map(Interface::vlanTagged)
Charles Chan65238242017-06-22 18:03:14 -0700783 .flatMap(Set::stream)
Charles Chan7ffd81f2017-02-08 15:52:08 -0800784 .collect(Collectors.toSet());
785 }
786
787 /**
788 * Returns native VLAN configured on given connect point.
789 * <p>
790 * Only returns the first match if there are multiple native VLAN configured
791 * on the connect point.
792 *
793 * @param connectPoint connect point
794 * @return native VLAN or null if not configured
795 */
Charles Chan65238242017-06-22 18:03:14 -0700796 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800797 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
798 return interfaces.stream()
799 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
800 .map(Interface::vlanNative)
801 .findFirst()
802 .orElse(null);
803 }
804
805 /**
Charles Chanf9a52702017-06-16 15:19:24 -0700806 * Returns internal VLAN for untagged hosts on given connect point.
807 * <p>
808 * The internal VLAN is either vlan-untagged for an access port,
809 * or vlan-native for a trunk port.
810 *
811 * @param connectPoint connect point
812 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
813 */
Pier Luigi96fe0772018-02-28 12:10:50 +0100814 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chanf9a52702017-06-16 15:19:24 -0700815 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
816 VlanId nativeVlanId = getNativeVlanId(connectPoint);
817 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
818 }
819
820 /**
Charles Chan65238242017-06-22 18:03:14 -0700821 * Returns optional pair device ID of given device.
822 *
823 * @param deviceId device ID
824 * @return optional pair device ID. Might be empty if pair device is not configured
825 */
826 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
827 SegmentRoutingDeviceConfig deviceConfig =
828 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
829 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
830 }
831 /**
832 * Returns optional pair device local port of given device.
833 *
834 * @param deviceId device ID
835 * @return optional pair device ID. Might be empty if pair device is not configured
836 */
837 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
838 SegmentRoutingDeviceConfig deviceConfig =
839 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
840 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
841 }
842
843 /**
Charles Chan2fde6d42017-08-23 14:46:43 -0700844 * Determine if current instance is the master of given connect point.
845 *
846 * @param cp connect point
847 * @return true if current instance is the master of given connect point
848 */
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700849 public boolean isMasterOf(ConnectPoint cp) {
Charles Chan2fde6d42017-08-23 14:46:43 -0700850 boolean isMaster = mastershipService.isLocalMaster(cp.deviceId());
851 if (!isMaster) {
852 log.debug(NOT_MASTER, cp);
853 }
854 return isMaster;
855 }
856
857 /**
Charles Chan9640c812017-08-23 13:55:39 -0700858 * Returns locations of given resolved route.
859 *
860 * @param resolvedRoute resolved route
861 * @return locations of nexthop. Might be empty if next hop is not found
862 */
863 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
864 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
865 return Optional.ofNullable(hostService.getHost(hostId))
866 .map(Host::locations).orElse(Sets.newHashSet())
867 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
868 }
869
870 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800871 * Returns vlan port map of given device.
872 *
873 * @param deviceId device id
874 * @return vlan-port multimap
875 */
876 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
877 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
878
879 interfaceService.getInterfaces().stream()
880 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
881 .forEach(intf -> {
882 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -0700883 intf.vlanTagged().forEach(vlanTagged ->
884 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
885 );
Charles Chan7ffd81f2017-02-08 15:52:08 -0800886 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
887 });
888 vlanPortMap.removeAll(VlanId.NONE);
889
890 return vlanPortMap;
891 }
892
893 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800894 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800895 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700896 *
897 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800898 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800899 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700900 */
Charles Chan65238242017-06-22 18:03:14 -0700901 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700902 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800903 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
904 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700905 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800906 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800907 + "device {} not found", deviceId);
908 return -1;
909 }
910 }
911
912 /**
913 * Returns the next objective ID for the given portNumber, given the treatment.
914 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700915 * would result in different objectives. If the next object does not exist,
916 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800917 *
918 * @param deviceId Device ID
919 * @param portNum port number on device for which NextObjective is queried
920 * @param treatment the actions to apply on the packets (should include outport)
921 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700922 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700923 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800924 */
925 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
926 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700927 TrafficSelector meta,
928 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -0800929 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
930 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -0700931 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -0800932 } else {
Charles Chane849c192016-01-11 18:28:54 -0800933 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
934 + " not found", deviceId);
935 return -1;
936 }
937 }
938
Saurav Dasc88d4662017-05-15 15:34:25 -0700939 /**
940 * Returns the group handler object for the specified device id.
941 *
942 * @param devId the device identifier
943 * @return the groupHandler object for the device id, or null if not found
944 */
Charles Chan65238242017-06-22 18:03:14 -0700945 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700946 return groupHandlerMap.get(devId);
947 }
948
949 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700950 * Returns the default routing handler object.
951 *
952 * @return the default routing handler object
953 */
954 public DefaultRoutingHandler getRoutingHandler() {
955 return defaultRoutingHandler;
956 }
957
sanghob35a6192015-04-01 13:05:26 -0700958 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700959 @Override
960 public void process(PacketContext context) {
961
962 if (context.isHandled()) {
963 return;
964 }
965
966 InboundPacket pkt = context.inPacket();
967 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800968
969 if (ethernet == null) {
970 return;
971 }
972
Saurav Das7bcbe702017-06-13 15:35:54 -0700973 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
974 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800975 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700976 log.warn("Received unexpected ARP packet on {}",
977 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -0700978 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800979 return;
sanghob35a6192015-04-01 13:05:26 -0700980 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800981 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
982 //ipHandler.addToPacketBuffer(ipv4Packet);
983 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
984 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700985 } else {
Charles Chan50035632017-01-13 17:20:44 -0800986 // NOTE: We don't support IP learning at this moment so this
987 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800988 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700989 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800990 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
991 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -0800992 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -0800993 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -0800994 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
995 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
996 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
997 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
998 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
999 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -07001000 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -07001001 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -08001002 }
1003 } else {
1004 // NOTE: We don't support IP learning at this moment so this
1005 // is not necessary. Also it causes duplication of DHCPv6 packets.
1006 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1007 }
sanghob35a6192015-04-01 13:05:26 -07001008 }
1009 }
1010 }
1011
sanghob35a6192015-04-01 13:05:26 -07001012 private class InternalEventHandler implements Runnable {
Charles Chan9b7217c2018-04-05 16:31:15 -07001013 private Event event;
1014
1015 InternalEventHandler(Event event) {
1016 this.event = event;
1017 }
1018
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001019 @Override
sanghob35a6192015-04-01 13:05:26 -07001020 public void run() {
Charles Chan9b7217c2018-04-05 16:31:15 -07001021 try {
1022 // TODO We should also change SR routing and PW to listen to TopologyEvents
1023 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1024 event.type() == LinkEvent.Type.LINK_UPDATED) {
1025 linkHandler.processLinkAdded((Link) event.subject());
1026 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1027 linkHandler.processLinkRemoved((Link) event.subject());
1028 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1029 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1030 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1031 DeviceId deviceId = ((Device) event.subject()).id();
1032 if (deviceService.isAvailable(deviceId)) {
1033 log.info("** DEVICE UP Processing device event {} "
1034 + "for available device {}",
1035 event.type(), ((Device) event.subject()).id());
1036 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001037 } else {
Charles Chan9b7217c2018-04-05 16:31:15 -07001038 log.info(" ** DEVICE DOWN Processing device event {}"
1039 + " for unavailable device {}",
1040 event.type(), ((Device) event.subject()).id());
1041 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001042 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001043 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1044 // typically these calls come when device is added first time
1045 // so port filtering rules are handled at the device_added event.
1046 // port added calls represent all ports on the device,
1047 // enabled or not.
1048 log.trace("** PORT ADDED {}/{} -> {}",
1049 ((DeviceEvent) event).subject().id(),
1050 ((DeviceEvent) event).port().number(),
1051 event.type());
1052 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1053 // these calls happen for every subsequent event
1054 // ports enabled, disabled, switch goes away, comes back
1055 log.info("** PORT UPDATED {}/{} -> {}",
1056 event.subject(),
1057 ((DeviceEvent) event).port(),
1058 event.type());
1059 processPortUpdated(((Device) event.subject()),
1060 ((DeviceEvent) event).port());
1061 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1062 // Process topology event, needed for all modules relying on
1063 // topology service for path computation
1064 TopologyEvent topologyEvent = (TopologyEvent) event;
1065 log.info("Processing topology event {}, topology age {}, reasons {}",
1066 event.type(), topologyEvent.subject().time(),
1067 topologyEvent.reasons().size());
1068 topologyHandler.processTopologyChange(topologyEvent.reasons());
1069 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1070 hostHandler.processHostAddedEvent((HostEvent) event);
1071 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1072 hostHandler.processHostMovedEvent((HostEvent) event);
1073 routeHandler.processHostMovedEvent((HostEvent) event);
1074 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1075 hostHandler.processHostRemovedEvent((HostEvent) event);
1076 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1077 hostHandler.processHostUpdatedEvent((HostEvent) event);
1078 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1079 routeHandler.processRouteAdded((RouteEvent) event);
1080 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1081 routeHandler.processRouteUpdated((RouteEvent) event);
1082 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1083 routeHandler.processRouteRemoved((RouteEvent) event);
1084 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1085 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1086 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1087 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1088 event.type() == McastEvent.Type.SINKS_ADDED ||
1089 event.type() == McastEvent.Type.SINKS_REMOVED ||
1090 event.type() == McastEvent.Type.ROUTE_ADDED ||
1091 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1092 mcastHandler.processMcastEvent((McastEvent) event);
1093 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1094 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1095 Class configClass = netcfgEvent.configClass();
1096 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1097 appCfgHandler.processAppConfigAdded(netcfgEvent);
1098 log.info("App config event .. configuring network");
1099 cfgListener.configureNetwork();
1100 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1101 log.info("Segment Routing Device Config added for {}", event.subject());
1102 cfgListener.configureNetwork();
1103 } else if (configClass.equals(XConnectConfig.class)) {
1104 xConnectHandler.processXConnectConfigAdded(netcfgEvent);
1105 } else if (configClass.equals(InterfaceConfig.class)) {
1106 log.info("Interface Config added for {}", event.subject());
1107 cfgListener.configureNetwork();
1108 } else {
1109 log.error("Unhandled config class: {}", configClass);
1110 }
1111 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1112 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1113 Class configClass = netcfgEvent.configClass();
1114 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1115 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1116 log.info("App config event .. configuring network");
1117 cfgListener.configureNetwork();
1118 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1119 log.info("Segment Routing Device Config updated for {}", event.subject());
1120 createOrUpdateDeviceConfiguration();
1121 } else if (configClass.equals(XConnectConfig.class)) {
1122 xConnectHandler.processXConnectConfigUpdated(netcfgEvent);
1123 } else if (configClass.equals(InterfaceConfig.class)) {
1124 log.info("Interface Config updated for {}", event.subject());
1125 createOrUpdateDeviceConfiguration();
1126 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1127 (InterfaceConfig) netcfgEvent.prevConfig().get());
1128 } else {
1129 log.error("Unhandled config class: {}", configClass);
1130 }
1131 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1132 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1133 Class configClass = netcfgEvent.configClass();
1134 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1135 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1136 log.info("App config event .. configuring network");
1137 cfgListener.configureNetwork();
1138 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1139 // TODO Handle sr device config removal
1140 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
1141 } else if (configClass.equals(XConnectConfig.class)) {
1142 xConnectHandler.processXConnectConfigRemoved(netcfgEvent);
1143 } else if (configClass.equals(InterfaceConfig.class)) {
1144 // TODO Handle interface removal
1145 log.info("InterfaceConfig removal is not handled in current implementation");
1146 } else {
1147 log.error("Unhandled config class: {}", configClass);
1148 }
1149 } else {
1150 log.warn("Unhandled event type: {}", event.type());
sanghob35a6192015-04-01 13:05:26 -07001151 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001152 } catch (Exception e) {
1153 log.error("SegmentRouting event handler thread thrown an exception: {}",
1154 e.getMessage(), e);
sanghob35a6192015-04-01 13:05:26 -07001155 }
sanghob35a6192015-04-01 13:05:26 -07001156 }
1157 }
1158
Saurav Das45f48152018-01-18 12:07:33 -08001159 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001160 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001161
1162 // NOTE: Punt ARP/NDP even when the device is not configured.
1163 // Host learning without network config is required for CORD config generator.
1164 routingRulePopulator.populateIpPunts(device.id());
1165 routingRulePopulator.populateArpNdpPunts(device.id());
1166
Charles Chan0b4e6182015-11-03 10:42:14 -08001167 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001168 log.warn("Device configuration unavailable. Device {} will be "
1169 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001170 return;
1171 }
Charles Chan2199c302016-04-23 17:36:10 -07001172 processDeviceAddedInternal(device.id());
1173 }
1174
1175 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001176 // Irrespective of whether the local is a MASTER or not for this device,
1177 // we need to create a SR-group-handler instance. This is because in a
1178 // multi-instance setup, any instance can initiate forwarding/next-objectives
1179 // for any switch (even if this instance is a SLAVE or not even connected
1180 // to the switch). To handle this, a default-group-handler instance is necessary
1181 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001182 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1183 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001184 DefaultGroupHandler groupHandler;
1185 try {
1186 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001187 createGroupHandler(deviceId,
1188 appId,
1189 deviceConfiguration,
1190 linkService,
1191 flowObjectiveService,
1192 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001193 } catch (DeviceConfigNotFoundException e) {
1194 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1195 return;
1196 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001197 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001198 deviceId);
1199 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001200 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001201
Charles Chan2199c302016-04-23 17:36:10 -07001202 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001203 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001204 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001205 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001206 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001207 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001208 }
Charles Chan5270ed02016-01-30 23:22:37 -08001209
Charles Chan03a73e02016-10-24 14:52:01 -07001210 appCfgHandler.init(deviceId);
Charles Chan2ff1bac2018-03-29 16:03:41 -07001211 hostHandler.init(deviceId);
Charles Chan03a73e02016-10-24 14:52:01 -07001212 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001213 }
1214
Saurav Das80980c72016-03-23 11:22:49 -07001215 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001216 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001217 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan47933752017-11-30 15:37:50 -08001218 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001219 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001220 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001221 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001222 portNextObjStore.entrySet().stream()
1223 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001224 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Das45f48152018-01-18 12:07:33 -08001225 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001226
Saurav Dasceccf242017-08-03 18:30:35 -07001227 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1228 if (gh != null) {
1229 gh.shutdown();
1230 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001231 // Note that a switch going down is associated with all of its links
1232 // going down as well, but it is treated as a single switch down event
Saurav Das5a356042018-04-06 20:16:01 -07001233 // while the link-downs are ignored. We cannot rely on the ordering of
1234 // events - i.e we cannot expect all link-downs to come before the
1235 // switch down - so we purge all seen-links for the switch before
1236 // handling route-path changes for the switch-down
Saurav Dasc88d4662017-05-15 15:34:25 -07001237 defaultRoutingHandler
Saurav Das604ab3a2018-03-18 21:28:15 -07001238 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Dasc568c342018-01-25 09:49:01 -08001239 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001240 xConnectHandler.removeDevice(device.id());
Saurav Dasa4020382018-02-14 14:14:54 -08001241
1242 // Cleanup all internal groupHandler stores for this device. Should be
1243 // done after all rerouting or rehashing has been completed
1244 groupHandlerMap.entrySet()
1245 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Das80980c72016-03-23 11:22:49 -07001246 }
1247
Saurav Das1a129a02016-11-18 15:21:57 -08001248 private void processPortUpdated(Device device, Port port) {
1249 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1250 log.warn("Device configuration uploading. Not handling port event for"
1251 + "dev: {} port: {}", device.id(), port.number());
1252 return;
1253 }
Saurav Das018605f2017-02-18 14:05:44 -08001254
1255 if (!mastershipService.isLocalMaster(device.id())) {
1256 log.debug("Not master for dev:{} .. not handling port updated event"
1257 + "for port {}", device.id(), port.number());
1258 return;
1259 }
1260
1261 // first we handle filtering rules associated with the port
1262 if (port.isEnabled()) {
1263 log.info("Switchport {}/{} enabled..programming filters",
1264 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001265 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001266 } else {
1267 log.info("Switchport {}/{} disabled..removing filters",
1268 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001269 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001270 }
Saurav Das1a129a02016-11-18 15:21:57 -08001271
1272 // portUpdated calls are for ports that have gone down or up. For switch
1273 // to switch ports, link-events should take care of any re-routing or
1274 // group editing necessary for port up/down. Here we only process edge ports
1275 // that are already configured.
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001276 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1277 VlanId untaggedVlan = getUntaggedVlanId(cp);
1278 VlanId nativeVlan = getNativeVlanId(cp);
1279 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001280
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001281 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001282 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das1a129a02016-11-18 15:21:57 -08001283 + "dev/port: {}/{}", device.id(), port.number());
1284 return;
1285 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001286 if (untaggedVlan != null) {
1287 processEdgePort(device, port, untaggedVlan, true);
1288 }
1289 if (nativeVlan != null) {
1290 processEdgePort(device, port, nativeVlan, true);
1291 }
1292 if (!taggedVlans.isEmpty()) {
1293 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1294 }
Saurav Das1a129a02016-11-18 15:21:57 -08001295 }
1296
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001297 private void processEdgePort(Device device, Port port, VlanId vlanId,
1298 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001299 boolean portUp = port.isEnabled();
1300 if (portUp) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001301 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001302 port.number(), vlanId);
Charles Chan47933752017-11-30 15:37:50 -08001303 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Das1a129a02016-11-18 15:21:57 -08001304 } else {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001305 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001306 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001307 }
1308
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001309 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001310 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001311 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001312 } else {
1313 log.warn("Group handler not found for dev:{}. Not handling edge port"
1314 + " {} event for port:{}", device.id(),
1315 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001316 }
1317 }
sangho1e575652015-05-14 00:39:53 -07001318
Charles Chan27fe1a52017-10-20 16:06:55 -07001319 private void createOrUpdateDeviceConfiguration() {
1320 if (deviceConfiguration == null) {
Saurav Dase7f51012018-02-09 17:26:45 -08001321 log.info("Creating new DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001322 deviceConfiguration = new DeviceConfiguration(this);
1323 } else {
Saurav Dase7f51012018-02-09 17:26:45 -08001324 log.info("Updating DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001325 deviceConfiguration.updateConfig();
1326 }
1327 }
1328
Pier Ventre10bd8d12016-11-26 21:05:22 -08001329 /**
1330 * Registers the given connect point with the NRS, this is necessary
1331 * to receive the NDP and ARP packets from the NRS.
1332 *
1333 * @param portToRegister connect point to register
1334 */
1335 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001336 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001337 portToRegister,
1338 neighbourHandler,
1339 appId
1340 );
1341 }
1342
Charles Chand6832882015-10-05 17:50:33 -07001343 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001344 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001345 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001346
Charles Chane849c192016-01-11 18:28:54 -08001347 /**
1348 * Constructs the internal network config listener.
1349 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001350 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001351 */
Charles Chan65238242017-06-22 18:03:14 -07001352 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001353 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001354 }
1355
Charles Chane849c192016-01-11 18:28:54 -08001356 /**
1357 * Reads network config and initializes related data structure accordingly.
1358 */
Charles Chan47933752017-11-30 15:37:50 -08001359 void configureNetwork() {
Saurav Dase7f51012018-02-09 17:26:45 -08001360 log.info("Configuring network ...");
Charles Chan27fe1a52017-10-20 16:06:55 -07001361 createOrUpdateDeviceConfiguration();
Charles Chan4636be02015-10-07 14:21:45 -07001362
Charles Chan2c15aca2016-11-09 20:51:44 -08001363 arpHandler = new ArpHandler(srManager);
1364 icmpHandler = new IcmpHandler(srManager);
1365 ipHandler = new IpHandler(srManager);
1366 routingRulePopulator = new RoutingRulePopulator(srManager);
1367 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001368
1369 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1370 groupHandlerMap, tunnelStore);
1371 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1372 flowObjectiveService,
1373 tunnelHandler, policyStore);
Saurav Das7bcbe702017-06-13 15:35:54 -07001374 // add a small delay to absorb multiple network config added notifications
1375 if (!programmingScheduled.get()) {
Saurav Dase7f51012018-02-09 17:26:45 -08001376 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
Saurav Das7bcbe702017-06-13 15:35:54 -07001377 programmingScheduled.set(true);
Charles Chan9b7217c2018-04-05 16:31:15 -07001378 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
Charles Chan4636be02015-10-07 14:21:45 -07001379 }
Charles Chan2199c302016-04-23 17:36:10 -07001380 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001381 }
1382
Charles Chand6832882015-10-05 17:50:33 -07001383 @Override
1384 public void event(NetworkConfigEvent event) {
Charles Chan7a888e82018-03-21 16:57:47 -07001385 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
1386 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1387 switch (event.type()) {
1388 case CONFIG_ADDED:
1389 case CONFIG_UPDATED:
1390 case CONFIG_REMOVED:
1391 log.trace("Schedule Network Config event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001392 mainEventExecutor.execute(new InternalEventHandler(event));
Charles Chan7a888e82018-03-21 16:57:47 -07001393 break;
1394 default:
1395 break;
Charles Chand6832882015-10-05 17:50:33 -07001396 }
1397 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001398
Charles Chan50443e82018-01-03 16:26:32 -08001399 @Override
1400 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase7f51012018-02-09 17:26:45 -08001401 if (event.type() == CONFIG_REGISTERED ||
1402 event.type() == CONFIG_UNREGISTERED) {
1403 log.debug("Ignore event {} due to type mismatch", event);
1404 return false;
Charles Chan50443e82018-01-03 16:26:32 -08001405 }
Saurav Dase7f51012018-02-09 17:26:45 -08001406
1407 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1408 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1409 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscd339592018-02-23 14:18:00 -08001410 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase7f51012018-02-09 17:26:45 -08001411 log.debug("Ignore event {} due to class mismatch", event);
1412 return false;
1413 }
1414
1415 return true;
Charles Chan50443e82018-01-03 16:26:32 -08001416 }
1417
Saurav Das7bcbe702017-06-13 15:35:54 -07001418 private final class ConfigChange implements Runnable {
1419 @Override
1420 public void run() {
1421 programmingScheduled.set(false);
Saurav Dase7f51012018-02-09 17:26:45 -08001422 log.info("Reacting to config changes after buffer delay");
Saurav Das7bcbe702017-06-13 15:35:54 -07001423 for (Device device : deviceService.getDevices()) {
1424 processDeviceAdded(device);
1425 }
1426 defaultRoutingHandler.startPopulationProcess();
1427 }
1428 }
Charles Chand6832882015-10-05 17:50:33 -07001429 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001430
Charles Chan7a888e82018-03-21 16:57:47 -07001431 private class InternalLinkListener implements LinkListener {
1432 @Override
1433 public void event(LinkEvent event) {
1434 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1435 event.type() == LinkEvent.Type.LINK_UPDATED ||
1436 event.type() == LinkEvent.Type.LINK_REMOVED) {
1437 log.trace("Schedule Link event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001438 mainEventExecutor.execute(new InternalEventHandler(event));
Charles Chan7a888e82018-03-21 16:57:47 -07001439 }
1440 }
1441 }
1442
1443 private class InternalDeviceListener implements DeviceListener {
1444 @Override
1445 public void event(DeviceEvent event) {
1446 switch (event.type()) {
1447 case DEVICE_ADDED:
1448 case PORT_UPDATED:
1449 case PORT_ADDED:
1450 case DEVICE_UPDATED:
1451 case DEVICE_AVAILABILITY_CHANGED:
1452 log.trace("Schedule Device event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001453 mainEventExecutor.execute(new InternalEventHandler(event));
Charles Chan7a888e82018-03-21 16:57:47 -07001454 break;
1455 default:
1456 }
1457 }
1458 }
1459
1460 private class InternalTopologyListener implements TopologyListener {
1461 @Override
1462 public void event(TopologyEvent event) {
1463 switch (event.type()) {
1464 case TOPOLOGY_CHANGED:
1465 log.trace("Schedule Topology event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001466 mainEventExecutor.execute(new InternalEventHandler(event));
Charles Chan7a888e82018-03-21 16:57:47 -07001467 break;
1468 default:
1469 }
1470 }
1471 }
1472
Charles Chan68aa62d2015-11-09 16:37:23 -08001473 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001474 @Override
1475 public void event(HostEvent event) {
Charles Chan68aa62d2015-11-09 16:37:23 -08001476 switch (event.type()) {
1477 case HOST_ADDED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001478 case HOST_MOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001479 case HOST_REMOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001480 case HOST_UPDATED:
Charles Chan7a888e82018-03-21 16:57:47 -07001481 log.trace("Schedule Host event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001482 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chan68aa62d2015-11-09 16:37:23 -08001483 break;
1484 default:
1485 log.warn("Unsupported host event type: {}", event.type());
1486 break;
1487 }
1488 }
1489 }
1490
Charles Chand55e84d2016-03-30 17:54:24 -07001491 private class InternalMcastListener implements McastListener {
1492 @Override
1493 public void event(McastEvent event) {
1494 switch (event.type()) {
Pier3ee24552018-03-14 16:47:32 -07001495 case SOURCES_ADDED:
1496 case SOURCES_REMOVED:
1497 case SINKS_ADDED:
1498 case SINKS_REMOVED:
Charles Chand55e84d2016-03-30 17:54:24 -07001499 case ROUTE_REMOVED:
Pier96f63cb2018-04-17 16:29:56 +02001500 case ROUTE_ADDED:
Charles Chan7a888e82018-03-21 16:57:47 -07001501 log.trace("Schedule Mcast event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001502 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi9930da52018-02-02 16:19:11 +01001503 break;
Charles Chand55e84d2016-03-30 17:54:24 -07001504 default:
Charles Chan7a888e82018-03-21 16:57:47 -07001505 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chand55e84d2016-03-30 17:54:24 -07001506 break;
1507 }
1508 }
1509 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001510
Charles Chan03a73e02016-10-24 14:52:01 -07001511 private class InternalRouteEventListener implements RouteListener {
1512 @Override
1513 public void event(RouteEvent event) {
1514 switch (event.type()) {
1515 case ROUTE_ADDED:
Charles Chan03a73e02016-10-24 14:52:01 -07001516 case ROUTE_UPDATED:
Charles Chan03a73e02016-10-24 14:52:01 -07001517 case ROUTE_REMOVED:
Charles Chan2fde6d42017-08-23 14:46:43 -07001518 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan7a888e82018-03-21 16:57:47 -07001519 log.trace("Schedule Route event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001520 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan2fde6d42017-08-23 14:46:43 -07001521 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001522 default:
Charles Chan7a888e82018-03-21 16:57:47 -07001523 log.warn("Unsupported route event type: {}", event.type());
Charles Chan03a73e02016-10-24 14:52:01 -07001524 break;
1525 }
1526 }
1527 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001528
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001529 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1530 try {
1531 Set<Interface> intfs = conf.getInterfaces();
1532 Set<Interface> prevIntfs = prevConf.getInterfaces();
1533
1534 // Now we only handle one interface config at each port.
1535 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1536 log.warn("Interface update aborted - one at a time is allowed, " +
1537 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1538 return;
1539 }
1540
1541 Interface intf = intfs.stream().findFirst().get();
1542 Interface prevIntf = prevIntfs.stream().findFirst().get();
1543
1544 DeviceId deviceId = intf.connectPoint().deviceId();
1545 PortNumber portNum = intf.connectPoint().port();
1546
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001547 removeSubnetConfig(prevIntf.connectPoint(),
1548 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1549 new HashSet<>(intf.ipAddressesList())));
1550
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001551 if (!prevIntf.vlanNative().equals(VlanId.NONE)
1552 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
1553 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1554 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
1555 // Update filtering objective and L2IG group bucket
1556 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
1557 } else {
1558 // RemoveVlanNative
1559 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1560 }
1561 }
1562
1563 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
1564 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1565 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1566 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
1567 // Update filtering objective and L2IG group bucket
1568 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
1569 } else {
1570 // RemoveVlanUntagged
1571 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1572 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001573 }
1574
1575 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1576 // RemoveVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001577 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
1578 .filter(i -> !intf.vlanUntagged().equals(i))
1579 .filter(i -> !intf.vlanNative().equals(i))
1580 .forEach(vlanId -> updateVlanConfigInternal(
1581 deviceId, portNum, vlanId, false, false));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001582 }
1583
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001584 if (!intf.vlanNative().equals(VlanId.NONE)
1585 && !prevIntf.vlanNative().equals(intf.vlanNative())
1586 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1587 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
1588 // Update filtering objective and L2IG group bucket
1589 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
1590 } else {
1591 // AddVlanNative
1592 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1593 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001594 }
1595
1596 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1597 // AddVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001598 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
1599 .filter(i -> !prevIntf.vlanUntagged().equals(i))
1600 .filter(i -> !prevIntf.vlanNative().equals(i))
1601 .forEach(vlanId -> updateVlanConfigInternal(
1602 deviceId, portNum, vlanId, false, true)
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001603 );
1604 }
1605
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001606 if (!intf.vlanUntagged().equals(VlanId.NONE)
1607 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1608 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
1609 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
1610 // Update filtering objective and L2IG group bucket
1611 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
1612 } else {
1613 // AddVlanUntagged
1614 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1615 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001616 }
1617 addSubnetConfig(prevIntf.connectPoint(),
1618 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1619 new HashSet<>(prevIntf.ipAddressesList())));
1620 } catch (ConfigException e) {
1621 log.error("Error in configuration");
1622 }
1623 }
1624
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001625 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
1626 VlanId vlanId, boolean pushVlan) {
1627 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1628 if (grpHandler == null) {
1629 log.warn("Failed to retrieve group handler for device {}", deviceId);
1630 return;
1631 }
1632
1633 // Update filtering objective for a single port
1634 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
1635 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
1636
1637 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
1638 // Update L2IG bucket of the port
1639 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
1640 } else {
1641 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1642 }
1643 }
1644
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001645 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1646 VlanId vlanId, boolean pushVlan, boolean install) {
1647 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1648 if (grpHandler == null) {
1649 log.warn("Failed to retrieve group handler for device {}", deviceId);
1650 return;
1651 }
1652
1653 // Update filtering objective for a single port
1654 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1655
1656 // Update filtering objective for multicast ingress port
1657 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1658
1659 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1660
1661 if (nextId != -1 && !install) {
1662 // Update next objective for a single port as an output port
1663 // Remove a single port from L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001664 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001665 // Remove L2 Bridging rule and L3 Unicast rule to the host
1666 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1667 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1668 // only if there is no port configured on that VLAN ID
1669 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1670 // Remove broadcast forwarding rule for the VLAN
1671 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1672 // Remove L2FG for VLAN
1673 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1674 } else {
1675 // Remove L2IG of the port
1676 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1677 }
1678 } else if (install) {
1679 if (nextId != -1) {
1680 // Add a single port to L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001681 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001682 } else {
1683 // Create L2FG for VLAN
1684 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1685 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1686 }
1687 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1688 } else {
1689 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1690 }
1691 }
1692
1693 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1694 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1695 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1696
1697 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1698 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1699 .filter(intf -> !intf.connectPoint().equals(cp))
1700 .flatMap(intf -> intf.ipAddressesList().stream())
1701 .collect(Collectors.toSet());
1702 // 1. Partial subnet population
1703 // Remove routing rules for removed subnet from previous configuration,
1704 // which does not also exist in other interfaces in the same device
1705 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1706 .map(InterfaceIpAddress::subnetAddress)
1707 .collect(Collectors.toSet());
1708
1709 defaultRoutingHandler.revokeSubnet(
1710 ipPrefixSet.stream()
1711 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1712 .collect(Collectors.toSet()));
1713
1714 // 2. Interface IP punts
1715 // Remove IP punts for old Intf address
1716 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1717 .map(InterfaceIpAddress::ipAddress)
1718 .collect(Collectors.toSet());
1719 ipAddressSet.stream()
1720 .map(InterfaceIpAddress::ipAddress)
1721 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1722 .forEach(interfaceIpAddress ->
1723 routingRulePopulator.revokeSingleIpPunts(
1724 cp.deviceId(), interfaceIpAddress));
1725
1726 // 3. Host unicast routing rule
1727 // Remove unicast routing rule
1728 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1729 }
1730
1731 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1732 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1733 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1734
1735 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1736 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1737 .filter(intf -> !intf.connectPoint().equals(cp))
1738 .flatMap(intf -> intf.ipAddressesList().stream())
1739 .collect(Collectors.toSet());
1740 // 1. Partial subnet population
1741 // Add routing rules for newly added subnet, which does not also exist in
1742 // other interfaces in the same device
1743 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1744 .map(InterfaceIpAddress::subnetAddress)
1745 .collect(Collectors.toSet());
1746
1747 defaultRoutingHandler.populateSubnet(
1748 Collections.singleton(cp),
1749 ipPrefixSet.stream()
1750 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1751 .collect(Collectors.toSet()));
1752
1753 // 2. Interface IP punts
1754 // Add IP punts for new 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.populateSingleIpPunts(
1763 cp.deviceId(), interfaceIpAddress));
1764
1765 // 3. Host unicast routing rule
1766 // Add unicast routing rule
1767 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1768 }
sanghob35a6192015-04-01 13:05:26 -07001769}