blob: 5dc76bce040120daff686b0e68c933a8f6e92550 [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;
Saurav Das201762d2018-04-21 17:19:48 -070041import org.onosproject.cluster.ClusterEvent;
42import org.onosproject.cluster.ClusterEventListener;
Pier Luigieba73a02018-01-16 10:47:50 +010043import org.onosproject.cluster.ClusterService;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070044import org.onosproject.cluster.LeadershipService;
Pier96f63cb2018-04-17 16:29:56 +020045import org.onosproject.cluster.NodeId;
sanghob35a6192015-04-01 13:05:26 -070046import org.onosproject.core.ApplicationId;
47import org.onosproject.core.CoreService;
48import org.onosproject.event.Event;
Charles Chan50bb6ef2018-04-18 18:41:05 -070049import org.onosproject.mastership.MastershipEvent;
50import org.onosproject.mastership.MastershipListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070051import org.onosproject.mastership.MastershipService;
Pier3ee24552018-03-14 16:47:32 -070052import org.onosproject.mcast.api.McastEvent;
53import org.onosproject.mcast.api.McastListener;
54import org.onosproject.mcast.api.MulticastRouteService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080055import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070056import org.onosproject.net.Device;
57import org.onosproject.net.DeviceId;
Charles Chan9640c812017-08-23 13:55:39 -070058import org.onosproject.net.Host;
59import org.onosproject.net.HostId;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070060import org.onosproject.net.Link;
61import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080062import org.onosproject.net.PortNumber;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070063import org.onosproject.net.config.ConfigException;
Charles Chand6832882015-10-05 17:50:33 -070064import org.onosproject.net.config.ConfigFactory;
65import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070066import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070067import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070068import org.onosproject.net.config.basics.InterfaceConfig;
69import org.onosproject.net.config.basics.McastConfig;
Charles Chand6832882015-10-05 17:50:33 -070070import org.onosproject.net.config.basics.SubjectFactories;
Saurav Das45f48152018-01-18 12:07:33 -080071import org.onosproject.net.device.DeviceAdminService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070072import org.onosproject.net.device.DeviceEvent;
73import org.onosproject.net.device.DeviceListener;
74import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080075import org.onosproject.net.flow.TrafficSelector;
76import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070077import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan68aa62d2015-11-09 16:37:23 -080078import org.onosproject.net.host.HostEvent;
79import org.onosproject.net.host.HostListener;
Charles Chanff79dd92018-06-01 16:33:48 -070080import org.onosproject.net.host.HostProbingService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080081import org.onosproject.net.host.HostService;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070082import org.onosproject.net.host.InterfaceIpAddress;
Pier96f63cb2018-04-17 16:29:56 +020083import org.onosproject.net.intent.WorkPartitionService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070084import org.onosproject.net.intf.Interface;
85import org.onosproject.net.intf.InterfaceService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080086import org.onosproject.net.link.LinkEvent;
87import org.onosproject.net.link.LinkListener;
88import org.onosproject.net.link.LinkService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070089import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080090import org.onosproject.net.packet.InboundPacket;
91import org.onosproject.net.packet.PacketContext;
92import org.onosproject.net.packet.PacketProcessor;
93import org.onosproject.net.packet.PacketService;
Pier Luigi83f919b2018-02-15 16:33:08 +010094import org.onosproject.net.topology.TopologyEvent;
95import org.onosproject.net.topology.TopologyListener;
Charles Chand55e84d2016-03-30 17:54:24 -070096import org.onosproject.net.topology.TopologyService;
Charles Chan9640c812017-08-23 13:55:39 -070097import org.onosproject.routeservice.ResolvedRoute;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070098import org.onosproject.routeservice.RouteEvent;
99import org.onosproject.routeservice.RouteListener;
100import org.onosproject.routeservice.RouteService;
Charles Chand55e84d2016-03-30 17:54:24 -0700101import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
102import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventref34966c2016-11-07 16:21:04 -0800103import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700104import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -0700105import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -0700106import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700107import org.onosproject.segmentrouting.grouphandler.DestinationSet;
108import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Pier Luigi96fe0772018-02-28 12:10:50 +0100109import org.onosproject.segmentrouting.mcast.McastHandler;
110import org.onosproject.segmentrouting.mcast.McastRole;
Pier3e793752018-04-19 16:47:06 +0200111import org.onosproject.segmentrouting.mcast.McastRoleStoreKey;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700112import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700113import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800114import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700115import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800116
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800117import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700118import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800119import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800120import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
121
Saurav Das7bcbe702017-06-13 15:35:54 -0700122import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700123import org.onosproject.segmentrouting.storekey.DummyVlanIdStoreKey;
Pier3e793752018-04-19 16:47:06 +0200124import org.onosproject.segmentrouting.mcast.McastStoreKey;
Charles Chand2990362016-04-18 13:44:03 -0700125import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -0800126import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -0700127import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Charles Chanc7b3c452018-06-19 20:31:57 -0700128import org.onosproject.segmentrouting.xconnect.api.XconnectService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700129import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700130import org.onosproject.store.service.EventuallyConsistentMap;
131import org.onosproject.store.service.EventuallyConsistentMapBuilder;
132import org.onosproject.store.service.StorageService;
133import org.onosproject.store.service.WallClockTimestamp;
Charles Chan47933752017-11-30 15:37:50 -0800134import org.osgi.service.component.ComponentContext;
sanghob35a6192015-04-01 13:05:26 -0700135import org.slf4j.Logger;
136import org.slf4j.LoggerFactory;
137
Saurav Das201762d2018-04-21 17:19:48 -0700138import java.time.Instant;
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700139import java.util.ArrayList;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800140import java.util.Collections;
Charles Chan47933752017-11-30 15:37:50 -0800141import java.util.Dictionary;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800142import java.util.HashSet;
143import java.util.List;
144import java.util.Map;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800145import java.util.Optional;
146import java.util.Set;
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200147import java.util.concurrent.CompletableFuture;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800148import java.util.concurrent.ConcurrentHashMap;
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200149import java.util.concurrent.CopyOnWriteArrayList;
Charles Chan07f15f22018-05-08 21:35:50 -0700150import java.util.concurrent.ExecutorService;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800151import java.util.concurrent.Executors;
152import java.util.concurrent.ScheduledExecutorService;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800153import java.util.concurrent.TimeUnit;
154import java.util.concurrent.atomic.AtomicBoolean;
155import java.util.stream.Collectors;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700156import java.util.stream.IntStream;
sanghob35a6192015-04-01 13:05:26 -0700157
Charles Chan3e783d02016-02-26 22:19:52 -0800158import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800159import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700160import static org.onlab.util.Tools.groupedThreads;
Saurav Dase7f51012018-02-09 17:26:45 -0800161import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
162import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Charles Chan3e783d02016-02-26 22:19:52 -0800163
Charles Chane849c192016-01-11 18:28:54 -0800164/**
165 * Segment routing manager.
166 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700167@Service
168@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700169public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700170
Charles Chan2c15aca2016-11-09 20:51:44 -0800171 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan2fde6d42017-08-23 14:46:43 -0700172 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sanghob35a6192015-04-01 13:05:26 -0700173
174 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700175 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700176
177 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800178 private NeighbourResolutionService neighbourResolutionService;
179
180 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100181 public CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700182
183 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700184 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700185
186 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700187 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700188
189 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chanff79dd92018-06-01 16:33:48 -0700190 HostProbingService probingService;
Charles Chan47933752017-11-30 15:37:50 -0800191
192 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100193 public DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700194
195 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Saurav Das45f48152018-01-18 12:07:33 -0800196 DeviceAdminService deviceAdminService;
197
198 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800199 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700200
201 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100202 public LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700203
Charles Chan5270ed02016-01-30 23:22:37 -0800204 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800205 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700206
207 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800208 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700209
210 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100211 public MulticastRouteService multicastRouteService;
Charles Chan03a73e02016-10-24 14:52:01 -0700212
213 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chanf237e1b2018-01-09 13:45:07 -0800214 public TopologyService topologyService;
Charles Chan03a73e02016-10-24 14:52:01 -0700215
216 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700217 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800218
219 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800220 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800221
Saurav Das80980c72016-03-23 11:22:49 -0700222 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800223 public InterfaceService interfaceService;
224
Pier Luigieba73a02018-01-16 10:47:50 +0100225 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
226 public ClusterService clusterService;
227
228 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier96f63cb2018-04-17 16:29:56 +0200229 public WorkPartitionService workPartitionService;
Pier Luigieba73a02018-01-16 10:47:50 +0100230
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700231 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
232 public LeadershipService leadershipService;
233
Charles Chanc7b3c452018-06-19 20:31:57 -0700234 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY)
235 public XconnectService xconnectService;
236
Charles Chan47933752017-11-30 15:37:50 -0800237 @Property(name = "activeProbing", boolValue = true,
238 label = "Enable active probing to discover dual-homed hosts.")
239 boolean activeProbing = true;
240
Saurav Das9a554292018-04-27 18:42:30 -0700241 @Property(name = "singleHomedDown", boolValue = false,
242 label = "Enable administratively taking down single-homed hosts "
243 + "when all uplinks are gone")
244 boolean singleHomedDown = false;
245
Andreas Pantelopoulosb2518012018-06-18 12:50:35 -0700246 @Property(name = "respondToUnknownHosts", boolValue = true,
247 label = "Enable this to respond to ARP/NDP requests from unknown hosts.")
248 boolean respondToUnknownHosts = true;
249
Charles Chan57465d32018-07-19 14:55:31 -0700250 @Property(name = "handleDoubleTaggedHosts", boolValue = false,
251 label = "Program flows and groups for double tagged hosts")
252 boolean handleDoubleTaggedHosts = false;
253
Charles Chan03a73e02016-10-24 14:52:01 -0700254 ArpHandler arpHandler = null;
255 IcmpHandler icmpHandler = null;
256 IpHandler ipHandler = null;
257 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700258 ApplicationId appId;
259 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700260
Charles Chan03a73e02016-10-24 14:52:01 -0700261 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700262 private TunnelHandler tunnelHandler = null;
263 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700264 private InternalPacketProcessor processor = null;
265 private InternalLinkListener linkListener = null;
266 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700267 private AppConfigHandler appCfgHandler = null;
Pier Luigi96fe0772018-02-28 12:10:50 +0100268 public XConnectHandler xConnectHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800269 McastHandler mcastHandler = null;
270 HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800271 private RouteHandler routeHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800272 LinkHandler linkHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800273 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800274 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigi83f919b2018-02-15 16:33:08 +0100275 private TopologyHandler topologyHandler = null;
Charles Chan5270ed02016-01-30 23:22:37 -0800276 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700277 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
278 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700279 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigi83f919b2018-02-15 16:33:08 +0100280 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chan50bb6ef2018-04-18 18:41:05 -0700281 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
Saurav Das201762d2018-04-21 17:19:48 -0700282 final InternalClusterListener clusterListener = new InternalClusterListener();
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200283 //Completable future for network configuration process to buffer config events handling during activation
284 private CompletableFuture<Boolean> networkConfigCompletion = null;
Charles Chan10b2fee2018-04-21 00:44:29 -0700285 private List<Event> queuedEvents = new CopyOnWriteArrayList<>();
sanghob35a6192015-04-01 13:05:26 -0700286
Charles Chan9b7217c2018-04-05 16:31:15 -0700287 // Handles device, link, topology and network config events
Charles Chan50bb6ef2018-04-18 18:41:05 -0700288 private ScheduledExecutorService mainEventExecutor;
sanghob35a6192015-04-01 13:05:26 -0700289
Charles Chan50bb6ef2018-04-18 18:41:05 -0700290 // Handles host, route and mcast events respectively
291 private ScheduledExecutorService hostEventExecutor;
292 private ScheduledExecutorService routeEventExecutor;
293 private ScheduledExecutorService mcastEventExecutor;
Charles Chan07f15f22018-05-08 21:35:50 -0700294 private ExecutorService packetExecutor;
Charles Chan9b7217c2018-04-05 16:31:15 -0700295
296 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chane849c192016-01-11 18:28:54 -0800297 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700298 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700299 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800300 */
Charles Chan47933752017-11-30 15:37:50 -0800301 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das7bcbe702017-06-13 15:35:54 -0700302 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800303 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700304 * Per device next objective ID store with (device id + vlanid) as key.
305 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800306 */
Charles Chan47933752017-11-30 15:37:50 -0800307 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800308 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800309 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700310 * Per device next objective ID store with (device id + port + treatment + meta) as key.
311 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chane849c192016-01-11 18:28:54 -0800312 */
Charles Chan47933752017-11-30 15:37:50 -0800313 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800314 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800315
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700316 /**
317 * Per port dummy VLAN ID store with (connect point + ip address) as key.
318 * Used to keep track on dummy VLAN ID allocation.
319 */
320 private EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId>
321 dummyVlanIdStore = null;
322
Saurav Das4ce45962015-11-24 23:21:05 -0800323 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
324 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700325
Saurav Das7bcbe702017-06-13 15:35:54 -0700326 private AtomicBoolean programmingScheduled = new AtomicBoolean();
327
Charles Chand55e84d2016-03-30 17:54:24 -0700328 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700329 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
330 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700331 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700332 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800333 public SegmentRoutingDeviceConfig createConfig() {
334 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700335 }
336 };
Pier Ventref34966c2016-11-07 16:21:04 -0800337
Charles Chand55e84d2016-03-30 17:54:24 -0700338 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700339 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
340 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700341 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800342 @Override
343 public SegmentRoutingAppConfig createConfig() {
344 return new SegmentRoutingAppConfig();
345 }
346 };
Pier Ventref34966c2016-11-07 16:21:04 -0800347
Charles Chanfc5c7802016-05-17 13:13:55 -0700348 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
349 new ConfigFactory<ApplicationId, XConnectConfig>(
350 SubjectFactories.APP_SUBJECT_FACTORY,
351 XConnectConfig.class, "xconnect") {
352 @Override
353 public XConnectConfig createConfig() {
354 return new XConnectConfig();
355 }
356 };
Pier Ventref34966c2016-11-07 16:21:04 -0800357
Charles Chand55e84d2016-03-30 17:54:24 -0700358 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700359 new ConfigFactory<ApplicationId, McastConfig>(
360 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700361 McastConfig.class, "multicast") {
362 @Override
363 public McastConfig createConfig() {
364 return new McastConfig();
365 }
366 };
367
Charles Chan116188d2016-02-18 14:22:42 -0800368 /**
369 * Segment Routing App ID.
370 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800371 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chane849c192016-01-11 18:28:54 -0800372 /**
373 * The default VLAN ID assigned to the interfaces without subnet config.
374 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800375 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Andreas Pantelopoulos192284c2018-05-01 14:56:05 -0700376 /**
377 * The Vlan id used to transport pseudowire traffic across the network.
378 */
379 public static final VlanId PSEUDOWIRE_VLAN = VlanId.vlanId((short) 4093);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700380
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700381 /**
382 * Minumum and maximum value of dummy VLAN ID to be allocated.
383 */
384 public static final int MIN_DUMMY_VLAN_ID = 2;
385 public static final int MAX_DUMMY_VLAN_ID = 4093;
386
Saurav Das9a554292018-04-27 18:42:30 -0700387 Instant lastEdgePortEvent = Instant.EPOCH;
388
sanghob35a6192015-04-01 13:05:26 -0700389 @Activate
Charles Chan47933752017-11-30 15:37:50 -0800390 protected void activate(ComponentContext context) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800391 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700392
Charles Chan50bb6ef2018-04-18 18:41:05 -0700393 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-main", "%d", log));
394 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-host", "%d", log));
395 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-route", "%d", log));
396 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-mcast", "%d", log));
Charles Chan07f15f22018-05-08 21:35:50 -0700397 packetExecutor = Executors.newSingleThreadExecutor(groupedThreads("sr-packet", "%d", log));
Charles Chan50bb6ef2018-04-18 18:41:05 -0700398
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700399 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700400 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700401 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700402 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700403 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700404 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700405 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700406 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700407 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700408
Charles Chan59cc16d2017-02-02 16:20:42 -0800409 log.debug("Creating EC map vlannextobjectivestore");
410 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
411 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
412 vlanNextObjStore = vlanNextObjMapBuilder
413 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700414 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700415 .withTimestampProvider((k, v) -> new WallClockTimestamp())
416 .build();
417
Saurav Das4ce45962015-11-24 23:21:05 -0800418 log.debug("Creating EC map subnetnextobjectivestore");
419 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
420 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
421 portNextObjStore = portNextObjMapBuilder
422 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700423 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800424 .withTimestampProvider((k, v) -> new WallClockTimestamp())
425 .build();
426
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700427 EventuallyConsistentMapBuilder<DummyVlanIdStoreKey, VlanId>
428 dummyVlanIdMapBuilder = storageService.eventuallyConsistentMapBuilder();
429 dummyVlanIdStore = dummyVlanIdMapBuilder
430 .withName("dummyvlanidstore")
431 .withSerializer(createSerializer())
432 .withTimestampProvider((k, v) -> new WallClockTimestamp())
433 .build();
434
sangho0b2b6d12015-05-20 22:16:38 -0700435 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
436 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700437 tunnelStore = tunnelMapBuilder
438 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700439 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700440 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700441 .build();
442
443 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
444 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700445 policyStore = policyMapBuilder
446 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700447 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700448 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700449 .build();
450
Saurav Das80980c72016-03-23 11:22:49 -0700451 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800452 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700453 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800454 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800455 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
456 "requestInterceptsEnabled", "false");
Charles Chand3baaba2017-08-08 15:13:37 -0700457 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800458 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700459 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800460 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800461 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
462 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800463 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
464 "forceUnprovision", "true");
Charles Chanef624ed2017-08-10 16:57:28 -0700465 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan73316522017-07-20 16:16:25 -0700466 "distributed", "true");
Charles Chan35a32322017-08-14 11:42:11 -0700467 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
468 "multihomingEnabled", "true");
Charles Chan807d87a2017-11-29 19:54:20 -0800469 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
470 "staleLinkAge", "15000");
471 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
472 "allowDuplicateIps", "false");
Charles Chan47933752017-11-30 15:37:50 -0800473 compCfgService.registerProperties(getClass());
474 modified(context);
Saurav Das80980c72016-03-23 11:22:49 -0700475
Charles Chanb8e10c82015-10-14 11:24:40 -0700476 processor = new InternalPacketProcessor();
477 linkListener = new InternalLinkListener();
478 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700479 appCfgHandler = new AppConfigHandler(this);
480 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700481 mcastHandler = new McastHandler(this);
482 hostHandler = new HostHandler(this);
Saurav Das45f48152018-01-18 12:07:33 -0800483 linkHandler = new LinkHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700484 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800485 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800486 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigi83f919b2018-02-15 16:33:08 +0100487 topologyHandler = new TopologyHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700488
Charles Chan3e783d02016-02-26 22:19:52 -0800489 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700490 cfgService.registerConfigFactory(deviceConfigFactory);
491 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700492 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700493 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase7f51012018-02-09 17:26:45 -0800494 log.info("Configuring network before adding listeners");
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200495
Charles Chan132393a2018-01-04 14:26:07 -0800496 cfgListener.configureNetwork();
497
Charles Chan5270ed02016-01-30 23:22:37 -0800498 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700499 packetService.addProcessor(processor, PacketProcessor.director(2));
500 linkService.addListener(linkListener);
501 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700502 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700503 routeService.addListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100504 topologyService.addListener(topologyListener);
Charles Chan50bb6ef2018-04-18 18:41:05 -0700505 mastershipService.addListener(mastershipListener);
Saurav Das201762d2018-04-21 17:19:48 -0700506 clusterService.addListener(clusterListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700507
Charles Chanb39777c2018-03-09 15:53:44 -0800508 linkHandler.init();
Andreas Pantelopoulos4a768c02018-01-11 07:53:48 -0800509 l2TunnelHandler.init();
510
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200511 networkConfigCompletion.whenComplete((value, ex) -> {
512 //setting to null for easier fall through
513 networkConfigCompletion = null;
514 //process all queued events
Charles Chan10b2fee2018-04-21 00:44:29 -0700515 queuedEvents.forEach(event -> {
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200516 mainEventExecutor.execute(new InternalEventHandler(event));
517 });
518 });
519
sanghob35a6192015-04-01 13:05:26 -0700520 log.info("Started");
521 }
522
Saurav Das45f48152018-01-18 12:07:33 -0800523 KryoNamespace.Builder createSerializer() {
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700524 return new KryoNamespace.Builder()
525 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700526 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800527 VlanNextObjectiveStoreKey.class,
528 DestinationSet.class,
Andreas Pantelopoulos67e09ae2018-05-29 13:11:14 -0700529 DestinationSet.DestinationSetType.class,
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800530 NextNeighbors.class,
531 Tunnel.class,
532 DefaultTunnel.class,
533 Policy.class,
534 TunnelPolicy.class,
535 Policy.Type.class,
536 PortNextObjectiveStoreKey.class,
537 XConnectStoreKey.class,
538 L2Tunnel.class,
539 L2TunnelPolicy.class,
540 DefaultL2Tunnel.class,
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700541 DefaultL2TunnelPolicy.class,
542 DummyVlanIdStoreKey.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700543 );
544 }
545
sanghob35a6192015-04-01 13:05:26 -0700546 @Deactivate
547 protected void deactivate() {
Charles Chan50bb6ef2018-04-18 18:41:05 -0700548 mainEventExecutor.shutdown();
549 hostEventExecutor.shutdown();
550 routeEventExecutor.shutdown();
551 mcastEventExecutor.shutdown();
Charles Chan07f15f22018-05-08 21:35:50 -0700552 packetExecutor.shutdown();
Charles Chan50bb6ef2018-04-18 18:41:05 -0700553
Ray Milkey749996b2018-05-11 09:59:19 -0700554 mainEventExecutor = null;
555 hostEventExecutor = null;
556 routeEventExecutor = null;
557 mcastEventExecutor = null;
558 packetExecutor = null;
559
Charles Chand6832882015-10-05 17:50:33 -0700560 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700561 cfgService.unregisterConfigFactory(deviceConfigFactory);
562 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700563 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700564 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan47933752017-11-30 15:37:50 -0800565 compCfgService.unregisterProperties(getClass(), false);
Charles Chand6832882015-10-05 17:50:33 -0700566
Charles Chanc7a8a682017-06-19 00:43:31 -0700567 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700568 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700569 linkService.removeListener(linkListener);
570 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700571 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700572 routeService.removeListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100573 topologyService.removeListener(topologyListener);
Charles Chan50bb6ef2018-04-18 18:41:05 -0700574 mastershipService.removeListener(mastershipListener);
Saurav Das201762d2018-04-21 17:19:48 -0700575 clusterService.removeListener(clusterListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700576
Charles Chan0aa674e2017-02-23 15:44:08 -0800577 neighbourResolutionService.unregisterNeighbourHandlers(appId);
578
sanghob35a6192015-04-01 13:05:26 -0700579 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700580 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700581 deviceListener = null;
Charles Chan6577f612018-04-19 13:10:01 -0700582 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chanb8e10c82015-10-14 11:24:40 -0700583 groupHandlerMap.clear();
Saurav Dasc6ff8f02018-04-23 18:42:12 -0700584 defaultRoutingHandler.shutdown();
Charles Chanb8e10c82015-10-14 11:24:40 -0700585
Saurav Das7bcbe702017-06-13 15:35:54 -0700586 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800587 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700588 portNextObjStore.destroy();
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700589 dummyVlanIdStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700590 tunnelStore.destroy();
591 policyStore.destroy();
Pier Luigib72201b2018-01-25 16:16:02 +0100592
593 mcastHandler.terminate();
sanghob35a6192015-04-01 13:05:26 -0700594 log.info("Stopped");
595 }
596
Charles Chan47933752017-11-30 15:37:50 -0800597 @Modified
598 private void modified(ComponentContext context) {
599 Dictionary<?, ?> properties = context.getProperties();
600 if (properties == null) {
601 return;
602 }
603
Andreas Pantelopoulosb2518012018-06-18 12:50:35 -0700604 String strActiveProbing = Tools.get(properties, "activeProbing");
605 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProbing);
Charles Chan47933752017-11-30 15:37:50 -0800606 if (expectActiveProbing != activeProbing) {
607 activeProbing = expectActiveProbing;
608 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
609 }
Saurav Das9a554292018-04-27 18:42:30 -0700610
611 String strSingleHomedDown = Tools.get(properties, "singleHomedDown");
612 boolean expectSingleHomedDown = Boolean.parseBoolean(strSingleHomedDown);
613 if (expectSingleHomedDown != singleHomedDown) {
614 singleHomedDown = expectSingleHomedDown;
615 log.info("{} downing of single homed hosts for lost uplinks",
616 singleHomedDown ? "Enabling" : "Disabling");
617 if (singleHomedDown && linkHandler != null) {
618 hostService.getHosts().forEach(host -> host.locations()
619 .forEach(loc -> {
620 if (interfaceService.isConfigured(loc)) {
621 linkHandler.checkUplinksForHost(loc);
622 }
623 }));
624 } else {
625 log.warn("Disabling singleHomedDown does not re-enable already "
626 + "downed ports for single-homed hosts");
627 }
628 }
Andreas Pantelopoulosb2518012018-06-18 12:50:35 -0700629
630 String strRespondToUnknownHosts = Tools.get(properties, "respondToUnknownHosts");
631 boolean expectRespondToUnknownHosts = Boolean.parseBoolean(strRespondToUnknownHosts);
632 if (expectRespondToUnknownHosts != respondToUnknownHosts) {
633 respondToUnknownHosts = expectRespondToUnknownHosts;
634 log.info("{} responding to ARPs/NDPs from unknown hosts", respondToUnknownHosts ? "Enabling" : "Disabling");
635 }
Charles Chan57465d32018-07-19 14:55:31 -0700636
637 String strHandleDoubleTaggedHosts = Tools.get(properties, "handleDoubleTaggedHosts");
638 boolean expectHandleDoubleTaggedHosts = Boolean.parseBoolean(strHandleDoubleTaggedHosts);
639 if (expectHandleDoubleTaggedHosts != handleDoubleTaggedHosts) {
640 handleDoubleTaggedHosts = expectHandleDoubleTaggedHosts;
641 log.info("{} programming for double tagged hosts ", handleDoubleTaggedHosts ? "Enabling" : "Disabling");
642
643 if (handleDoubleTaggedHosts) {
644 hostHandler.populateAllDoubleTaggedHost();
645 } else {
646 hostHandler.revokeAllDoubleTaggedHost();
647 }
648 }
Charles Chan47933752017-11-30 15:37:50 -0800649 }
650
sangho1e575652015-05-14 00:39:53 -0700651 @Override
652 public List<Tunnel> getTunnels() {
653 return tunnelHandler.getTunnels();
654 }
655
656 @Override
sangho71abe1b2015-06-29 14:58:47 -0700657 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
658 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700659 }
660
661 @Override
sangho71abe1b2015-06-29 14:58:47 -0700662 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700663 for (Policy policy: policyHandler.getPolicies()) {
664 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
665 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
666 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
667 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700668 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700669 }
670 }
671 }
sangho71abe1b2015-06-29 14:58:47 -0700672 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700673 }
674
675 @Override
sangho71abe1b2015-06-29 14:58:47 -0700676 public PolicyHandler.Result removePolicy(Policy policy) {
677 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700678 }
679
680 @Override
sangho71abe1b2015-06-29 14:58:47 -0700681 public PolicyHandler.Result createPolicy(Policy policy) {
682 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700683 }
684
685 @Override
686 public List<Policy> getPolicies() {
687 return policyHandler.getPolicies();
688 }
689
Saurav Das59232cf2016-04-27 18:35:50 -0700690 @Override
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700691 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
692 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700693 }
694
695 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800696 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700697 return l2TunnelHandler.getL2Tunnels();
698 }
699
700 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800701 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700702 return l2TunnelHandler.getL2Policies();
703 }
704
Saurav Das5a356042018-04-06 20:16:01 -0700705 @Override
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700706 @Deprecated
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700707 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
708
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700709 // get both added and pending pseudowires
710 List<L2TunnelDescription> pseudowires = new ArrayList<>();
711 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
712 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700713 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700714
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700715 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
716
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700717 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
718 L2TunnelHandler.Result res;
719 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
720 res = addPseudowire(pw);
721 if (res != L2TunnelHandler.Result.SUCCESS) {
722 log.error("Pseudowire with id {} can not be instantiated !", res);
723 retRes = res;
724 }
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700725 }
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700726
727 return retRes;
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700728 }
729
730 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800731 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700732 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700733 }
734
735 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800736 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700737 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700738 }
739
740 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700741 public void rerouteNetwork() {
742 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700743 }
744
Charles Chanc81c45b2016-10-20 17:02:44 -0700745 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800746 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
747 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800748 deviceConfiguration.getRouters().forEach(device ->
749 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700750 return deviceSubnetMap;
751 }
752
Saurav Dasc88d4662017-05-15 15:34:25 -0700753
754 @Override
755 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
756 if (defaultRoutingHandler != null) {
757 return defaultRoutingHandler.getCurrentEmcpSpgMap();
758 } else {
759 return null;
760 }
761 }
762
763 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700764 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
765 if (dsNextObjStore != null) {
766 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700767 } else {
768 return ImmutableMap.of();
769 }
770 }
771
Saurav Dasceccf242017-08-03 18:30:35 -0700772 @Override
773 public void verifyGroups(DeviceId id) {
774 DefaultGroupHandler gh = groupHandlerMap.get(id);
775 if (gh != null) {
776 gh.triggerBucketCorrector();
777 }
778 }
779
Saurav Dasc568c342018-01-25 09:49:01 -0800780 @Override
781 public ImmutableMap<Link, Boolean> getSeenLinks() {
782 return linkHandler.getSeenLinks();
783 }
784
785 @Override
786 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
787 return linkHandler.getDownedPorts();
788 }
789
Pier Luigib29144d2018-01-15 18:06:43 +0100790 @Override
791 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
792 return mcastHandler.getMcastNextIds(mcastIp);
793 }
794
795 @Override
Pier Luigi96fe0772018-02-28 12:10:50 +0100796 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigib29144d2018-01-15 18:06:43 +0100797 return mcastHandler.getMcastRoles(mcastIp);
798 }
799
800 @Override
Pier3e793752018-04-19 16:47:06 +0200801 public Map<McastRoleStoreKey, McastRole> getMcastRoles(IpAddress mcastIp, ConnectPoint sourcecp) {
802 return mcastHandler.getMcastRoles(mcastIp, sourcecp);
803 }
804
805 @Override
Pier Luigib29144d2018-01-15 18:06:43 +0100806 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
807 return mcastHandler.getMcastPaths(mcastIp);
808 }
809
Pier96f63cb2018-04-17 16:29:56 +0200810 @Override
Pierb1fe7382018-04-17 17:25:22 +0200811 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
812 ConnectPoint sourcecp) {
813 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
814 }
815
816 @Override
Pier96f63cb2018-04-17 16:29:56 +0200817 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
818 return mcastHandler.getMcastLeaders(mcastIp);
819 }
820
Charles Chan8bc75ee2018-04-17 18:56:53 -0700821 @Override
822 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
823 return defaultRoutingHandler == null ? ImmutableMap.of() :
824 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
825 }
826
827 @Override
828 public Map<DeviceId, Boolean> getShouldProgramCache() {
829 return defaultRoutingHandler == null ? ImmutableMap.of() :
830 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
831 }
832
Charles Chanc7b3c452018-06-19 20:31:57 -0700833 @Override
Ray Milkeye4afdb52017-04-05 09:42:04 -0700834 public ApplicationId appId() {
835 return appId;
836 }
837
838 /**
839 * Returns the device configuration.
840 *
841 * @return device configuration
842 */
843 public DeviceConfiguration deviceConfiguration() {
844 return deviceConfiguration;
845 }
846
847 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700848 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700849 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700850 *
851 * @return next objective ID store
852 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700853 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
854 dsNextObjStore() {
855 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700856 }
857
858 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700859 * Per device next objective ID store with (device id + vlanid) as key.
860 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700861 *
862 * @return vlan next object store
863 */
864 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
865 return vlanNextObjStore;
866 }
867
868 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700869 * Per device next objective ID store with (device id + port + treatment + meta) as key.
870 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700871 *
872 * @return port next object store.
873 */
874 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
875 return portNextObjStore;
876 }
877
878 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700879 * Per port dummy VLAN ID store with (connect point + ip address) as key.
880 * Used to keep track on dummy VLAN ID allocation.
881 *
882 * @return dummy vlan id store.
883 */
884 public EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId> dummyVlanIdStore() {
885 return dummyVlanIdStore;
886 }
887
888 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700889 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
890 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700891 *
892 * @return MPLS-ECMP value
893 */
894 public boolean getMplsEcmp() {
895 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
896 .getConfig(this.appId, SegmentRoutingAppConfig.class);
897 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
898 }
899
900 /**
sanghof9d0bf12015-05-19 11:57:42 -0700901 * Returns the tunnel object with the tunnel ID.
902 *
903 * @param tunnelId Tunnel ID
904 * @return Tunnel reference
905 */
sangho1e575652015-05-14 00:39:53 -0700906 public Tunnel getTunnel(String tunnelId) {
907 return tunnelHandler.getTunnel(tunnelId);
908 }
909
Charles Chanc7b3c452018-06-19 20:31:57 -0700910 @Override
Pier Luigi96fe0772018-02-28 12:10:50 +0100911 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chan971d7ba2018-05-01 11:50:20 -0700912 VlanId untaggedVlanId = interfaceService.getUntaggedVlanId(connectPoint);
913 VlanId nativeVlanId = interfaceService.getNativeVlanId(connectPoint);
Charles Chanf9a52702017-06-16 15:19:24 -0700914 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
915 }
916
Charles Chanc7b3c452018-06-19 20:31:57 -0700917 @Override
918 public Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
Charles Chan65238242017-06-22 18:03:14 -0700919 SegmentRoutingDeviceConfig deviceConfig =
920 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
921 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
922 }
Charles Chanc7b3c452018-06-19 20:31:57 -0700923
924 @Override
Saurav Das9a554292018-04-27 18:42:30 -0700925 public Optional<PortNumber> getPairLocalPort(DeviceId deviceId) {
Charles Chan65238242017-06-22 18:03:14 -0700926 SegmentRoutingDeviceConfig deviceConfig =
927 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
928 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
929 }
930
931 /**
Charles Chan9640c812017-08-23 13:55:39 -0700932 * Returns locations of given resolved route.
933 *
934 * @param resolvedRoute resolved route
935 * @return locations of nexthop. Might be empty if next hop is not found
936 */
937 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
938 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
939 return Optional.ofNullable(hostService.getHost(hostId))
940 .map(Host::locations).orElse(Sets.newHashSet())
941 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
942 }
943
944 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800945 * Returns vlan port map of given device.
946 *
947 * @param deviceId device id
948 * @return vlan-port multimap
949 */
950 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
951 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
952
953 interfaceService.getInterfaces().stream()
954 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
955 .forEach(intf -> {
956 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -0700957 intf.vlanTagged().forEach(vlanTagged ->
958 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
959 );
Charles Chan7ffd81f2017-02-08 15:52:08 -0800960 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
961 });
962 vlanPortMap.removeAll(VlanId.NONE);
963
964 return vlanPortMap;
965 }
966
967 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800968 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800969 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700970 *
971 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800972 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800973 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700974 */
Charles Chan65238242017-06-22 18:03:14 -0700975 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700976 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800977 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
978 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700979 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800980 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800981 + "device {} not found", deviceId);
982 return -1;
983 }
984 }
985
986 /**
987 * Returns the next objective ID for the given portNumber, given the treatment.
988 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700989 * would result in different objectives. If the next object does not exist,
990 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800991 *
992 * @param deviceId Device ID
993 * @param portNum port number on device for which NextObjective is queried
994 * @param treatment the actions to apply on the packets (should include outport)
995 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700996 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700997 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800998 */
999 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
1000 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -07001001 TrafficSelector meta,
1002 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -08001003 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1004 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -07001005 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -08001006 } else {
Charles Chane849c192016-01-11 18:28:54 -08001007 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
1008 + " not found", deviceId);
1009 return -1;
1010 }
1011 }
1012
Saurav Dasc88d4662017-05-15 15:34:25 -07001013 /**
1014 * Returns the group handler object for the specified device id.
1015 *
1016 * @param devId the device identifier
1017 * @return the groupHandler object for the device id, or null if not found
1018 */
Charles Chan65238242017-06-22 18:03:14 -07001019 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001020 return groupHandlerMap.get(devId);
1021 }
1022
1023 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001024 * Returns the default routing handler object.
1025 *
1026 * @return the default routing handler object
1027 */
1028 public DefaultRoutingHandler getRoutingHandler() {
1029 return defaultRoutingHandler;
1030 }
1031
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001032 /**
1033 * Returns new dummy VLAN ID.
1034 * Dummy VLAN ID should be unique in each connect point.
1035 *
1036 * @param cp connect point
1037 * @param ipAddress IP address
1038 * @return new dummy VLAN ID. Returns VlanId.NONE if no VLAN ID is available.
1039 */
1040 public synchronized VlanId allocateDummyVlanId(ConnectPoint cp, IpAddress ipAddress) {
1041 Set<VlanId> usedVlanId = Sets.union(getVlanPortMap(cp.deviceId()).keySet(),
1042 dummyVlanIdStore.entrySet().stream().filter(entry ->
1043 (entry.getKey()).connectPoint().equals(cp))
1044 .map(Map.Entry::getValue)
1045 .collect(Collectors.toSet()));
1046
1047 VlanId dummyVlanId = IntStream.range(MIN_DUMMY_VLAN_ID, MAX_DUMMY_VLAN_ID).mapToObj(
1048 i -> VlanId.vlanId((short) (i & 0xFFFF))
1049 ).filter(vlanId -> !usedVlanId.contains(vlanId)).findFirst().orElse(VlanId.NONE);
1050
1051 if (!dummyVlanId.equals(VlanId.NONE)) {
1052 this.dummyVlanIdStore.put(new DummyVlanIdStoreKey(cp, ipAddress), dummyVlanId);
1053 log.debug("Dummy VLAN ID {} is allocated to {}, {}", dummyVlanId, cp, ipAddress);
1054 } else {
1055 log.error("Failed to allocate dummy VLAN ID for {}, {}", cp, ipAddress);
1056 }
1057 return dummyVlanId;
1058 }
1059
1060
sanghob35a6192015-04-01 13:05:26 -07001061 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -07001062 @Override
1063 public void process(PacketContext context) {
Charles Chan07f15f22018-05-08 21:35:50 -07001064 packetExecutor.execute(() -> processPacketInternal(context));
1065 }
sanghob35a6192015-04-01 13:05:26 -07001066
Charles Chan07f15f22018-05-08 21:35:50 -07001067 private void processPacketInternal(PacketContext context) {
sanghob35a6192015-04-01 13:05:26 -07001068 if (context.isHandled()) {
1069 return;
1070 }
1071
1072 InboundPacket pkt = context.inPacket();
1073 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -08001074
1075 if (ethernet == null) {
1076 return;
1077 }
1078
Saurav Das7bcbe702017-06-13 15:35:54 -07001079 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1080 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001081 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001082 log.warn("Received unexpected ARP packet on {}",
1083 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -07001084 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -08001085 return;
sanghob35a6192015-04-01 13:05:26 -07001086 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -08001087 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1088 //ipHandler.addToPacketBuffer(ipv4Packet);
1089 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1090 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001091 } else {
Charles Chan50035632017-01-13 17:20:44 -08001092 // NOTE: We don't support IP learning at this moment so this
1093 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -08001094 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001095 }
Pier Ventre10bd8d12016-11-26 21:05:22 -08001096 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1097 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -08001098 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -08001099 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -08001100 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1101 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1102 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1103 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1104 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1105 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -07001106 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -07001107 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -08001108 }
1109 } else {
1110 // NOTE: We don't support IP learning at this moment so this
1111 // is not necessary. Also it causes duplication of DHCPv6 packets.
1112 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1113 }
sanghob35a6192015-04-01 13:05:26 -07001114 }
1115 }
1116 }
1117
sanghob35a6192015-04-01 13:05:26 -07001118 private class InternalEventHandler implements Runnable {
Charles Chan9b7217c2018-04-05 16:31:15 -07001119 private Event event;
1120
1121 InternalEventHandler(Event event) {
1122 this.event = event;
1123 }
1124
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001125 @Override
sanghob35a6192015-04-01 13:05:26 -07001126 public void run() {
Charles Chan9b7217c2018-04-05 16:31:15 -07001127 try {
1128 // TODO We should also change SR routing and PW to listen to TopologyEvents
1129 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1130 event.type() == LinkEvent.Type.LINK_UPDATED) {
1131 linkHandler.processLinkAdded((Link) event.subject());
1132 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1133 linkHandler.processLinkRemoved((Link) event.subject());
1134 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1135 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1136 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1137 DeviceId deviceId = ((Device) event.subject()).id();
1138 if (deviceService.isAvailable(deviceId)) {
1139 log.info("** DEVICE UP Processing device event {} "
1140 + "for available device {}",
1141 event.type(), ((Device) event.subject()).id());
1142 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001143 } else {
Charles Chan9b7217c2018-04-05 16:31:15 -07001144 log.info(" ** DEVICE DOWN Processing device event {}"
1145 + " for unavailable device {}",
1146 event.type(), ((Device) event.subject()).id());
1147 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001148 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001149 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1150 // typically these calls come when device is added first time
1151 // so port filtering rules are handled at the device_added event.
1152 // port added calls represent all ports on the device,
1153 // enabled or not.
1154 log.trace("** PORT ADDED {}/{} -> {}",
1155 ((DeviceEvent) event).subject().id(),
1156 ((DeviceEvent) event).port().number(),
1157 event.type());
1158 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1159 // these calls happen for every subsequent event
1160 // ports enabled, disabled, switch goes away, comes back
1161 log.info("** PORT UPDATED {}/{} -> {}",
1162 event.subject(),
1163 ((DeviceEvent) event).port(),
1164 event.type());
Saurav Das9a554292018-04-27 18:42:30 -07001165 processPortUpdatedInternal(((Device) event.subject()),
Charles Chan9b7217c2018-04-05 16:31:15 -07001166 ((DeviceEvent) event).port());
1167 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1168 // Process topology event, needed for all modules relying on
1169 // topology service for path computation
1170 TopologyEvent topologyEvent = (TopologyEvent) event;
1171 log.info("Processing topology event {}, topology age {}, reasons {}",
1172 event.type(), topologyEvent.subject().time(),
1173 topologyEvent.reasons().size());
1174 topologyHandler.processTopologyChange(topologyEvent.reasons());
1175 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1176 hostHandler.processHostAddedEvent((HostEvent) event);
1177 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1178 hostHandler.processHostMovedEvent((HostEvent) event);
1179 routeHandler.processHostMovedEvent((HostEvent) event);
1180 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1181 hostHandler.processHostRemovedEvent((HostEvent) event);
1182 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1183 hostHandler.processHostUpdatedEvent((HostEvent) event);
1184 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1185 routeHandler.processRouteAdded((RouteEvent) event);
1186 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1187 routeHandler.processRouteUpdated((RouteEvent) event);
1188 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1189 routeHandler.processRouteRemoved((RouteEvent) event);
1190 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1191 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1192 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1193 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1194 event.type() == McastEvent.Type.SINKS_ADDED ||
1195 event.type() == McastEvent.Type.SINKS_REMOVED ||
1196 event.type() == McastEvent.Type.ROUTE_ADDED ||
1197 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1198 mcastHandler.processMcastEvent((McastEvent) event);
1199 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1200 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1201 Class configClass = netcfgEvent.configClass();
1202 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1203 appCfgHandler.processAppConfigAdded(netcfgEvent);
1204 log.info("App config event .. configuring network");
1205 cfgListener.configureNetwork();
1206 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1207 log.info("Segment Routing Device Config added for {}", event.subject());
1208 cfgListener.configureNetwork();
1209 } else if (configClass.equals(XConnectConfig.class)) {
1210 xConnectHandler.processXConnectConfigAdded(netcfgEvent);
1211 } else if (configClass.equals(InterfaceConfig.class)) {
1212 log.info("Interface Config added for {}", event.subject());
1213 cfgListener.configureNetwork();
1214 } else {
1215 log.error("Unhandled config class: {}", configClass);
1216 }
1217 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1218 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1219 Class configClass = netcfgEvent.configClass();
1220 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1221 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1222 log.info("App config event .. configuring network");
1223 cfgListener.configureNetwork();
1224 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1225 log.info("Segment Routing Device Config updated for {}", event.subject());
1226 createOrUpdateDeviceConfiguration();
1227 } else if (configClass.equals(XConnectConfig.class)) {
1228 xConnectHandler.processXConnectConfigUpdated(netcfgEvent);
1229 } else if (configClass.equals(InterfaceConfig.class)) {
1230 log.info("Interface Config updated for {}", event.subject());
1231 createOrUpdateDeviceConfiguration();
1232 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1233 (InterfaceConfig) netcfgEvent.prevConfig().get());
1234 } else {
1235 log.error("Unhandled config class: {}", configClass);
1236 }
1237 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1238 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1239 Class configClass = netcfgEvent.configClass();
1240 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1241 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1242 log.info("App config event .. configuring network");
1243 cfgListener.configureNetwork();
1244 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1245 // TODO Handle sr device config removal
1246 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
1247 } else if (configClass.equals(XConnectConfig.class)) {
1248 xConnectHandler.processXConnectConfigRemoved(netcfgEvent);
1249 } else if (configClass.equals(InterfaceConfig.class)) {
1250 // TODO Handle interface removal
1251 log.info("InterfaceConfig removal is not handled in current implementation");
1252 } else {
1253 log.error("Unhandled config class: {}", configClass);
1254 }
Saurav Das201762d2018-04-21 17:19:48 -07001255 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1256 MastershipEvent me = (MastershipEvent) event;
1257 DeviceId deviceId = me.subject();
1258 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1259 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1260 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1261 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1262 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1263 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan9b7217c2018-04-05 16:31:15 -07001264 } else {
1265 log.warn("Unhandled event type: {}", event.type());
sanghob35a6192015-04-01 13:05:26 -07001266 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001267 } catch (Exception e) {
1268 log.error("SegmentRouting event handler thread thrown an exception: {}",
1269 e.getMessage(), e);
sanghob35a6192015-04-01 13:05:26 -07001270 }
sanghob35a6192015-04-01 13:05:26 -07001271 }
1272 }
1273
Saurav Das45f48152018-01-18 12:07:33 -08001274 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001275 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001276
1277 // NOTE: Punt ARP/NDP even when the device is not configured.
1278 // Host learning without network config is required for CORD config generator.
1279 routingRulePopulator.populateIpPunts(device.id());
1280 routingRulePopulator.populateArpNdpPunts(device.id());
1281
Charles Chan0b4e6182015-11-03 10:42:14 -08001282 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001283 log.warn("Device configuration unavailable. Device {} will be "
1284 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001285 return;
1286 }
Charles Chan2199c302016-04-23 17:36:10 -07001287 processDeviceAddedInternal(device.id());
1288 }
1289
1290 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001291 // Irrespective of whether the local is a MASTER or not for this device,
1292 // we need to create a SR-group-handler instance. This is because in a
1293 // multi-instance setup, any instance can initiate forwarding/next-objectives
1294 // for any switch (even if this instance is a SLAVE or not even connected
1295 // to the switch). To handle this, a default-group-handler instance is necessary
1296 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001297 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1298 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001299 DefaultGroupHandler groupHandler;
1300 try {
1301 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001302 createGroupHandler(deviceId,
1303 appId,
1304 deviceConfiguration,
1305 linkService,
1306 flowObjectiveService,
1307 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001308 } catch (DeviceConfigNotFoundException e) {
1309 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1310 return;
1311 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001312 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001313 deviceId);
1314 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001315 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001316
Charles Chan2199c302016-04-23 17:36:10 -07001317 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001318 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001319 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001320 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001321 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001322 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001323 }
Charles Chan5270ed02016-01-30 23:22:37 -08001324
Charles Chan03a73e02016-10-24 14:52:01 -07001325 appCfgHandler.init(deviceId);
Charles Chan94549652018-04-29 18:11:37 -07001326 hostEventExecutor.execute(() -> hostHandler.init(deviceId));
1327 routeEventExecutor.execute(() -> routeHandler.init(deviceId));
sanghob35a6192015-04-01 13:05:26 -07001328 }
1329
Saurav Das80980c72016-03-23 11:22:49 -07001330 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001331 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001332 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan47933752017-11-30 15:37:50 -08001333 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001334 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001335 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001336 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001337 portNextObjStore.entrySet().stream()
1338 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001339 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Das45f48152018-01-18 12:07:33 -08001340 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001341
Saurav Dasceccf242017-08-03 18:30:35 -07001342 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1343 if (gh != null) {
1344 gh.shutdown();
1345 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001346 // Note that a switch going down is associated with all of its links
1347 // going down as well, but it is treated as a single switch down event
Saurav Das5a356042018-04-06 20:16:01 -07001348 // while the link-downs are ignored. We cannot rely on the ordering of
1349 // events - i.e we cannot expect all link-downs to come before the
1350 // switch down - so we purge all seen-links for the switch before
1351 // handling route-path changes for the switch-down
Saurav Dasc88d4662017-05-15 15:34:25 -07001352 defaultRoutingHandler
Saurav Das604ab3a2018-03-18 21:28:15 -07001353 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Dasc568c342018-01-25 09:49:01 -08001354 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001355 xConnectHandler.removeDevice(device.id());
Saurav Dasa4020382018-02-14 14:14:54 -08001356
1357 // Cleanup all internal groupHandler stores for this device. Should be
1358 // done after all rerouting or rehashing has been completed
1359 groupHandlerMap.entrySet()
1360 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Das80980c72016-03-23 11:22:49 -07001361 }
1362
Saurav Dasc6ff8f02018-04-23 18:42:12 -07001363 /**
1364 * Purge the destinationSet nextObjective store of entries with this device
1365 * as key. Erases app-level knowledge of hashed groups in this device.
1366 *
1367 * @param devId the device identifier
1368 */
Saurav Das201762d2018-04-21 17:19:48 -07001369 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Dasc6ff8f02018-04-23 18:42:12 -07001370 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Das201762d2018-04-21 17:19:48 -07001371 dsNextObjStore.entrySet().stream()
1372 .filter(entry -> entry.getKey().deviceId().equals(devId))
1373 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1374 }
1375
Saurav Das9a554292018-04-27 18:42:30 -07001376 private void processPortUpdatedInternal(Device device, Port port) {
Saurav Das1a129a02016-11-18 15:21:57 -08001377 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1378 log.warn("Device configuration uploading. Not handling port event for"
1379 + "dev: {} port: {}", device.id(), port.number());
1380 return;
1381 }
Saurav Das018605f2017-02-18 14:05:44 -08001382
Saurav Das9a554292018-04-27 18:42:30 -07001383 if (interfaceService.isConfigured(new ConnectPoint(device.id(), port.number()))) {
1384 lastEdgePortEvent = Instant.now();
1385 }
1386
Saurav Das018605f2017-02-18 14:05:44 -08001387 if (!mastershipService.isLocalMaster(device.id())) {
1388 log.debug("Not master for dev:{} .. not handling port updated event"
1389 + "for port {}", device.id(), port.number());
1390 return;
1391 }
Saurav Das9a554292018-04-27 18:42:30 -07001392 processPortUpdated(device.id(), port);
1393 }
Saurav Das018605f2017-02-18 14:05:44 -08001394
Saurav Das9a554292018-04-27 18:42:30 -07001395 /**
1396 * Adds or remove filtering rules for the given switchport. If switchport is
1397 * an edge facing port, additionally handles host probing and broadcast
1398 * rules. Must be called by local master of device.
1399 *
1400 * @param deviceId the device identifier
1401 * @param port the port to update
1402 */
1403 void processPortUpdated(DeviceId deviceId, Port port) {
Saurav Das018605f2017-02-18 14:05:44 -08001404 // first we handle filtering rules associated with the port
1405 if (port.isEnabled()) {
1406 log.info("Switchport {}/{} enabled..programming filters",
Saurav Das9a554292018-04-27 18:42:30 -07001407 deviceId, port.number());
1408 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001409 } else {
1410 log.info("Switchport {}/{} disabled..removing filters",
Saurav Das9a554292018-04-27 18:42:30 -07001411 deviceId, port.number());
1412 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001413 }
Saurav Das1a129a02016-11-18 15:21:57 -08001414
1415 // portUpdated calls are for ports that have gone down or up. For switch
1416 // to switch ports, link-events should take care of any re-routing or
1417 // group editing necessary for port up/down. Here we only process edge ports
1418 // that are already configured.
Saurav Das9a554292018-04-27 18:42:30 -07001419 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
Charles Chan971d7ba2018-05-01 11:50:20 -07001420 VlanId untaggedVlan = interfaceService.getUntaggedVlanId(cp);
1421 VlanId nativeVlan = interfaceService.getNativeVlanId(cp);
1422 Set<VlanId> taggedVlans = interfaceService.getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001423
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001424 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001425 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das9a554292018-04-27 18:42:30 -07001426 + "dev/port: {}/{}", deviceId, port.number());
Saurav Das1a129a02016-11-18 15:21:57 -08001427 return;
1428 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001429 if (untaggedVlan != null) {
Saurav Das9a554292018-04-27 18:42:30 -07001430 processEdgePort(deviceId, port, untaggedVlan, true);
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001431 }
1432 if (nativeVlan != null) {
Saurav Das9a554292018-04-27 18:42:30 -07001433 processEdgePort(deviceId, port, nativeVlan, true);
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001434 }
1435 if (!taggedVlans.isEmpty()) {
Saurav Das9a554292018-04-27 18:42:30 -07001436 taggedVlans.forEach(tag -> processEdgePort(deviceId, port, tag, false));
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001437 }
Saurav Das1a129a02016-11-18 15:21:57 -08001438 }
1439
Saurav Das9a554292018-04-27 18:42:30 -07001440 private void processEdgePort(DeviceId deviceId, Port port, VlanId vlanId,
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001441 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001442 boolean portUp = port.isEnabled();
1443 if (portUp) {
Saurav Das9a554292018-04-27 18:42:30 -07001444 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", deviceId,
Charles Chan59cc16d2017-02-02 16:20:42 -08001445 port.number(), vlanId);
Charles Chan94549652018-04-29 18:11:37 -07001446 hostEventExecutor.execute(() -> hostHandler.processPortUp(new ConnectPoint(deviceId, port.number())));
Saurav Das1a129a02016-11-18 15:21:57 -08001447 } else {
Saurav Das9a554292018-04-27 18:42:30 -07001448 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", deviceId,
Charles Chan59cc16d2017-02-02 16:20:42 -08001449 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001450 }
1451
Saurav Das9a554292018-04-27 18:42:30 -07001452 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001453 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001454 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001455 } else {
1456 log.warn("Group handler not found for dev:{}. Not handling edge port"
Saurav Das9a554292018-04-27 18:42:30 -07001457 + " {} event for port:{}", deviceId,
Saurav Das1a129a02016-11-18 15:21:57 -08001458 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001459 }
1460 }
sangho1e575652015-05-14 00:39:53 -07001461
Charles Chan27fe1a52017-10-20 16:06:55 -07001462 private void createOrUpdateDeviceConfiguration() {
1463 if (deviceConfiguration == null) {
Saurav Dase7f51012018-02-09 17:26:45 -08001464 log.info("Creating new DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001465 deviceConfiguration = new DeviceConfiguration(this);
1466 } else {
Saurav Dase7f51012018-02-09 17:26:45 -08001467 log.info("Updating DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001468 deviceConfiguration.updateConfig();
1469 }
1470 }
1471
Charles Chan50bb6ef2018-04-18 18:41:05 -07001472 private void createOrUpdateDefaultRoutingHandler() {
1473 if (defaultRoutingHandler == null) {
1474 log.info("Creating new DefaultRoutingHandler");
1475 defaultRoutingHandler = new DefaultRoutingHandler(this);
1476 } else {
1477 log.info("Updating DefaultRoutingHandler");
1478 defaultRoutingHandler.update(this);
1479 }
1480 }
1481
Pier Ventre10bd8d12016-11-26 21:05:22 -08001482 /**
1483 * Registers the given connect point with the NRS, this is necessary
1484 * to receive the NDP and ARP packets from the NRS.
1485 *
1486 * @param portToRegister connect point to register
1487 */
1488 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001489 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001490 portToRegister,
1491 neighbourHandler,
1492 appId
1493 );
1494 }
1495
Charles Chand6832882015-10-05 17:50:33 -07001496 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001497 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001498 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001499
Charles Chane849c192016-01-11 18:28:54 -08001500 /**
1501 * Constructs the internal network config listener.
1502 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001503 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001504 */
Charles Chan65238242017-06-22 18:03:14 -07001505 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001506 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001507 }
1508
Charles Chane849c192016-01-11 18:28:54 -08001509 /**
1510 * Reads network config and initializes related data structure accordingly.
1511 */
Charles Chan47933752017-11-30 15:37:50 -08001512 void configureNetwork() {
Saurav Dase7f51012018-02-09 17:26:45 -08001513 log.info("Configuring network ...");
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001514
1515 // Setting handling of network configuration events completable future
1516 // The completable future is needed because of the async behaviour of the configureNetwork,
1517 // listener registration and event arrival
1518 // Enables us to buffer the events and execute them when the configure network is done.
1519 networkConfigCompletion = new CompletableFuture<>();
1520
1521 // add a small delay to absorb multiple network config added notifications
1522 if (!programmingScheduled.get()) {
1523 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1524 programmingScheduled.set(true);
1525 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1526 }
1527
Charles Chan27fe1a52017-10-20 16:06:55 -07001528 createOrUpdateDeviceConfiguration();
Charles Chan4636be02015-10-07 14:21:45 -07001529
Charles Chan2c15aca2016-11-09 20:51:44 -08001530 arpHandler = new ArpHandler(srManager);
1531 icmpHandler = new IcmpHandler(srManager);
1532 ipHandler = new IpHandler(srManager);
1533 routingRulePopulator = new RoutingRulePopulator(srManager);
Charles Chan50bb6ef2018-04-18 18:41:05 -07001534 createOrUpdateDefaultRoutingHandler();
Charles Chan4636be02015-10-07 14:21:45 -07001535
1536 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1537 groupHandlerMap, tunnelStore);
1538 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1539 flowObjectiveService,
1540 tunnelHandler, policyStore);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001541 networkConfigCompletion.complete(true);
1542
Charles Chan2199c302016-04-23 17:36:10 -07001543 mcastHandler.init();
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001544
Charles Chan4636be02015-10-07 14:21:45 -07001545 }
1546
Charles Chand6832882015-10-05 17:50:33 -07001547 @Override
1548 public void event(NetworkConfigEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001549 if (mainEventExecutor == null) {
1550 return;
1551 }
Charles Chan7a888e82018-03-21 16:57:47 -07001552 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
1553 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1554 switch (event.type()) {
1555 case CONFIG_ADDED:
1556 case CONFIG_UPDATED:
1557 case CONFIG_REMOVED:
1558 log.trace("Schedule Network Config event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001559 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1560 mainEventExecutor.execute(new InternalEventHandler(event));
1561 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001562 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001563 }
Charles Chan7a888e82018-03-21 16:57:47 -07001564 break;
1565 default:
1566 break;
Charles Chand6832882015-10-05 17:50:33 -07001567 }
1568 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001569
Charles Chan50443e82018-01-03 16:26:32 -08001570 @Override
1571 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase7f51012018-02-09 17:26:45 -08001572 if (event.type() == CONFIG_REGISTERED ||
1573 event.type() == CONFIG_UNREGISTERED) {
1574 log.debug("Ignore event {} due to type mismatch", event);
1575 return false;
Charles Chan50443e82018-01-03 16:26:32 -08001576 }
Saurav Dase7f51012018-02-09 17:26:45 -08001577
1578 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1579 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1580 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscd339592018-02-23 14:18:00 -08001581 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase7f51012018-02-09 17:26:45 -08001582 log.debug("Ignore event {} due to class mismatch", event);
1583 return false;
1584 }
1585
1586 return true;
Charles Chan50443e82018-01-03 16:26:32 -08001587 }
1588
Saurav Das7bcbe702017-06-13 15:35:54 -07001589 private final class ConfigChange implements Runnable {
1590 @Override
1591 public void run() {
1592 programmingScheduled.set(false);
Saurav Dase7f51012018-02-09 17:26:45 -08001593 log.info("Reacting to config changes after buffer delay");
Saurav Das7bcbe702017-06-13 15:35:54 -07001594 for (Device device : deviceService.getDevices()) {
1595 processDeviceAdded(device);
1596 }
1597 defaultRoutingHandler.startPopulationProcess();
1598 }
1599 }
Charles Chand6832882015-10-05 17:50:33 -07001600 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001601
Charles Chan7a888e82018-03-21 16:57:47 -07001602 private class InternalLinkListener implements LinkListener {
1603 @Override
1604 public void event(LinkEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001605 if (mainEventExecutor == null) {
1606 return;
1607 }
Charles Chan7a888e82018-03-21 16:57:47 -07001608 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1609 event.type() == LinkEvent.Type.LINK_UPDATED ||
1610 event.type() == LinkEvent.Type.LINK_REMOVED) {
1611 log.trace("Schedule Link event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001612 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1613 mainEventExecutor.execute(new InternalEventHandler(event));
1614 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001615 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001616 }
Charles Chan7a888e82018-03-21 16:57:47 -07001617 }
1618 }
1619 }
1620
1621 private class InternalDeviceListener implements DeviceListener {
1622 @Override
1623 public void event(DeviceEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001624 if (mainEventExecutor == null) {
1625 return;
1626 }
Charles Chan7a888e82018-03-21 16:57:47 -07001627 switch (event.type()) {
1628 case DEVICE_ADDED:
1629 case PORT_UPDATED:
1630 case PORT_ADDED:
1631 case DEVICE_UPDATED:
1632 case DEVICE_AVAILABILITY_CHANGED:
1633 log.trace("Schedule Device event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001634 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1635 mainEventExecutor.execute(new InternalEventHandler(event));
1636 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001637 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001638 }
Charles Chan7a888e82018-03-21 16:57:47 -07001639 break;
1640 default:
1641 }
1642 }
1643 }
1644
1645 private class InternalTopologyListener implements TopologyListener {
1646 @Override
1647 public void event(TopologyEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001648 if (mainEventExecutor == null) {
1649 return;
1650 }
Charles Chan7a888e82018-03-21 16:57:47 -07001651 switch (event.type()) {
1652 case TOPOLOGY_CHANGED:
1653 log.trace("Schedule Topology event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001654 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1655 mainEventExecutor.execute(new InternalEventHandler(event));
1656 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001657 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001658 }
Charles Chan7a888e82018-03-21 16:57:47 -07001659 break;
1660 default:
1661 }
1662 }
1663 }
1664
Charles Chan68aa62d2015-11-09 16:37:23 -08001665 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001666 @Override
1667 public void event(HostEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001668 if (hostEventExecutor == null) {
1669 return;
1670 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001671 switch (event.type()) {
1672 case HOST_ADDED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001673 case HOST_MOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001674 case HOST_REMOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001675 case HOST_UPDATED:
Charles Chan7a888e82018-03-21 16:57:47 -07001676 log.trace("Schedule Host event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001677 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chan68aa62d2015-11-09 16:37:23 -08001678 break;
1679 default:
1680 log.warn("Unsupported host event type: {}", event.type());
1681 break;
1682 }
1683 }
1684 }
1685
Charles Chand55e84d2016-03-30 17:54:24 -07001686 private class InternalMcastListener implements McastListener {
1687 @Override
1688 public void event(McastEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001689 if (mcastEventExecutor == null) {
1690 return;
1691 }
Charles Chand55e84d2016-03-30 17:54:24 -07001692 switch (event.type()) {
Pier3ee24552018-03-14 16:47:32 -07001693 case SOURCES_ADDED:
1694 case SOURCES_REMOVED:
1695 case SINKS_ADDED:
1696 case SINKS_REMOVED:
Charles Chand55e84d2016-03-30 17:54:24 -07001697 case ROUTE_REMOVED:
Pier96f63cb2018-04-17 16:29:56 +02001698 case ROUTE_ADDED:
Charles Chan7a888e82018-03-21 16:57:47 -07001699 log.trace("Schedule Mcast event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001700 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi9930da52018-02-02 16:19:11 +01001701 break;
Charles Chand55e84d2016-03-30 17:54:24 -07001702 default:
Charles Chan7a888e82018-03-21 16:57:47 -07001703 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chand55e84d2016-03-30 17:54:24 -07001704 break;
1705 }
1706 }
1707 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001708
Charles Chan03a73e02016-10-24 14:52:01 -07001709 private class InternalRouteEventListener implements RouteListener {
1710 @Override
1711 public void event(RouteEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001712 if (routeEventExecutor == null) {
1713 return;
1714 }
Charles Chan03a73e02016-10-24 14:52:01 -07001715 switch (event.type()) {
1716 case ROUTE_ADDED:
Charles Chan03a73e02016-10-24 14:52:01 -07001717 case ROUTE_UPDATED:
Charles Chan03a73e02016-10-24 14:52:01 -07001718 case ROUTE_REMOVED:
Charles Chan2fde6d42017-08-23 14:46:43 -07001719 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan7a888e82018-03-21 16:57:47 -07001720 log.trace("Schedule Route event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001721 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan2fde6d42017-08-23 14:46:43 -07001722 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001723 default:
Charles Chan7a888e82018-03-21 16:57:47 -07001724 log.warn("Unsupported route event type: {}", event.type());
Charles Chan03a73e02016-10-24 14:52:01 -07001725 break;
1726 }
1727 }
1728 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001729
Charles Chan50bb6ef2018-04-18 18:41:05 -07001730 private class InternalMastershipListener implements MastershipListener {
1731 @Override
1732 public void event(MastershipEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001733 if (mainEventExecutor == null) {
1734 return;
1735 }
Charles Chan50bb6ef2018-04-18 18:41:05 -07001736 switch (event.type()) {
Saurav Das201762d2018-04-21 17:19:48 -07001737 case MASTER_CHANGED:
1738 log.debug("Mastership event: {}/{}", event.subject(),
1739 event.roleInfo());
1740 mainEventExecutor.execute(new InternalEventHandler(event));
1741 break;
1742 case BACKUPS_CHANGED:
1743 case SUSPENDED:
1744 default:
1745 log.debug("Mastership event type {} not handled", event.type());
1746 break;
Charles Chan50bb6ef2018-04-18 18:41:05 -07001747 }
1748 }
1749 }
1750
Saurav Das201762d2018-04-21 17:19:48 -07001751 class InternalClusterListener implements ClusterEventListener {
1752 private Instant lastClusterEvent = Instant.EPOCH;
1753
1754 long timeSinceLastClusterEvent() {
1755 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
1756 }
1757
1758 @Override
1759 public void event(ClusterEvent event) {
1760 switch (event.type()) {
1761 case INSTANCE_ACTIVATED:
1762 case INSTANCE_ADDED:
1763 case INSTANCE_READY:
1764 log.debug("Cluster event {} ignored", event.type());
1765 break;
1766 case INSTANCE_DEACTIVATED:
1767 case INSTANCE_REMOVED:
1768 log.info("** Cluster event {}", event.type());
1769 lastClusterEvent = Instant.now();
1770 break;
1771 default:
1772 break;
1773 }
1774
1775 }
1776
1777 }
1778
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001779 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1780 try {
1781 Set<Interface> intfs = conf.getInterfaces();
1782 Set<Interface> prevIntfs = prevConf.getInterfaces();
1783
1784 // Now we only handle one interface config at each port.
1785 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1786 log.warn("Interface update aborted - one at a time is allowed, " +
1787 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1788 return;
1789 }
1790
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001791 //The system is in an incoherent state, abort
1792 if (defaultRoutingHandler == null) {
1793 log.warn("Interface update aborted, defaultRoutingHandler is null");
1794 return;
1795 }
1796
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001797 Interface intf = intfs.stream().findFirst().get();
1798 Interface prevIntf = prevIntfs.stream().findFirst().get();
1799
1800 DeviceId deviceId = intf.connectPoint().deviceId();
1801 PortNumber portNum = intf.connectPoint().port();
1802
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001803 removeSubnetConfig(prevIntf.connectPoint(),
1804 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1805 new HashSet<>(intf.ipAddressesList())));
1806
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001807 if (!prevIntf.vlanNative().equals(VlanId.NONE)
1808 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
1809 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1810 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
1811 // Update filtering objective and L2IG group bucket
1812 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
1813 } else {
1814 // RemoveVlanNative
1815 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1816 }
1817 }
1818
1819 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
1820 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1821 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1822 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
1823 // Update filtering objective and L2IG group bucket
1824 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
1825 } else {
1826 // RemoveVlanUntagged
1827 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1828 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001829 }
1830
1831 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1832 // RemoveVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001833 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
1834 .filter(i -> !intf.vlanUntagged().equals(i))
1835 .filter(i -> !intf.vlanNative().equals(i))
1836 .forEach(vlanId -> updateVlanConfigInternal(
1837 deviceId, portNum, vlanId, false, false));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001838 }
1839
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001840 if (!intf.vlanNative().equals(VlanId.NONE)
1841 && !prevIntf.vlanNative().equals(intf.vlanNative())
1842 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1843 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
1844 // Update filtering objective and L2IG group bucket
1845 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
1846 } else {
1847 // AddVlanNative
1848 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1849 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001850 }
1851
1852 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1853 // AddVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001854 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
1855 .filter(i -> !prevIntf.vlanUntagged().equals(i))
1856 .filter(i -> !prevIntf.vlanNative().equals(i))
1857 .forEach(vlanId -> updateVlanConfigInternal(
1858 deviceId, portNum, vlanId, false, true)
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001859 );
1860 }
1861
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001862 if (!intf.vlanUntagged().equals(VlanId.NONE)
1863 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1864 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
1865 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
1866 // Update filtering objective and L2IG group bucket
1867 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
1868 } else {
1869 // AddVlanUntagged
1870 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1871 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001872 }
1873 addSubnetConfig(prevIntf.connectPoint(),
1874 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1875 new HashSet<>(prevIntf.ipAddressesList())));
1876 } catch (ConfigException e) {
1877 log.error("Error in configuration");
1878 }
1879 }
1880
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001881 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
1882 VlanId vlanId, boolean pushVlan) {
1883 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1884 if (grpHandler == null) {
1885 log.warn("Failed to retrieve group handler for device {}", deviceId);
1886 return;
1887 }
1888
1889 // Update filtering objective for a single port
1890 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
1891 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
1892
1893 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
1894 // Update L2IG bucket of the port
1895 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
1896 } else {
1897 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1898 }
1899 }
1900
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001901 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1902 VlanId vlanId, boolean pushVlan, boolean install) {
1903 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1904 if (grpHandler == null) {
1905 log.warn("Failed to retrieve group handler for device {}", deviceId);
1906 return;
1907 }
1908
1909 // Update filtering objective for a single port
1910 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1911
1912 // Update filtering objective for multicast ingress port
1913 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1914
1915 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1916
1917 if (nextId != -1 && !install) {
1918 // Update next objective for a single port as an output port
1919 // Remove a single port from L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001920 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001921 // Remove L2 Bridging rule and L3 Unicast rule to the host
Charles Chan94549652018-04-29 18:11:37 -07001922 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
1923 vlanId, pushVlan, install));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001924 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1925 // only if there is no port configured on that VLAN ID
1926 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1927 // Remove broadcast forwarding rule for the VLAN
1928 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1929 // Remove L2FG for VLAN
1930 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1931 } else {
1932 // Remove L2IG of the port
1933 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1934 }
1935 } else if (install) {
1936 if (nextId != -1) {
1937 // Add a single port to L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001938 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001939 } else {
1940 // Create L2FG for VLAN
1941 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1942 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1943 }
Charles Chan94549652018-04-29 18:11:37 -07001944 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
1945 vlanId, pushVlan, install));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001946 } else {
1947 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1948 }
1949 }
1950
1951 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1952 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1953 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1954
1955 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1956 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1957 .filter(intf -> !intf.connectPoint().equals(cp))
1958 .flatMap(intf -> intf.ipAddressesList().stream())
1959 .collect(Collectors.toSet());
1960 // 1. Partial subnet population
1961 // Remove routing rules for removed subnet from previous configuration,
1962 // which does not also exist in other interfaces in the same device
1963 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1964 .map(InterfaceIpAddress::subnetAddress)
1965 .collect(Collectors.toSet());
1966
1967 defaultRoutingHandler.revokeSubnet(
1968 ipPrefixSet.stream()
1969 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1970 .collect(Collectors.toSet()));
1971
1972 // 2. Interface IP punts
1973 // Remove IP punts for old Intf address
1974 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1975 .map(InterfaceIpAddress::ipAddress)
1976 .collect(Collectors.toSet());
1977 ipAddressSet.stream()
1978 .map(InterfaceIpAddress::ipAddress)
1979 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1980 .forEach(interfaceIpAddress ->
1981 routingRulePopulator.revokeSingleIpPunts(
1982 cp.deviceId(), interfaceIpAddress));
1983
1984 // 3. Host unicast routing rule
1985 // Remove unicast routing rule
Charles Chan94549652018-04-29 18:11:37 -07001986 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001987 }
1988
1989 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1990 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1991 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1992
1993 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1994 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1995 .filter(intf -> !intf.connectPoint().equals(cp))
1996 .flatMap(intf -> intf.ipAddressesList().stream())
1997 .collect(Collectors.toSet());
1998 // 1. Partial subnet population
1999 // Add routing rules for newly added subnet, which does not also exist in
2000 // other interfaces in the same device
2001 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2002 .map(InterfaceIpAddress::subnetAddress)
2003 .collect(Collectors.toSet());
2004
2005 defaultRoutingHandler.populateSubnet(
2006 Collections.singleton(cp),
2007 ipPrefixSet.stream()
2008 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2009 .collect(Collectors.toSet()));
2010
2011 // 2. Interface IP punts
2012 // Add IP punts for new Intf address
2013 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2014 .map(InterfaceIpAddress::ipAddress)
2015 .collect(Collectors.toSet());
2016 ipAddressSet.stream()
2017 .map(InterfaceIpAddress::ipAddress)
2018 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2019 .forEach(interfaceIpAddress ->
2020 routingRulePopulator.populateSingleIpPunts(
2021 cp.deviceId(), interfaceIpAddress));
2022
2023 // 3. Host unicast routing rule
2024 // Add unicast routing rule
Charles Chan94549652018-04-29 18:11:37 -07002025 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002026 }
sanghob35a6192015-04-01 13:05:26 -07002027}