blob: 559f60cd55ebb211e94abb01a18155b9361a6718 [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;
42import org.onosproject.cluster.LeadershipService;
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;
Pier Ventre10bd8d12016-11-26 21:05:22 -080047import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070048import org.onosproject.net.Device;
49import org.onosproject.net.DeviceId;
Charles Chan9640c812017-08-23 13:55:39 -070050import org.onosproject.net.Host;
51import org.onosproject.net.HostId;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070052import org.onosproject.net.Link;
53import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080054import org.onosproject.net.PortNumber;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070055import org.onosproject.net.config.ConfigException;
Charles Chand6832882015-10-05 17:50:33 -070056import org.onosproject.net.config.ConfigFactory;
57import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070058import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070059import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070060import org.onosproject.net.config.basics.InterfaceConfig;
61import org.onosproject.net.config.basics.McastConfig;
Charles Chand6832882015-10-05 17:50:33 -070062import org.onosproject.net.config.basics.SubjectFactories;
Saurav Das45f48152018-01-18 12:07:33 -080063import org.onosproject.net.device.DeviceAdminService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070064import org.onosproject.net.device.DeviceEvent;
65import org.onosproject.net.device.DeviceListener;
66import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080067import org.onosproject.net.flow.TrafficSelector;
68import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070069import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan68aa62d2015-11-09 16:37:23 -080070import org.onosproject.net.host.HostEvent;
71import org.onosproject.net.host.HostListener;
Charles Chan47933752017-11-30 15:37:50 -080072import org.onosproject.net.host.HostLocationProbingService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080073import org.onosproject.net.host.HostService;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070074import org.onosproject.net.host.InterfaceIpAddress;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070075import org.onosproject.net.intf.Interface;
76import org.onosproject.net.intf.InterfaceService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080077import org.onosproject.net.link.LinkEvent;
78import org.onosproject.net.link.LinkListener;
79import org.onosproject.net.link.LinkService;
Charles Chand55e84d2016-03-30 17:54:24 -070080import org.onosproject.net.mcast.McastEvent;
81import org.onosproject.net.mcast.McastListener;
82import org.onosproject.net.mcast.MulticastRouteService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070083import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080084import org.onosproject.net.packet.InboundPacket;
85import org.onosproject.net.packet.PacketContext;
86import org.onosproject.net.packet.PacketProcessor;
87import org.onosproject.net.packet.PacketService;
Pier Luigi83f919b2018-02-15 16:33:08 +010088import org.onosproject.net.topology.TopologyEvent;
89import org.onosproject.net.topology.TopologyListener;
Charles Chand55e84d2016-03-30 17:54:24 -070090import org.onosproject.net.topology.TopologyService;
Charles Chan9640c812017-08-23 13:55:39 -070091import org.onosproject.routeservice.ResolvedRoute;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070092import org.onosproject.routeservice.RouteEvent;
93import org.onosproject.routeservice.RouteListener;
94import org.onosproject.routeservice.RouteService;
Charles Chand55e84d2016-03-30 17:54:24 -070095import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
96import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventref34966c2016-11-07 16:21:04 -080097import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070098import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -070099import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -0700100import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700101import org.onosproject.segmentrouting.grouphandler.DestinationSet;
102import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Pier Luigi96fe0772018-02-28 12:10:50 +0100103import org.onosproject.segmentrouting.mcast.McastHandler;
104import org.onosproject.segmentrouting.mcast.McastRole;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700105import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800106import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700107import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800108import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
109
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800110import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700111import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800112import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800113import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
114
Saurav Das7bcbe702017-06-13 15:35:54 -0700115import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Pier Luigib29144d2018-01-15 18:06:43 +0100116import org.onosproject.segmentrouting.storekey.McastStoreKey;
Charles Chand2990362016-04-18 13:44:03 -0700117import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -0800118import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -0700119import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700120import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700121import org.onosproject.store.service.EventuallyConsistentMap;
122import org.onosproject.store.service.EventuallyConsistentMapBuilder;
123import org.onosproject.store.service.StorageService;
124import org.onosproject.store.service.WallClockTimestamp;
Charles Chan47933752017-11-30 15:37:50 -0800125import org.osgi.service.component.ComponentContext;
sanghob35a6192015-04-01 13:05:26 -0700126import org.slf4j.Logger;
127import org.slf4j.LoggerFactory;
128
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800129import java.util.Collections;
Charles Chan47933752017-11-30 15:37:50 -0800130import java.util.Dictionary;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800131import java.util.HashSet;
132import java.util.List;
133import java.util.Map;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800134import java.util.Optional;
135import java.util.Set;
136import java.util.concurrent.ConcurrentHashMap;
137import java.util.concurrent.ConcurrentLinkedQueue;
138import java.util.concurrent.Executors;
139import java.util.concurrent.ScheduledExecutorService;
140import java.util.concurrent.ScheduledFuture;
141import 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;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800150import static org.onosproject.segmentrouting.pwaas.PwaasUtil.configurationValidity;
Charles Chan3e783d02016-02-26 22:19:52 -0800151
Charles Chane849c192016-01-11 18:28:54 -0800152/**
153 * Segment routing manager.
154 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700155@Service
156@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700157public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700158
Charles Chan2c15aca2016-11-09 20:51:44 -0800159 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan2fde6d42017-08-23 14:46:43 -0700160 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sanghob35a6192015-04-01 13:05:26 -0700161
162 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700163 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700164
165 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800166 private NeighbourResolutionService neighbourResolutionService;
167
168 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100169 public CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700170
171 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700172 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700173
174 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700175 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700176
177 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan47933752017-11-30 15:37:50 -0800178 HostLocationProbingService probingService;
179
180 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100181 public DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700182
183 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Saurav Das45f48152018-01-18 12:07:33 -0800184 DeviceAdminService deviceAdminService;
185
186 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800187 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700188
189 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100190 public LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700191
Charles Chan5270ed02016-01-30 23:22:37 -0800192 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800193 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700194
195 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800196 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700197
198 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100199 public MulticastRouteService multicastRouteService;
Charles Chan03a73e02016-10-24 14:52:01 -0700200
201 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chanf237e1b2018-01-09 13:45:07 -0800202 public TopologyService topologyService;
Charles Chan03a73e02016-10-24 14:52:01 -0700203
204 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700205 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800206
207 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800208 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800209
Saurav Das80980c72016-03-23 11:22:49 -0700210 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800211 public InterfaceService interfaceService;
212
Pier Luigieba73a02018-01-16 10:47:50 +0100213 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
214 public ClusterService clusterService;
215
216 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
217 public LeadershipService leadershipService;
218
Charles Chan47933752017-11-30 15:37:50 -0800219 @Property(name = "activeProbing", boolValue = true,
220 label = "Enable active probing to discover dual-homed hosts.")
221 boolean activeProbing = true;
222
Charles Chan03a73e02016-10-24 14:52:01 -0700223 ArpHandler arpHandler = null;
224 IcmpHandler icmpHandler = null;
225 IpHandler ipHandler = null;
226 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700227 ApplicationId appId;
228 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700229
Charles Chan03a73e02016-10-24 14:52:01 -0700230 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700231 private TunnelHandler tunnelHandler = null;
232 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700233 private InternalPacketProcessor processor = null;
234 private InternalLinkListener linkListener = null;
235 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700236 private AppConfigHandler appCfgHandler = null;
Pier Luigi96fe0772018-02-28 12:10:50 +0100237 public XConnectHandler xConnectHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800238 McastHandler mcastHandler = null;
239 HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800240 private RouteHandler routeHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800241 LinkHandler linkHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800242 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800243 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigi83f919b2018-02-15 16:33:08 +0100244 private TopologyHandler topologyHandler = null;
sanghob35a6192015-04-01 13:05:26 -0700245 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan5270ed02016-01-30 23:22:37 -0800246 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700247 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
248 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700249 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigi83f919b2018-02-15 16:33:08 +0100250 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
sanghob35a6192015-04-01 13:05:26 -0700251
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700252 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700253 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sanghob35a6192015-04-01 13:05:26 -0700254
Saurav Das4ce45962015-11-24 23:21:05 -0800255 @SuppressWarnings("unused")
sanghob35a6192015-04-01 13:05:26 -0700256 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800257 @SuppressWarnings("rawtypes")
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700258 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Saurav Das45f48152018-01-18 12:07:33 -0800259 Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chane849c192016-01-11 18:28:54 -0800260 new ConcurrentHashMap<>();
261 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700262 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700263 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800264 */
Charles Chan47933752017-11-30 15:37:50 -0800265 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das7bcbe702017-06-13 15:35:54 -0700266 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800267 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700268 * Per device next objective ID store with (device id + vlanid) as key.
269 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800270 */
Charles Chan47933752017-11-30 15:37:50 -0800271 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800272 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800273 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700274 * Per device next objective ID store with (device id + port + treatment + meta) as key.
275 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chane849c192016-01-11 18:28:54 -0800276 */
Charles Chan47933752017-11-30 15:37:50 -0800277 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800278 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800279
Saurav Das4ce45962015-11-24 23:21:05 -0800280 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
281 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700282
Saurav Das7bcbe702017-06-13 15:35:54 -0700283 private AtomicBoolean programmingScheduled = new AtomicBoolean();
284
Charles Chand55e84d2016-03-30 17:54:24 -0700285 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700286 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
287 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700288 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700289 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800290 public SegmentRoutingDeviceConfig createConfig() {
291 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700292 }
293 };
Pier Ventref34966c2016-11-07 16:21:04 -0800294
Charles Chand55e84d2016-03-30 17:54:24 -0700295 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700296 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
297 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700298 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800299 @Override
300 public SegmentRoutingAppConfig createConfig() {
301 return new SegmentRoutingAppConfig();
302 }
303 };
Pier Ventref34966c2016-11-07 16:21:04 -0800304
Charles Chanfc5c7802016-05-17 13:13:55 -0700305 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
306 new ConfigFactory<ApplicationId, XConnectConfig>(
307 SubjectFactories.APP_SUBJECT_FACTORY,
308 XConnectConfig.class, "xconnect") {
309 @Override
310 public XConnectConfig createConfig() {
311 return new XConnectConfig();
312 }
313 };
Pier Ventref34966c2016-11-07 16:21:04 -0800314
Charles Chand55e84d2016-03-30 17:54:24 -0700315 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700316 new ConfigFactory<ApplicationId, McastConfig>(
317 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700318 McastConfig.class, "multicast") {
319 @Override
320 public McastConfig createConfig() {
321 return new McastConfig();
322 }
323 };
324
Charles Chan65238242017-06-22 18:03:14 -0700325 private static final Object THREAD_SCHED_LOCK = new Object();
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700326 private static int numOfEventsQueued = 0;
327 private static int numOfEventsExecuted = 0;
sanghob35a6192015-04-01 13:05:26 -0700328 private static int numOfHandlerExecution = 0;
329 private static int numOfHandlerScheduled = 0;
330
Charles Chan116188d2016-02-18 14:22:42 -0800331 /**
332 * Segment Routing App ID.
333 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800334 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan59cc16d2017-02-02 16:20:42 -0800335
Charles Chane849c192016-01-11 18:28:54 -0800336 /**
337 * The default VLAN ID assigned to the interfaces without subnet config.
338 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800339 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700340
sanghob35a6192015-04-01 13:05:26 -0700341 @Activate
Charles Chan47933752017-11-30 15:37:50 -0800342 protected void activate(ComponentContext context) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800343 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700344
345 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700346 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700347 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700348 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700349 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700350 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700351 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700352 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700353 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700354
Charles Chan59cc16d2017-02-02 16:20:42 -0800355 log.debug("Creating EC map vlannextobjectivestore");
356 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
357 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
358 vlanNextObjStore = vlanNextObjMapBuilder
359 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700360 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700361 .withTimestampProvider((k, v) -> new WallClockTimestamp())
362 .build();
363
Saurav Das4ce45962015-11-24 23:21:05 -0800364 log.debug("Creating EC map subnetnextobjectivestore");
365 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
366 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
367 portNextObjStore = portNextObjMapBuilder
368 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700369 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800370 .withTimestampProvider((k, v) -> new WallClockTimestamp())
371 .build();
372
sangho0b2b6d12015-05-20 22:16:38 -0700373 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
374 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700375 tunnelStore = tunnelMapBuilder
376 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700377 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700378 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700379 .build();
380
381 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
382 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700383 policyStore = policyMapBuilder
384 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700385 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700386 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700387 .build();
388
Saurav Das80980c72016-03-23 11:22:49 -0700389 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800390 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700391 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800392 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800393 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
394 "requestInterceptsEnabled", "false");
Charles Chand3baaba2017-08-08 15:13:37 -0700395 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800396 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700397 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800398 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800399 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
400 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800401 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
402 "forceUnprovision", "true");
Charles Chanef624ed2017-08-10 16:57:28 -0700403 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan73316522017-07-20 16:16:25 -0700404 "distributed", "true");
Charles Chan35a32322017-08-14 11:42:11 -0700405 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
406 "multihomingEnabled", "true");
Charles Chan807d87a2017-11-29 19:54:20 -0800407 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
408 "staleLinkAge", "15000");
409 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
410 "allowDuplicateIps", "false");
Charles Chan47933752017-11-30 15:37:50 -0800411 compCfgService.registerProperties(getClass());
412 modified(context);
Saurav Das80980c72016-03-23 11:22:49 -0700413
Charles Chanb8e10c82015-10-14 11:24:40 -0700414 processor = new InternalPacketProcessor();
415 linkListener = new InternalLinkListener();
416 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700417 appCfgHandler = new AppConfigHandler(this);
418 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700419 mcastHandler = new McastHandler(this);
420 hostHandler = new HostHandler(this);
Saurav Das45f48152018-01-18 12:07:33 -0800421 linkHandler = new LinkHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700422 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800423 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800424 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigi83f919b2018-02-15 16:33:08 +0100425 topologyHandler = new TopologyHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700426
Charles Chan3e783d02016-02-26 22:19:52 -0800427 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700428 cfgService.registerConfigFactory(deviceConfigFactory);
429 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700430 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700431 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase7f51012018-02-09 17:26:45 -0800432 log.info("Configuring network before adding listeners");
Charles Chan132393a2018-01-04 14:26:07 -0800433 cfgListener.configureNetwork();
434
Charles Chan5270ed02016-01-30 23:22:37 -0800435 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700436 packetService.addProcessor(processor, PacketProcessor.director(2));
437 linkService.addListener(linkListener);
438 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700439 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700440 routeService.addListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100441 topologyService.addListener(topologyListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700442
Andreas Pantelopoulos4a768c02018-01-11 07:53:48 -0800443 l2TunnelHandler.init();
444
sanghob35a6192015-04-01 13:05:26 -0700445 log.info("Started");
446 }
447
Saurav Das45f48152018-01-18 12:07:33 -0800448 KryoNamespace.Builder createSerializer() {
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700449 return new KryoNamespace.Builder()
450 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700451 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800452 VlanNextObjectiveStoreKey.class,
453 DestinationSet.class,
454 NextNeighbors.class,
455 Tunnel.class,
456 DefaultTunnel.class,
457 Policy.class,
458 TunnelPolicy.class,
459 Policy.Type.class,
460 PortNextObjectiveStoreKey.class,
461 XConnectStoreKey.class,
462 L2Tunnel.class,
463 L2TunnelPolicy.class,
464 DefaultL2Tunnel.class,
465 DefaultL2TunnelPolicy.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700466 );
467 }
468
sanghob35a6192015-04-01 13:05:26 -0700469 @Deactivate
470 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700471 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700472 cfgService.unregisterConfigFactory(deviceConfigFactory);
473 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700474 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700475 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan47933752017-11-30 15:37:50 -0800476 compCfgService.unregisterProperties(getClass(), false);
Charles Chand6832882015-10-05 17:50:33 -0700477
Charles Chanc7a8a682017-06-19 00:43:31 -0700478 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700479 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700480 linkService.removeListener(linkListener);
481 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700482 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700483 routeService.removeListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100484 topologyService.removeListener(topologyListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700485
Charles Chan0aa674e2017-02-23 15:44:08 -0800486 neighbourResolutionService.unregisterNeighbourHandlers(appId);
487
sanghob35a6192015-04-01 13:05:26 -0700488 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700489 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700490 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700491 groupHandlerMap.clear();
492
Saurav Das7bcbe702017-06-13 15:35:54 -0700493 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800494 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700495 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700496 tunnelStore.destroy();
497 policyStore.destroy();
Pier Luigib72201b2018-01-25 16:16:02 +0100498
499 mcastHandler.terminate();
sanghob35a6192015-04-01 13:05:26 -0700500 log.info("Stopped");
501 }
502
Charles Chan47933752017-11-30 15:37:50 -0800503 @Modified
504 private void modified(ComponentContext context) {
505 Dictionary<?, ?> properties = context.getProperties();
506 if (properties == null) {
507 return;
508 }
509
510 String strActiveProving = Tools.get(properties, "activeProbing");
511 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
512
513 if (expectActiveProbing != activeProbing) {
514 activeProbing = expectActiveProbing;
515 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
516 }
517 }
518
sangho1e575652015-05-14 00:39:53 -0700519 @Override
520 public List<Tunnel> getTunnels() {
521 return tunnelHandler.getTunnels();
522 }
523
524 @Override
sangho71abe1b2015-06-29 14:58:47 -0700525 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
526 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700527 }
528
529 @Override
sangho71abe1b2015-06-29 14:58:47 -0700530 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700531 for (Policy policy: policyHandler.getPolicies()) {
532 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
533 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
534 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
535 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700536 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700537 }
538 }
539 }
sangho71abe1b2015-06-29 14:58:47 -0700540 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700541 }
542
543 @Override
sangho71abe1b2015-06-29 14:58:47 -0700544 public PolicyHandler.Result removePolicy(Policy policy) {
545 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700546 }
547
548 @Override
sangho71abe1b2015-06-29 14:58:47 -0700549 public PolicyHandler.Result createPolicy(Policy policy) {
550 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700551 }
552
553 @Override
554 public List<Policy> getPolicies() {
555 return policyHandler.getPolicies();
556 }
557
Saurav Das59232cf2016-04-27 18:35:50 -0700558 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800559 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700560 return l2TunnelHandler.getL2Tunnels();
561 }
562
563 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800564 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700565 return l2TunnelHandler.getL2Policies();
566 }
567
568 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800569 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
570
571 List<L2Tunnel> tunnels = getL2Tunnels();
572 List<L2TunnelPolicy> policies = getL2Policies();
573
574 // combine polices and tunnels to pseudowires
575 List<L2TunnelDescription> pseudowires = tunnels.stream()
576 .map(l2Tunnel -> {
577 L2TunnelPolicy policy = null;
578 for (L2TunnelPolicy l2Policy : policies) {
579 if (l2Policy.tunnelId() == l2Tunnel.tunnelId()) {
580 policy = l2Policy;
581 break;
582 }
583 }
584
585 return new DefaultL2TunnelDescription(l2Tunnel, policy);
586 })
587 .collect(Collectors.toList());
588
589
590 // creating a new list with the new pseudowire
591 Set<L2TunnelDescription> newPseudowires = new HashSet<>(pseudowires);
592
593 // corner case where we try to add the exact same pseudowire
594 if (newPseudowires.contains(l2TunnelDescription)) {
595 log.info("Pseudowire with {} already exists!", l2TunnelDescription);
596 return L2TunnelHandler.Result.SUCCESS;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700597 }
598
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800599 // add the new pseudowire to the Set
600 newPseudowires.add(l2TunnelDescription);
601
602 // validate the new set of pseudowires
603 boolean res = configurationValidity(newPseudowires);
604 if (res) {
605
606 // deploy a set with ONLY the new pseudowire
607 newPseudowires = new HashSet<>();
608 newPseudowires.add(l2TunnelDescription);
609 l2TunnelHandler.deploy(newPseudowires);
610
611 log.info("Pseudowire with {} deployment started, check log for any errors in this process!",
612 l2TunnelDescription.l2Tunnel().tunnelId());
613 return L2TunnelHandler.Result.SUCCESS;
614 } else {
615
616 log.error("Pseudowire with {} can not be added!", l2TunnelDescription.l2Tunnel().tunnelId());
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700617 return L2TunnelHandler.Result.ADDITION_ERROR;
618 }
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700619 }
620
621 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800622 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700623
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800624 List<L2Tunnel> tunnels = getL2Tunnels();
625 List<L2TunnelPolicy> policies = getL2Policies();
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700626
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800627 // get the pseudowire, if it exists
628 List<L2TunnelDescription> pseudowires = tunnels.stream().map(l2Tunnel -> {
629 L2TunnelPolicy policy = null;
630 for (L2TunnelPolicy l2Policy : policies) {
631 if (l2Policy.tunnelId() == l2Tunnel.tunnelId()) {
632 policy = l2Policy;
633 break;
634 }
635 }
636
637 return new DefaultL2TunnelDescription(l2Tunnel, policy);
638 }).filter(l2desc ->
639 l2desc.l2Tunnel().tunnelId() == pwId
640 ).collect(Collectors.toList());
641
642 if (pseudowires.size() == 0) {
643 log.error("Pseudowire with id {} does not exist", pwId);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700644 return L2TunnelHandler.Result.REMOVAL_ERROR;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800645 } else {
646
647 l2TunnelHandler.tearDown(new HashSet<>(pseudowires));
648
649 log.info("Removal of pseudowire with {} started, check log for any errors in this process!",
650 pwId);
651 return L2TunnelHandler.Result.SUCCESS;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700652 }
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700653 }
654
655 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700656 public void rerouteNetwork() {
657 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700658 }
659
Charles Chanc81c45b2016-10-20 17:02:44 -0700660 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800661 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
662 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800663 deviceConfiguration.getRouters().forEach(device ->
664 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700665 return deviceSubnetMap;
666 }
667
Saurav Dasc88d4662017-05-15 15:34:25 -0700668
669 @Override
670 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
671 if (defaultRoutingHandler != null) {
672 return defaultRoutingHandler.getCurrentEmcpSpgMap();
673 } else {
674 return null;
675 }
676 }
677
678 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700679 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
680 if (dsNextObjStore != null) {
681 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700682 } else {
683 return ImmutableMap.of();
684 }
685 }
686
Saurav Dasceccf242017-08-03 18:30:35 -0700687 @Override
688 public void verifyGroups(DeviceId id) {
689 DefaultGroupHandler gh = groupHandlerMap.get(id);
690 if (gh != null) {
691 gh.triggerBucketCorrector();
692 }
693 }
694
Saurav Dasc568c342018-01-25 09:49:01 -0800695 @Override
696 public ImmutableMap<Link, Boolean> getSeenLinks() {
697 return linkHandler.getSeenLinks();
698 }
699
700 @Override
701 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
702 return linkHandler.getDownedPorts();
703 }
704
Pier Luigib29144d2018-01-15 18:06:43 +0100705 @Override
706 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
707 return mcastHandler.getMcastNextIds(mcastIp);
708 }
709
710 @Override
Pier Luigi96fe0772018-02-28 12:10:50 +0100711 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigib29144d2018-01-15 18:06:43 +0100712 return mcastHandler.getMcastRoles(mcastIp);
713 }
714
715 @Override
716 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
717 return mcastHandler.getMcastPaths(mcastIp);
718 }
719
sanghof9d0bf12015-05-19 11:57:42 -0700720 /**
Ray Milkeye4afdb52017-04-05 09:42:04 -0700721 * Extracts the application ID from the manager.
722 *
723 * @return application ID
724 */
725 public ApplicationId appId() {
726 return appId;
727 }
728
729 /**
730 * Returns the device configuration.
731 *
732 * @return device configuration
733 */
734 public DeviceConfiguration deviceConfiguration() {
735 return deviceConfiguration;
736 }
737
738 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700739 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700740 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700741 *
742 * @return next objective ID store
743 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700744 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
745 dsNextObjStore() {
746 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700747 }
748
749 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700750 * Per device next objective ID store with (device id + vlanid) as key.
751 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700752 *
753 * @return vlan next object store
754 */
755 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
756 return vlanNextObjStore;
757 }
758
759 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700760 * Per device next objective ID store with (device id + port + treatment + meta) as key.
761 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700762 *
763 * @return port next object store.
764 */
765 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
766 return portNextObjStore;
767 }
768
769 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700770 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
771 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700772 *
773 * @return MPLS-ECMP value
774 */
775 public boolean getMplsEcmp() {
776 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
777 .getConfig(this.appId, SegmentRoutingAppConfig.class);
778 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
779 }
780
781 /**
sanghof9d0bf12015-05-19 11:57:42 -0700782 * Returns the tunnel object with the tunnel ID.
783 *
784 * @param tunnelId Tunnel ID
785 * @return Tunnel reference
786 */
sangho1e575652015-05-14 00:39:53 -0700787 public Tunnel getTunnel(String tunnelId) {
788 return tunnelHandler.getTunnel(tunnelId);
789 }
790
Charles Chan7ffd81f2017-02-08 15:52:08 -0800791 // TODO Consider moving these to InterfaceService
sanghob35a6192015-04-01 13:05:26 -0700792 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800793 * Returns untagged VLAN configured on given connect point.
Charles Chan7ffd81f2017-02-08 15:52:08 -0800794 * <p>
795 * Only returns the first match if there are multiple untagged VLAN configured
796 * on the connect point.
sanghob35a6192015-04-01 13:05:26 -0700797 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800798 * @param connectPoint connect point
799 * @return untagged VLAN or null if not configured
sanghob35a6192015-04-01 13:05:26 -0700800 */
Charles Chan65238242017-06-22 18:03:14 -0700801 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800802 return interfaceService.getInterfacesByPort(connectPoint).stream()
803 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
804 .map(Interface::vlanUntagged)
805 .findFirst().orElse(null);
sanghob35a6192015-04-01 13:05:26 -0700806 }
807
sangho1e575652015-05-14 00:39:53 -0700808 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800809 * Returns tagged VLAN configured on given connect point.
810 * <p>
811 * Returns all matches if there are multiple tagged VLAN configured
812 * on the connect point.
813 *
814 * @param connectPoint connect point
815 * @return tagged VLAN or empty set if not configured
816 */
Charles Chan65238242017-06-22 18:03:14 -0700817 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800818 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
819 return interfaces.stream()
820 .map(Interface::vlanTagged)
Charles Chan65238242017-06-22 18:03:14 -0700821 .flatMap(Set::stream)
Charles Chan7ffd81f2017-02-08 15:52:08 -0800822 .collect(Collectors.toSet());
823 }
824
825 /**
826 * Returns native VLAN configured on given connect point.
827 * <p>
828 * Only returns the first match if there are multiple native VLAN configured
829 * on the connect point.
830 *
831 * @param connectPoint connect point
832 * @return native VLAN or null if not configured
833 */
Charles Chan65238242017-06-22 18:03:14 -0700834 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800835 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
836 return interfaces.stream()
837 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
838 .map(Interface::vlanNative)
839 .findFirst()
840 .orElse(null);
841 }
842
843 /**
Charles Chanf9a52702017-06-16 15:19:24 -0700844 * Returns internal VLAN for untagged hosts on given connect point.
845 * <p>
846 * The internal VLAN is either vlan-untagged for an access port,
847 * or vlan-native for a trunk port.
848 *
849 * @param connectPoint connect point
850 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
851 */
Pier Luigi96fe0772018-02-28 12:10:50 +0100852 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chanf9a52702017-06-16 15:19:24 -0700853 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
854 VlanId nativeVlanId = getNativeVlanId(connectPoint);
855 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
856 }
857
858 /**
Charles Chan65238242017-06-22 18:03:14 -0700859 * Returns optional pair device ID of given device.
860 *
861 * @param deviceId device ID
862 * @return optional pair device ID. Might be empty if pair device is not configured
863 */
864 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
865 SegmentRoutingDeviceConfig deviceConfig =
866 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
867 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
868 }
869 /**
870 * Returns optional pair device local port of given device.
871 *
872 * @param deviceId device ID
873 * @return optional pair device ID. Might be empty if pair device is not configured
874 */
875 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
876 SegmentRoutingDeviceConfig deviceConfig =
877 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
878 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
879 }
880
881 /**
Charles Chan2fde6d42017-08-23 14:46:43 -0700882 * Determine if current instance is the master of given connect point.
883 *
884 * @param cp connect point
885 * @return true if current instance is the master of given connect point
886 */
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700887 public boolean isMasterOf(ConnectPoint cp) {
Charles Chan2fde6d42017-08-23 14:46:43 -0700888 boolean isMaster = mastershipService.isLocalMaster(cp.deviceId());
889 if (!isMaster) {
890 log.debug(NOT_MASTER, cp);
891 }
892 return isMaster;
893 }
894
895 /**
Charles Chan9640c812017-08-23 13:55:39 -0700896 * Returns locations of given resolved route.
897 *
898 * @param resolvedRoute resolved route
899 * @return locations of nexthop. Might be empty if next hop is not found
900 */
901 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
902 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
903 return Optional.ofNullable(hostService.getHost(hostId))
904 .map(Host::locations).orElse(Sets.newHashSet())
905 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
906 }
907
908 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800909 * Returns vlan port map of given device.
910 *
911 * @param deviceId device id
912 * @return vlan-port multimap
913 */
914 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
915 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
916
917 interfaceService.getInterfaces().stream()
918 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
919 .forEach(intf -> {
920 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -0700921 intf.vlanTagged().forEach(vlanTagged ->
922 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
923 );
Charles Chan7ffd81f2017-02-08 15:52:08 -0800924 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
925 });
926 vlanPortMap.removeAll(VlanId.NONE);
927
928 return vlanPortMap;
929 }
930
931 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800932 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800933 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700934 *
935 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800936 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800937 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700938 */
Charles Chan65238242017-06-22 18:03:14 -0700939 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700940 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800941 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
942 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700943 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800944 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800945 + "device {} not found", deviceId);
946 return -1;
947 }
948 }
949
950 /**
951 * Returns the next objective ID for the given portNumber, given the treatment.
952 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700953 * would result in different objectives. If the next object does not exist,
954 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800955 *
956 * @param deviceId Device ID
957 * @param portNum port number on device for which NextObjective is queried
958 * @param treatment the actions to apply on the packets (should include outport)
959 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700960 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700961 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800962 */
963 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
964 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700965 TrafficSelector meta,
966 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -0800967 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
968 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -0700969 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -0800970 } else {
Charles Chane849c192016-01-11 18:28:54 -0800971 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
972 + " not found", deviceId);
973 return -1;
974 }
975 }
976
Saurav Dasc88d4662017-05-15 15:34:25 -0700977 /**
978 * Returns the group handler object for the specified device id.
979 *
980 * @param devId the device identifier
981 * @return the groupHandler object for the device id, or null if not found
982 */
Charles Chan65238242017-06-22 18:03:14 -0700983 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700984 return groupHandlerMap.get(devId);
985 }
986
987 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700988 * Returns the default routing handler object.
989 *
990 * @return the default routing handler object
991 */
992 public DefaultRoutingHandler getRoutingHandler() {
993 return defaultRoutingHandler;
994 }
995
sanghob35a6192015-04-01 13:05:26 -0700996 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700997 @Override
998 public void process(PacketContext context) {
999
1000 if (context.isHandled()) {
1001 return;
1002 }
1003
1004 InboundPacket pkt = context.inPacket();
1005 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -08001006
1007 if (ethernet == null) {
1008 return;
1009 }
1010
Saurav Das7bcbe702017-06-13 15:35:54 -07001011 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1012 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001013 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001014 log.warn("Received unexpected ARP packet on {}",
1015 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -07001016 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -08001017 return;
sanghob35a6192015-04-01 13:05:26 -07001018 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -08001019 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1020 //ipHandler.addToPacketBuffer(ipv4Packet);
1021 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1022 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001023 } else {
Charles Chan50035632017-01-13 17:20:44 -08001024 // NOTE: We don't support IP learning at this moment so this
1025 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -08001026 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001027 }
Pier Ventre10bd8d12016-11-26 21:05:22 -08001028 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1029 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -08001030 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -08001031 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -08001032 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1033 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1034 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1035 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1036 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1037 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -07001038 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -07001039 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -08001040 }
1041 } else {
1042 // NOTE: We don't support IP learning at this moment so this
1043 // is not necessary. Also it causes duplication of DHCPv6 packets.
1044 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1045 }
sanghob35a6192015-04-01 13:05:26 -07001046 }
1047 }
1048 }
1049
1050 private class InternalLinkListener implements LinkListener {
1051 @Override
1052 public void event(LinkEvent event) {
Charles Chanb1f8c762017-03-29 16:39:05 -07001053 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1054 event.type() == LinkEvent.Type.LINK_UPDATED ||
1055 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001056 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -07001057 scheduleEventHandlerIfNotScheduled(event);
1058 }
1059 }
1060 }
1061
1062 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -07001063 @Override
1064 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -07001065 switch (event.type()) {
1066 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -08001067 case PORT_UPDATED:
1068 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -07001069 case DEVICE_UPDATED:
1070 case DEVICE_AVAILABILITY_CHANGED:
Saurav Dasc88d4662017-05-15 15:34:25 -07001071 log.trace("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -07001072 scheduleEventHandlerIfNotScheduled(event);
1073 break;
1074 default:
1075 }
1076 }
1077 }
1078
Pier Luigi83f919b2018-02-15 16:33:08 +01001079 /**
1080 * Internal listener for topology events.
1081 */
1082 private class InternalTopologyListener implements TopologyListener {
1083 @Override
1084 public void event(TopologyEvent event) {
1085 switch (event.type()) {
1086 case TOPOLOGY_CHANGED:
1087 log.debug("Event {} received from TopologyService", event.type());
1088 scheduleEventHandlerIfNotScheduled(event);
1089 break;
1090 default:
1091 }
1092 }
1093 }
1094
Saurav Das4ce45962015-11-24 23:21:05 -08001095 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -07001096 private void scheduleEventHandlerIfNotScheduled(Event event) {
Charles Chan65238242017-06-22 18:03:14 -07001097 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001098 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001099 numOfEventsQueued++;
1100
1101 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
1102 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001103 eventHandlerFuture = executorService
1104 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
1105 numOfHandlerScheduled++;
1106 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -07001107 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001108 numOfEventsQueued,
1109 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -07001110 }
sanghob35a6192015-04-01 13:05:26 -07001111 }
1112
1113 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001114 @Override
sanghob35a6192015-04-01 13:05:26 -07001115 public void run() {
Pier Luigieefa2762018-02-12 09:56:20 +01001116 while (true) {
1117 try {
Saurav Das4ce45962015-11-24 23:21:05 -08001118 @SuppressWarnings("rawtypes")
Charles Chan65238242017-06-22 18:03:14 -07001119 Event event;
1120 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001121 if (!eventQueue.isEmpty()) {
1122 event = eventQueue.poll();
1123 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001124 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001125 numOfHandlerExecution++;
1126 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
1127 numOfHandlerExecution, numOfEventsExecuted);
1128 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001129 }
sangho20eff1d2015-04-13 15:15:58 -07001130 }
Pier Luigi83f919b2018-02-15 16:33:08 +01001131 // TODO We should also change SR routing and PW to listen to TopologyEvents
Charles Chanb1f8c762017-03-29 16:39:05 -07001132 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1133 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Das45f48152018-01-18 12:07:33 -08001134 linkHandler.processLinkAdded((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001135 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Saurav Das45f48152018-01-18 12:07:33 -08001136 linkHandler.processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001137 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1138 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1139 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001140 DeviceId deviceId = ((Device) event.subject()).id();
1141 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001142 log.info("Processing device event {} for available device {}",
1143 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001144 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -07001145 } else {
1146 log.info("Processing device event {} for unavailable device {}",
Pier Luigieefa2762018-02-12 09:56:20 +01001147 event.type(), ((Device) event.subject()).id());
Saurav Das80980c72016-03-23 11:22:49 -07001148 processDeviceRemoved((Device) event.subject());
1149 }
Saurav Das1a129a02016-11-18 15:21:57 -08001150 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001151 // typically these calls come when device is added first time
1152 // so port filtering rules are handled at the device_added event.
1153 // port added calls represent all ports on the device,
1154 // enabled or not.
Saurav Dasc88d4662017-05-15 15:34:25 -07001155 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001156 ((DeviceEvent) event).subject().id(),
1157 ((DeviceEvent) event).port().number(),
1158 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -08001159 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001160 // these calls happen for every subsequent event
1161 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -08001162 log.info("** PORT UPDATED {}/{} -> {}",
1163 event.subject(),
1164 ((DeviceEvent) event).port(),
1165 event.type());
1166 processPortUpdated(((Device) event.subject()),
1167 ((DeviceEvent) event).port());
Pier Luigi83f919b2018-02-15 16:33:08 +01001168 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1169 // Process topology event, needed for all modules relying on
1170 // topology service for path computation
1171 TopologyEvent topologyEvent = (TopologyEvent) event;
1172 log.info("Processing topology event {}, topology age {}, reasons {}",
1173 event.type(), topologyEvent.subject().time(),
1174 topologyEvent.reasons().size());
1175 topologyHandler.processTopologyChange(topologyEvent.reasons());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001176 } else {
1177 log.warn("Unhandled event type: {}", event.type());
1178 }
Pier Luigieefa2762018-02-12 09:56:20 +01001179 } catch (Exception e) {
1180 log.error("SegmentRouting event handler thread thrown an exception: {}",
1181 e.getMessage(), e);
sanghob35a6192015-04-01 13:05:26 -07001182 }
1183 }
sanghob35a6192015-04-01 13:05:26 -07001184 }
1185 }
1186
Saurav Das45f48152018-01-18 12:07:33 -08001187 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001188 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001189
1190 // NOTE: Punt ARP/NDP even when the device is not configured.
1191 // Host learning without network config is required for CORD config generator.
1192 routingRulePopulator.populateIpPunts(device.id());
1193 routingRulePopulator.populateArpNdpPunts(device.id());
1194
Charles Chan0b4e6182015-11-03 10:42:14 -08001195 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001196 log.warn("Device configuration unavailable. Device {} will be "
1197 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001198 return;
1199 }
Charles Chan2199c302016-04-23 17:36:10 -07001200 processDeviceAddedInternal(device.id());
1201 }
1202
1203 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001204 // Irrespective of whether the local is a MASTER or not for this device,
1205 // we need to create a SR-group-handler instance. This is because in a
1206 // multi-instance setup, any instance can initiate forwarding/next-objectives
1207 // for any switch (even if this instance is a SLAVE or not even connected
1208 // to the switch). To handle this, a default-group-handler instance is necessary
1209 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001210 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1211 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001212 DefaultGroupHandler groupHandler;
1213 try {
1214 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001215 createGroupHandler(deviceId,
1216 appId,
1217 deviceConfiguration,
1218 linkService,
1219 flowObjectiveService,
1220 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001221 } catch (DeviceConfigNotFoundException e) {
1222 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1223 return;
1224 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001225 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001226 deviceId);
1227 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001228 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001229
Charles Chan2199c302016-04-23 17:36:10 -07001230 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001231 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan03a73e02016-10-24 14:52:01 -07001232 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001233 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001234 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001235 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001236 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001237 }
Charles Chan5270ed02016-01-30 23:22:37 -08001238
Charles Chan03a73e02016-10-24 14:52:01 -07001239 appCfgHandler.init(deviceId);
1240 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001241 }
1242
Saurav Das80980c72016-03-23 11:22:49 -07001243 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001244 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001245 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan47933752017-11-30 15:37:50 -08001246 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001247 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001248 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001249 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001250 portNextObjStore.entrySet().stream()
1251 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001252 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Das45f48152018-01-18 12:07:33 -08001253 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001254
Saurav Dasceccf242017-08-03 18:30:35 -07001255 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1256 if (gh != null) {
1257 gh.shutdown();
1258 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001259 // Note that a switch going down is associated with all of its links
1260 // going down as well, but it is treated as a single switch down event
1261 // while the link-downs are ignored.
1262 defaultRoutingHandler
1263 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001264 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001265 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -07001266 }
1267
Saurav Das1a129a02016-11-18 15:21:57 -08001268 private void processPortUpdated(Device device, Port port) {
1269 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1270 log.warn("Device configuration uploading. Not handling port event for"
1271 + "dev: {} port: {}", device.id(), port.number());
1272 return;
1273 }
Saurav Das018605f2017-02-18 14:05:44 -08001274
1275 if (!mastershipService.isLocalMaster(device.id())) {
1276 log.debug("Not master for dev:{} .. not handling port updated event"
1277 + "for port {}", device.id(), port.number());
1278 return;
1279 }
1280
1281 // first we handle filtering rules associated with the port
1282 if (port.isEnabled()) {
1283 log.info("Switchport {}/{} enabled..programming filters",
1284 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001285 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001286 } else {
1287 log.info("Switchport {}/{} disabled..removing filters",
1288 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001289 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001290 }
Saurav Das1a129a02016-11-18 15:21:57 -08001291
1292 // portUpdated calls are for ports that have gone down or up. For switch
1293 // to switch ports, link-events should take care of any re-routing or
1294 // group editing necessary for port up/down. Here we only process edge ports
1295 // that are already configured.
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001296 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1297 VlanId untaggedVlan = getUntaggedVlanId(cp);
1298 VlanId nativeVlan = getNativeVlanId(cp);
1299 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001300
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001301 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001302 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das1a129a02016-11-18 15:21:57 -08001303 + "dev/port: {}/{}", device.id(), port.number());
1304 return;
1305 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001306 if (untaggedVlan != null) {
1307 processEdgePort(device, port, untaggedVlan, true);
1308 }
1309 if (nativeVlan != null) {
1310 processEdgePort(device, port, nativeVlan, true);
1311 }
1312 if (!taggedVlans.isEmpty()) {
1313 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1314 }
Saurav Das1a129a02016-11-18 15:21:57 -08001315 }
1316
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001317 private void processEdgePort(Device device, Port port, VlanId vlanId,
1318 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001319 boolean portUp = port.isEnabled();
1320 if (portUp) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001321 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001322 port.number(), vlanId);
Charles Chan47933752017-11-30 15:37:50 -08001323 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Das1a129a02016-11-18 15:21:57 -08001324 } else {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001325 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001326 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001327 }
1328
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001329 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001330 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001331 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001332 } else {
1333 log.warn("Group handler not found for dev:{}. Not handling edge port"
1334 + " {} event for port:{}", device.id(),
1335 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001336 }
1337 }
sangho1e575652015-05-14 00:39:53 -07001338
Charles Chan27fe1a52017-10-20 16:06:55 -07001339 private void createOrUpdateDeviceConfiguration() {
1340 if (deviceConfiguration == null) {
Saurav Dase7f51012018-02-09 17:26:45 -08001341 log.info("Creating new DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001342 deviceConfiguration = new DeviceConfiguration(this);
1343 } else {
Saurav Dase7f51012018-02-09 17:26:45 -08001344 log.info("Updating DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001345 deviceConfiguration.updateConfig();
1346 }
1347 }
1348
Pier Ventre10bd8d12016-11-26 21:05:22 -08001349 /**
1350 * Registers the given connect point with the NRS, this is necessary
1351 * to receive the NDP and ARP packets from the NRS.
1352 *
1353 * @param portToRegister connect point to register
1354 */
1355 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001356 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001357 portToRegister,
1358 neighbourHandler,
1359 appId
1360 );
1361 }
1362
Charles Chand6832882015-10-05 17:50:33 -07001363 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001364 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001365 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001366
Charles Chane849c192016-01-11 18:28:54 -08001367 /**
1368 * Constructs the internal network config listener.
1369 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001370 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001371 */
Charles Chan65238242017-06-22 18:03:14 -07001372 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001373 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001374 }
1375
Charles Chane849c192016-01-11 18:28:54 -08001376 /**
1377 * Reads network config and initializes related data structure accordingly.
1378 */
Charles Chan47933752017-11-30 15:37:50 -08001379 void configureNetwork() {
Saurav Dase7f51012018-02-09 17:26:45 -08001380 log.info("Configuring network ...");
Charles Chan27fe1a52017-10-20 16:06:55 -07001381 createOrUpdateDeviceConfiguration();
Charles Chan4636be02015-10-07 14:21:45 -07001382
Charles Chan2c15aca2016-11-09 20:51:44 -08001383 arpHandler = new ArpHandler(srManager);
1384 icmpHandler = new IcmpHandler(srManager);
1385 ipHandler = new IpHandler(srManager);
1386 routingRulePopulator = new RoutingRulePopulator(srManager);
1387 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001388
1389 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1390 groupHandlerMap, tunnelStore);
1391 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1392 flowObjectiveService,
1393 tunnelHandler, policyStore);
Saurav Das7bcbe702017-06-13 15:35:54 -07001394 // add a small delay to absorb multiple network config added notifications
1395 if (!programmingScheduled.get()) {
Saurav Dase7f51012018-02-09 17:26:45 -08001396 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
Saurav Das7bcbe702017-06-13 15:35:54 -07001397 programmingScheduled.set(true);
1398 executorService.schedule(new ConfigChange(), PROGRAM_DELAY,
1399 TimeUnit.SECONDS);
Charles Chan4636be02015-10-07 14:21:45 -07001400 }
Charles Chan2199c302016-04-23 17:36:10 -07001401 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001402 }
1403
Charles Chand6832882015-10-05 17:50:33 -07001404 @Override
1405 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001406 // TODO move this part to NetworkConfigEventHandler
1407 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1408 switch (event.type()) {
1409 case CONFIG_ADDED:
Charles Chan278ce832017-06-26 15:25:09 -07001410 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan5270ed02016-01-30 23:22:37 -08001411 configureNetwork();
1412 break;
1413 case CONFIG_UPDATED:
Charles Chan278ce832017-06-26 15:25:09 -07001414 log.info("Segment Routing Config updated for {}", event.subject());
Charles Chan27fe1a52017-10-20 16:06:55 -07001415 createOrUpdateDeviceConfiguration();
Charles Chan278ce832017-06-26 15:25:09 -07001416 // TODO support dynamic configuration
1417 break;
1418 default:
1419 break;
1420 }
1421 } else if (event.configClass().equals(InterfaceConfig.class)) {
1422 switch (event.type()) {
1423 case CONFIG_ADDED:
1424 log.info("Interface Config added for {}", event.subject());
1425 configureNetwork();
1426 break;
1427 case CONFIG_UPDATED:
1428 log.info("Interface Config updated for {}", event.subject());
Charles Chan27fe1a52017-10-20 16:06:55 -07001429 createOrUpdateDeviceConfiguration();
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001430
1431 // Following code will be uncommented when [CORD-634] is fully implemented.
1432 // [CORD-634] Add dynamic config support for interfaces
1433 updateInterface((InterfaceConfig) event.config().get(),
1434 (InterfaceConfig) event.prevConfig().get());
Charles Chan5270ed02016-01-30 23:22:37 -08001435 // TODO support dynamic configuration
1436 break;
1437 default:
1438 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001439 }
Charles Chan5270ed02016-01-30 23:22:37 -08001440 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001441 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001442 switch (event.type()) {
1443 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001444 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001445 break;
1446 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001447 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001448 break;
1449 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001450 appCfgHandler.processAppConfigRemoved(event);
1451 break;
1452 default:
1453 break;
1454 }
Saurav Dase7f51012018-02-09 17:26:45 -08001455 log.info("App config event .. configuring network");
Charles Chan03a73e02016-10-24 14:52:01 -07001456 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001457 } else if (event.configClass().equals(XConnectConfig.class)) {
1458 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1459 switch (event.type()) {
1460 case CONFIG_ADDED:
1461 xConnectHandler.processXConnectConfigAdded(event);
1462 break;
1463 case CONFIG_UPDATED:
1464 xConnectHandler.processXConnectConfigUpdated(event);
1465 break;
1466 case CONFIG_REMOVED:
1467 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001468 break;
1469 default:
1470 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001471 }
Charles Chand6832882015-10-05 17:50:33 -07001472 }
1473 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001474
Charles Chan50443e82018-01-03 16:26:32 -08001475 @Override
1476 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase7f51012018-02-09 17:26:45 -08001477 if (event.type() == CONFIG_REGISTERED ||
1478 event.type() == CONFIG_UNREGISTERED) {
1479 log.debug("Ignore event {} due to type mismatch", event);
1480 return false;
Charles Chan50443e82018-01-03 16:26:32 -08001481 }
Saurav Dase7f51012018-02-09 17:26:45 -08001482
1483 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1484 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1485 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscd339592018-02-23 14:18:00 -08001486 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase7f51012018-02-09 17:26:45 -08001487 log.debug("Ignore event {} due to class mismatch", event);
1488 return false;
1489 }
1490
1491 return true;
Charles Chan50443e82018-01-03 16:26:32 -08001492 }
1493
Saurav Das7bcbe702017-06-13 15:35:54 -07001494 private final class ConfigChange implements Runnable {
1495 @Override
1496 public void run() {
1497 programmingScheduled.set(false);
Saurav Dase7f51012018-02-09 17:26:45 -08001498 log.info("Reacting to config changes after buffer delay");
Saurav Das7bcbe702017-06-13 15:35:54 -07001499 for (Device device : deviceService.getDevices()) {
1500 processDeviceAdded(device);
1501 }
1502 defaultRoutingHandler.startPopulationProcess();
1503 }
1504 }
Charles Chand6832882015-10-05 17:50:33 -07001505 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001506
1507 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001508 @Override
1509 public void event(HostEvent event) {
Charles Chan68aa62d2015-11-09 16:37:23 -08001510 switch (event.type()) {
1511 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001512 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001513 break;
1514 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001515 hostHandler.processHostMovedEvent(event);
Charles Chan2fde6d42017-08-23 14:46:43 -07001516 routeHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001517 break;
1518 case HOST_REMOVED:
Charles Chanf9a52702017-06-16 15:19:24 -07001519 hostHandler.processHostRemovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001520 break;
1521 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001522 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001523 break;
1524 default:
1525 log.warn("Unsupported host event type: {}", event.type());
1526 break;
1527 }
1528 }
1529 }
1530
Charles Chand55e84d2016-03-30 17:54:24 -07001531 private class InternalMcastListener implements McastListener {
1532 @Override
1533 public void event(McastEvent event) {
1534 switch (event.type()) {
1535 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001536 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001537 break;
Pier Luigi57d41792018-02-26 12:31:38 +01001538 case SOURCE_UPDATED:
1539 mcastHandler.processSourceUpdated(event);
1540 break;
Charles Chand55e84d2016-03-30 17:54:24 -07001541 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001542 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001543 break;
1544 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001545 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001546 break;
Charles Chand55e84d2016-03-30 17:54:24 -07001547 case ROUTE_REMOVED:
Pier Luigi9930da52018-02-02 16:19:11 +01001548 mcastHandler.processRouteRemoved(event);
1549 break;
1550 case ROUTE_ADDED:
Charles Chand55e84d2016-03-30 17:54:24 -07001551 default:
1552 break;
1553 }
1554 }
1555 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001556
Charles Chan03a73e02016-10-24 14:52:01 -07001557 private class InternalRouteEventListener implements RouteListener {
1558 @Override
1559 public void event(RouteEvent event) {
1560 switch (event.type()) {
1561 case ROUTE_ADDED:
1562 routeHandler.processRouteAdded(event);
1563 break;
1564 case ROUTE_UPDATED:
1565 routeHandler.processRouteUpdated(event);
1566 break;
1567 case ROUTE_REMOVED:
1568 routeHandler.processRouteRemoved(event);
1569 break;
Charles Chan2fde6d42017-08-23 14:46:43 -07001570 case ALTERNATIVE_ROUTES_CHANGED:
1571 routeHandler.processAlternativeRoutesChanged(event);
1572 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001573 default:
1574 break;
1575 }
1576 }
1577 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001578
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001579 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1580 try {
1581 Set<Interface> intfs = conf.getInterfaces();
1582 Set<Interface> prevIntfs = prevConf.getInterfaces();
1583
1584 // Now we only handle one interface config at each port.
1585 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1586 log.warn("Interface update aborted - one at a time is allowed, " +
1587 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1588 return;
1589 }
1590
1591 Interface intf = intfs.stream().findFirst().get();
1592 Interface prevIntf = prevIntfs.stream().findFirst().get();
1593
1594 DeviceId deviceId = intf.connectPoint().deviceId();
1595 PortNumber portNum = intf.connectPoint().port();
1596
1597 if (!mastershipService.isLocalMaster(deviceId)) {
1598 log.debug("CONFIG_UPDATED event for interfaces should be " +
1599 "handled by master node for device {}", deviceId);
1600 return;
1601 }
1602
1603 removeSubnetConfig(prevIntf.connectPoint(),
1604 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1605 new HashSet<>(intf.ipAddressesList())));
1606
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001607 if (!prevIntf.vlanNative().equals(VlanId.NONE)
1608 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
1609 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1610 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
1611 // Update filtering objective and L2IG group bucket
1612 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
1613 } else {
1614 // RemoveVlanNative
1615 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1616 }
1617 }
1618
1619 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
1620 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1621 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1622 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
1623 // Update filtering objective and L2IG group bucket
1624 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
1625 } else {
1626 // RemoveVlanUntagged
1627 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1628 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001629 }
1630
1631 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1632 // RemoveVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001633 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
1634 .filter(i -> !intf.vlanUntagged().equals(i))
1635 .filter(i -> !intf.vlanNative().equals(i))
1636 .forEach(vlanId -> updateVlanConfigInternal(
1637 deviceId, portNum, vlanId, false, false));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001638 }
1639
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001640 if (!intf.vlanNative().equals(VlanId.NONE)
1641 && !prevIntf.vlanNative().equals(intf.vlanNative())
1642 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1643 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
1644 // Update filtering objective and L2IG group bucket
1645 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
1646 } else {
1647 // AddVlanNative
1648 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1649 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001650 }
1651
1652 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1653 // AddVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001654 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
1655 .filter(i -> !prevIntf.vlanUntagged().equals(i))
1656 .filter(i -> !prevIntf.vlanNative().equals(i))
1657 .forEach(vlanId -> updateVlanConfigInternal(
1658 deviceId, portNum, vlanId, false, true)
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001659 );
1660 }
1661
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001662 if (!intf.vlanUntagged().equals(VlanId.NONE)
1663 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1664 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
1665 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
1666 // Update filtering objective and L2IG group bucket
1667 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
1668 } else {
1669 // AddVlanUntagged
1670 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1671 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001672 }
1673 addSubnetConfig(prevIntf.connectPoint(),
1674 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1675 new HashSet<>(prevIntf.ipAddressesList())));
1676 } catch (ConfigException e) {
1677 log.error("Error in configuration");
1678 }
1679 }
1680
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001681 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
1682 VlanId vlanId, boolean pushVlan) {
1683 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1684 if (grpHandler == null) {
1685 log.warn("Failed to retrieve group handler for device {}", deviceId);
1686 return;
1687 }
1688
1689 // Update filtering objective for a single port
1690 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
1691 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
1692
1693 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
1694 // Update L2IG bucket of the port
1695 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
1696 } else {
1697 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1698 }
1699 }
1700
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001701 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1702 VlanId vlanId, boolean pushVlan, boolean install) {
1703 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1704 if (grpHandler == null) {
1705 log.warn("Failed to retrieve group handler for device {}", deviceId);
1706 return;
1707 }
1708
1709 // Update filtering objective for a single port
1710 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1711
1712 // Update filtering objective for multicast ingress port
1713 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1714
1715 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1716
1717 if (nextId != -1 && !install) {
1718 // Update next objective for a single port as an output port
1719 // Remove a single port from L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001720 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001721 // Remove L2 Bridging rule and L3 Unicast rule to the host
1722 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1723 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1724 // only if there is no port configured on that VLAN ID
1725 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1726 // Remove broadcast forwarding rule for the VLAN
1727 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1728 // Remove L2FG for VLAN
1729 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1730 } else {
1731 // Remove L2IG of the port
1732 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1733 }
1734 } else if (install) {
1735 if (nextId != -1) {
1736 // Add a single port to L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001737 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001738 } else {
1739 // Create L2FG for VLAN
1740 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1741 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1742 }
1743 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1744 } else {
1745 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1746 }
1747 }
1748
1749 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1750 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1751 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1752
1753 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1754 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1755 .filter(intf -> !intf.connectPoint().equals(cp))
1756 .flatMap(intf -> intf.ipAddressesList().stream())
1757 .collect(Collectors.toSet());
1758 // 1. Partial subnet population
1759 // Remove routing rules for removed subnet from previous configuration,
1760 // which does not also exist in other interfaces in the same device
1761 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1762 .map(InterfaceIpAddress::subnetAddress)
1763 .collect(Collectors.toSet());
1764
1765 defaultRoutingHandler.revokeSubnet(
1766 ipPrefixSet.stream()
1767 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1768 .collect(Collectors.toSet()));
1769
1770 // 2. Interface IP punts
1771 // Remove IP punts for old Intf address
1772 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1773 .map(InterfaceIpAddress::ipAddress)
1774 .collect(Collectors.toSet());
1775 ipAddressSet.stream()
1776 .map(InterfaceIpAddress::ipAddress)
1777 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1778 .forEach(interfaceIpAddress ->
1779 routingRulePopulator.revokeSingleIpPunts(
1780 cp.deviceId(), interfaceIpAddress));
1781
1782 // 3. Host unicast routing rule
1783 // Remove unicast routing rule
1784 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1785 }
1786
1787 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1788 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1789 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1790
1791 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1792 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1793 .filter(intf -> !intf.connectPoint().equals(cp))
1794 .flatMap(intf -> intf.ipAddressesList().stream())
1795 .collect(Collectors.toSet());
1796 // 1. Partial subnet population
1797 // Add routing rules for newly added subnet, which does not also exist in
1798 // other interfaces in the same device
1799 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1800 .map(InterfaceIpAddress::subnetAddress)
1801 .collect(Collectors.toSet());
1802
1803 defaultRoutingHandler.populateSubnet(
1804 Collections.singleton(cp),
1805 ipPrefixSet.stream()
1806 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1807 .collect(Collectors.toSet()));
1808
1809 // 2. Interface IP punts
1810 // Add IP punts for new Intf address
1811 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1812 .map(InterfaceIpAddress::ipAddress)
1813 .collect(Collectors.toSet());
1814 ipAddressSet.stream()
1815 .map(InterfaceIpAddress::ipAddress)
1816 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1817 .forEach(interfaceIpAddress ->
1818 routingRulePopulator.populateSingleIpPunts(
1819 cp.deviceId(), interfaceIpAddress));
1820
1821 // 3. Host unicast routing rule
1822 // Add unicast routing rule
1823 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1824 }
sanghob35a6192015-04-01 13:05:26 -07001825}