blob: a1182927ef93ab4589d0e457f63c7ca91f1bc94e [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;
Charles Chanf170b702019-02-27 11:46:32 -080030import org.apache.felix.scr.annotations.ReferencePolicy;
sangho1e575652015-05-14 00:39:53 -070031import org.apache.felix.scr.annotations.Service;
sanghob35a6192015-04-01 13:05:26 -070032import org.onlab.packet.Ethernet;
Pier Ventre735b8c82016-12-02 08:16:05 -080033import org.onlab.packet.ICMP6;
Charles Chanc42e84e2015-10-20 16:24:19 -070034import org.onlab.packet.IPv4;
Pier Ventre10bd8d12016-11-26 21:05:22 -080035import org.onlab.packet.IPv6;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070036import org.onlab.packet.IpAddress;
Charles Chanc42e84e2015-10-20 16:24:19 -070037import org.onlab.packet.IpPrefix;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070038import org.onlab.packet.VlanId;
Ruchi Sahota5d800282019-01-28 01:08:18 +000039import org.onlab.packet.MacAddress;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070040import org.onlab.util.KryoNamespace;
Charles Chan47933752017-11-30 15:37:50 -080041import org.onlab.util.Tools;
Saurav Das80980c72016-03-23 11:22:49 -070042import org.onosproject.cfg.ComponentConfigService;
Saurav Dasc6dc1772018-04-21 17:19:48 -070043import org.onosproject.cluster.ClusterEvent;
44import org.onosproject.cluster.ClusterEventListener;
Pier Luigieba73a02018-01-16 10:47:50 +010045import org.onosproject.cluster.ClusterService;
Jonghwan Hyuna76bf032018-04-09 09:40:50 -070046import org.onosproject.cluster.LeadershipService;
Pier4694e092018-04-17 16:29:56 +020047import org.onosproject.cluster.NodeId;
sanghob35a6192015-04-01 13:05:26 -070048import org.onosproject.core.ApplicationId;
49import org.onosproject.core.CoreService;
50import org.onosproject.event.Event;
Charles Chana8487b02018-04-18 18:41:05 -070051import org.onosproject.mastership.MastershipEvent;
52import org.onosproject.mastership.MastershipListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070053import org.onosproject.mastership.MastershipService;
Pier3ee24552018-03-14 16:47:32 -070054import org.onosproject.mcast.api.McastEvent;
55import org.onosproject.mcast.api.McastListener;
56import org.onosproject.mcast.api.MulticastRouteService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080057import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070058import org.onosproject.net.Device;
59import org.onosproject.net.DeviceId;
Charles Chan9640c812017-08-23 13:55:39 -070060import org.onosproject.net.Host;
61import org.onosproject.net.HostId;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070062import org.onosproject.net.Link;
63import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080064import org.onosproject.net.PortNumber;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070065import org.onosproject.net.config.ConfigException;
Charles Chand6832882015-10-05 17:50:33 -070066import org.onosproject.net.config.ConfigFactory;
67import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070068import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070069import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070070import org.onosproject.net.config.basics.InterfaceConfig;
71import org.onosproject.net.config.basics.McastConfig;
Charles Chand6832882015-10-05 17:50:33 -070072import org.onosproject.net.config.basics.SubjectFactories;
Saurav Das45f48152018-01-18 12:07:33 -080073import org.onosproject.net.device.DeviceAdminService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070074import org.onosproject.net.device.DeviceEvent;
75import org.onosproject.net.device.DeviceListener;
76import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080077import org.onosproject.net.flow.TrafficSelector;
78import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070079import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan1fa010c2018-08-19 19:21:46 -070080import org.onosproject.net.flowobjective.NextObjective;
Charles Chan68aa62d2015-11-09 16:37:23 -080081import org.onosproject.net.host.HostEvent;
82import org.onosproject.net.host.HostListener;
Charles Chand1d321b2018-06-01 16:33:48 -070083import org.onosproject.net.host.HostProbingService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080084import org.onosproject.net.host.HostService;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070085import org.onosproject.net.host.InterfaceIpAddress;
Pier4694e092018-04-17 16:29:56 +020086import org.onosproject.net.intent.WorkPartitionService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070087import org.onosproject.net.intf.Interface;
88import org.onosproject.net.intf.InterfaceService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080089import org.onosproject.net.link.LinkEvent;
90import org.onosproject.net.link.LinkListener;
91import org.onosproject.net.link.LinkService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070092import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080093import org.onosproject.net.packet.InboundPacket;
94import org.onosproject.net.packet.PacketContext;
95import org.onosproject.net.packet.PacketProcessor;
96import org.onosproject.net.packet.PacketService;
Pier Luigi83f919b2018-02-15 16:33:08 +010097import org.onosproject.net.topology.TopologyEvent;
98import org.onosproject.net.topology.TopologyListener;
Charles Chand55e84d2016-03-30 17:54:24 -070099import org.onosproject.net.topology.TopologyService;
Charles Chan9640c812017-08-23 13:55:39 -0700100import org.onosproject.routeservice.ResolvedRoute;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700101import org.onosproject.routeservice.RouteEvent;
102import org.onosproject.routeservice.RouteListener;
103import org.onosproject.routeservice.RouteService;
Charles Chand55e84d2016-03-30 17:54:24 -0700104import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
105import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventref34966c2016-11-07 16:21:04 -0800106import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700107import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -0700108import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -0700109import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700110import org.onosproject.segmentrouting.grouphandler.DestinationSet;
111import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Pier Luigi96fe0772018-02-28 12:10:50 +0100112import org.onosproject.segmentrouting.mcast.McastHandler;
113import org.onosproject.segmentrouting.mcast.McastRole;
Pier7631c492018-04-19 16:47:06 +0200114import org.onosproject.segmentrouting.mcast.McastRoleStoreKey;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700115import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700116import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800117import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700118import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800119
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800120import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700121import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800122import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800123import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
124
Saurav Das7bcbe702017-06-13 15:35:54 -0700125import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Jonghwan Hyuna76bf032018-04-09 09:40:50 -0700126import org.onosproject.segmentrouting.storekey.DummyVlanIdStoreKey;
Pier7631c492018-04-19 16:47:06 +0200127import org.onosproject.segmentrouting.mcast.McastStoreKey;
Charles Chand2990362016-04-18 13:44:03 -0700128import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -0800129import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Ruchi Sahota5d800282019-01-28 01:08:18 +0000130import org.onosproject.segmentrouting.storekey.MacVlanNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -0700131import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Charles Chan4e87b3e2018-06-19 20:31:57 -0700132import org.onosproject.segmentrouting.xconnect.api.XconnectService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700133import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700134import org.onosproject.store.service.EventuallyConsistentMap;
135import org.onosproject.store.service.EventuallyConsistentMapBuilder;
136import org.onosproject.store.service.StorageService;
137import org.onosproject.store.service.WallClockTimestamp;
Charles Chan47933752017-11-30 15:37:50 -0800138import org.osgi.service.component.ComponentContext;
sanghob35a6192015-04-01 13:05:26 -0700139import org.slf4j.Logger;
140import org.slf4j.LoggerFactory;
141
Saurav Dasc6dc1772018-04-21 17:19:48 -0700142import java.time.Instant;
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700143import java.util.ArrayList;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800144import java.util.Collections;
Charles Chan47933752017-11-30 15:37:50 -0800145import java.util.Dictionary;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800146import java.util.HashSet;
147import java.util.List;
148import java.util.Map;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800149import java.util.Optional;
150import java.util.Set;
Andrea Campanellaab482f92018-04-17 12:09:34 +0200151import java.util.concurrent.CompletableFuture;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800152import java.util.concurrent.ConcurrentHashMap;
Andrea Campanellaab482f92018-04-17 12:09:34 +0200153import java.util.concurrent.CopyOnWriteArrayList;
Charles Chan595fdc52018-05-08 21:35:50 -0700154import java.util.concurrent.ExecutorService;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800155import java.util.concurrent.Executors;
156import java.util.concurrent.ScheduledExecutorService;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800157import java.util.concurrent.TimeUnit;
158import java.util.concurrent.atomic.AtomicBoolean;
159import java.util.stream.Collectors;
Jonghwan Hyuna76bf032018-04-09 09:40:50 -0700160import java.util.stream.IntStream;
sanghob35a6192015-04-01 13:05:26 -0700161
Charles Chan3e783d02016-02-26 22:19:52 -0800162import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800163import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700164import static org.onlab.util.Tools.groupedThreads;
Saurav Dase7f51012018-02-09 17:26:45 -0800165import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
166import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Charles Chan3e783d02016-02-26 22:19:52 -0800167
Charles Chane849c192016-01-11 18:28:54 -0800168/**
169 * Segment routing manager.
170 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700171@Service
172@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700173public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700174
Charles Chan2c15aca2016-11-09 20:51:44 -0800175 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan2fde6d42017-08-23 14:46:43 -0700176 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sanghob35a6192015-04-01 13:05:26 -0700177
178 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700179 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700180
181 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan772351a2019-03-22 10:04:27 -0700182 public NeighbourResolutionService neighbourResolutionService;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800183
184 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100185 public CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700186
187 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700188 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700189
190 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700191 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700192
193 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chand1d321b2018-06-01 16:33:48 -0700194 HostProbingService probingService;
Charles Chan47933752017-11-30 15:37:50 -0800195
196 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100197 public DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700198
199 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Saurav Das45f48152018-01-18 12:07:33 -0800200 DeviceAdminService deviceAdminService;
201
202 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800203 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700204
205 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100206 public LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700207
Charles Chan5270ed02016-01-30 23:22:37 -0800208 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800209 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700210
211 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800212 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700213
214 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100215 public MulticastRouteService multicastRouteService;
Charles Chan03a73e02016-10-24 14:52:01 -0700216
217 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chanf237e1b2018-01-09 13:45:07 -0800218 public TopologyService topologyService;
Charles Chan03a73e02016-10-24 14:52:01 -0700219
220 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan022d6672019-04-17 14:20:26 -0700221 public RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800222
223 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800224 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800225
Saurav Das80980c72016-03-23 11:22:49 -0700226 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800227 public InterfaceService interfaceService;
228
Pier Luigieba73a02018-01-16 10:47:50 +0100229 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
230 public ClusterService clusterService;
231
232 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier4694e092018-04-17 16:29:56 +0200233 public WorkPartitionService workPartitionService;
Pier Luigieba73a02018-01-16 10:47:50 +0100234
Jonghwan Hyuna76bf032018-04-09 09:40:50 -0700235 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
236 public LeadershipService leadershipService;
237
Charles Chanf170b702019-02-27 11:46:32 -0800238 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY,
239 bind = "bindXconnectService",
240 unbind = "unbindXconnectService",
241 policy = ReferencePolicy.DYNAMIC)
Charles Chan4e87b3e2018-06-19 20:31:57 -0700242 public XconnectService xconnectService;
243
Charles Chan47933752017-11-30 15:37:50 -0800244 @Property(name = "activeProbing", boolValue = true,
245 label = "Enable active probing to discover dual-homed hosts.")
246 boolean activeProbing = true;
247
Mayank Tiwari5b3f4602018-10-29 18:27:35 -0400248 @Property(name = "symmetricProbing", boolValue = false,
249 label = "Enable only send probe on the same port number of the pair device")
250 boolean symmetricProbing = false;
251
Saurav Das41b49a92018-04-27 18:42:30 -0700252 @Property(name = "singleHomedDown", boolValue = false,
253 label = "Enable administratively taking down single-homed hosts "
254 + "when all uplinks are gone")
255 boolean singleHomedDown = false;
256
Andreas Pantelopoulos7bf9e442018-06-18 12:50:35 -0700257 @Property(name = "respondToUnknownHosts", boolValue = true,
258 label = "Enable this to respond to ARP/NDP requests from unknown hosts.")
259 boolean respondToUnknownHosts = true;
260
Charles Chan5b9a11a2018-07-24 16:40:35 -0700261 @Property(name = "routeDoubleTaggedHosts", boolValue = false,
262 label = "Program flows and groups to pop and route double tagged hosts")
263 boolean routeDoubleTaggedHosts = false;
Charles Chan3d44eb92018-07-19 14:55:31 -0700264
Saurav Das3c82f192018-08-13 15:34:26 -0700265 private static final int DEFAULT_INTERNAL_VLAN = 4094;
266 @Property(name = "defaultInternalVlan", intValue = DEFAULT_INTERNAL_VLAN,
267 label = "internal vlan assigned by default to unconfigured ports")
268 private int defaultInternalVlan = DEFAULT_INTERNAL_VLAN;
269
270 private static final int PW_TRANSPORT_VLAN = 4090;
271 @Property(name = "pwTransportVlan", intValue = PW_TRANSPORT_VLAN,
272 label = "vlan used for transport of pseudowires between switches")
273 private int pwTransportVlan = PW_TRANSPORT_VLAN;
274
Sneha Prem1da248c2019-04-07 02:06:07 -0400275 @Property(name = "routeSimplification", boolValue = false,
276 label = "Enable route simplification")
277 boolean routeSimplification = false;
278
Charles Chan03a73e02016-10-24 14:52:01 -0700279 ArpHandler arpHandler = null;
280 IcmpHandler icmpHandler = null;
281 IpHandler ipHandler = null;
282 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700283 ApplicationId appId;
284 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700285
Charles Chan03a73e02016-10-24 14:52:01 -0700286 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700287 private TunnelHandler tunnelHandler = null;
288 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700289 private InternalPacketProcessor processor = null;
290 private InternalLinkListener linkListener = null;
291 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700292 private AppConfigHandler appCfgHandler = null;
Pier Luigi96fe0772018-02-28 12:10:50 +0100293 public XConnectHandler xConnectHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800294 McastHandler mcastHandler = null;
295 HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800296 private RouteHandler routeHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800297 LinkHandler linkHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800298 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800299 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigi83f919b2018-02-15 16:33:08 +0100300 private TopologyHandler topologyHandler = null;
Charles Chan5270ed02016-01-30 23:22:37 -0800301 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700302 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
303 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700304 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigi83f919b2018-02-15 16:33:08 +0100305 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chana8487b02018-04-18 18:41:05 -0700306 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
Saurav Dasc6dc1772018-04-21 17:19:48 -0700307 final InternalClusterListener clusterListener = new InternalClusterListener();
Andrea Campanellaab482f92018-04-17 12:09:34 +0200308 //Completable future for network configuration process to buffer config events handling during activation
309 private CompletableFuture<Boolean> networkConfigCompletion = null;
Ray Milkeyeadad4a2018-11-16 15:15:14 -0800310 private final Object networkConfigCompletionLock = new Object();
Charles Chane437a802018-04-21 00:44:29 -0700311 private List<Event> queuedEvents = new CopyOnWriteArrayList<>();
sanghob35a6192015-04-01 13:05:26 -0700312
Charles Chan9b7217c2018-04-05 16:31:15 -0700313 // Handles device, link, topology and network config events
Charles Chana8487b02018-04-18 18:41:05 -0700314 private ScheduledExecutorService mainEventExecutor;
sanghob35a6192015-04-01 13:05:26 -0700315
Charles Chana8487b02018-04-18 18:41:05 -0700316 // Handles host, route and mcast events respectively
317 private ScheduledExecutorService hostEventExecutor;
318 private ScheduledExecutorService routeEventExecutor;
319 private ScheduledExecutorService mcastEventExecutor;
Charles Chan595fdc52018-05-08 21:35:50 -0700320 private ExecutorService packetExecutor;
Charles Chanba06a3c2019-01-23 15:03:17 -0800321 ExecutorService neighborExecutor;
Charles Chan9b7217c2018-04-05 16:31:15 -0700322
323 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chane849c192016-01-11 18:28:54 -0800324 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700325 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700326 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800327 */
Charles Chan47933752017-11-30 15:37:50 -0800328 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das7bcbe702017-06-13 15:35:54 -0700329 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800330 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700331 * Per device next objective ID store with (device id + vlanid) as key.
332 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800333 */
Charles Chan47933752017-11-30 15:37:50 -0800334 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800335 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800336 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700337 * Per device next objective ID store with (device id + port + treatment + meta) as key.
Ruchi Sahota5d800282019-01-28 01:08:18 +0000338 * Used to keep track on L2 interface group and L3 unicast group information for direct hosts.
Charles Chane849c192016-01-11 18:28:54 -0800339 */
Charles Chan47933752017-11-30 15:37:50 -0800340 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800341 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800342
Jonghwan Hyuna76bf032018-04-09 09:40:50 -0700343 /**
344 * Per port dummy VLAN ID store with (connect point + ip address) as key.
345 * Used to keep track on dummy VLAN ID allocation.
346 */
347 private EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId>
348 dummyVlanIdStore = null;
349
Ruchi Sahota5d800282019-01-28 01:08:18 +0000350 /**
351 * Per device next objective ID store with (device id + MAC address + vlan) as key.
352 * Used to keep track of L3 unicast group for indirect hosts.
353 */
354 private EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer>
355 macVlanNextObjStore = null;
356
Saurav Das4ce45962015-11-24 23:21:05 -0800357 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
358 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700359
Saurav Das7bcbe702017-06-13 15:35:54 -0700360 private AtomicBoolean programmingScheduled = new AtomicBoolean();
361
Charles Chand55e84d2016-03-30 17:54:24 -0700362 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700363 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
364 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700365 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700366 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800367 public SegmentRoutingDeviceConfig createConfig() {
368 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700369 }
370 };
Pier Ventref34966c2016-11-07 16:21:04 -0800371
Charles Chand55e84d2016-03-30 17:54:24 -0700372 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700373 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
374 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700375 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800376 @Override
377 public SegmentRoutingAppConfig createConfig() {
378 return new SegmentRoutingAppConfig();
379 }
380 };
Pier Ventref34966c2016-11-07 16:21:04 -0800381
Charles Chanfc5c7802016-05-17 13:13:55 -0700382 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
383 new ConfigFactory<ApplicationId, XConnectConfig>(
384 SubjectFactories.APP_SUBJECT_FACTORY,
385 XConnectConfig.class, "xconnect") {
386 @Override
387 public XConnectConfig createConfig() {
388 return new XConnectConfig();
389 }
390 };
Pier Ventref34966c2016-11-07 16:21:04 -0800391
Charles Chand55e84d2016-03-30 17:54:24 -0700392 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700393 new ConfigFactory<ApplicationId, McastConfig>(
394 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700395 McastConfig.class, "multicast") {
396 @Override
397 public McastConfig createConfig() {
398 return new McastConfig();
399 }
400 };
401
Charles Chan116188d2016-02-18 14:22:42 -0800402 /**
403 * Segment Routing App ID.
404 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800405 public static final String APP_NAME = "org.onosproject.segmentrouting";
Saurav Das0e99e2b2015-10-28 12:39:42 -0700406
Jonghwan Hyuna76bf032018-04-09 09:40:50 -0700407 /**
408 * Minumum and maximum value of dummy VLAN ID to be allocated.
409 */
410 public static final int MIN_DUMMY_VLAN_ID = 2;
411 public static final int MAX_DUMMY_VLAN_ID = 4093;
412
Charles Chanba06a3c2019-01-23 15:03:17 -0800413 private static final int DEFAULT_POOL_SIZE = 32;
414
Saurav Das41b49a92018-04-27 18:42:30 -0700415 Instant lastEdgePortEvent = Instant.EPOCH;
416
Charles Chanf170b702019-02-27 11:46:32 -0800417 protected void bindXconnectService(XconnectService xconnectService) {
418 if (this.xconnectService == null) {
419 log.info("Binding XconnectService");
420 this.xconnectService = xconnectService;
421 } else {
422 log.warn("Trying to bind XconnectService but it is already bound");
423 }
424 }
425
426 protected void unbindXconnectService(XconnectService xconnectService) {
427 if (this.xconnectService == xconnectService) {
428 log.info("Unbinding XconnectService");
429 this.xconnectService = null;
430 } else {
431 log.warn("Trying to unbind XconnectService but it is already unbound");
432 }
433 }
434
sanghob35a6192015-04-01 13:05:26 -0700435 @Activate
Charles Chan47933752017-11-30 15:37:50 -0800436 protected void activate(ComponentContext context) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800437 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700438
Charles Chanba06a3c2019-01-23 15:03:17 -0800439 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(
440 groupedThreads("onos/sr", "event-main-%d", log));
441 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(
442 groupedThreads("onos/sr", "event-host-%d", log));
443 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(
444 groupedThreads("onos/sr", "event-route-%d", log));
445 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(
446 groupedThreads("onos/sr", "event-mcast-%d", log));
447 packetExecutor = Executors.newSingleThreadExecutor(groupedThreads("onos/sr", "packet-%d", log));
448 neighborExecutor = Executors.newFixedThreadPool(DEFAULT_POOL_SIZE,
449 groupedThreads("onos/sr", "neighbor-%d", log));
Charles Chana8487b02018-04-18 18:41:05 -0700450
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700451 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700452 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700453 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700454 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700455 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700456 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700457 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700458 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700459 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700460
Charles Chan59cc16d2017-02-02 16:20:42 -0800461 log.debug("Creating EC map vlannextobjectivestore");
462 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
463 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
464 vlanNextObjStore = vlanNextObjMapBuilder
465 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700466 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700467 .withTimestampProvider((k, v) -> new WallClockTimestamp())
468 .build();
469
Ruchi Sahota5d800282019-01-28 01:08:18 +0000470 log.debug("Creating EC map macvlannextobjectivestore");
471 EventuallyConsistentMapBuilder<MacVlanNextObjectiveStoreKey, Integer>
472 macVlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
473 macVlanNextObjStore = macVlanNextObjMapBuilder
474 .withName("macvlannextobjectivestore")
475 .withSerializer(createSerializer())
476 .withTimestampProvider((k, v) -> new WallClockTimestamp())
477 .build();
478
Saurav Das4ce45962015-11-24 23:21:05 -0800479 log.debug("Creating EC map subnetnextobjectivestore");
480 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
481 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
482 portNextObjStore = portNextObjMapBuilder
483 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700484 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800485 .withTimestampProvider((k, v) -> new WallClockTimestamp())
486 .build();
487
Jonghwan Hyuna76bf032018-04-09 09:40:50 -0700488 EventuallyConsistentMapBuilder<DummyVlanIdStoreKey, VlanId>
489 dummyVlanIdMapBuilder = storageService.eventuallyConsistentMapBuilder();
490 dummyVlanIdStore = dummyVlanIdMapBuilder
491 .withName("dummyvlanidstore")
492 .withSerializer(createSerializer())
493 .withTimestampProvider((k, v) -> new WallClockTimestamp())
494 .build();
495
sangho0b2b6d12015-05-20 22:16:38 -0700496 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
497 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700498 tunnelStore = tunnelMapBuilder
499 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700500 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700501 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700502 .build();
503
504 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
505 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700506 policyStore = policyMapBuilder
507 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700508 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700509 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700510 .build();
511
Saurav Das80980c72016-03-23 11:22:49 -0700512 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Charles Chandc037982019-04-10 17:12:20 -0700513 "purgeOnDisconnection", "true", false);
Saurav Das80980c72016-03-23 11:22:49 -0700514 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Charles Chandc037982019-04-10 17:12:20 -0700515 "purgeOnDisconnection", "true", false);
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800516 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
Charles Chandc037982019-04-10 17:12:20 -0700517 "requestInterceptsEnabled", "false", false);
Charles Chand3baaba2017-08-08 15:13:37 -0700518 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Charles Chandc037982019-04-10 17:12:20 -0700519 "requestInterceptsEnabled", "false", false);
Charles Chanc760f382017-07-24 15:56:10 -0700520 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Charles Chandc037982019-04-10 17:12:20 -0700521 "arpEnabled", "false", false);
Pier Luigi9b1d6262017-02-02 22:31:34 -0800522 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
Charles Chandc037982019-04-10 17:12:20 -0700523 "greedyLearningIpv6", "true", false);
Charles Chanc6d227e2017-02-28 15:15:17 -0800524 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
Charles Chandc037982019-04-10 17:12:20 -0700525 "forceUnprovision", "true", false);
Charles Chanef624ed2017-08-10 16:57:28 -0700526 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chandc037982019-04-10 17:12:20 -0700527 "distributed", "true", false);
Charles Chan35a32322017-08-14 11:42:11 -0700528 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
Charles Chandc037982019-04-10 17:12:20 -0700529 "multihomingEnabled", "true", false);
Charles Chan807d87a2017-11-29 19:54:20 -0800530 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
Charles Chandc037982019-04-10 17:12:20 -0700531 "staleLinkAge", "15000", false);
Charles Chan807d87a2017-11-29 19:54:20 -0800532 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
Charles Chandc037982019-04-10 17:12:20 -0700533 "allowDuplicateIps", "false", false);
Charles Chan47933752017-11-30 15:37:50 -0800534 compCfgService.registerProperties(getClass());
535 modified(context);
Saurav Das80980c72016-03-23 11:22:49 -0700536
Charles Chanb8e10c82015-10-14 11:24:40 -0700537 processor = new InternalPacketProcessor();
538 linkListener = new InternalLinkListener();
539 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700540 appCfgHandler = new AppConfigHandler(this);
541 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700542 mcastHandler = new McastHandler(this);
543 hostHandler = new HostHandler(this);
Saurav Das45f48152018-01-18 12:07:33 -0800544 linkHandler = new LinkHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700545 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800546 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800547 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigi83f919b2018-02-15 16:33:08 +0100548 topologyHandler = new TopologyHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700549
Charles Chan3e783d02016-02-26 22:19:52 -0800550 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700551 cfgService.registerConfigFactory(deviceConfigFactory);
552 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700553 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700554 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase7f51012018-02-09 17:26:45 -0800555 log.info("Configuring network before adding listeners");
Andrea Campanellaab482f92018-04-17 12:09:34 +0200556
Charles Chan132393a2018-01-04 14:26:07 -0800557 cfgListener.configureNetwork();
558
Charles Chan5270ed02016-01-30 23:22:37 -0800559 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700560 packetService.addProcessor(processor, PacketProcessor.director(2));
561 linkService.addListener(linkListener);
562 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700563 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700564 routeService.addListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100565 topologyService.addListener(topologyListener);
Charles Chana8487b02018-04-18 18:41:05 -0700566 mastershipService.addListener(mastershipListener);
Saurav Dasc6dc1772018-04-21 17:19:48 -0700567 clusterService.addListener(clusterListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700568
Charles Chanb39777c2018-03-09 15:53:44 -0800569 linkHandler.init();
Andreas Pantelopoulos4a768c02018-01-11 07:53:48 -0800570 l2TunnelHandler.init();
571
Ray Milkeyeadad4a2018-11-16 15:15:14 -0800572 synchronized (networkConfigCompletionLock) {
573 networkConfigCompletion.whenComplete((value, ex) -> {
574 //setting to null for easier fall through
575 networkConfigCompletion = null;
576 //process all queued events
577 queuedEvents.forEach(event -> {
578 mainEventExecutor.execute(new InternalEventHandler(event));
579 });
Andrea Campanellaab482f92018-04-17 12:09:34 +0200580 });
Ray Milkeyeadad4a2018-11-16 15:15:14 -0800581 }
Andrea Campanellaab482f92018-04-17 12:09:34 +0200582
sanghob35a6192015-04-01 13:05:26 -0700583 log.info("Started");
584 }
585
Saurav Das45f48152018-01-18 12:07:33 -0800586 KryoNamespace.Builder createSerializer() {
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700587 return new KryoNamespace.Builder()
588 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700589 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800590 VlanNextObjectiveStoreKey.class,
591 DestinationSet.class,
Andreas Pantelopoulos6c3ec952018-05-29 13:11:14 -0700592 DestinationSet.DestinationSetType.class,
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800593 NextNeighbors.class,
594 Tunnel.class,
595 DefaultTunnel.class,
596 Policy.class,
597 TunnelPolicy.class,
598 Policy.Type.class,
599 PortNextObjectiveStoreKey.class,
600 XConnectStoreKey.class,
601 L2Tunnel.class,
602 L2TunnelPolicy.class,
603 DefaultL2Tunnel.class,
Jonghwan Hyuna76bf032018-04-09 09:40:50 -0700604 DefaultL2TunnelPolicy.class,
Ruchi Sahota5d800282019-01-28 01:08:18 +0000605 DummyVlanIdStoreKey.class,
606 MacVlanNextObjectiveStoreKey.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700607 );
608 }
609
sanghob35a6192015-04-01 13:05:26 -0700610 @Deactivate
611 protected void deactivate() {
Charles Chana8487b02018-04-18 18:41:05 -0700612 mainEventExecutor.shutdown();
613 hostEventExecutor.shutdown();
614 routeEventExecutor.shutdown();
615 mcastEventExecutor.shutdown();
Charles Chan595fdc52018-05-08 21:35:50 -0700616 packetExecutor.shutdown();
Charles Chanba06a3c2019-01-23 15:03:17 -0800617 neighborExecutor.shutdown();
Charles Chana8487b02018-04-18 18:41:05 -0700618
Ray Milkey59bc0722018-05-11 09:59:19 -0700619 mainEventExecutor = null;
620 hostEventExecutor = null;
621 routeEventExecutor = null;
622 mcastEventExecutor = null;
623 packetExecutor = null;
Charles Chanba06a3c2019-01-23 15:03:17 -0800624 neighborExecutor = null;
Ray Milkey59bc0722018-05-11 09:59:19 -0700625
Charles Chand6832882015-10-05 17:50:33 -0700626 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700627 cfgService.unregisterConfigFactory(deviceConfigFactory);
628 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700629 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700630 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan47933752017-11-30 15:37:50 -0800631 compCfgService.unregisterProperties(getClass(), false);
Charles Chand6832882015-10-05 17:50:33 -0700632
Charles Chanc7a8a682017-06-19 00:43:31 -0700633 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700634 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700635 linkService.removeListener(linkListener);
636 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700637 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700638 routeService.removeListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100639 topologyService.removeListener(topologyListener);
Charles Chana8487b02018-04-18 18:41:05 -0700640 mastershipService.removeListener(mastershipListener);
Saurav Dasc6dc1772018-04-21 17:19:48 -0700641 clusterService.removeListener(clusterListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700642
Charles Chan0aa674e2017-02-23 15:44:08 -0800643 neighbourResolutionService.unregisterNeighbourHandlers(appId);
644
sanghob35a6192015-04-01 13:05:26 -0700645 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700646 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700647 deviceListener = null;
Charles Chan5621b9e2018-04-19 13:10:01 -0700648 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chanb8e10c82015-10-14 11:24:40 -0700649 groupHandlerMap.clear();
Saurav Das60ca8d52018-04-23 18:42:12 -0700650 defaultRoutingHandler.shutdown();
Charles Chanb8e10c82015-10-14 11:24:40 -0700651
Saurav Das7bcbe702017-06-13 15:35:54 -0700652 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800653 vlanNextObjStore.destroy();
Ruchi Sahota5d800282019-01-28 01:08:18 +0000654 macVlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700655 portNextObjStore.destroy();
Jonghwan Hyuna76bf032018-04-09 09:40:50 -0700656 dummyVlanIdStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700657 tunnelStore.destroy();
658 policyStore.destroy();
Pier Luigib72201b2018-01-25 16:16:02 +0100659
660 mcastHandler.terminate();
pier094526f2019-05-17 20:47:06 +0200661 hostHandler.terminate();
sanghob35a6192015-04-01 13:05:26 -0700662 log.info("Stopped");
663 }
664
Charles Chan47933752017-11-30 15:37:50 -0800665 @Modified
666 private void modified(ComponentContext context) {
667 Dictionary<?, ?> properties = context.getProperties();
668 if (properties == null) {
669 return;
670 }
671
Andreas Pantelopoulos7bf9e442018-06-18 12:50:35 -0700672 String strActiveProbing = Tools.get(properties, "activeProbing");
673 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProbing);
Charles Chan47933752017-11-30 15:37:50 -0800674 if (expectActiveProbing != activeProbing) {
675 activeProbing = expectActiveProbing;
676 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
677 }
Saurav Das41b49a92018-04-27 18:42:30 -0700678
Mayank Tiwari5b3f4602018-10-29 18:27:35 -0400679 String strSymmetricProving = Tools.get(properties, "symmetricProbing");
680 boolean expectSymmetricProbing = Boolean.parseBoolean(strSymmetricProving);
681 if (expectSymmetricProbing != symmetricProbing) {
682 symmetricProbing = expectSymmetricProbing;
683 log.info("{} symmetric probing", symmetricProbing ? "Enabling" : "Disabling");
684 }
685
Saurav Das41b49a92018-04-27 18:42:30 -0700686 String strSingleHomedDown = Tools.get(properties, "singleHomedDown");
687 boolean expectSingleHomedDown = Boolean.parseBoolean(strSingleHomedDown);
688 if (expectSingleHomedDown != singleHomedDown) {
689 singleHomedDown = expectSingleHomedDown;
690 log.info("{} downing of single homed hosts for lost uplinks",
691 singleHomedDown ? "Enabling" : "Disabling");
692 if (singleHomedDown && linkHandler != null) {
693 hostService.getHosts().forEach(host -> host.locations()
694 .forEach(loc -> {
695 if (interfaceService.isConfigured(loc)) {
696 linkHandler.checkUplinksForHost(loc);
697 }
698 }));
699 } else {
700 log.warn("Disabling singleHomedDown does not re-enable already "
701 + "downed ports for single-homed hosts");
702 }
703 }
Andreas Pantelopoulos7bf9e442018-06-18 12:50:35 -0700704
705 String strRespondToUnknownHosts = Tools.get(properties, "respondToUnknownHosts");
706 boolean expectRespondToUnknownHosts = Boolean.parseBoolean(strRespondToUnknownHosts);
707 if (expectRespondToUnknownHosts != respondToUnknownHosts) {
708 respondToUnknownHosts = expectRespondToUnknownHosts;
709 log.info("{} responding to ARPs/NDPs from unknown hosts", respondToUnknownHosts ? "Enabling" : "Disabling");
710 }
Charles Chan3d44eb92018-07-19 14:55:31 -0700711
Charles Chan5b9a11a2018-07-24 16:40:35 -0700712 String strRouteDoubleTaggedHosts = Tools.get(properties, "routeDoubleTaggedHosts");
713 boolean expectRouteDoubleTaggedHosts = Boolean.parseBoolean(strRouteDoubleTaggedHosts);
714 if (expectRouteDoubleTaggedHosts != routeDoubleTaggedHosts) {
715 routeDoubleTaggedHosts = expectRouteDoubleTaggedHosts;
716 log.info("{} routing for double tagged hosts", routeDoubleTaggedHosts ? "Enabling" : "Disabling");
Charles Chan3d44eb92018-07-19 14:55:31 -0700717
Charles Chan5b9a11a2018-07-24 16:40:35 -0700718 if (routeDoubleTaggedHosts) {
Charles Chan3d44eb92018-07-19 14:55:31 -0700719 hostHandler.populateAllDoubleTaggedHost();
720 } else {
721 hostHandler.revokeAllDoubleTaggedHost();
722 }
723 }
Saurav Das3c82f192018-08-13 15:34:26 -0700724
725 String strDefaultInternalVlan = Tools.get(properties, "defaultInternalVlan");
726 int defIntVlan = Integer.parseInt(strDefaultInternalVlan);
727 if (defIntVlan != defaultInternalVlan) {
728 if (canUseVlanId(defIntVlan)) {
729 log.warn("Default internal vlan value changed from {} to {}.. "
730 + "re-programming filtering rules, but NOT any groups already "
731 + "created with the former value", defaultInternalVlan, defIntVlan);
732 VlanId oldDefIntVlan = VlanId.vlanId((short) defaultInternalVlan);
733 defaultInternalVlan = defIntVlan;
734 routingRulePopulator
735 .updateSpecialVlanFilteringRules(true, oldDefIntVlan,
736 VlanId.vlanId((short) defIntVlan));
737 } else {
738 log.warn("Cannot change default internal vlan to unusable "
739 + "value {}", defIntVlan);
740 }
741 }
742
743 String strPwTxpVlan = Tools.get(properties, "pwTransportVlan");
744 int pwTxpVlan = Integer.parseInt(strPwTxpVlan);
745 if (pwTxpVlan != pwTransportVlan) {
746 if (canUseVlanId(pwTxpVlan)) {
747 log.warn("Pseudowire transport vlan value changed from {} to {}.. "
748 + "re-programming filtering rules, but NOT any groups already "
749 + "created with the former value", pwTransportVlan,
750 pwTxpVlan);
751 VlanId oldPwTxpVlan = VlanId.vlanId((short) pwTransportVlan);
752 pwTransportVlan = pwTxpVlan;
753 routingRulePopulator
754 .updateSpecialVlanFilteringRules(false, oldPwTxpVlan,
755 VlanId.vlanId((short) pwTxpVlan));
756 } else {
757 log.warn("Cannot change pseudowire transport vlan to unusable "
758 + "value {}", pwTxpVlan);
759 }
760 }
761
Sneha Prem1da248c2019-04-07 02:06:07 -0400762 String strRouteSimplification = Tools.get(properties, "routeSimplification");
763 boolean expectRouteSimplification = Boolean.parseBoolean(strRouteSimplification);
764 if (expectRouteSimplification != routeSimplification) {
765 routeSimplification = expectRouteSimplification;
766 log.info("{} route simplification", routeSimplification ? "Enabling" : "Disabling");
767 }
768
Saurav Das3c82f192018-08-13 15:34:26 -0700769 }
770
771 /**
772 * Returns true if given vlan id is not being used in the system currently,
773 * either as one of the default system wide vlans or as one of the
774 * configured interface vlans.
775 *
776 * @param vlanId given vlan id
777 * @return true if vlan is not currently in use
778 */
779 public boolean canUseVlanId(int vlanId) {
780 if (vlanId >= 4095 || vlanId <= 1) {
781 log.error("Vlan id {} value is not in valid range 2 <--> 4094",
782 vlanId);
783 return false;
784 }
785
786 VlanId vid = VlanId.vlanId((short) vlanId);
787 if (getDefaultInternalVlan().equals(vid) || getPwTransportVlan().equals(vid)) {
788 log.warn("Vlan id {} value is already in use system-wide. "
789 + "DefaultInternalVlan:{} PwTransportVlan:{} ", vlanId,
790 getDefaultInternalVlan(), getPwTransportVlan());
791 return false;
792 }
793
794 if (interfaceService.inUse(vid)) {
795 log.warn("Vlan id {} value is already in use on a configured "
796 + "interface in the system", vlanId);
797 return false;
798 }
799 return true;
800 }
801
802 /**
803 * Returns the VlanId assigned internally by default to unconfigured ports.
804 *
805 * @return the default internal vlan id
806 */
807 public VlanId getDefaultInternalVlan() {
808 return VlanId.vlanId((short) defaultInternalVlan);
809 }
810
811 /**
812 * Returns the Vlan id used to transport pseudowire traffic across the
813 * network.
814 *
815 * @return the pseudowire transport vlan id
816 */
817 public VlanId getPwTransportVlan() {
818 return VlanId.vlanId((short) pwTransportVlan);
Charles Chan47933752017-11-30 15:37:50 -0800819 }
820
sangho1e575652015-05-14 00:39:53 -0700821 @Override
822 public List<Tunnel> getTunnels() {
823 return tunnelHandler.getTunnels();
824 }
825
826 @Override
sangho71abe1b2015-06-29 14:58:47 -0700827 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
828 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700829 }
830
831 @Override
sangho71abe1b2015-06-29 14:58:47 -0700832 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700833 for (Policy policy: policyHandler.getPolicies()) {
834 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
835 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
836 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
837 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700838 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700839 }
840 }
841 }
sangho71abe1b2015-06-29 14:58:47 -0700842 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700843 }
844
845 @Override
sangho71abe1b2015-06-29 14:58:47 -0700846 public PolicyHandler.Result removePolicy(Policy policy) {
847 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700848 }
849
850 @Override
sangho71abe1b2015-06-29 14:58:47 -0700851 public PolicyHandler.Result createPolicy(Policy policy) {
852 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700853 }
854
855 @Override
856 public List<Policy> getPolicies() {
857 return policyHandler.getPolicies();
858 }
859
Saurav Das59232cf2016-04-27 18:35:50 -0700860 @Override
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700861 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
862 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700863 }
864
865 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800866 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700867 return l2TunnelHandler.getL2Tunnels();
868 }
869
870 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800871 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700872 return l2TunnelHandler.getL2Policies();
873 }
874
Saurav Das5a356042018-04-06 20:16:01 -0700875 @Override
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700876 @Deprecated
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700877 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
878
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700879 // get both added and pending pseudowires
880 List<L2TunnelDescription> pseudowires = new ArrayList<>();
881 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
882 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700883 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700884
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700885 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
886
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700887 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
888 L2TunnelHandler.Result res;
889 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
890 res = addPseudowire(pw);
891 if (res != L2TunnelHandler.Result.SUCCESS) {
892 log.error("Pseudowire with id {} can not be instantiated !", res);
893 retRes = res;
894 }
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700895 }
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700896
897 return retRes;
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700898 }
899
900 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800901 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700902 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700903 }
904
905 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800906 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700907 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700908 }
909
910 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700911 public void rerouteNetwork() {
912 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700913 }
914
Charles Chanc81c45b2016-10-20 17:02:44 -0700915 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800916 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
917 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800918 deviceConfiguration.getRouters().forEach(device ->
919 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700920 return deviceSubnetMap;
921 }
922
Saurav Dasc88d4662017-05-15 15:34:25 -0700923
924 @Override
925 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
926 if (defaultRoutingHandler != null) {
927 return defaultRoutingHandler.getCurrentEmcpSpgMap();
928 } else {
929 return null;
930 }
931 }
932
933 @Override
Charles Chan1fa010c2018-08-19 19:21:46 -0700934 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDstNextObjStore() {
Saurav Das7bcbe702017-06-13 15:35:54 -0700935 if (dsNextObjStore != null) {
936 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700937 } else {
938 return ImmutableMap.of();
939 }
940 }
941
Saurav Dasceccf242017-08-03 18:30:35 -0700942 @Override
Charles Chan1fa010c2018-08-19 19:21:46 -0700943 public ImmutableMap<VlanNextObjectiveStoreKey, Integer> getVlanNextObjStore() {
944 if (vlanNextObjStore != null) {
945 return ImmutableMap.copyOf(vlanNextObjStore.entrySet());
946 } else {
947 return ImmutableMap.of();
948 }
949 }
950
951 @Override
Ruchi Sahota5d800282019-01-28 01:08:18 +0000952 public ImmutableMap<MacVlanNextObjectiveStoreKey, Integer> getMacVlanNextObjStore() {
953 if (macVlanNextObjStore != null) {
954 return ImmutableMap.copyOf(macVlanNextObjStore.entrySet());
955 } else {
956 return ImmutableMap.of();
957 }
958 }
959
960 @Override
Charles Chan1fa010c2018-08-19 19:21:46 -0700961 public ImmutableMap<PortNextObjectiveStoreKey, Integer> getPortNextObjStore() {
962 if (portNextObjStore != null) {
963 return ImmutableMap.copyOf(portNextObjStore.entrySet());
964 } else {
965 return ImmutableMap.of();
966 }
967 }
968
969 @Override
970 public ImmutableMap<String, NextObjective> getPwInitNext() {
971 if (l2TunnelHandler != null) {
972 return l2TunnelHandler.getInitNext();
973 } else {
974 return ImmutableMap.of();
975 }
976 }
977
978 @Override
979 public ImmutableMap<String, NextObjective> getPwTermNext() {
980 if (l2TunnelHandler != null) {
981 return l2TunnelHandler.getTermNext();
982 } else {
983 return ImmutableMap.of();
984 }
985 }
986
987 @Override
988 public void invalidateNextObj(int nextId) {
989 if (dsNextObjStore != null) {
990 dsNextObjStore.entrySet().forEach(e -> {
991 if (e.getValue().nextId() == nextId) {
992 dsNextObjStore.remove(e.getKey());
993 }
994 });
995 }
996 if (vlanNextObjStore != null) {
997 vlanNextObjStore.entrySet().forEach(e -> {
998 if (e.getValue() == nextId) {
999 vlanNextObjStore.remove(e.getKey());
1000 }
1001 });
1002 }
Ruchi Sahota5d800282019-01-28 01:08:18 +00001003 if (macVlanNextObjStore != null) {
1004 macVlanNextObjStore.entrySet().forEach(e -> {
1005 if (e.getValue() == nextId) {
1006 macVlanNextObjStore.remove(e.getKey());
1007 }
1008 });
1009 }
Charles Chan1fa010c2018-08-19 19:21:46 -07001010 if (portNextObjStore != null) {
1011 portNextObjStore.entrySet().forEach(e -> {
1012 if (e.getValue() == nextId) {
1013 portNextObjStore.remove(e.getKey());
1014 }
1015 });
1016 }
1017 if (mcastHandler != null) {
1018 mcastHandler.removeNextId(nextId);
1019 }
1020 if (l2TunnelHandler != null) {
1021 l2TunnelHandler.removeNextId(nextId);
1022 }
1023 if (xconnectService != null) {
1024 xconnectService.removeNextId(nextId);
1025 }
1026 }
1027
1028 @Override
Saurav Dasceccf242017-08-03 18:30:35 -07001029 public void verifyGroups(DeviceId id) {
1030 DefaultGroupHandler gh = groupHandlerMap.get(id);
1031 if (gh != null) {
1032 gh.triggerBucketCorrector();
1033 }
1034 }
1035
Saurav Dasc568c342018-01-25 09:49:01 -08001036 @Override
1037 public ImmutableMap<Link, Boolean> getSeenLinks() {
1038 return linkHandler.getSeenLinks();
1039 }
1040
1041 @Override
1042 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
1043 return linkHandler.getDownedPorts();
1044 }
1045
Pier Luigib29144d2018-01-15 18:06:43 +01001046 @Override
1047 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
Charles Chan1fa010c2018-08-19 19:21:46 -07001048 return mcastHandler.getNextIds(mcastIp);
Pier Luigib29144d2018-01-15 18:06:43 +01001049 }
1050
1051 @Override
Pier Luigi96fe0772018-02-28 12:10:50 +01001052 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigib29144d2018-01-15 18:06:43 +01001053 return mcastHandler.getMcastRoles(mcastIp);
1054 }
1055
1056 @Override
Pier7631c492018-04-19 16:47:06 +02001057 public Map<McastRoleStoreKey, McastRole> getMcastRoles(IpAddress mcastIp, ConnectPoint sourcecp) {
1058 return mcastHandler.getMcastRoles(mcastIp, sourcecp);
1059 }
1060
1061 @Override
Pier Luigib29144d2018-01-15 18:06:43 +01001062 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
1063 return mcastHandler.getMcastPaths(mcastIp);
1064 }
1065
Pier4694e092018-04-17 16:29:56 +02001066 @Override
Piere7e8e3c2018-04-17 17:25:22 +02001067 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
1068 ConnectPoint sourcecp) {
1069 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
1070 }
1071
1072 @Override
Pier4694e092018-04-17 16:29:56 +02001073 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
1074 return mcastHandler.getMcastLeaders(mcastIp);
1075 }
1076
Charles Chan09879082018-04-17 18:56:53 -07001077 @Override
1078 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
1079 return defaultRoutingHandler == null ? ImmutableMap.of() :
1080 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
1081 }
1082
1083 @Override
1084 public Map<DeviceId, Boolean> getShouldProgramCache() {
1085 return defaultRoutingHandler == null ? ImmutableMap.of() :
1086 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
1087 }
1088
Charles Chan4e87b3e2018-06-19 20:31:57 -07001089 @Override
jayakumarthazhath46945b62018-10-01 00:51:54 +05301090 public boolean shouldProgram(DeviceId deviceId) {
1091 return defaultRoutingHandler.shouldProgram(deviceId);
1092 }
1093
1094 @Override
Ray Milkeye4afdb52017-04-05 09:42:04 -07001095 public ApplicationId appId() {
1096 return appId;
1097 }
1098
1099 /**
1100 * Returns the device configuration.
1101 *
1102 * @return device configuration
1103 */
1104 public DeviceConfiguration deviceConfiguration() {
1105 return deviceConfiguration;
1106 }
1107
1108 /**
Saurav Das7bcbe702017-06-13 15:35:54 -07001109 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -07001110 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -07001111 *
1112 * @return next objective ID store
1113 */
Saurav Das7bcbe702017-06-13 15:35:54 -07001114 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
1115 dsNextObjStore() {
1116 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -07001117 }
1118
1119 /**
Charles Chan5bc32632017-08-22 15:07:34 -07001120 * Per device next objective ID store with (device id + vlanid) as key.
1121 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -07001122 *
1123 * @return vlan next object store
1124 */
1125 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
1126 return vlanNextObjStore;
1127 }
1128
1129 /**
Ruchi Sahota5d800282019-01-28 01:08:18 +00001130 * Per device next objective ID store with (device id + MAC address + vlan) as key.
1131 * Used to keep track on L3 Unicast group information for indirect hosts.
1132 *
1133 * @return mac vlan next object store
1134 */
1135 public EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer> macVlanNextObjStore() {
1136 return macVlanNextObjStore;
1137 }
1138
1139 /**
Charles Chan5bc32632017-08-22 15:07:34 -07001140 * Per device next objective ID store with (device id + port + treatment + meta) as key.
Ruchi Sahota5d800282019-01-28 01:08:18 +00001141 * Used to keep track on L2 interface group and L3 unicast group information for direct hosts.
Ray Milkeye4afdb52017-04-05 09:42:04 -07001142 *
1143 * @return port next object store.
1144 */
1145 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
1146 return portNextObjStore;
1147 }
1148
1149 /**
Jonghwan Hyuna76bf032018-04-09 09:40:50 -07001150 * Per port dummy VLAN ID store with (connect point + ip address) as key.
1151 * Used to keep track on dummy VLAN ID allocation.
1152 *
1153 * @return dummy vlan id store.
1154 */
1155 public EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId> dummyVlanIdStore() {
1156 return dummyVlanIdStore;
1157 }
1158
1159 /**
Saurav Das7bcbe702017-06-13 15:35:54 -07001160 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
1161 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -07001162 *
1163 * @return MPLS-ECMP value
1164 */
1165 public boolean getMplsEcmp() {
1166 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
1167 .getConfig(this.appId, SegmentRoutingAppConfig.class);
1168 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
1169 }
1170
1171 /**
sanghof9d0bf12015-05-19 11:57:42 -07001172 * Returns the tunnel object with the tunnel ID.
1173 *
1174 * @param tunnelId Tunnel ID
1175 * @return Tunnel reference
1176 */
sangho1e575652015-05-14 00:39:53 -07001177 public Tunnel getTunnel(String tunnelId) {
1178 return tunnelHandler.getTunnel(tunnelId);
1179 }
1180
Charles Chan4e87b3e2018-06-19 20:31:57 -07001181 @Override
Pier Luigi96fe0772018-02-28 12:10:50 +01001182 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chanb30213c2018-05-01 11:50:20 -07001183 VlanId untaggedVlanId = interfaceService.getUntaggedVlanId(connectPoint);
1184 VlanId nativeVlanId = interfaceService.getNativeVlanId(connectPoint);
Charles Chanf9a52702017-06-16 15:19:24 -07001185 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
1186 }
1187
Charles Chan4e87b3e2018-06-19 20:31:57 -07001188 @Override
1189 public Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
Charles Chan65238242017-06-22 18:03:14 -07001190 SegmentRoutingDeviceConfig deviceConfig =
1191 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1192 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
1193 }
Charles Chan4e87b3e2018-06-19 20:31:57 -07001194
1195 @Override
Saurav Das41b49a92018-04-27 18:42:30 -07001196 public Optional<PortNumber> getPairLocalPort(DeviceId deviceId) {
Charles Chan65238242017-06-22 18:03:14 -07001197 SegmentRoutingDeviceConfig deviceConfig =
1198 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1199 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
1200 }
1201
1202 /**
Charles Chan9640c812017-08-23 13:55:39 -07001203 * Returns locations of given resolved route.
1204 *
1205 * @param resolvedRoute resolved route
1206 * @return locations of nexthop. Might be empty if next hop is not found
1207 */
Charles Chan022d6672019-04-17 14:20:26 -07001208 public Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
Charles Chan9640c812017-08-23 13:55:39 -07001209 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
1210 return Optional.ofNullable(hostService.getHost(hostId))
1211 .map(Host::locations).orElse(Sets.newHashSet())
1212 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
1213 }
1214
1215 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -08001216 * Returns vlan port map of given device.
1217 *
1218 * @param deviceId device id
1219 * @return vlan-port multimap
1220 */
1221 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
1222 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
1223
1224 interfaceService.getInterfaces().stream()
1225 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
1226 .forEach(intf -> {
1227 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -07001228 intf.vlanTagged().forEach(vlanTagged ->
1229 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
1230 );
Charles Chan7ffd81f2017-02-08 15:52:08 -08001231 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
1232 });
1233 vlanPortMap.removeAll(VlanId.NONE);
1234
1235 return vlanPortMap;
1236 }
1237
1238 /**
Charles Chan59cc16d2017-02-02 16:20:42 -08001239 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -08001240 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -07001241 *
1242 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -08001243 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -08001244 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -07001245 */
Charles Chan65238242017-06-22 18:03:14 -07001246 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -07001247 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -08001248 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
1249 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001250 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -08001251 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -08001252 + "device {} not found", deviceId);
1253 return -1;
1254 }
1255 }
1256
1257 /**
1258 * Returns the next objective ID for the given portNumber, given the treatment.
1259 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -07001260 * would result in different objectives. If the next object does not exist,
1261 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -08001262 *
1263 * @param deviceId Device ID
1264 * @param portNum port number on device for which NextObjective is queried
1265 * @param treatment the actions to apply on the packets (should include outport)
1266 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -07001267 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -07001268 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -08001269 */
1270 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
1271 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -07001272 TrafficSelector meta,
1273 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -08001274 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1275 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -07001276 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -08001277 } else {
Charles Chane849c192016-01-11 18:28:54 -08001278 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
1279 + " not found", deviceId);
1280 return -1;
1281 }
1282 }
1283
Saurav Dasc88d4662017-05-15 15:34:25 -07001284 /**
Ruchi Sahota5d800282019-01-28 01:08:18 +00001285 * Returns the next Objective ID for the given mac and vlan, given the treatment.
1286 * There could be multiple different treatments to the same outport, which
1287 * would result in different objectives. If the next object does not exist,
1288 * and should be created, a new one is created and its id is returned.
1289 *
1290 * @param deviceId Device ID
1291 * @param macAddr mac of host for which Next ID is required.
1292 * @param vlanId vlan of host for which Next ID is required.
Ruchi Sahotab555e592019-05-09 17:26:14 -04001293 * @param port port with which to create the Next Obj.
Ruchi Sahota5d800282019-01-28 01:08:18 +00001294 * @param createIfMissing true if a next object should be created if not found
1295 * @return next objective ID or -1 if an error occurred during retrieval or creation
1296 */
1297 public int getMacVlanNextObjectiveId(DeviceId deviceId, MacAddress macAddr, VlanId vlanId,
Ruchi Sahotab555e592019-05-09 17:26:14 -04001298 PortNumber port, boolean createIfMissing) {
Ruchi Sahota5d800282019-01-28 01:08:18 +00001299 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1300 if (ghdlr != null) {
Ruchi Sahotab555e592019-05-09 17:26:14 -04001301 return ghdlr.getMacVlanNextObjectiveId(macAddr, vlanId, port, createIfMissing);
Ruchi Sahota5d800282019-01-28 01:08:18 +00001302 } else {
1303 log.warn("getMacVlanNextObjectiveId query - groupHandler for device {}"
1304 + " not found", deviceId);
1305 return -1;
1306 }
1307 }
1308
1309 /**
Ruchi Sahota5d800282019-01-28 01:08:18 +00001310 * Updates the next objective for the given nextId .
1311 *
1312 * @param deviceId Device ID
1313 * @param hostMac mac of host for which Next obj is to be updated.
1314 * @param hostVlanId vlan of host for which Next obj is to be updated.
1315 * @param port port with which to update the Next Obj.
1316 * @param nextId of Next Obj which needs to be updated.
1317 */
1318 public void updateMacVlanTreatment(DeviceId deviceId, MacAddress hostMac,
1319 VlanId hostVlanId, PortNumber port, int nextId) {
pierf0ee0402019-11-26 11:48:48 +01001320 // Check if we are the king of this device
1321 // just one instance should perform this update
1322 if (!defaultRoutingHandler.shouldProgram(deviceId)) {
1323 log.debug("This instance is not handling the routing towards the "
1324 + "device {}", deviceId);
1325 return;
1326 }
1327 // Get the handler and perform the update
Ruchi Sahota5d800282019-01-28 01:08:18 +00001328 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1329 if (ghdlr != null) {
1330 ghdlr.updateL3UcastGroupBucket(hostMac, hostVlanId, port, nextId);
1331 } else {
1332 log.warn("updateL3UcastGroupBucket query - groupHandler for device {}"
1333 + " not found", deviceId);
1334 }
1335 }
1336
1337
1338 /**
Saurav Dasc88d4662017-05-15 15:34:25 -07001339 * Returns the group handler object for the specified device id.
1340 *
1341 * @param devId the device identifier
1342 * @return the groupHandler object for the device id, or null if not found
1343 */
Charles Chan65238242017-06-22 18:03:14 -07001344 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001345 return groupHandlerMap.get(devId);
1346 }
1347
1348 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001349 * Returns the default routing handler object.
1350 *
1351 * @return the default routing handler object
1352 */
1353 public DefaultRoutingHandler getRoutingHandler() {
1354 return defaultRoutingHandler;
1355 }
1356
Jonghwan Hyuna76bf032018-04-09 09:40:50 -07001357 /**
1358 * Returns new dummy VLAN ID.
1359 * Dummy VLAN ID should be unique in each connect point.
1360 *
1361 * @param cp connect point
1362 * @param ipAddress IP address
1363 * @return new dummy VLAN ID. Returns VlanId.NONE if no VLAN ID is available.
1364 */
1365 public synchronized VlanId allocateDummyVlanId(ConnectPoint cp, IpAddress ipAddress) {
1366 Set<VlanId> usedVlanId = Sets.union(getVlanPortMap(cp.deviceId()).keySet(),
1367 dummyVlanIdStore.entrySet().stream().filter(entry ->
1368 (entry.getKey()).connectPoint().equals(cp))
1369 .map(Map.Entry::getValue)
1370 .collect(Collectors.toSet()));
1371
1372 VlanId dummyVlanId = IntStream.range(MIN_DUMMY_VLAN_ID, MAX_DUMMY_VLAN_ID).mapToObj(
1373 i -> VlanId.vlanId((short) (i & 0xFFFF))
1374 ).filter(vlanId -> !usedVlanId.contains(vlanId)).findFirst().orElse(VlanId.NONE);
1375
1376 if (!dummyVlanId.equals(VlanId.NONE)) {
1377 this.dummyVlanIdStore.put(new DummyVlanIdStoreKey(cp, ipAddress), dummyVlanId);
1378 log.debug("Dummy VLAN ID {} is allocated to {}, {}", dummyVlanId, cp, ipAddress);
1379 } else {
1380 log.error("Failed to allocate dummy VLAN ID for {}, {}", cp, ipAddress);
1381 }
1382 return dummyVlanId;
1383 }
1384
1385
sanghob35a6192015-04-01 13:05:26 -07001386 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -07001387 @Override
1388 public void process(PacketContext context) {
Charles Chan595fdc52018-05-08 21:35:50 -07001389 packetExecutor.execute(() -> processPacketInternal(context));
1390 }
sanghob35a6192015-04-01 13:05:26 -07001391
Charles Chan595fdc52018-05-08 21:35:50 -07001392 private void processPacketInternal(PacketContext context) {
sanghob35a6192015-04-01 13:05:26 -07001393 if (context.isHandled()) {
1394 return;
1395 }
1396
1397 InboundPacket pkt = context.inPacket();
1398 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -08001399
1400 if (ethernet == null) {
1401 return;
1402 }
1403
Saurav Das7bcbe702017-06-13 15:35:54 -07001404 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1405 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001406 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001407 log.warn("Received unexpected ARP packet on {}",
1408 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -07001409 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -08001410 return;
sanghob35a6192015-04-01 13:05:26 -07001411 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -08001412 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1413 //ipHandler.addToPacketBuffer(ipv4Packet);
1414 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1415 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001416 } else {
Charles Chan50035632017-01-13 17:20:44 -08001417 // NOTE: We don't support IP learning at this moment so this
1418 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -08001419 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001420 }
Pier Ventre10bd8d12016-11-26 21:05:22 -08001421 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1422 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -08001423 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -08001424 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -08001425 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1426 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1427 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1428 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1429 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1430 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -07001431 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -07001432 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -08001433 }
1434 } else {
1435 // NOTE: We don't support IP learning at this moment so this
1436 // is not necessary. Also it causes duplication of DHCPv6 packets.
1437 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1438 }
sanghob35a6192015-04-01 13:05:26 -07001439 }
1440 }
1441 }
1442
sanghob35a6192015-04-01 13:05:26 -07001443 private class InternalEventHandler implements Runnable {
Charles Chan9b7217c2018-04-05 16:31:15 -07001444 private Event event;
1445
1446 InternalEventHandler(Event event) {
1447 this.event = event;
1448 }
1449
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001450 @Override
sanghob35a6192015-04-01 13:05:26 -07001451 public void run() {
Charles Chan9b7217c2018-04-05 16:31:15 -07001452 try {
1453 // TODO We should also change SR routing and PW to listen to TopologyEvents
1454 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1455 event.type() == LinkEvent.Type.LINK_UPDATED) {
1456 linkHandler.processLinkAdded((Link) event.subject());
1457 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1458 linkHandler.processLinkRemoved((Link) event.subject());
1459 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1460 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1461 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1462 DeviceId deviceId = ((Device) event.subject()).id();
1463 if (deviceService.isAvailable(deviceId)) {
1464 log.info("** DEVICE UP Processing device event {} "
1465 + "for available device {}",
1466 event.type(), ((Device) event.subject()).id());
1467 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001468 } else {
Charles Chan9b7217c2018-04-05 16:31:15 -07001469 log.info(" ** DEVICE DOWN Processing device event {}"
1470 + " for unavailable device {}",
1471 event.type(), ((Device) event.subject()).id());
1472 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001473 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001474 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1475 // typically these calls come when device is added first time
1476 // so port filtering rules are handled at the device_added event.
1477 // port added calls represent all ports on the device,
1478 // enabled or not.
1479 log.trace("** PORT ADDED {}/{} -> {}",
1480 ((DeviceEvent) event).subject().id(),
1481 ((DeviceEvent) event).port().number(),
1482 event.type());
1483 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1484 // these calls happen for every subsequent event
1485 // ports enabled, disabled, switch goes away, comes back
1486 log.info("** PORT UPDATED {}/{} -> {}",
1487 event.subject(),
1488 ((DeviceEvent) event).port(),
1489 event.type());
Saurav Das41b49a92018-04-27 18:42:30 -07001490 processPortUpdatedInternal(((Device) event.subject()),
Charles Chan9b7217c2018-04-05 16:31:15 -07001491 ((DeviceEvent) event).port());
1492 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1493 // Process topology event, needed for all modules relying on
1494 // topology service for path computation
1495 TopologyEvent topologyEvent = (TopologyEvent) event;
1496 log.info("Processing topology event {}, topology age {}, reasons {}",
1497 event.type(), topologyEvent.subject().time(),
1498 topologyEvent.reasons().size());
1499 topologyHandler.processTopologyChange(topologyEvent.reasons());
1500 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1501 hostHandler.processHostAddedEvent((HostEvent) event);
1502 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1503 hostHandler.processHostMovedEvent((HostEvent) event);
1504 routeHandler.processHostMovedEvent((HostEvent) event);
1505 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1506 hostHandler.processHostRemovedEvent((HostEvent) event);
1507 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1508 hostHandler.processHostUpdatedEvent((HostEvent) event);
1509 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1510 routeHandler.processRouteAdded((RouteEvent) event);
1511 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1512 routeHandler.processRouteUpdated((RouteEvent) event);
1513 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1514 routeHandler.processRouteRemoved((RouteEvent) event);
1515 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1516 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1517 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1518 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1519 event.type() == McastEvent.Type.SINKS_ADDED ||
1520 event.type() == McastEvent.Type.SINKS_REMOVED ||
1521 event.type() == McastEvent.Type.ROUTE_ADDED ||
1522 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1523 mcastHandler.processMcastEvent((McastEvent) event);
1524 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1525 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1526 Class configClass = netcfgEvent.configClass();
1527 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1528 appCfgHandler.processAppConfigAdded(netcfgEvent);
1529 log.info("App config event .. configuring network");
1530 cfgListener.configureNetwork();
1531 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1532 log.info("Segment Routing Device Config added for {}", event.subject());
1533 cfgListener.configureNetwork();
1534 } else if (configClass.equals(XConnectConfig.class)) {
1535 xConnectHandler.processXConnectConfigAdded(netcfgEvent);
1536 } else if (configClass.equals(InterfaceConfig.class)) {
1537 log.info("Interface Config added for {}", event.subject());
1538 cfgListener.configureNetwork();
1539 } else {
1540 log.error("Unhandled config class: {}", configClass);
1541 }
1542 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1543 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1544 Class configClass = netcfgEvent.configClass();
1545 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1546 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1547 log.info("App config event .. configuring network");
1548 cfgListener.configureNetwork();
1549 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1550 log.info("Segment Routing Device Config updated for {}", event.subject());
1551 createOrUpdateDeviceConfiguration();
1552 } else if (configClass.equals(XConnectConfig.class)) {
1553 xConnectHandler.processXConnectConfigUpdated(netcfgEvent);
1554 } else if (configClass.equals(InterfaceConfig.class)) {
1555 log.info("Interface Config updated for {}", event.subject());
1556 createOrUpdateDeviceConfiguration();
1557 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1558 (InterfaceConfig) netcfgEvent.prevConfig().get());
1559 } else {
1560 log.error("Unhandled config class: {}", configClass);
1561 }
1562 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1563 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1564 Class configClass = netcfgEvent.configClass();
1565 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1566 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1567 log.info("App config event .. configuring network");
1568 cfgListener.configureNetwork();
1569 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1570 // TODO Handle sr device config removal
1571 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
1572 } else if (configClass.equals(XConnectConfig.class)) {
1573 xConnectHandler.processXConnectConfigRemoved(netcfgEvent);
1574 } else if (configClass.equals(InterfaceConfig.class)) {
1575 // TODO Handle interface removal
1576 log.info("InterfaceConfig removal is not handled in current implementation");
1577 } else {
1578 log.error("Unhandled config class: {}", configClass);
1579 }
Saurav Dasc6dc1772018-04-21 17:19:48 -07001580 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1581 MastershipEvent me = (MastershipEvent) event;
1582 DeviceId deviceId = me.subject();
1583 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1584 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1585 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1586 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1587 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1588 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan9b7217c2018-04-05 16:31:15 -07001589 } else {
1590 log.warn("Unhandled event type: {}", event.type());
sanghob35a6192015-04-01 13:05:26 -07001591 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001592 } catch (Exception e) {
1593 log.error("SegmentRouting event handler thread thrown an exception: {}",
1594 e.getMessage(), e);
sanghob35a6192015-04-01 13:05:26 -07001595 }
sanghob35a6192015-04-01 13:05:26 -07001596 }
1597 }
1598
Saurav Das45f48152018-01-18 12:07:33 -08001599 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001600 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001601
1602 // NOTE: Punt ARP/NDP even when the device is not configured.
1603 // Host learning without network config is required for CORD config generator.
1604 routingRulePopulator.populateIpPunts(device.id());
1605 routingRulePopulator.populateArpNdpPunts(device.id());
1606
Charles Chan0b4e6182015-11-03 10:42:14 -08001607 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001608 log.warn("Device configuration unavailable. Device {} will be "
1609 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001610 return;
1611 }
Charles Chan2199c302016-04-23 17:36:10 -07001612 processDeviceAddedInternal(device.id());
1613 }
1614
1615 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001616 // Irrespective of whether the local is a MASTER or not for this device,
1617 // we need to create a SR-group-handler instance. This is because in a
1618 // multi-instance setup, any instance can initiate forwarding/next-objectives
1619 // for any switch (even if this instance is a SLAVE or not even connected
1620 // to the switch). To handle this, a default-group-handler instance is necessary
1621 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001622 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1623 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001624 DefaultGroupHandler groupHandler;
1625 try {
1626 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001627 createGroupHandler(deviceId,
1628 appId,
1629 deviceConfiguration,
1630 linkService,
1631 flowObjectiveService,
1632 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001633 } catch (DeviceConfigNotFoundException e) {
1634 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1635 return;
1636 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001637 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001638 deviceId);
1639 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001640 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001641
Charles Chan2199c302016-04-23 17:36:10 -07001642 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001643 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001644 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001645 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001646 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001647 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001648 }
Charles Chan5270ed02016-01-30 23:22:37 -08001649
Charles Chan03a73e02016-10-24 14:52:01 -07001650 appCfgHandler.init(deviceId);
Charles Chana03ab8b2018-04-29 18:11:37 -07001651 hostEventExecutor.execute(() -> hostHandler.init(deviceId));
1652 routeEventExecutor.execute(() -> routeHandler.init(deviceId));
sanghob35a6192015-04-01 13:05:26 -07001653 }
1654
Saurav Das80980c72016-03-23 11:22:49 -07001655 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001656 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001657 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan47933752017-11-30 15:37:50 -08001658 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001659 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001660 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001661 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Ruchi Sahota5d800282019-01-28 01:08:18 +00001662 macVlanNextObjStore.entrySet().stream()
1663 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1664 .forEach(entry -> macVlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001665 portNextObjStore.entrySet().stream()
1666 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001667 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Das45f48152018-01-18 12:07:33 -08001668 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001669
Saurav Dasceccf242017-08-03 18:30:35 -07001670 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1671 if (gh != null) {
1672 gh.shutdown();
1673 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001674 // Note that a switch going down is associated with all of its links
1675 // going down as well, but it is treated as a single switch down event
Saurav Das5a356042018-04-06 20:16:01 -07001676 // while the link-downs are ignored. We cannot rely on the ordering of
1677 // events - i.e we cannot expect all link-downs to come before the
1678 // switch down - so we purge all seen-links for the switch before
1679 // handling route-path changes for the switch-down
Saurav Dasc88d4662017-05-15 15:34:25 -07001680 defaultRoutingHandler
Saurav Das604ab3a2018-03-18 21:28:15 -07001681 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Dasc568c342018-01-25 09:49:01 -08001682 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001683 xConnectHandler.removeDevice(device.id());
Saurav Dasa4020382018-02-14 14:14:54 -08001684
1685 // Cleanup all internal groupHandler stores for this device. Should be
1686 // done after all rerouting or rehashing has been completed
1687 groupHandlerMap.entrySet()
1688 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Das80980c72016-03-23 11:22:49 -07001689 }
1690
Saurav Das60ca8d52018-04-23 18:42:12 -07001691 /**
1692 * Purge the destinationSet nextObjective store of entries with this device
1693 * as key. Erases app-level knowledge of hashed groups in this device.
1694 *
1695 * @param devId the device identifier
1696 */
Saurav Dasc6dc1772018-04-21 17:19:48 -07001697 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Das60ca8d52018-04-23 18:42:12 -07001698 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Dasc6dc1772018-04-21 17:19:48 -07001699 dsNextObjStore.entrySet().stream()
1700 .filter(entry -> entry.getKey().deviceId().equals(devId))
1701 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1702 }
1703
Saurav Das41b49a92018-04-27 18:42:30 -07001704 private void processPortUpdatedInternal(Device device, Port port) {
Saurav Das1a129a02016-11-18 15:21:57 -08001705 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1706 log.warn("Device configuration uploading. Not handling port event for"
1707 + "dev: {} port: {}", device.id(), port.number());
1708 return;
1709 }
Saurav Das018605f2017-02-18 14:05:44 -08001710
Saurav Das41b49a92018-04-27 18:42:30 -07001711 if (interfaceService.isConfigured(new ConnectPoint(device.id(), port.number()))) {
1712 lastEdgePortEvent = Instant.now();
1713 }
1714
Saurav Das018605f2017-02-18 14:05:44 -08001715 if (!mastershipService.isLocalMaster(device.id())) {
1716 log.debug("Not master for dev:{} .. not handling port updated event"
1717 + "for port {}", device.id(), port.number());
1718 return;
1719 }
Saurav Das41b49a92018-04-27 18:42:30 -07001720 processPortUpdated(device.id(), port);
1721 }
Saurav Das018605f2017-02-18 14:05:44 -08001722
Saurav Das41b49a92018-04-27 18:42:30 -07001723 /**
1724 * Adds or remove filtering rules for the given switchport. If switchport is
1725 * an edge facing port, additionally handles host probing and broadcast
1726 * rules. Must be called by local master of device.
1727 *
1728 * @param deviceId the device identifier
1729 * @param port the port to update
1730 */
1731 void processPortUpdated(DeviceId deviceId, Port port) {
Saurav Das018605f2017-02-18 14:05:44 -08001732 // first we handle filtering rules associated with the port
1733 if (port.isEnabled()) {
1734 log.info("Switchport {}/{} enabled..programming filters",
Saurav Das41b49a92018-04-27 18:42:30 -07001735 deviceId, port.number());
1736 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001737 } else {
1738 log.info("Switchport {}/{} disabled..removing filters",
Saurav Das41b49a92018-04-27 18:42:30 -07001739 deviceId, port.number());
1740 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001741 }
Saurav Das1a129a02016-11-18 15:21:57 -08001742
1743 // portUpdated calls are for ports that have gone down or up. For switch
1744 // to switch ports, link-events should take care of any re-routing or
1745 // group editing necessary for port up/down. Here we only process edge ports
1746 // that are already configured.
Saurav Das41b49a92018-04-27 18:42:30 -07001747 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
Charles Chanb30213c2018-05-01 11:50:20 -07001748 VlanId untaggedVlan = interfaceService.getUntaggedVlanId(cp);
1749 VlanId nativeVlan = interfaceService.getNativeVlanId(cp);
1750 Set<VlanId> taggedVlans = interfaceService.getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001751
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001752 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001753 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das41b49a92018-04-27 18:42:30 -07001754 + "dev/port: {}/{}", deviceId, port.number());
Saurav Das1a129a02016-11-18 15:21:57 -08001755 return;
1756 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001757 if (untaggedVlan != null) {
Saurav Das41b49a92018-04-27 18:42:30 -07001758 processEdgePort(deviceId, port, untaggedVlan, true);
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001759 }
1760 if (nativeVlan != null) {
Saurav Das41b49a92018-04-27 18:42:30 -07001761 processEdgePort(deviceId, port, nativeVlan, true);
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001762 }
1763 if (!taggedVlans.isEmpty()) {
Saurav Das41b49a92018-04-27 18:42:30 -07001764 taggedVlans.forEach(tag -> processEdgePort(deviceId, port, tag, false));
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001765 }
Saurav Das1a129a02016-11-18 15:21:57 -08001766 }
1767
Saurav Das41b49a92018-04-27 18:42:30 -07001768 private void processEdgePort(DeviceId deviceId, Port port, VlanId vlanId,
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001769 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001770 boolean portUp = port.isEnabled();
1771 if (portUp) {
Saurav Das41b49a92018-04-27 18:42:30 -07001772 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", deviceId,
Charles Chan59cc16d2017-02-02 16:20:42 -08001773 port.number(), vlanId);
Charles Chana03ab8b2018-04-29 18:11:37 -07001774 hostEventExecutor.execute(() -> hostHandler.processPortUp(new ConnectPoint(deviceId, port.number())));
Saurav Das1a129a02016-11-18 15:21:57 -08001775 } else {
Saurav Das41b49a92018-04-27 18:42:30 -07001776 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", deviceId,
Charles Chan59cc16d2017-02-02 16:20:42 -08001777 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001778 }
1779
Saurav Das41b49a92018-04-27 18:42:30 -07001780 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001781 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001782 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001783 } else {
1784 log.warn("Group handler not found for dev:{}. Not handling edge port"
Saurav Das41b49a92018-04-27 18:42:30 -07001785 + " {} event for port:{}", deviceId,
Saurav Das1a129a02016-11-18 15:21:57 -08001786 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001787 }
1788 }
sangho1e575652015-05-14 00:39:53 -07001789
Charles Chan27fe1a52017-10-20 16:06:55 -07001790 private void createOrUpdateDeviceConfiguration() {
1791 if (deviceConfiguration == null) {
Saurav Dase7f51012018-02-09 17:26:45 -08001792 log.info("Creating new DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001793 deviceConfiguration = new DeviceConfiguration(this);
1794 } else {
Saurav Dase7f51012018-02-09 17:26:45 -08001795 log.info("Updating DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001796 deviceConfiguration.updateConfig();
1797 }
1798 }
1799
Charles Chana8487b02018-04-18 18:41:05 -07001800 private void createOrUpdateDefaultRoutingHandler() {
1801 if (defaultRoutingHandler == null) {
1802 log.info("Creating new DefaultRoutingHandler");
1803 defaultRoutingHandler = new DefaultRoutingHandler(this);
1804 } else {
1805 log.info("Updating DefaultRoutingHandler");
1806 defaultRoutingHandler.update(this);
1807 }
1808 }
1809
Pier Ventre10bd8d12016-11-26 21:05:22 -08001810 /**
1811 * Registers the given connect point with the NRS, this is necessary
1812 * to receive the NDP and ARP packets from the NRS.
1813 *
1814 * @param portToRegister connect point to register
1815 */
1816 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001817 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001818 portToRegister,
1819 neighbourHandler,
1820 appId
1821 );
1822 }
1823
Charles Chand6832882015-10-05 17:50:33 -07001824 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001825 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001826 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001827
Charles Chane849c192016-01-11 18:28:54 -08001828 /**
1829 * Constructs the internal network config listener.
1830 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001831 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001832 */
Charles Chan65238242017-06-22 18:03:14 -07001833 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001834 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001835 }
1836
Charles Chane849c192016-01-11 18:28:54 -08001837 /**
1838 * Reads network config and initializes related data structure accordingly.
1839 */
Charles Chan47933752017-11-30 15:37:50 -08001840 void configureNetwork() {
Saurav Dase7f51012018-02-09 17:26:45 -08001841 log.info("Configuring network ...");
Andrea Campanellaab482f92018-04-17 12:09:34 +02001842
1843 // Setting handling of network configuration events completable future
1844 // The completable future is needed because of the async behaviour of the configureNetwork,
1845 // listener registration and event arrival
1846 // Enables us to buffer the events and execute them when the configure network is done.
Ray Milkeyeadad4a2018-11-16 15:15:14 -08001847 synchronized (networkConfigCompletionLock) {
1848 networkConfigCompletion = new CompletableFuture<>();
Andrea Campanellaab482f92018-04-17 12:09:34 +02001849
Ray Milkeyeadad4a2018-11-16 15:15:14 -08001850 // add a small delay to absorb multiple network config added notifications
1851 if (!programmingScheduled.get()) {
1852 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1853 programmingScheduled.set(true);
1854 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1855 }
1856
1857 createOrUpdateDeviceConfiguration();
1858
1859 arpHandler = new ArpHandler(srManager);
1860 icmpHandler = new IcmpHandler(srManager);
1861 ipHandler = new IpHandler(srManager);
1862 routingRulePopulator = new RoutingRulePopulator(srManager);
1863 createOrUpdateDefaultRoutingHandler();
1864
1865 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1866 groupHandlerMap, tunnelStore);
1867 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1868 flowObjectiveService,
1869 tunnelHandler, policyStore);
1870 networkConfigCompletion.complete(true);
Andrea Campanellaab482f92018-04-17 12:09:34 +02001871 }
1872
Charles Chan2199c302016-04-23 17:36:10 -07001873 mcastHandler.init();
Andrea Campanellaab482f92018-04-17 12:09:34 +02001874
Charles Chan4636be02015-10-07 14:21:45 -07001875 }
1876
Charles Chand6832882015-10-05 17:50:33 -07001877 @Override
1878 public void event(NetworkConfigEvent event) {
Ray Milkey59bc0722018-05-11 09:59:19 -07001879 if (mainEventExecutor == null) {
1880 return;
1881 }
Charles Chan7a888e82018-03-21 16:57:47 -07001882 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
1883 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1884 switch (event.type()) {
1885 case CONFIG_ADDED:
1886 case CONFIG_UPDATED:
1887 case CONFIG_REMOVED:
1888 log.trace("Schedule Network Config event {}", event);
Andrea Campanellaab482f92018-04-17 12:09:34 +02001889 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1890 mainEventExecutor.execute(new InternalEventHandler(event));
1891 } else {
Charles Chane437a802018-04-21 00:44:29 -07001892 queuedEvents.add(event);
Andrea Campanellaab482f92018-04-17 12:09:34 +02001893 }
Charles Chan7a888e82018-03-21 16:57:47 -07001894 break;
1895 default:
1896 break;
Charles Chand6832882015-10-05 17:50:33 -07001897 }
1898 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001899
Charles Chan50443e82018-01-03 16:26:32 -08001900 @Override
1901 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase7f51012018-02-09 17:26:45 -08001902 if (event.type() == CONFIG_REGISTERED ||
1903 event.type() == CONFIG_UNREGISTERED) {
1904 log.debug("Ignore event {} due to type mismatch", event);
1905 return false;
Charles Chan50443e82018-01-03 16:26:32 -08001906 }
Saurav Dase7f51012018-02-09 17:26:45 -08001907
1908 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1909 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1910 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscd339592018-02-23 14:18:00 -08001911 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase7f51012018-02-09 17:26:45 -08001912 log.debug("Ignore event {} due to class mismatch", event);
1913 return false;
1914 }
1915
1916 return true;
Charles Chan50443e82018-01-03 16:26:32 -08001917 }
1918
Saurav Das7bcbe702017-06-13 15:35:54 -07001919 private final class ConfigChange implements Runnable {
1920 @Override
1921 public void run() {
1922 programmingScheduled.set(false);
Saurav Dase7f51012018-02-09 17:26:45 -08001923 log.info("Reacting to config changes after buffer delay");
Saurav Das7bcbe702017-06-13 15:35:54 -07001924 for (Device device : deviceService.getDevices()) {
1925 processDeviceAdded(device);
1926 }
1927 defaultRoutingHandler.startPopulationProcess();
1928 }
1929 }
Charles Chand6832882015-10-05 17:50:33 -07001930 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001931
Charles Chan7a888e82018-03-21 16:57:47 -07001932 private class InternalLinkListener implements LinkListener {
1933 @Override
1934 public void event(LinkEvent event) {
Ray Milkey59bc0722018-05-11 09:59:19 -07001935 if (mainEventExecutor == null) {
1936 return;
1937 }
Charles Chan7a888e82018-03-21 16:57:47 -07001938 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1939 event.type() == LinkEvent.Type.LINK_UPDATED ||
1940 event.type() == LinkEvent.Type.LINK_REMOVED) {
1941 log.trace("Schedule Link event {}", event);
Andrea Campanellaab482f92018-04-17 12:09:34 +02001942 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1943 mainEventExecutor.execute(new InternalEventHandler(event));
1944 } else {
Charles Chane437a802018-04-21 00:44:29 -07001945 queuedEvents.add(event);
Andrea Campanellaab482f92018-04-17 12:09:34 +02001946 }
Charles Chan7a888e82018-03-21 16:57:47 -07001947 }
1948 }
1949 }
1950
1951 private class InternalDeviceListener implements DeviceListener {
1952 @Override
1953 public void event(DeviceEvent event) {
Ray Milkey59bc0722018-05-11 09:59:19 -07001954 if (mainEventExecutor == null) {
1955 return;
1956 }
Charles Chan7a888e82018-03-21 16:57:47 -07001957 switch (event.type()) {
1958 case DEVICE_ADDED:
1959 case PORT_UPDATED:
1960 case PORT_ADDED:
1961 case DEVICE_UPDATED:
1962 case DEVICE_AVAILABILITY_CHANGED:
1963 log.trace("Schedule Device event {}", event);
Andrea Campanellaab482f92018-04-17 12:09:34 +02001964 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1965 mainEventExecutor.execute(new InternalEventHandler(event));
1966 } else {
Charles Chane437a802018-04-21 00:44:29 -07001967 queuedEvents.add(event);
Andrea Campanellaab482f92018-04-17 12:09:34 +02001968 }
Charles Chan7a888e82018-03-21 16:57:47 -07001969 break;
1970 default:
1971 }
1972 }
1973 }
1974
1975 private class InternalTopologyListener implements TopologyListener {
1976 @Override
1977 public void event(TopologyEvent event) {
Ray Milkey59bc0722018-05-11 09:59:19 -07001978 if (mainEventExecutor == null) {
1979 return;
1980 }
Charles Chan7a888e82018-03-21 16:57:47 -07001981 switch (event.type()) {
1982 case TOPOLOGY_CHANGED:
1983 log.trace("Schedule Topology event {}", event);
Andrea Campanellaab482f92018-04-17 12:09:34 +02001984 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1985 mainEventExecutor.execute(new InternalEventHandler(event));
1986 } else {
Charles Chane437a802018-04-21 00:44:29 -07001987 queuedEvents.add(event);
Andrea Campanellaab482f92018-04-17 12:09:34 +02001988 }
Charles Chan7a888e82018-03-21 16:57:47 -07001989 break;
1990 default:
1991 }
1992 }
1993 }
1994
Charles Chan68aa62d2015-11-09 16:37:23 -08001995 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001996 @Override
1997 public void event(HostEvent event) {
Ray Milkey59bc0722018-05-11 09:59:19 -07001998 if (hostEventExecutor == null) {
1999 return;
2000 }
Charles Chan68aa62d2015-11-09 16:37:23 -08002001 switch (event.type()) {
2002 case HOST_ADDED:
Charles Chan68aa62d2015-11-09 16:37:23 -08002003 case HOST_MOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08002004 case HOST_REMOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08002005 case HOST_UPDATED:
Charles Chan7a888e82018-03-21 16:57:47 -07002006 log.trace("Schedule Host event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07002007 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chan68aa62d2015-11-09 16:37:23 -08002008 break;
2009 default:
2010 log.warn("Unsupported host event type: {}", event.type());
2011 break;
2012 }
2013 }
2014 }
2015
Charles Chand55e84d2016-03-30 17:54:24 -07002016 private class InternalMcastListener implements McastListener {
2017 @Override
2018 public void event(McastEvent event) {
Ray Milkey59bc0722018-05-11 09:59:19 -07002019 if (mcastEventExecutor == null) {
2020 return;
2021 }
Charles Chand55e84d2016-03-30 17:54:24 -07002022 switch (event.type()) {
Pier3ee24552018-03-14 16:47:32 -07002023 case SOURCES_ADDED:
2024 case SOURCES_REMOVED:
2025 case SINKS_ADDED:
2026 case SINKS_REMOVED:
Charles Chand55e84d2016-03-30 17:54:24 -07002027 case ROUTE_REMOVED:
Pier4694e092018-04-17 16:29:56 +02002028 case ROUTE_ADDED:
Charles Chan7a888e82018-03-21 16:57:47 -07002029 log.trace("Schedule Mcast event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07002030 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi9930da52018-02-02 16:19:11 +01002031 break;
Charles Chand55e84d2016-03-30 17:54:24 -07002032 default:
Charles Chan7a888e82018-03-21 16:57:47 -07002033 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chand55e84d2016-03-30 17:54:24 -07002034 break;
2035 }
2036 }
2037 }
Charles Chan35fd1a72016-06-13 18:54:31 -07002038
Charles Chan03a73e02016-10-24 14:52:01 -07002039 private class InternalRouteEventListener implements RouteListener {
2040 @Override
2041 public void event(RouteEvent event) {
Ray Milkey59bc0722018-05-11 09:59:19 -07002042 if (routeEventExecutor == null) {
2043 return;
2044 }
Charles Chan03a73e02016-10-24 14:52:01 -07002045 switch (event.type()) {
2046 case ROUTE_ADDED:
Charles Chan03a73e02016-10-24 14:52:01 -07002047 case ROUTE_UPDATED:
Charles Chan03a73e02016-10-24 14:52:01 -07002048 case ROUTE_REMOVED:
Charles Chan2fde6d42017-08-23 14:46:43 -07002049 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan7a888e82018-03-21 16:57:47 -07002050 log.trace("Schedule Route event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07002051 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan2fde6d42017-08-23 14:46:43 -07002052 break;
Charles Chan03a73e02016-10-24 14:52:01 -07002053 default:
Charles Chan7a888e82018-03-21 16:57:47 -07002054 log.warn("Unsupported route event type: {}", event.type());
Charles Chan03a73e02016-10-24 14:52:01 -07002055 break;
2056 }
2057 }
2058 }
Saurav Dasc88d4662017-05-15 15:34:25 -07002059
Charles Chana8487b02018-04-18 18:41:05 -07002060 private class InternalMastershipListener implements MastershipListener {
2061 @Override
2062 public void event(MastershipEvent event) {
Ray Milkey59bc0722018-05-11 09:59:19 -07002063 if (mainEventExecutor == null) {
2064 return;
2065 }
Charles Chana8487b02018-04-18 18:41:05 -07002066 switch (event.type()) {
Saurav Dasc6dc1772018-04-21 17:19:48 -07002067 case MASTER_CHANGED:
2068 log.debug("Mastership event: {}/{}", event.subject(),
2069 event.roleInfo());
2070 mainEventExecutor.execute(new InternalEventHandler(event));
2071 break;
2072 case BACKUPS_CHANGED:
2073 case SUSPENDED:
2074 default:
2075 log.debug("Mastership event type {} not handled", event.type());
2076 break;
Charles Chana8487b02018-04-18 18:41:05 -07002077 }
2078 }
2079 }
2080
Saurav Dasc6dc1772018-04-21 17:19:48 -07002081 class InternalClusterListener implements ClusterEventListener {
2082 private Instant lastClusterEvent = Instant.EPOCH;
2083
2084 long timeSinceLastClusterEvent() {
2085 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
2086 }
2087
2088 @Override
2089 public void event(ClusterEvent event) {
2090 switch (event.type()) {
2091 case INSTANCE_ACTIVATED:
2092 case INSTANCE_ADDED:
2093 case INSTANCE_READY:
2094 log.debug("Cluster event {} ignored", event.type());
2095 break;
2096 case INSTANCE_DEACTIVATED:
2097 case INSTANCE_REMOVED:
2098 log.info("** Cluster event {}", event.type());
2099 lastClusterEvent = Instant.now();
2100 break;
2101 default:
2102 break;
2103 }
2104
2105 }
2106
2107 }
2108
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002109 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
2110 try {
2111 Set<Interface> intfs = conf.getInterfaces();
2112 Set<Interface> prevIntfs = prevConf.getInterfaces();
2113
2114 // Now we only handle one interface config at each port.
2115 if (intfs.size() != 1 || prevIntfs.size() != 1) {
2116 log.warn("Interface update aborted - one at a time is allowed, " +
2117 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
2118 return;
2119 }
2120
Andrea Campanellaab482f92018-04-17 12:09:34 +02002121 //The system is in an incoherent state, abort
2122 if (defaultRoutingHandler == null) {
2123 log.warn("Interface update aborted, defaultRoutingHandler is null");
2124 return;
2125 }
2126
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002127 Interface intf = intfs.stream().findFirst().get();
2128 Interface prevIntf = prevIntfs.stream().findFirst().get();
2129
2130 DeviceId deviceId = intf.connectPoint().deviceId();
2131 PortNumber portNum = intf.connectPoint().port();
2132
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002133 removeSubnetConfig(prevIntf.connectPoint(),
2134 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
2135 new HashSet<>(intf.ipAddressesList())));
2136
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002137 if (!prevIntf.vlanNative().equals(VlanId.NONE)
2138 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
2139 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
2140 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
2141 // Update filtering objective and L2IG group bucket
2142 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
2143 } else {
2144 // RemoveVlanNative
2145 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
2146 }
2147 }
2148
2149 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
2150 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2151 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2152 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
2153 // Update filtering objective and L2IG group bucket
2154 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
2155 } else {
2156 // RemoveVlanUntagged
2157 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
2158 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002159 }
2160
2161 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2162 // RemoveVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002163 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
2164 .filter(i -> !intf.vlanUntagged().equals(i))
2165 .filter(i -> !intf.vlanNative().equals(i))
2166 .forEach(vlanId -> updateVlanConfigInternal(
2167 deviceId, portNum, vlanId, false, false));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002168 }
2169
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002170 if (!intf.vlanNative().equals(VlanId.NONE)
2171 && !prevIntf.vlanNative().equals(intf.vlanNative())
2172 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2173 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
2174 // Update filtering objective and L2IG group bucket
2175 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
2176 } else {
2177 // AddVlanNative
2178 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
2179 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002180 }
2181
2182 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2183 // AddVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002184 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
2185 .filter(i -> !prevIntf.vlanUntagged().equals(i))
2186 .filter(i -> !prevIntf.vlanNative().equals(i))
2187 .forEach(vlanId -> updateVlanConfigInternal(
2188 deviceId, portNum, vlanId, false, true)
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002189 );
2190 }
2191
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002192 if (!intf.vlanUntagged().equals(VlanId.NONE)
2193 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2194 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
2195 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
2196 // Update filtering objective and L2IG group bucket
2197 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
2198 } else {
2199 // AddVlanUntagged
2200 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
2201 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002202 }
2203 addSubnetConfig(prevIntf.connectPoint(),
2204 Sets.difference(new HashSet<>(intf.ipAddressesList()),
2205 new HashSet<>(prevIntf.ipAddressesList())));
2206 } catch (ConfigException e) {
2207 log.error("Error in configuration");
2208 }
2209 }
2210
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002211 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
2212 VlanId vlanId, boolean pushVlan) {
2213 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2214 if (grpHandler == null) {
2215 log.warn("Failed to retrieve group handler for device {}", deviceId);
2216 return;
2217 }
2218
2219 // Update filtering objective for a single port
2220 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
2221 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
2222
2223 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
2224 // Update L2IG bucket of the port
2225 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
2226 } else {
2227 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2228 }
2229 }
2230
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002231 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
2232 VlanId vlanId, boolean pushVlan, boolean install) {
2233 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2234 if (grpHandler == null) {
2235 log.warn("Failed to retrieve group handler for device {}", deviceId);
2236 return;
2237 }
2238
2239 // Update filtering objective for a single port
2240 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
2241
2242 // Update filtering objective for multicast ingress port
2243 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
2244
2245 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
2246
2247 if (nextId != -1 && !install) {
2248 // Update next objective for a single port as an output port
2249 // Remove a single port from L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002250 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002251 // Remove L2 Bridging rule and L3 Unicast rule to the host
Charles Chana03ab8b2018-04-29 18:11:37 -07002252 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2253 vlanId, pushVlan, install));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002254 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
2255 // only if there is no port configured on that VLAN ID
2256 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
2257 // Remove broadcast forwarding rule for the VLAN
2258 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2259 // Remove L2FG for VLAN
2260 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
2261 } else {
2262 // Remove L2IG of the port
2263 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
2264 }
2265 } else if (install) {
2266 if (nextId != -1) {
2267 // Add a single port to L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002268 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002269 } else {
2270 // Create L2FG for VLAN
2271 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
2272 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2273 }
Charles Chana03ab8b2018-04-29 18:11:37 -07002274 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2275 vlanId, pushVlan, install));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002276 } else {
2277 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2278 }
2279 }
2280
2281 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2282 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2283 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2284
2285 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2286 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2287 .filter(intf -> !intf.connectPoint().equals(cp))
2288 .flatMap(intf -> intf.ipAddressesList().stream())
2289 .collect(Collectors.toSet());
2290 // 1. Partial subnet population
2291 // Remove routing rules for removed subnet from previous configuration,
2292 // which does not also exist in other interfaces in the same device
2293 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2294 .map(InterfaceIpAddress::subnetAddress)
2295 .collect(Collectors.toSet());
2296
2297 defaultRoutingHandler.revokeSubnet(
2298 ipPrefixSet.stream()
2299 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2300 .collect(Collectors.toSet()));
2301
2302 // 2. Interface IP punts
2303 // Remove IP punts for old Intf address
2304 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2305 .map(InterfaceIpAddress::ipAddress)
2306 .collect(Collectors.toSet());
2307 ipAddressSet.stream()
2308 .map(InterfaceIpAddress::ipAddress)
2309 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2310 .forEach(interfaceIpAddress ->
2311 routingRulePopulator.revokeSingleIpPunts(
2312 cp.deviceId(), interfaceIpAddress));
2313
2314 // 3. Host unicast routing rule
2315 // Remove unicast routing rule
Charles Chana03ab8b2018-04-29 18:11:37 -07002316 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002317 }
2318
2319 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2320 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2321 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2322
2323 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2324 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2325 .filter(intf -> !intf.connectPoint().equals(cp))
2326 .flatMap(intf -> intf.ipAddressesList().stream())
2327 .collect(Collectors.toSet());
2328 // 1. Partial subnet population
2329 // Add routing rules for newly added subnet, which does not also exist in
2330 // other interfaces in the same device
2331 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2332 .map(InterfaceIpAddress::subnetAddress)
2333 .collect(Collectors.toSet());
2334
2335 defaultRoutingHandler.populateSubnet(
2336 Collections.singleton(cp),
2337 ipPrefixSet.stream()
2338 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2339 .collect(Collectors.toSet()));
2340
2341 // 2. Interface IP punts
2342 // Add IP punts for new Intf address
2343 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2344 .map(InterfaceIpAddress::ipAddress)
2345 .collect(Collectors.toSet());
2346 ipAddressSet.stream()
2347 .map(InterfaceIpAddress::ipAddress)
2348 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2349 .forEach(interfaceIpAddress ->
2350 routingRulePopulator.populateSingleIpPunts(
2351 cp.deviceId(), interfaceIpAddress));
2352
2353 // 3. Host unicast routing rule
2354 // Add unicast routing rule
Charles Chana03ab8b2018-04-29 18:11:37 -07002355 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002356 }
sanghob35a6192015-04-01 13:05:26 -07002357}