blob: 98514b1511585ed2aa20ed0c781f291a20c3aab9 [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();
sanghob35a6192015-04-01 13:05:26 -0700661 log.info("Stopped");
662 }
663
Charles Chan47933752017-11-30 15:37:50 -0800664 @Modified
665 private void modified(ComponentContext context) {
666 Dictionary<?, ?> properties = context.getProperties();
667 if (properties == null) {
668 return;
669 }
670
Andreas Pantelopoulos7bf9e442018-06-18 12:50:35 -0700671 String strActiveProbing = Tools.get(properties, "activeProbing");
672 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProbing);
Charles Chan47933752017-11-30 15:37:50 -0800673 if (expectActiveProbing != activeProbing) {
674 activeProbing = expectActiveProbing;
675 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
676 }
Saurav Das41b49a92018-04-27 18:42:30 -0700677
Mayank Tiwari5b3f4602018-10-29 18:27:35 -0400678 String strSymmetricProving = Tools.get(properties, "symmetricProbing");
679 boolean expectSymmetricProbing = Boolean.parseBoolean(strSymmetricProving);
680 if (expectSymmetricProbing != symmetricProbing) {
681 symmetricProbing = expectSymmetricProbing;
682 log.info("{} symmetric probing", symmetricProbing ? "Enabling" : "Disabling");
683 }
684
Saurav Das41b49a92018-04-27 18:42:30 -0700685 String strSingleHomedDown = Tools.get(properties, "singleHomedDown");
686 boolean expectSingleHomedDown = Boolean.parseBoolean(strSingleHomedDown);
687 if (expectSingleHomedDown != singleHomedDown) {
688 singleHomedDown = expectSingleHomedDown;
689 log.info("{} downing of single homed hosts for lost uplinks",
690 singleHomedDown ? "Enabling" : "Disabling");
691 if (singleHomedDown && linkHandler != null) {
692 hostService.getHosts().forEach(host -> host.locations()
693 .forEach(loc -> {
694 if (interfaceService.isConfigured(loc)) {
695 linkHandler.checkUplinksForHost(loc);
696 }
697 }));
698 } else {
699 log.warn("Disabling singleHomedDown does not re-enable already "
700 + "downed ports for single-homed hosts");
701 }
702 }
Andreas Pantelopoulos7bf9e442018-06-18 12:50:35 -0700703
704 String strRespondToUnknownHosts = Tools.get(properties, "respondToUnknownHosts");
705 boolean expectRespondToUnknownHosts = Boolean.parseBoolean(strRespondToUnknownHosts);
706 if (expectRespondToUnknownHosts != respondToUnknownHosts) {
707 respondToUnknownHosts = expectRespondToUnknownHosts;
708 log.info("{} responding to ARPs/NDPs from unknown hosts", respondToUnknownHosts ? "Enabling" : "Disabling");
709 }
Charles Chan3d44eb92018-07-19 14:55:31 -0700710
Charles Chan5b9a11a2018-07-24 16:40:35 -0700711 String strRouteDoubleTaggedHosts = Tools.get(properties, "routeDoubleTaggedHosts");
712 boolean expectRouteDoubleTaggedHosts = Boolean.parseBoolean(strRouteDoubleTaggedHosts);
713 if (expectRouteDoubleTaggedHosts != routeDoubleTaggedHosts) {
714 routeDoubleTaggedHosts = expectRouteDoubleTaggedHosts;
715 log.info("{} routing for double tagged hosts", routeDoubleTaggedHosts ? "Enabling" : "Disabling");
Charles Chan3d44eb92018-07-19 14:55:31 -0700716
Charles Chan5b9a11a2018-07-24 16:40:35 -0700717 if (routeDoubleTaggedHosts) {
Charles Chan3d44eb92018-07-19 14:55:31 -0700718 hostHandler.populateAllDoubleTaggedHost();
719 } else {
720 hostHandler.revokeAllDoubleTaggedHost();
721 }
722 }
Saurav Das3c82f192018-08-13 15:34:26 -0700723
724 String strDefaultInternalVlan = Tools.get(properties, "defaultInternalVlan");
725 int defIntVlan = Integer.parseInt(strDefaultInternalVlan);
726 if (defIntVlan != defaultInternalVlan) {
727 if (canUseVlanId(defIntVlan)) {
728 log.warn("Default internal vlan value changed from {} to {}.. "
729 + "re-programming filtering rules, but NOT any groups already "
730 + "created with the former value", defaultInternalVlan, defIntVlan);
731 VlanId oldDefIntVlan = VlanId.vlanId((short) defaultInternalVlan);
732 defaultInternalVlan = defIntVlan;
733 routingRulePopulator
734 .updateSpecialVlanFilteringRules(true, oldDefIntVlan,
735 VlanId.vlanId((short) defIntVlan));
736 } else {
737 log.warn("Cannot change default internal vlan to unusable "
738 + "value {}", defIntVlan);
739 }
740 }
741
742 String strPwTxpVlan = Tools.get(properties, "pwTransportVlan");
743 int pwTxpVlan = Integer.parseInt(strPwTxpVlan);
744 if (pwTxpVlan != pwTransportVlan) {
745 if (canUseVlanId(pwTxpVlan)) {
746 log.warn("Pseudowire transport vlan value changed from {} to {}.. "
747 + "re-programming filtering rules, but NOT any groups already "
748 + "created with the former value", pwTransportVlan,
749 pwTxpVlan);
750 VlanId oldPwTxpVlan = VlanId.vlanId((short) pwTransportVlan);
751 pwTransportVlan = pwTxpVlan;
752 routingRulePopulator
753 .updateSpecialVlanFilteringRules(false, oldPwTxpVlan,
754 VlanId.vlanId((short) pwTxpVlan));
755 } else {
756 log.warn("Cannot change pseudowire transport vlan to unusable "
757 + "value {}", pwTxpVlan);
758 }
759 }
760
Sneha Prem1da248c2019-04-07 02:06:07 -0400761 String strRouteSimplification = Tools.get(properties, "routeSimplification");
762 boolean expectRouteSimplification = Boolean.parseBoolean(strRouteSimplification);
763 if (expectRouteSimplification != routeSimplification) {
764 routeSimplification = expectRouteSimplification;
765 log.info("{} route simplification", routeSimplification ? "Enabling" : "Disabling");
766 }
767
Saurav Das3c82f192018-08-13 15:34:26 -0700768 }
769
770 /**
771 * Returns true if given vlan id is not being used in the system currently,
772 * either as one of the default system wide vlans or as one of the
773 * configured interface vlans.
774 *
775 * @param vlanId given vlan id
776 * @return true if vlan is not currently in use
777 */
778 public boolean canUseVlanId(int vlanId) {
779 if (vlanId >= 4095 || vlanId <= 1) {
780 log.error("Vlan id {} value is not in valid range 2 <--> 4094",
781 vlanId);
782 return false;
783 }
784
785 VlanId vid = VlanId.vlanId((short) vlanId);
786 if (getDefaultInternalVlan().equals(vid) || getPwTransportVlan().equals(vid)) {
787 log.warn("Vlan id {} value is already in use system-wide. "
788 + "DefaultInternalVlan:{} PwTransportVlan:{} ", vlanId,
789 getDefaultInternalVlan(), getPwTransportVlan());
790 return false;
791 }
792
793 if (interfaceService.inUse(vid)) {
794 log.warn("Vlan id {} value is already in use on a configured "
795 + "interface in the system", vlanId);
796 return false;
797 }
798 return true;
799 }
800
801 /**
802 * Returns the VlanId assigned internally by default to unconfigured ports.
803 *
804 * @return the default internal vlan id
805 */
806 public VlanId getDefaultInternalVlan() {
807 return VlanId.vlanId((short) defaultInternalVlan);
808 }
809
810 /**
811 * Returns the Vlan id used to transport pseudowire traffic across the
812 * network.
813 *
814 * @return the pseudowire transport vlan id
815 */
816 public VlanId getPwTransportVlan() {
817 return VlanId.vlanId((short) pwTransportVlan);
Charles Chan47933752017-11-30 15:37:50 -0800818 }
819
sangho1e575652015-05-14 00:39:53 -0700820 @Override
821 public List<Tunnel> getTunnels() {
822 return tunnelHandler.getTunnels();
823 }
824
825 @Override
sangho71abe1b2015-06-29 14:58:47 -0700826 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
827 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700828 }
829
830 @Override
sangho71abe1b2015-06-29 14:58:47 -0700831 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700832 for (Policy policy: policyHandler.getPolicies()) {
833 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
834 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
835 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
836 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700837 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700838 }
839 }
840 }
sangho71abe1b2015-06-29 14:58:47 -0700841 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700842 }
843
844 @Override
sangho71abe1b2015-06-29 14:58:47 -0700845 public PolicyHandler.Result removePolicy(Policy policy) {
846 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700847 }
848
849 @Override
sangho71abe1b2015-06-29 14:58:47 -0700850 public PolicyHandler.Result createPolicy(Policy policy) {
851 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700852 }
853
854 @Override
855 public List<Policy> getPolicies() {
856 return policyHandler.getPolicies();
857 }
858
Saurav Das59232cf2016-04-27 18:35:50 -0700859 @Override
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700860 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
861 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700862 }
863
864 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800865 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700866 return l2TunnelHandler.getL2Tunnels();
867 }
868
869 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800870 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700871 return l2TunnelHandler.getL2Policies();
872 }
873
Saurav Das5a356042018-04-06 20:16:01 -0700874 @Override
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700875 @Deprecated
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700876 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
877
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700878 // get both added and pending pseudowires
879 List<L2TunnelDescription> pseudowires = new ArrayList<>();
880 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
881 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700882 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700883
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700884 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
885
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700886 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
887 L2TunnelHandler.Result res;
888 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
889 res = addPseudowire(pw);
890 if (res != L2TunnelHandler.Result.SUCCESS) {
891 log.error("Pseudowire with id {} can not be instantiated !", res);
892 retRes = res;
893 }
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700894 }
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700895
896 return retRes;
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700897 }
898
899 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800900 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700901 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700902 }
903
904 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800905 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700906 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700907 }
908
909 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700910 public void rerouteNetwork() {
911 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700912 }
913
Charles Chanc81c45b2016-10-20 17:02:44 -0700914 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800915 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
916 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800917 deviceConfiguration.getRouters().forEach(device ->
918 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700919 return deviceSubnetMap;
920 }
921
Saurav Dasc88d4662017-05-15 15:34:25 -0700922
923 @Override
924 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
925 if (defaultRoutingHandler != null) {
926 return defaultRoutingHandler.getCurrentEmcpSpgMap();
927 } else {
928 return null;
929 }
930 }
931
932 @Override
Charles Chan1fa010c2018-08-19 19:21:46 -0700933 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDstNextObjStore() {
Saurav Das7bcbe702017-06-13 15:35:54 -0700934 if (dsNextObjStore != null) {
935 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700936 } else {
937 return ImmutableMap.of();
938 }
939 }
940
Saurav Dasceccf242017-08-03 18:30:35 -0700941 @Override
Charles Chan1fa010c2018-08-19 19:21:46 -0700942 public ImmutableMap<VlanNextObjectiveStoreKey, Integer> getVlanNextObjStore() {
943 if (vlanNextObjStore != null) {
944 return ImmutableMap.copyOf(vlanNextObjStore.entrySet());
945 } else {
946 return ImmutableMap.of();
947 }
948 }
949
950 @Override
Ruchi Sahota5d800282019-01-28 01:08:18 +0000951 public ImmutableMap<MacVlanNextObjectiveStoreKey, Integer> getMacVlanNextObjStore() {
952 if (macVlanNextObjStore != null) {
953 return ImmutableMap.copyOf(macVlanNextObjStore.entrySet());
954 } else {
955 return ImmutableMap.of();
956 }
957 }
958
959 @Override
Charles Chan1fa010c2018-08-19 19:21:46 -0700960 public ImmutableMap<PortNextObjectiveStoreKey, Integer> getPortNextObjStore() {
961 if (portNextObjStore != null) {
962 return ImmutableMap.copyOf(portNextObjStore.entrySet());
963 } else {
964 return ImmutableMap.of();
965 }
966 }
967
968 @Override
969 public ImmutableMap<String, NextObjective> getPwInitNext() {
970 if (l2TunnelHandler != null) {
971 return l2TunnelHandler.getInitNext();
972 } else {
973 return ImmutableMap.of();
974 }
975 }
976
977 @Override
978 public ImmutableMap<String, NextObjective> getPwTermNext() {
979 if (l2TunnelHandler != null) {
980 return l2TunnelHandler.getTermNext();
981 } else {
982 return ImmutableMap.of();
983 }
984 }
985
986 @Override
987 public void invalidateNextObj(int nextId) {
988 if (dsNextObjStore != null) {
989 dsNextObjStore.entrySet().forEach(e -> {
990 if (e.getValue().nextId() == nextId) {
991 dsNextObjStore.remove(e.getKey());
992 }
993 });
994 }
995 if (vlanNextObjStore != null) {
996 vlanNextObjStore.entrySet().forEach(e -> {
997 if (e.getValue() == nextId) {
998 vlanNextObjStore.remove(e.getKey());
999 }
1000 });
1001 }
Ruchi Sahota5d800282019-01-28 01:08:18 +00001002 if (macVlanNextObjStore != null) {
1003 macVlanNextObjStore.entrySet().forEach(e -> {
1004 if (e.getValue() == nextId) {
1005 macVlanNextObjStore.remove(e.getKey());
1006 }
1007 });
1008 }
Charles Chan1fa010c2018-08-19 19:21:46 -07001009 if (portNextObjStore != null) {
1010 portNextObjStore.entrySet().forEach(e -> {
1011 if (e.getValue() == nextId) {
1012 portNextObjStore.remove(e.getKey());
1013 }
1014 });
1015 }
1016 if (mcastHandler != null) {
1017 mcastHandler.removeNextId(nextId);
1018 }
1019 if (l2TunnelHandler != null) {
1020 l2TunnelHandler.removeNextId(nextId);
1021 }
1022 if (xconnectService != null) {
1023 xconnectService.removeNextId(nextId);
1024 }
1025 }
1026
1027 @Override
Saurav Dasceccf242017-08-03 18:30:35 -07001028 public void verifyGroups(DeviceId id) {
1029 DefaultGroupHandler gh = groupHandlerMap.get(id);
1030 if (gh != null) {
1031 gh.triggerBucketCorrector();
1032 }
1033 }
1034
Saurav Dasc568c342018-01-25 09:49:01 -08001035 @Override
1036 public ImmutableMap<Link, Boolean> getSeenLinks() {
1037 return linkHandler.getSeenLinks();
1038 }
1039
1040 @Override
1041 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
1042 return linkHandler.getDownedPorts();
1043 }
1044
Pier Luigib29144d2018-01-15 18:06:43 +01001045 @Override
1046 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
Charles Chan1fa010c2018-08-19 19:21:46 -07001047 return mcastHandler.getNextIds(mcastIp);
Pier Luigib29144d2018-01-15 18:06:43 +01001048 }
1049
1050 @Override
Pier Luigi96fe0772018-02-28 12:10:50 +01001051 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigib29144d2018-01-15 18:06:43 +01001052 return mcastHandler.getMcastRoles(mcastIp);
1053 }
1054
1055 @Override
Pier7631c492018-04-19 16:47:06 +02001056 public Map<McastRoleStoreKey, McastRole> getMcastRoles(IpAddress mcastIp, ConnectPoint sourcecp) {
1057 return mcastHandler.getMcastRoles(mcastIp, sourcecp);
1058 }
1059
1060 @Override
Pier Luigib29144d2018-01-15 18:06:43 +01001061 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
1062 return mcastHandler.getMcastPaths(mcastIp);
1063 }
1064
Pier4694e092018-04-17 16:29:56 +02001065 @Override
Piere7e8e3c2018-04-17 17:25:22 +02001066 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
1067 ConnectPoint sourcecp) {
1068 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
1069 }
1070
1071 @Override
Pier4694e092018-04-17 16:29:56 +02001072 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
1073 return mcastHandler.getMcastLeaders(mcastIp);
1074 }
1075
Charles Chan09879082018-04-17 18:56:53 -07001076 @Override
1077 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
1078 return defaultRoutingHandler == null ? ImmutableMap.of() :
1079 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
1080 }
1081
1082 @Override
1083 public Map<DeviceId, Boolean> getShouldProgramCache() {
1084 return defaultRoutingHandler == null ? ImmutableMap.of() :
1085 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
1086 }
1087
Charles Chan4e87b3e2018-06-19 20:31:57 -07001088 @Override
jayakumarthazhath46945b62018-10-01 00:51:54 +05301089 public boolean shouldProgram(DeviceId deviceId) {
1090 return defaultRoutingHandler.shouldProgram(deviceId);
1091 }
1092
1093 @Override
Ray Milkeye4afdb52017-04-05 09:42:04 -07001094 public ApplicationId appId() {
1095 return appId;
1096 }
1097
1098 /**
1099 * Returns the device configuration.
1100 *
1101 * @return device configuration
1102 */
1103 public DeviceConfiguration deviceConfiguration() {
1104 return deviceConfiguration;
1105 }
1106
1107 /**
Saurav Das7bcbe702017-06-13 15:35:54 -07001108 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -07001109 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -07001110 *
1111 * @return next objective ID store
1112 */
Saurav Das7bcbe702017-06-13 15:35:54 -07001113 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
1114 dsNextObjStore() {
1115 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -07001116 }
1117
1118 /**
Charles Chan5bc32632017-08-22 15:07:34 -07001119 * Per device next objective ID store with (device id + vlanid) as key.
1120 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -07001121 *
1122 * @return vlan next object store
1123 */
1124 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
1125 return vlanNextObjStore;
1126 }
1127
1128 /**
Ruchi Sahota5d800282019-01-28 01:08:18 +00001129 * Per device next objective ID store with (device id + MAC address + vlan) as key.
1130 * Used to keep track on L3 Unicast group information for indirect hosts.
1131 *
1132 * @return mac vlan next object store
1133 */
1134 public EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer> macVlanNextObjStore() {
1135 return macVlanNextObjStore;
1136 }
1137
1138 /**
Charles Chan5bc32632017-08-22 15:07:34 -07001139 * Per device next objective ID store with (device id + port + treatment + meta) as key.
Ruchi Sahota5d800282019-01-28 01:08:18 +00001140 * Used to keep track on L2 interface group and L3 unicast group information for direct hosts.
Ray Milkeye4afdb52017-04-05 09:42:04 -07001141 *
1142 * @return port next object store.
1143 */
1144 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
1145 return portNextObjStore;
1146 }
1147
1148 /**
Jonghwan Hyuna76bf032018-04-09 09:40:50 -07001149 * Per port dummy VLAN ID store with (connect point + ip address) as key.
1150 * Used to keep track on dummy VLAN ID allocation.
1151 *
1152 * @return dummy vlan id store.
1153 */
1154 public EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId> dummyVlanIdStore() {
1155 return dummyVlanIdStore;
1156 }
1157
1158 /**
Saurav Das7bcbe702017-06-13 15:35:54 -07001159 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
1160 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -07001161 *
1162 * @return MPLS-ECMP value
1163 */
1164 public boolean getMplsEcmp() {
1165 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
1166 .getConfig(this.appId, SegmentRoutingAppConfig.class);
1167 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
1168 }
1169
1170 /**
sanghof9d0bf12015-05-19 11:57:42 -07001171 * Returns the tunnel object with the tunnel ID.
1172 *
1173 * @param tunnelId Tunnel ID
1174 * @return Tunnel reference
1175 */
sangho1e575652015-05-14 00:39:53 -07001176 public Tunnel getTunnel(String tunnelId) {
1177 return tunnelHandler.getTunnel(tunnelId);
1178 }
1179
Charles Chan4e87b3e2018-06-19 20:31:57 -07001180 @Override
Pier Luigi96fe0772018-02-28 12:10:50 +01001181 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chanb30213c2018-05-01 11:50:20 -07001182 VlanId untaggedVlanId = interfaceService.getUntaggedVlanId(connectPoint);
1183 VlanId nativeVlanId = interfaceService.getNativeVlanId(connectPoint);
Charles Chanf9a52702017-06-16 15:19:24 -07001184 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
1185 }
1186
Charles Chan4e87b3e2018-06-19 20:31:57 -07001187 @Override
1188 public Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
Charles Chan65238242017-06-22 18:03:14 -07001189 SegmentRoutingDeviceConfig deviceConfig =
1190 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1191 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
1192 }
Charles Chan4e87b3e2018-06-19 20:31:57 -07001193
1194 @Override
Saurav Das41b49a92018-04-27 18:42:30 -07001195 public Optional<PortNumber> getPairLocalPort(DeviceId deviceId) {
Charles Chan65238242017-06-22 18:03:14 -07001196 SegmentRoutingDeviceConfig deviceConfig =
1197 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1198 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
1199 }
1200
1201 /**
Charles Chan9640c812017-08-23 13:55:39 -07001202 * Returns locations of given resolved route.
1203 *
1204 * @param resolvedRoute resolved route
1205 * @return locations of nexthop. Might be empty if next hop is not found
1206 */
Charles Chan022d6672019-04-17 14:20:26 -07001207 public Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
Charles Chan9640c812017-08-23 13:55:39 -07001208 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
1209 return Optional.ofNullable(hostService.getHost(hostId))
1210 .map(Host::locations).orElse(Sets.newHashSet())
1211 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
1212 }
1213
1214 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -08001215 * Returns vlan port map of given device.
1216 *
1217 * @param deviceId device id
1218 * @return vlan-port multimap
1219 */
1220 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
1221 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
1222
1223 interfaceService.getInterfaces().stream()
1224 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
1225 .forEach(intf -> {
1226 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -07001227 intf.vlanTagged().forEach(vlanTagged ->
1228 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
1229 );
Charles Chan7ffd81f2017-02-08 15:52:08 -08001230 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
1231 });
1232 vlanPortMap.removeAll(VlanId.NONE);
1233
1234 return vlanPortMap;
1235 }
1236
1237 /**
Charles Chan59cc16d2017-02-02 16:20:42 -08001238 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -08001239 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -07001240 *
1241 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -08001242 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -08001243 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -07001244 */
Charles Chan65238242017-06-22 18:03:14 -07001245 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -07001246 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -08001247 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
1248 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001249 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -08001250 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -08001251 + "device {} not found", deviceId);
1252 return -1;
1253 }
1254 }
1255
1256 /**
1257 * Returns the next objective ID for the given portNumber, given the treatment.
1258 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -07001259 * would result in different objectives. If the next object does not exist,
1260 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -08001261 *
1262 * @param deviceId Device ID
1263 * @param portNum port number on device for which NextObjective is queried
1264 * @param treatment the actions to apply on the packets (should include outport)
1265 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -07001266 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -07001267 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -08001268 */
1269 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
1270 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -07001271 TrafficSelector meta,
1272 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -08001273 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1274 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -07001275 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -08001276 } else {
Charles Chane849c192016-01-11 18:28:54 -08001277 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
1278 + " not found", deviceId);
1279 return -1;
1280 }
1281 }
1282
Saurav Dasc88d4662017-05-15 15:34:25 -07001283 /**
Ruchi Sahota5d800282019-01-28 01:08:18 +00001284 * Returns the next Objective ID for the given mac and vlan, given the treatment.
1285 * There could be multiple different treatments to the same outport, which
1286 * would result in different objectives. If the next object does not exist,
1287 * and should be created, a new one is created and its id is returned.
1288 *
1289 * @param deviceId Device ID
1290 * @param macAddr mac of host for which Next ID is required.
1291 * @param vlanId vlan of host for which Next ID is required.
Ruchi Sahotab555e592019-05-09 17:26:14 -04001292 * @param port port with which to create the Next Obj.
Ruchi Sahota5d800282019-01-28 01:08:18 +00001293 * @param createIfMissing true if a next object should be created if not found
1294 * @return next objective ID or -1 if an error occurred during retrieval or creation
1295 */
1296 public int getMacVlanNextObjectiveId(DeviceId deviceId, MacAddress macAddr, VlanId vlanId,
Ruchi Sahotab555e592019-05-09 17:26:14 -04001297 PortNumber port, boolean createIfMissing) {
Ruchi Sahota5d800282019-01-28 01:08:18 +00001298 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1299 if (ghdlr != null) {
Ruchi Sahotab555e592019-05-09 17:26:14 -04001300 return ghdlr.getMacVlanNextObjectiveId(macAddr, vlanId, port, createIfMissing);
Ruchi Sahota5d800282019-01-28 01:08:18 +00001301 } else {
1302 log.warn("getMacVlanNextObjectiveId query - groupHandler for device {}"
1303 + " not found", deviceId);
1304 return -1;
1305 }
1306 }
1307
1308 /**
Ruchi Sahota5d800282019-01-28 01:08:18 +00001309 * Updates the next objective for the given nextId .
1310 *
1311 * @param deviceId Device ID
1312 * @param hostMac mac of host for which Next obj is to be updated.
1313 * @param hostVlanId vlan of host for which Next obj is to be updated.
1314 * @param port port with which to update the Next Obj.
1315 * @param nextId of Next Obj which needs to be updated.
1316 */
1317 public void updateMacVlanTreatment(DeviceId deviceId, MacAddress hostMac,
1318 VlanId hostVlanId, PortNumber port, int nextId) {
1319 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1320 if (ghdlr != null) {
1321 ghdlr.updateL3UcastGroupBucket(hostMac, hostVlanId, port, nextId);
1322 } else {
1323 log.warn("updateL3UcastGroupBucket query - groupHandler for device {}"
1324 + " not found", deviceId);
1325 }
1326 }
1327
1328
1329 /**
Saurav Dasc88d4662017-05-15 15:34:25 -07001330 * Returns the group handler object for the specified device id.
1331 *
1332 * @param devId the device identifier
1333 * @return the groupHandler object for the device id, or null if not found
1334 */
Charles Chan65238242017-06-22 18:03:14 -07001335 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001336 return groupHandlerMap.get(devId);
1337 }
1338
1339 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001340 * Returns the default routing handler object.
1341 *
1342 * @return the default routing handler object
1343 */
1344 public DefaultRoutingHandler getRoutingHandler() {
1345 return defaultRoutingHandler;
1346 }
1347
Jonghwan Hyuna76bf032018-04-09 09:40:50 -07001348 /**
1349 * Returns new dummy VLAN ID.
1350 * Dummy VLAN ID should be unique in each connect point.
1351 *
1352 * @param cp connect point
1353 * @param ipAddress IP address
1354 * @return new dummy VLAN ID. Returns VlanId.NONE if no VLAN ID is available.
1355 */
1356 public synchronized VlanId allocateDummyVlanId(ConnectPoint cp, IpAddress ipAddress) {
1357 Set<VlanId> usedVlanId = Sets.union(getVlanPortMap(cp.deviceId()).keySet(),
1358 dummyVlanIdStore.entrySet().stream().filter(entry ->
1359 (entry.getKey()).connectPoint().equals(cp))
1360 .map(Map.Entry::getValue)
1361 .collect(Collectors.toSet()));
1362
1363 VlanId dummyVlanId = IntStream.range(MIN_DUMMY_VLAN_ID, MAX_DUMMY_VLAN_ID).mapToObj(
1364 i -> VlanId.vlanId((short) (i & 0xFFFF))
1365 ).filter(vlanId -> !usedVlanId.contains(vlanId)).findFirst().orElse(VlanId.NONE);
1366
1367 if (!dummyVlanId.equals(VlanId.NONE)) {
1368 this.dummyVlanIdStore.put(new DummyVlanIdStoreKey(cp, ipAddress), dummyVlanId);
1369 log.debug("Dummy VLAN ID {} is allocated to {}, {}", dummyVlanId, cp, ipAddress);
1370 } else {
1371 log.error("Failed to allocate dummy VLAN ID for {}, {}", cp, ipAddress);
1372 }
1373 return dummyVlanId;
1374 }
1375
1376
sanghob35a6192015-04-01 13:05:26 -07001377 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -07001378 @Override
1379 public void process(PacketContext context) {
Charles Chan595fdc52018-05-08 21:35:50 -07001380 packetExecutor.execute(() -> processPacketInternal(context));
1381 }
sanghob35a6192015-04-01 13:05:26 -07001382
Charles Chan595fdc52018-05-08 21:35:50 -07001383 private void processPacketInternal(PacketContext context) {
sanghob35a6192015-04-01 13:05:26 -07001384 if (context.isHandled()) {
1385 return;
1386 }
1387
1388 InboundPacket pkt = context.inPacket();
1389 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -08001390
1391 if (ethernet == null) {
1392 return;
1393 }
1394
Saurav Das7bcbe702017-06-13 15:35:54 -07001395 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1396 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001397 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001398 log.warn("Received unexpected ARP packet on {}",
1399 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -07001400 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -08001401 return;
sanghob35a6192015-04-01 13:05:26 -07001402 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -08001403 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1404 //ipHandler.addToPacketBuffer(ipv4Packet);
1405 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1406 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001407 } else {
Charles Chan50035632017-01-13 17:20:44 -08001408 // NOTE: We don't support IP learning at this moment so this
1409 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -08001410 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001411 }
Pier Ventre10bd8d12016-11-26 21:05:22 -08001412 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1413 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -08001414 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -08001415 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -08001416 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1417 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1418 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1419 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1420 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1421 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -07001422 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -07001423 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -08001424 }
1425 } else {
1426 // NOTE: We don't support IP learning at this moment so this
1427 // is not necessary. Also it causes duplication of DHCPv6 packets.
1428 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1429 }
sanghob35a6192015-04-01 13:05:26 -07001430 }
1431 }
1432 }
1433
sanghob35a6192015-04-01 13:05:26 -07001434 private class InternalEventHandler implements Runnable {
Charles Chan9b7217c2018-04-05 16:31:15 -07001435 private Event event;
1436
1437 InternalEventHandler(Event event) {
1438 this.event = event;
1439 }
1440
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001441 @Override
sanghob35a6192015-04-01 13:05:26 -07001442 public void run() {
Charles Chan9b7217c2018-04-05 16:31:15 -07001443 try {
1444 // TODO We should also change SR routing and PW to listen to TopologyEvents
1445 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1446 event.type() == LinkEvent.Type.LINK_UPDATED) {
1447 linkHandler.processLinkAdded((Link) event.subject());
1448 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1449 linkHandler.processLinkRemoved((Link) event.subject());
1450 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1451 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1452 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1453 DeviceId deviceId = ((Device) event.subject()).id();
1454 if (deviceService.isAvailable(deviceId)) {
1455 log.info("** DEVICE UP Processing device event {} "
1456 + "for available device {}",
1457 event.type(), ((Device) event.subject()).id());
1458 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001459 } else {
Charles Chan9b7217c2018-04-05 16:31:15 -07001460 log.info(" ** DEVICE DOWN Processing device event {}"
1461 + " for unavailable device {}",
1462 event.type(), ((Device) event.subject()).id());
1463 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001464 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001465 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1466 // typically these calls come when device is added first time
1467 // so port filtering rules are handled at the device_added event.
1468 // port added calls represent all ports on the device,
1469 // enabled or not.
1470 log.trace("** PORT ADDED {}/{} -> {}",
1471 ((DeviceEvent) event).subject().id(),
1472 ((DeviceEvent) event).port().number(),
1473 event.type());
1474 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1475 // these calls happen for every subsequent event
1476 // ports enabled, disabled, switch goes away, comes back
1477 log.info("** PORT UPDATED {}/{} -> {}",
1478 event.subject(),
1479 ((DeviceEvent) event).port(),
1480 event.type());
Saurav Das41b49a92018-04-27 18:42:30 -07001481 processPortUpdatedInternal(((Device) event.subject()),
Charles Chan9b7217c2018-04-05 16:31:15 -07001482 ((DeviceEvent) event).port());
1483 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1484 // Process topology event, needed for all modules relying on
1485 // topology service for path computation
1486 TopologyEvent topologyEvent = (TopologyEvent) event;
1487 log.info("Processing topology event {}, topology age {}, reasons {}",
1488 event.type(), topologyEvent.subject().time(),
1489 topologyEvent.reasons().size());
1490 topologyHandler.processTopologyChange(topologyEvent.reasons());
1491 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1492 hostHandler.processHostAddedEvent((HostEvent) event);
1493 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1494 hostHandler.processHostMovedEvent((HostEvent) event);
1495 routeHandler.processHostMovedEvent((HostEvent) event);
1496 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1497 hostHandler.processHostRemovedEvent((HostEvent) event);
1498 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1499 hostHandler.processHostUpdatedEvent((HostEvent) event);
1500 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1501 routeHandler.processRouteAdded((RouteEvent) event);
1502 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1503 routeHandler.processRouteUpdated((RouteEvent) event);
1504 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1505 routeHandler.processRouteRemoved((RouteEvent) event);
1506 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1507 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1508 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1509 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1510 event.type() == McastEvent.Type.SINKS_ADDED ||
1511 event.type() == McastEvent.Type.SINKS_REMOVED ||
1512 event.type() == McastEvent.Type.ROUTE_ADDED ||
1513 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1514 mcastHandler.processMcastEvent((McastEvent) event);
1515 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1516 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1517 Class configClass = netcfgEvent.configClass();
1518 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1519 appCfgHandler.processAppConfigAdded(netcfgEvent);
1520 log.info("App config event .. configuring network");
1521 cfgListener.configureNetwork();
1522 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1523 log.info("Segment Routing Device Config added for {}", event.subject());
1524 cfgListener.configureNetwork();
1525 } else if (configClass.equals(XConnectConfig.class)) {
1526 xConnectHandler.processXConnectConfigAdded(netcfgEvent);
1527 } else if (configClass.equals(InterfaceConfig.class)) {
1528 log.info("Interface Config added for {}", event.subject());
1529 cfgListener.configureNetwork();
1530 } else {
1531 log.error("Unhandled config class: {}", configClass);
1532 }
1533 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1534 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1535 Class configClass = netcfgEvent.configClass();
1536 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1537 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1538 log.info("App config event .. configuring network");
1539 cfgListener.configureNetwork();
1540 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1541 log.info("Segment Routing Device Config updated for {}", event.subject());
1542 createOrUpdateDeviceConfiguration();
1543 } else if (configClass.equals(XConnectConfig.class)) {
1544 xConnectHandler.processXConnectConfigUpdated(netcfgEvent);
1545 } else if (configClass.equals(InterfaceConfig.class)) {
1546 log.info("Interface Config updated for {}", event.subject());
1547 createOrUpdateDeviceConfiguration();
1548 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1549 (InterfaceConfig) netcfgEvent.prevConfig().get());
1550 } else {
1551 log.error("Unhandled config class: {}", configClass);
1552 }
1553 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1554 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1555 Class configClass = netcfgEvent.configClass();
1556 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1557 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1558 log.info("App config event .. configuring network");
1559 cfgListener.configureNetwork();
1560 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1561 // TODO Handle sr device config removal
1562 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
1563 } else if (configClass.equals(XConnectConfig.class)) {
1564 xConnectHandler.processXConnectConfigRemoved(netcfgEvent);
1565 } else if (configClass.equals(InterfaceConfig.class)) {
1566 // TODO Handle interface removal
1567 log.info("InterfaceConfig removal is not handled in current implementation");
1568 } else {
1569 log.error("Unhandled config class: {}", configClass);
1570 }
Saurav Dasc6dc1772018-04-21 17:19:48 -07001571 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1572 MastershipEvent me = (MastershipEvent) event;
1573 DeviceId deviceId = me.subject();
1574 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1575 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1576 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1577 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1578 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1579 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan9b7217c2018-04-05 16:31:15 -07001580 } else {
1581 log.warn("Unhandled event type: {}", event.type());
sanghob35a6192015-04-01 13:05:26 -07001582 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001583 } catch (Exception e) {
1584 log.error("SegmentRouting event handler thread thrown an exception: {}",
1585 e.getMessage(), e);
sanghob35a6192015-04-01 13:05:26 -07001586 }
sanghob35a6192015-04-01 13:05:26 -07001587 }
1588 }
1589
Saurav Das45f48152018-01-18 12:07:33 -08001590 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001591 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001592
1593 // NOTE: Punt ARP/NDP even when the device is not configured.
1594 // Host learning without network config is required for CORD config generator.
1595 routingRulePopulator.populateIpPunts(device.id());
1596 routingRulePopulator.populateArpNdpPunts(device.id());
1597
Charles Chan0b4e6182015-11-03 10:42:14 -08001598 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001599 log.warn("Device configuration unavailable. Device {} will be "
1600 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001601 return;
1602 }
Charles Chan2199c302016-04-23 17:36:10 -07001603 processDeviceAddedInternal(device.id());
1604 }
1605
1606 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001607 // Irrespective of whether the local is a MASTER or not for this device,
1608 // we need to create a SR-group-handler instance. This is because in a
1609 // multi-instance setup, any instance can initiate forwarding/next-objectives
1610 // for any switch (even if this instance is a SLAVE or not even connected
1611 // to the switch). To handle this, a default-group-handler instance is necessary
1612 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001613 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1614 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001615 DefaultGroupHandler groupHandler;
1616 try {
1617 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001618 createGroupHandler(deviceId,
1619 appId,
1620 deviceConfiguration,
1621 linkService,
1622 flowObjectiveService,
1623 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001624 } catch (DeviceConfigNotFoundException e) {
1625 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1626 return;
1627 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001628 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001629 deviceId);
1630 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001631 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001632
Charles Chan2199c302016-04-23 17:36:10 -07001633 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001634 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001635 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001636 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001637 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001638 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001639 }
Charles Chan5270ed02016-01-30 23:22:37 -08001640
Charles Chan03a73e02016-10-24 14:52:01 -07001641 appCfgHandler.init(deviceId);
Charles Chana03ab8b2018-04-29 18:11:37 -07001642 hostEventExecutor.execute(() -> hostHandler.init(deviceId));
1643 routeEventExecutor.execute(() -> routeHandler.init(deviceId));
sanghob35a6192015-04-01 13:05:26 -07001644 }
1645
Saurav Das80980c72016-03-23 11:22:49 -07001646 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001647 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001648 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan47933752017-11-30 15:37:50 -08001649 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001650 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001651 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001652 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Ruchi Sahota5d800282019-01-28 01:08:18 +00001653 macVlanNextObjStore.entrySet().stream()
1654 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1655 .forEach(entry -> macVlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001656 portNextObjStore.entrySet().stream()
1657 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001658 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Das45f48152018-01-18 12:07:33 -08001659 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001660
Saurav Dasceccf242017-08-03 18:30:35 -07001661 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1662 if (gh != null) {
1663 gh.shutdown();
1664 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001665 // Note that a switch going down is associated with all of its links
1666 // going down as well, but it is treated as a single switch down event
Saurav Das5a356042018-04-06 20:16:01 -07001667 // while the link-downs are ignored. We cannot rely on the ordering of
1668 // events - i.e we cannot expect all link-downs to come before the
1669 // switch down - so we purge all seen-links for the switch before
1670 // handling route-path changes for the switch-down
Saurav Dasc88d4662017-05-15 15:34:25 -07001671 defaultRoutingHandler
Saurav Das604ab3a2018-03-18 21:28:15 -07001672 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Dasc568c342018-01-25 09:49:01 -08001673 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001674 xConnectHandler.removeDevice(device.id());
Saurav Dasa4020382018-02-14 14:14:54 -08001675
1676 // Cleanup all internal groupHandler stores for this device. Should be
1677 // done after all rerouting or rehashing has been completed
1678 groupHandlerMap.entrySet()
1679 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Das80980c72016-03-23 11:22:49 -07001680 }
1681
Saurav Das60ca8d52018-04-23 18:42:12 -07001682 /**
1683 * Purge the destinationSet nextObjective store of entries with this device
1684 * as key. Erases app-level knowledge of hashed groups in this device.
1685 *
1686 * @param devId the device identifier
1687 */
Saurav Dasc6dc1772018-04-21 17:19:48 -07001688 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Das60ca8d52018-04-23 18:42:12 -07001689 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Dasc6dc1772018-04-21 17:19:48 -07001690 dsNextObjStore.entrySet().stream()
1691 .filter(entry -> entry.getKey().deviceId().equals(devId))
1692 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1693 }
1694
Saurav Das41b49a92018-04-27 18:42:30 -07001695 private void processPortUpdatedInternal(Device device, Port port) {
Saurav Das1a129a02016-11-18 15:21:57 -08001696 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1697 log.warn("Device configuration uploading. Not handling port event for"
1698 + "dev: {} port: {}", device.id(), port.number());
1699 return;
1700 }
Saurav Das018605f2017-02-18 14:05:44 -08001701
Saurav Das41b49a92018-04-27 18:42:30 -07001702 if (interfaceService.isConfigured(new ConnectPoint(device.id(), port.number()))) {
1703 lastEdgePortEvent = Instant.now();
1704 }
1705
Saurav Das018605f2017-02-18 14:05:44 -08001706 if (!mastershipService.isLocalMaster(device.id())) {
1707 log.debug("Not master for dev:{} .. not handling port updated event"
1708 + "for port {}", device.id(), port.number());
1709 return;
1710 }
Saurav Das41b49a92018-04-27 18:42:30 -07001711 processPortUpdated(device.id(), port);
1712 }
Saurav Das018605f2017-02-18 14:05:44 -08001713
Saurav Das41b49a92018-04-27 18:42:30 -07001714 /**
1715 * Adds or remove filtering rules for the given switchport. If switchport is
1716 * an edge facing port, additionally handles host probing and broadcast
1717 * rules. Must be called by local master of device.
1718 *
1719 * @param deviceId the device identifier
1720 * @param port the port to update
1721 */
1722 void processPortUpdated(DeviceId deviceId, Port port) {
Saurav Das018605f2017-02-18 14:05:44 -08001723 // first we handle filtering rules associated with the port
1724 if (port.isEnabled()) {
1725 log.info("Switchport {}/{} enabled..programming filters",
Saurav Das41b49a92018-04-27 18:42:30 -07001726 deviceId, port.number());
1727 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001728 } else {
1729 log.info("Switchport {}/{} disabled..removing filters",
Saurav Das41b49a92018-04-27 18:42:30 -07001730 deviceId, port.number());
1731 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001732 }
Saurav Das1a129a02016-11-18 15:21:57 -08001733
1734 // portUpdated calls are for ports that have gone down or up. For switch
1735 // to switch ports, link-events should take care of any re-routing or
1736 // group editing necessary for port up/down. Here we only process edge ports
1737 // that are already configured.
Saurav Das41b49a92018-04-27 18:42:30 -07001738 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
Charles Chanb30213c2018-05-01 11:50:20 -07001739 VlanId untaggedVlan = interfaceService.getUntaggedVlanId(cp);
1740 VlanId nativeVlan = interfaceService.getNativeVlanId(cp);
1741 Set<VlanId> taggedVlans = interfaceService.getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001742
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001743 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001744 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das41b49a92018-04-27 18:42:30 -07001745 + "dev/port: {}/{}", deviceId, port.number());
Saurav Das1a129a02016-11-18 15:21:57 -08001746 return;
1747 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001748 if (untaggedVlan != null) {
Saurav Das41b49a92018-04-27 18:42:30 -07001749 processEdgePort(deviceId, port, untaggedVlan, true);
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001750 }
1751 if (nativeVlan != null) {
Saurav Das41b49a92018-04-27 18:42:30 -07001752 processEdgePort(deviceId, port, nativeVlan, true);
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001753 }
1754 if (!taggedVlans.isEmpty()) {
Saurav Das41b49a92018-04-27 18:42:30 -07001755 taggedVlans.forEach(tag -> processEdgePort(deviceId, port, tag, false));
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001756 }
Saurav Das1a129a02016-11-18 15:21:57 -08001757 }
1758
Saurav Das41b49a92018-04-27 18:42:30 -07001759 private void processEdgePort(DeviceId deviceId, Port port, VlanId vlanId,
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001760 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001761 boolean portUp = port.isEnabled();
1762 if (portUp) {
Saurav Das41b49a92018-04-27 18:42:30 -07001763 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", deviceId,
Charles Chan59cc16d2017-02-02 16:20:42 -08001764 port.number(), vlanId);
Charles Chana03ab8b2018-04-29 18:11:37 -07001765 hostEventExecutor.execute(() -> hostHandler.processPortUp(new ConnectPoint(deviceId, port.number())));
Saurav Das1a129a02016-11-18 15:21:57 -08001766 } else {
Saurav Das41b49a92018-04-27 18:42:30 -07001767 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", deviceId,
Charles Chan59cc16d2017-02-02 16:20:42 -08001768 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001769 }
1770
Saurav Das41b49a92018-04-27 18:42:30 -07001771 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001772 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001773 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001774 } else {
1775 log.warn("Group handler not found for dev:{}. Not handling edge port"
Saurav Das41b49a92018-04-27 18:42:30 -07001776 + " {} event for port:{}", deviceId,
Saurav Das1a129a02016-11-18 15:21:57 -08001777 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001778 }
1779 }
sangho1e575652015-05-14 00:39:53 -07001780
Charles Chan27fe1a52017-10-20 16:06:55 -07001781 private void createOrUpdateDeviceConfiguration() {
1782 if (deviceConfiguration == null) {
Saurav Dase7f51012018-02-09 17:26:45 -08001783 log.info("Creating new DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001784 deviceConfiguration = new DeviceConfiguration(this);
1785 } else {
Saurav Dase7f51012018-02-09 17:26:45 -08001786 log.info("Updating DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001787 deviceConfiguration.updateConfig();
1788 }
1789 }
1790
Charles Chana8487b02018-04-18 18:41:05 -07001791 private void createOrUpdateDefaultRoutingHandler() {
1792 if (defaultRoutingHandler == null) {
1793 log.info("Creating new DefaultRoutingHandler");
1794 defaultRoutingHandler = new DefaultRoutingHandler(this);
1795 } else {
1796 log.info("Updating DefaultRoutingHandler");
1797 defaultRoutingHandler.update(this);
1798 }
1799 }
1800
Pier Ventre10bd8d12016-11-26 21:05:22 -08001801 /**
1802 * Registers the given connect point with the NRS, this is necessary
1803 * to receive the NDP and ARP packets from the NRS.
1804 *
1805 * @param portToRegister connect point to register
1806 */
1807 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001808 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001809 portToRegister,
1810 neighbourHandler,
1811 appId
1812 );
1813 }
1814
Charles Chand6832882015-10-05 17:50:33 -07001815 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001816 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001817 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001818
Charles Chane849c192016-01-11 18:28:54 -08001819 /**
1820 * Constructs the internal network config listener.
1821 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001822 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001823 */
Charles Chan65238242017-06-22 18:03:14 -07001824 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001825 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001826 }
1827
Charles Chane849c192016-01-11 18:28:54 -08001828 /**
1829 * Reads network config and initializes related data structure accordingly.
1830 */
Charles Chan47933752017-11-30 15:37:50 -08001831 void configureNetwork() {
Saurav Dase7f51012018-02-09 17:26:45 -08001832 log.info("Configuring network ...");
Andrea Campanellaab482f92018-04-17 12:09:34 +02001833
1834 // Setting handling of network configuration events completable future
1835 // The completable future is needed because of the async behaviour of the configureNetwork,
1836 // listener registration and event arrival
1837 // Enables us to buffer the events and execute them when the configure network is done.
Ray Milkeyeadad4a2018-11-16 15:15:14 -08001838 synchronized (networkConfigCompletionLock) {
1839 networkConfigCompletion = new CompletableFuture<>();
Andrea Campanellaab482f92018-04-17 12:09:34 +02001840
Ray Milkeyeadad4a2018-11-16 15:15:14 -08001841 // add a small delay to absorb multiple network config added notifications
1842 if (!programmingScheduled.get()) {
1843 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1844 programmingScheduled.set(true);
1845 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1846 }
1847
1848 createOrUpdateDeviceConfiguration();
1849
1850 arpHandler = new ArpHandler(srManager);
1851 icmpHandler = new IcmpHandler(srManager);
1852 ipHandler = new IpHandler(srManager);
1853 routingRulePopulator = new RoutingRulePopulator(srManager);
1854 createOrUpdateDefaultRoutingHandler();
1855
1856 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1857 groupHandlerMap, tunnelStore);
1858 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1859 flowObjectiveService,
1860 tunnelHandler, policyStore);
1861 networkConfigCompletion.complete(true);
Andrea Campanellaab482f92018-04-17 12:09:34 +02001862 }
1863
Charles Chan2199c302016-04-23 17:36:10 -07001864 mcastHandler.init();
Andrea Campanellaab482f92018-04-17 12:09:34 +02001865
Charles Chan4636be02015-10-07 14:21:45 -07001866 }
1867
Charles Chand6832882015-10-05 17:50:33 -07001868 @Override
1869 public void event(NetworkConfigEvent event) {
Ray Milkey59bc0722018-05-11 09:59:19 -07001870 if (mainEventExecutor == null) {
1871 return;
1872 }
Charles Chan7a888e82018-03-21 16:57:47 -07001873 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
1874 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1875 switch (event.type()) {
1876 case CONFIG_ADDED:
1877 case CONFIG_UPDATED:
1878 case CONFIG_REMOVED:
1879 log.trace("Schedule Network Config event {}", event);
Andrea Campanellaab482f92018-04-17 12:09:34 +02001880 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1881 mainEventExecutor.execute(new InternalEventHandler(event));
1882 } else {
Charles Chane437a802018-04-21 00:44:29 -07001883 queuedEvents.add(event);
Andrea Campanellaab482f92018-04-17 12:09:34 +02001884 }
Charles Chan7a888e82018-03-21 16:57:47 -07001885 break;
1886 default:
1887 break;
Charles Chand6832882015-10-05 17:50:33 -07001888 }
1889 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001890
Charles Chan50443e82018-01-03 16:26:32 -08001891 @Override
1892 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase7f51012018-02-09 17:26:45 -08001893 if (event.type() == CONFIG_REGISTERED ||
1894 event.type() == CONFIG_UNREGISTERED) {
1895 log.debug("Ignore event {} due to type mismatch", event);
1896 return false;
Charles Chan50443e82018-01-03 16:26:32 -08001897 }
Saurav Dase7f51012018-02-09 17:26:45 -08001898
1899 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1900 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1901 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscd339592018-02-23 14:18:00 -08001902 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase7f51012018-02-09 17:26:45 -08001903 log.debug("Ignore event {} due to class mismatch", event);
1904 return false;
1905 }
1906
1907 return true;
Charles Chan50443e82018-01-03 16:26:32 -08001908 }
1909
Saurav Das7bcbe702017-06-13 15:35:54 -07001910 private final class ConfigChange implements Runnable {
1911 @Override
1912 public void run() {
1913 programmingScheduled.set(false);
Saurav Dase7f51012018-02-09 17:26:45 -08001914 log.info("Reacting to config changes after buffer delay");
Saurav Das7bcbe702017-06-13 15:35:54 -07001915 for (Device device : deviceService.getDevices()) {
1916 processDeviceAdded(device);
1917 }
1918 defaultRoutingHandler.startPopulationProcess();
1919 }
1920 }
Charles Chand6832882015-10-05 17:50:33 -07001921 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001922
Charles Chan7a888e82018-03-21 16:57:47 -07001923 private class InternalLinkListener implements LinkListener {
1924 @Override
1925 public void event(LinkEvent event) {
Ray Milkey59bc0722018-05-11 09:59:19 -07001926 if (mainEventExecutor == null) {
1927 return;
1928 }
Charles Chan7a888e82018-03-21 16:57:47 -07001929 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1930 event.type() == LinkEvent.Type.LINK_UPDATED ||
1931 event.type() == LinkEvent.Type.LINK_REMOVED) {
1932 log.trace("Schedule Link event {}", event);
Andrea Campanellaab482f92018-04-17 12:09:34 +02001933 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1934 mainEventExecutor.execute(new InternalEventHandler(event));
1935 } else {
Charles Chane437a802018-04-21 00:44:29 -07001936 queuedEvents.add(event);
Andrea Campanellaab482f92018-04-17 12:09:34 +02001937 }
Charles Chan7a888e82018-03-21 16:57:47 -07001938 }
1939 }
1940 }
1941
1942 private class InternalDeviceListener implements DeviceListener {
1943 @Override
1944 public void event(DeviceEvent event) {
Ray Milkey59bc0722018-05-11 09:59:19 -07001945 if (mainEventExecutor == null) {
1946 return;
1947 }
Charles Chan7a888e82018-03-21 16:57:47 -07001948 switch (event.type()) {
1949 case DEVICE_ADDED:
1950 case PORT_UPDATED:
1951 case PORT_ADDED:
1952 case DEVICE_UPDATED:
1953 case DEVICE_AVAILABILITY_CHANGED:
1954 log.trace("Schedule Device event {}", event);
Andrea Campanellaab482f92018-04-17 12:09:34 +02001955 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1956 mainEventExecutor.execute(new InternalEventHandler(event));
1957 } else {
Charles Chane437a802018-04-21 00:44:29 -07001958 queuedEvents.add(event);
Andrea Campanellaab482f92018-04-17 12:09:34 +02001959 }
Charles Chan7a888e82018-03-21 16:57:47 -07001960 break;
1961 default:
1962 }
1963 }
1964 }
1965
1966 private class InternalTopologyListener implements TopologyListener {
1967 @Override
1968 public void event(TopologyEvent event) {
Ray Milkey59bc0722018-05-11 09:59:19 -07001969 if (mainEventExecutor == null) {
1970 return;
1971 }
Charles Chan7a888e82018-03-21 16:57:47 -07001972 switch (event.type()) {
1973 case TOPOLOGY_CHANGED:
1974 log.trace("Schedule Topology event {}", event);
Andrea Campanellaab482f92018-04-17 12:09:34 +02001975 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1976 mainEventExecutor.execute(new InternalEventHandler(event));
1977 } else {
Charles Chane437a802018-04-21 00:44:29 -07001978 queuedEvents.add(event);
Andrea Campanellaab482f92018-04-17 12:09:34 +02001979 }
Charles Chan7a888e82018-03-21 16:57:47 -07001980 break;
1981 default:
1982 }
1983 }
1984 }
1985
Charles Chan68aa62d2015-11-09 16:37:23 -08001986 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001987 @Override
1988 public void event(HostEvent event) {
Ray Milkey59bc0722018-05-11 09:59:19 -07001989 if (hostEventExecutor == null) {
1990 return;
1991 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001992 switch (event.type()) {
1993 case HOST_ADDED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001994 case HOST_MOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001995 case HOST_REMOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001996 case HOST_UPDATED:
Charles Chan7a888e82018-03-21 16:57:47 -07001997 log.trace("Schedule Host event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001998 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chan68aa62d2015-11-09 16:37:23 -08001999 break;
2000 default:
2001 log.warn("Unsupported host event type: {}", event.type());
2002 break;
2003 }
2004 }
2005 }
2006
Charles Chand55e84d2016-03-30 17:54:24 -07002007 private class InternalMcastListener implements McastListener {
2008 @Override
2009 public void event(McastEvent event) {
Ray Milkey59bc0722018-05-11 09:59:19 -07002010 if (mcastEventExecutor == null) {
2011 return;
2012 }
Charles Chand55e84d2016-03-30 17:54:24 -07002013 switch (event.type()) {
Pier3ee24552018-03-14 16:47:32 -07002014 case SOURCES_ADDED:
2015 case SOURCES_REMOVED:
2016 case SINKS_ADDED:
2017 case SINKS_REMOVED:
Charles Chand55e84d2016-03-30 17:54:24 -07002018 case ROUTE_REMOVED:
Pier4694e092018-04-17 16:29:56 +02002019 case ROUTE_ADDED:
Charles Chan7a888e82018-03-21 16:57:47 -07002020 log.trace("Schedule Mcast event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07002021 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi9930da52018-02-02 16:19:11 +01002022 break;
Charles Chand55e84d2016-03-30 17:54:24 -07002023 default:
Charles Chan7a888e82018-03-21 16:57:47 -07002024 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chand55e84d2016-03-30 17:54:24 -07002025 break;
2026 }
2027 }
2028 }
Charles Chan35fd1a72016-06-13 18:54:31 -07002029
Charles Chan03a73e02016-10-24 14:52:01 -07002030 private class InternalRouteEventListener implements RouteListener {
2031 @Override
2032 public void event(RouteEvent event) {
Ray Milkey59bc0722018-05-11 09:59:19 -07002033 if (routeEventExecutor == null) {
2034 return;
2035 }
Charles Chan03a73e02016-10-24 14:52:01 -07002036 switch (event.type()) {
2037 case ROUTE_ADDED:
Charles Chan03a73e02016-10-24 14:52:01 -07002038 case ROUTE_UPDATED:
Charles Chan03a73e02016-10-24 14:52:01 -07002039 case ROUTE_REMOVED:
Charles Chan2fde6d42017-08-23 14:46:43 -07002040 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan7a888e82018-03-21 16:57:47 -07002041 log.trace("Schedule Route event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07002042 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan2fde6d42017-08-23 14:46:43 -07002043 break;
Charles Chan03a73e02016-10-24 14:52:01 -07002044 default:
Charles Chan7a888e82018-03-21 16:57:47 -07002045 log.warn("Unsupported route event type: {}", event.type());
Charles Chan03a73e02016-10-24 14:52:01 -07002046 break;
2047 }
2048 }
2049 }
Saurav Dasc88d4662017-05-15 15:34:25 -07002050
Charles Chana8487b02018-04-18 18:41:05 -07002051 private class InternalMastershipListener implements MastershipListener {
2052 @Override
2053 public void event(MastershipEvent event) {
Ray Milkey59bc0722018-05-11 09:59:19 -07002054 if (mainEventExecutor == null) {
2055 return;
2056 }
Charles Chana8487b02018-04-18 18:41:05 -07002057 switch (event.type()) {
Saurav Dasc6dc1772018-04-21 17:19:48 -07002058 case MASTER_CHANGED:
2059 log.debug("Mastership event: {}/{}", event.subject(),
2060 event.roleInfo());
2061 mainEventExecutor.execute(new InternalEventHandler(event));
2062 break;
2063 case BACKUPS_CHANGED:
2064 case SUSPENDED:
2065 default:
2066 log.debug("Mastership event type {} not handled", event.type());
2067 break;
Charles Chana8487b02018-04-18 18:41:05 -07002068 }
2069 }
2070 }
2071
Saurav Dasc6dc1772018-04-21 17:19:48 -07002072 class InternalClusterListener implements ClusterEventListener {
2073 private Instant lastClusterEvent = Instant.EPOCH;
2074
2075 long timeSinceLastClusterEvent() {
2076 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
2077 }
2078
2079 @Override
2080 public void event(ClusterEvent event) {
2081 switch (event.type()) {
2082 case INSTANCE_ACTIVATED:
2083 case INSTANCE_ADDED:
2084 case INSTANCE_READY:
2085 log.debug("Cluster event {} ignored", event.type());
2086 break;
2087 case INSTANCE_DEACTIVATED:
2088 case INSTANCE_REMOVED:
2089 log.info("** Cluster event {}", event.type());
2090 lastClusterEvent = Instant.now();
2091 break;
2092 default:
2093 break;
2094 }
2095
2096 }
2097
2098 }
2099
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002100 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
2101 try {
2102 Set<Interface> intfs = conf.getInterfaces();
2103 Set<Interface> prevIntfs = prevConf.getInterfaces();
2104
2105 // Now we only handle one interface config at each port.
2106 if (intfs.size() != 1 || prevIntfs.size() != 1) {
2107 log.warn("Interface update aborted - one at a time is allowed, " +
2108 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
2109 return;
2110 }
2111
Andrea Campanellaab482f92018-04-17 12:09:34 +02002112 //The system is in an incoherent state, abort
2113 if (defaultRoutingHandler == null) {
2114 log.warn("Interface update aborted, defaultRoutingHandler is null");
2115 return;
2116 }
2117
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002118 Interface intf = intfs.stream().findFirst().get();
2119 Interface prevIntf = prevIntfs.stream().findFirst().get();
2120
2121 DeviceId deviceId = intf.connectPoint().deviceId();
2122 PortNumber portNum = intf.connectPoint().port();
2123
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002124 removeSubnetConfig(prevIntf.connectPoint(),
2125 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
2126 new HashSet<>(intf.ipAddressesList())));
2127
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002128 if (!prevIntf.vlanNative().equals(VlanId.NONE)
2129 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
2130 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
2131 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
2132 // Update filtering objective and L2IG group bucket
2133 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
2134 } else {
2135 // RemoveVlanNative
2136 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
2137 }
2138 }
2139
2140 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
2141 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2142 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2143 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
2144 // Update filtering objective and L2IG group bucket
2145 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
2146 } else {
2147 // RemoveVlanUntagged
2148 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
2149 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002150 }
2151
2152 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2153 // RemoveVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002154 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
2155 .filter(i -> !intf.vlanUntagged().equals(i))
2156 .filter(i -> !intf.vlanNative().equals(i))
2157 .forEach(vlanId -> updateVlanConfigInternal(
2158 deviceId, portNum, vlanId, false, false));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002159 }
2160
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002161 if (!intf.vlanNative().equals(VlanId.NONE)
2162 && !prevIntf.vlanNative().equals(intf.vlanNative())
2163 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2164 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
2165 // Update filtering objective and L2IG group bucket
2166 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
2167 } else {
2168 // AddVlanNative
2169 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
2170 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002171 }
2172
2173 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2174 // AddVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002175 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
2176 .filter(i -> !prevIntf.vlanUntagged().equals(i))
2177 .filter(i -> !prevIntf.vlanNative().equals(i))
2178 .forEach(vlanId -> updateVlanConfigInternal(
2179 deviceId, portNum, vlanId, false, true)
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002180 );
2181 }
2182
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002183 if (!intf.vlanUntagged().equals(VlanId.NONE)
2184 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2185 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
2186 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
2187 // Update filtering objective and L2IG group bucket
2188 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
2189 } else {
2190 // AddVlanUntagged
2191 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
2192 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002193 }
2194 addSubnetConfig(prevIntf.connectPoint(),
2195 Sets.difference(new HashSet<>(intf.ipAddressesList()),
2196 new HashSet<>(prevIntf.ipAddressesList())));
2197 } catch (ConfigException e) {
2198 log.error("Error in configuration");
2199 }
2200 }
2201
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002202 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
2203 VlanId vlanId, boolean pushVlan) {
2204 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2205 if (grpHandler == null) {
2206 log.warn("Failed to retrieve group handler for device {}", deviceId);
2207 return;
2208 }
2209
2210 // Update filtering objective for a single port
2211 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
2212 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
2213
2214 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
2215 // Update L2IG bucket of the port
2216 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
2217 } else {
2218 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2219 }
2220 }
2221
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002222 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
2223 VlanId vlanId, boolean pushVlan, boolean install) {
2224 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2225 if (grpHandler == null) {
2226 log.warn("Failed to retrieve group handler for device {}", deviceId);
2227 return;
2228 }
2229
2230 // Update filtering objective for a single port
2231 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
2232
2233 // Update filtering objective for multicast ingress port
2234 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
2235
2236 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
2237
2238 if (nextId != -1 && !install) {
2239 // Update next objective for a single port as an output port
2240 // Remove a single port from L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002241 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002242 // Remove L2 Bridging rule and L3 Unicast rule to the host
Charles Chana03ab8b2018-04-29 18:11:37 -07002243 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2244 vlanId, pushVlan, install));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002245 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
2246 // only if there is no port configured on that VLAN ID
2247 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
2248 // Remove broadcast forwarding rule for the VLAN
2249 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2250 // Remove L2FG for VLAN
2251 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
2252 } else {
2253 // Remove L2IG of the port
2254 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
2255 }
2256 } else if (install) {
2257 if (nextId != -1) {
2258 // Add a single port to L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002259 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002260 } else {
2261 // Create L2FG for VLAN
2262 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
2263 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2264 }
Charles Chana03ab8b2018-04-29 18:11:37 -07002265 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2266 vlanId, pushVlan, install));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002267 } else {
2268 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2269 }
2270 }
2271
2272 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2273 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2274 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2275
2276 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2277 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2278 .filter(intf -> !intf.connectPoint().equals(cp))
2279 .flatMap(intf -> intf.ipAddressesList().stream())
2280 .collect(Collectors.toSet());
2281 // 1. Partial subnet population
2282 // Remove routing rules for removed subnet from previous configuration,
2283 // which does not also exist in other interfaces in the same device
2284 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2285 .map(InterfaceIpAddress::subnetAddress)
2286 .collect(Collectors.toSet());
2287
2288 defaultRoutingHandler.revokeSubnet(
2289 ipPrefixSet.stream()
2290 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2291 .collect(Collectors.toSet()));
2292
2293 // 2. Interface IP punts
2294 // Remove IP punts for old Intf address
2295 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2296 .map(InterfaceIpAddress::ipAddress)
2297 .collect(Collectors.toSet());
2298 ipAddressSet.stream()
2299 .map(InterfaceIpAddress::ipAddress)
2300 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2301 .forEach(interfaceIpAddress ->
2302 routingRulePopulator.revokeSingleIpPunts(
2303 cp.deviceId(), interfaceIpAddress));
2304
2305 // 3. Host unicast routing rule
2306 // Remove unicast routing rule
Charles Chana03ab8b2018-04-29 18:11:37 -07002307 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002308 }
2309
2310 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2311 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2312 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2313
2314 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2315 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2316 .filter(intf -> !intf.connectPoint().equals(cp))
2317 .flatMap(intf -> intf.ipAddressesList().stream())
2318 .collect(Collectors.toSet());
2319 // 1. Partial subnet population
2320 // Add routing rules for newly added subnet, which does not also exist in
2321 // other interfaces in the same device
2322 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2323 .map(InterfaceIpAddress::subnetAddress)
2324 .collect(Collectors.toSet());
2325
2326 defaultRoutingHandler.populateSubnet(
2327 Collections.singleton(cp),
2328 ipPrefixSet.stream()
2329 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2330 .collect(Collectors.toSet()));
2331
2332 // 2. Interface IP punts
2333 // Add IP punts for new Intf address
2334 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2335 .map(InterfaceIpAddress::ipAddress)
2336 .collect(Collectors.toSet());
2337 ipAddressSet.stream()
2338 .map(InterfaceIpAddress::ipAddress)
2339 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2340 .forEach(interfaceIpAddress ->
2341 routingRulePopulator.populateSingleIpPunts(
2342 cp.deviceId(), interfaceIpAddress));
2343
2344 // 3. Host unicast routing rule
2345 // Add unicast routing rule
Charles Chana03ab8b2018-04-29 18:11:37 -07002346 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002347 }
sanghob35a6192015-04-01 13:05:26 -07002348}