blob: 864a92451a1395d007639357df3fc818d005db6d [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sanghob35a6192015-04-01 13:05:26 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.segmentrouting;
17
Jonathan Hart8ca2bc02017-11-30 18:23:42 -080018import com.google.common.collect.HashMultimap;
19import com.google.common.collect.ImmutableMap;
20import com.google.common.collect.Maps;
21import com.google.common.collect.Multimap;
Charles Chan9640c812017-08-23 13:55:39 -070022import com.google.common.collect.Sets;
sanghob35a6192015-04-01 13:05:26 -070023import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
Charles Chan47933752017-11-30 15:37:50 -080026import org.apache.felix.scr.annotations.Modified;
27import org.apache.felix.scr.annotations.Property;
sanghob35a6192015-04-01 13:05:26 -070028import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho1e575652015-05-14 00:39:53 -070030import org.apache.felix.scr.annotations.Service;
sanghob35a6192015-04-01 13:05:26 -070031import org.onlab.packet.Ethernet;
Pier Ventre735b8c82016-12-02 08:16:05 -080032import org.onlab.packet.ICMP6;
Charles Chanc42e84e2015-10-20 16:24:19 -070033import org.onlab.packet.IPv4;
Pier Ventre10bd8d12016-11-26 21:05:22 -080034import org.onlab.packet.IPv6;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070035import org.onlab.packet.IpAddress;
Charles Chanc42e84e2015-10-20 16:24:19 -070036import org.onlab.packet.IpPrefix;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070037import org.onlab.packet.VlanId;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070038import org.onlab.util.KryoNamespace;
Charles Chan47933752017-11-30 15:37:50 -080039import org.onlab.util.Tools;
Saurav Das80980c72016-03-23 11:22:49 -070040import org.onosproject.cfg.ComponentConfigService;
Saurav Das201762d2018-04-21 17:19:48 -070041import org.onosproject.cluster.ClusterEvent;
42import org.onosproject.cluster.ClusterEventListener;
Pier Luigieba73a02018-01-16 10:47:50 +010043import org.onosproject.cluster.ClusterService;
Pier96f63cb2018-04-17 16:29:56 +020044import org.onosproject.cluster.NodeId;
sanghob35a6192015-04-01 13:05:26 -070045import org.onosproject.core.ApplicationId;
46import org.onosproject.core.CoreService;
47import org.onosproject.event.Event;
Charles Chan50bb6ef2018-04-18 18:41:05 -070048import org.onosproject.mastership.MastershipEvent;
49import org.onosproject.mastership.MastershipListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070050import org.onosproject.mastership.MastershipService;
Pier3ee24552018-03-14 16:47:32 -070051import org.onosproject.mcast.api.McastEvent;
52import org.onosproject.mcast.api.McastListener;
53import org.onosproject.mcast.api.MulticastRouteService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080054import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070055import org.onosproject.net.Device;
56import org.onosproject.net.DeviceId;
Charles Chan9640c812017-08-23 13:55:39 -070057import org.onosproject.net.Host;
58import org.onosproject.net.HostId;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070059import org.onosproject.net.Link;
60import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080061import org.onosproject.net.PortNumber;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070062import org.onosproject.net.config.ConfigException;
Charles Chand6832882015-10-05 17:50:33 -070063import org.onosproject.net.config.ConfigFactory;
64import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070065import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070066import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070067import org.onosproject.net.config.basics.InterfaceConfig;
68import org.onosproject.net.config.basics.McastConfig;
Charles Chand6832882015-10-05 17:50:33 -070069import org.onosproject.net.config.basics.SubjectFactories;
Saurav Das45f48152018-01-18 12:07:33 -080070import org.onosproject.net.device.DeviceAdminService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070071import org.onosproject.net.device.DeviceEvent;
72import org.onosproject.net.device.DeviceListener;
73import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080074import org.onosproject.net.flow.TrafficSelector;
75import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070076import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan68aa62d2015-11-09 16:37:23 -080077import org.onosproject.net.host.HostEvent;
78import org.onosproject.net.host.HostListener;
Charles Chan47933752017-11-30 15:37:50 -080079import org.onosproject.net.host.HostLocationProbingService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080080import org.onosproject.net.host.HostService;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070081import org.onosproject.net.host.InterfaceIpAddress;
Pier96f63cb2018-04-17 16:29:56 +020082import org.onosproject.net.intent.WorkPartitionService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070083import org.onosproject.net.intf.Interface;
84import org.onosproject.net.intf.InterfaceService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080085import org.onosproject.net.link.LinkEvent;
86import org.onosproject.net.link.LinkListener;
87import org.onosproject.net.link.LinkService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070088import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080089import org.onosproject.net.packet.InboundPacket;
90import org.onosproject.net.packet.PacketContext;
91import org.onosproject.net.packet.PacketProcessor;
92import org.onosproject.net.packet.PacketService;
Pier Luigi83f919b2018-02-15 16:33:08 +010093import org.onosproject.net.topology.TopologyEvent;
94import org.onosproject.net.topology.TopologyListener;
Charles Chand55e84d2016-03-30 17:54:24 -070095import org.onosproject.net.topology.TopologyService;
Charles Chan9640c812017-08-23 13:55:39 -070096import org.onosproject.routeservice.ResolvedRoute;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070097import org.onosproject.routeservice.RouteEvent;
98import org.onosproject.routeservice.RouteListener;
99import org.onosproject.routeservice.RouteService;
Charles Chand55e84d2016-03-30 17:54:24 -0700100import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
101import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventref34966c2016-11-07 16:21:04 -0800102import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700103import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -0700104import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -0700105import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700106import org.onosproject.segmentrouting.grouphandler.DestinationSet;
107import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Pier Luigi96fe0772018-02-28 12:10:50 +0100108import org.onosproject.segmentrouting.mcast.McastHandler;
109import org.onosproject.segmentrouting.mcast.McastRole;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700110import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700111import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800112import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700113import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800114
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800115import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700116import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800117import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800118import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
119
Saurav Das7bcbe702017-06-13 15:35:54 -0700120import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Pier Luigib29144d2018-01-15 18:06:43 +0100121import org.onosproject.segmentrouting.storekey.McastStoreKey;
Charles Chand2990362016-04-18 13:44:03 -0700122import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -0800123import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -0700124import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700125import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700126import org.onosproject.store.service.EventuallyConsistentMap;
127import org.onosproject.store.service.EventuallyConsistentMapBuilder;
128import org.onosproject.store.service.StorageService;
129import org.onosproject.store.service.WallClockTimestamp;
Charles Chan47933752017-11-30 15:37:50 -0800130import org.osgi.service.component.ComponentContext;
sanghob35a6192015-04-01 13:05:26 -0700131import org.slf4j.Logger;
132import org.slf4j.LoggerFactory;
133
Saurav Das201762d2018-04-21 17:19:48 -0700134import java.time.Instant;
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700135import java.util.ArrayList;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800136import java.util.Collections;
Charles Chan47933752017-11-30 15:37:50 -0800137import java.util.Dictionary;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800138import java.util.HashSet;
139import java.util.List;
140import java.util.Map;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800141import java.util.Optional;
142import java.util.Set;
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200143import java.util.concurrent.CompletableFuture;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800144import java.util.concurrent.ConcurrentHashMap;
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200145import java.util.concurrent.CopyOnWriteArrayList;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800146import java.util.concurrent.Executors;
147import java.util.concurrent.ScheduledExecutorService;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800148import java.util.concurrent.TimeUnit;
149import java.util.concurrent.atomic.AtomicBoolean;
150import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -0700151
Charles Chan3e783d02016-02-26 22:19:52 -0800152import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800153import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700154import static org.onlab.util.Tools.groupedThreads;
Saurav Dase7f51012018-02-09 17:26:45 -0800155import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
156import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Charles Chan3e783d02016-02-26 22:19:52 -0800157
Charles Chane849c192016-01-11 18:28:54 -0800158/**
159 * Segment routing manager.
160 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700161@Service
162@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700163public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700164
Charles Chan2c15aca2016-11-09 20:51:44 -0800165 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan2fde6d42017-08-23 14:46:43 -0700166 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sanghob35a6192015-04-01 13:05:26 -0700167
168 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700169 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700170
171 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800172 private NeighbourResolutionService neighbourResolutionService;
173
174 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100175 public CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700176
177 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700178 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700179
180 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700181 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700182
183 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan47933752017-11-30 15:37:50 -0800184 HostLocationProbingService probingService;
185
186 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100187 public DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700188
189 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Saurav Das45f48152018-01-18 12:07:33 -0800190 DeviceAdminService deviceAdminService;
191
192 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800193 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700194
195 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100196 public LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700197
Charles Chan5270ed02016-01-30 23:22:37 -0800198 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800199 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700200
201 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800202 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700203
204 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100205 public MulticastRouteService multicastRouteService;
Charles Chan03a73e02016-10-24 14:52:01 -0700206
207 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chanf237e1b2018-01-09 13:45:07 -0800208 public TopologyService topologyService;
Charles Chan03a73e02016-10-24 14:52:01 -0700209
210 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700211 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800212
213 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800214 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800215
Saurav Das80980c72016-03-23 11:22:49 -0700216 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800217 public InterfaceService interfaceService;
218
Pier Luigieba73a02018-01-16 10:47:50 +0100219 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
220 public ClusterService clusterService;
221
222 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier96f63cb2018-04-17 16:29:56 +0200223 public WorkPartitionService workPartitionService;
Pier Luigieba73a02018-01-16 10:47:50 +0100224
Charles Chan47933752017-11-30 15:37:50 -0800225 @Property(name = "activeProbing", boolValue = true,
226 label = "Enable active probing to discover dual-homed hosts.")
227 boolean activeProbing = true;
228
Charles Chan03a73e02016-10-24 14:52:01 -0700229 ArpHandler arpHandler = null;
230 IcmpHandler icmpHandler = null;
231 IpHandler ipHandler = null;
232 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700233 ApplicationId appId;
234 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700235
Charles Chan03a73e02016-10-24 14:52:01 -0700236 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700237 private TunnelHandler tunnelHandler = null;
238 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700239 private InternalPacketProcessor processor = null;
240 private InternalLinkListener linkListener = null;
241 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700242 private AppConfigHandler appCfgHandler = null;
Pier Luigi96fe0772018-02-28 12:10:50 +0100243 public XConnectHandler xConnectHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800244 McastHandler mcastHandler = null;
245 HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800246 private RouteHandler routeHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800247 LinkHandler linkHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800248 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800249 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigi83f919b2018-02-15 16:33:08 +0100250 private TopologyHandler topologyHandler = null;
Charles Chan5270ed02016-01-30 23:22:37 -0800251 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700252 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
253 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700254 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigi83f919b2018-02-15 16:33:08 +0100255 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chan50bb6ef2018-04-18 18:41:05 -0700256 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
Saurav Das201762d2018-04-21 17:19:48 -0700257 final InternalClusterListener clusterListener = new InternalClusterListener();
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200258 //Completable future for network configuration process to buffer config events handling during activation
259 private CompletableFuture<Boolean> networkConfigCompletion = null;
Charles Chan10b2fee2018-04-21 00:44:29 -0700260 private List<Event> queuedEvents = new CopyOnWriteArrayList<>();
sanghob35a6192015-04-01 13:05:26 -0700261
Charles Chan9b7217c2018-04-05 16:31:15 -0700262 // Handles device, link, topology and network config events
Charles Chan50bb6ef2018-04-18 18:41:05 -0700263 private ScheduledExecutorService mainEventExecutor;
sanghob35a6192015-04-01 13:05:26 -0700264
Charles Chan50bb6ef2018-04-18 18:41:05 -0700265 // Handles host, route and mcast events respectively
266 private ScheduledExecutorService hostEventExecutor;
267 private ScheduledExecutorService routeEventExecutor;
268 private ScheduledExecutorService mcastEventExecutor;
Charles Chan9b7217c2018-04-05 16:31:15 -0700269
270 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chane849c192016-01-11 18:28:54 -0800271 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700272 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700273 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800274 */
Charles Chan47933752017-11-30 15:37:50 -0800275 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das7bcbe702017-06-13 15:35:54 -0700276 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800277 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700278 * Per device next objective ID store with (device id + vlanid) as key.
279 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800280 */
Charles Chan47933752017-11-30 15:37:50 -0800281 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800282 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800283 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700284 * Per device next objective ID store with (device id + port + treatment + meta) as key.
285 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chane849c192016-01-11 18:28:54 -0800286 */
Charles Chan47933752017-11-30 15:37:50 -0800287 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800288 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800289
Saurav Das4ce45962015-11-24 23:21:05 -0800290 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
291 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700292
Saurav Das7bcbe702017-06-13 15:35:54 -0700293 private AtomicBoolean programmingScheduled = new AtomicBoolean();
294
Charles Chand55e84d2016-03-30 17:54:24 -0700295 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700296 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
297 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700298 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700299 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800300 public SegmentRoutingDeviceConfig createConfig() {
301 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700302 }
303 };
Pier Ventref34966c2016-11-07 16:21:04 -0800304
Charles Chand55e84d2016-03-30 17:54:24 -0700305 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700306 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
307 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700308 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800309 @Override
310 public SegmentRoutingAppConfig createConfig() {
311 return new SegmentRoutingAppConfig();
312 }
313 };
Pier Ventref34966c2016-11-07 16:21:04 -0800314
Charles Chanfc5c7802016-05-17 13:13:55 -0700315 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
316 new ConfigFactory<ApplicationId, XConnectConfig>(
317 SubjectFactories.APP_SUBJECT_FACTORY,
318 XConnectConfig.class, "xconnect") {
319 @Override
320 public XConnectConfig createConfig() {
321 return new XConnectConfig();
322 }
323 };
Pier Ventref34966c2016-11-07 16:21:04 -0800324
Charles Chand55e84d2016-03-30 17:54:24 -0700325 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700326 new ConfigFactory<ApplicationId, McastConfig>(
327 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700328 McastConfig.class, "multicast") {
329 @Override
330 public McastConfig createConfig() {
331 return new McastConfig();
332 }
333 };
334
Charles Chan116188d2016-02-18 14:22:42 -0800335 /**
336 * Segment Routing App ID.
337 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800338 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan59cc16d2017-02-02 16:20:42 -0800339
Charles Chane849c192016-01-11 18:28:54 -0800340 /**
341 * The default VLAN ID assigned to the interfaces without subnet config.
342 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800343 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700344
sanghob35a6192015-04-01 13:05:26 -0700345 @Activate
Charles Chan47933752017-11-30 15:37:50 -0800346 protected void activate(ComponentContext context) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800347 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700348
Charles Chan50bb6ef2018-04-18 18:41:05 -0700349 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-main", "%d", log));
350 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-host", "%d", log));
351 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-route", "%d", log));
352 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-mcast", "%d", log));
353
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700354 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700355 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700356 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700357 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700358 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700359 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700360 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700361 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700362 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700363
Charles Chan59cc16d2017-02-02 16:20:42 -0800364 log.debug("Creating EC map vlannextobjectivestore");
365 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
366 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
367 vlanNextObjStore = vlanNextObjMapBuilder
368 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700369 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700370 .withTimestampProvider((k, v) -> new WallClockTimestamp())
371 .build();
372
Saurav Das4ce45962015-11-24 23:21:05 -0800373 log.debug("Creating EC map subnetnextobjectivestore");
374 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
375 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
376 portNextObjStore = portNextObjMapBuilder
377 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700378 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800379 .withTimestampProvider((k, v) -> new WallClockTimestamp())
380 .build();
381
sangho0b2b6d12015-05-20 22:16:38 -0700382 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
383 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700384 tunnelStore = tunnelMapBuilder
385 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700386 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700387 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700388 .build();
389
390 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
391 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700392 policyStore = policyMapBuilder
393 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700394 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700395 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700396 .build();
397
Saurav Das80980c72016-03-23 11:22:49 -0700398 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800399 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700400 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800401 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800402 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
403 "requestInterceptsEnabled", "false");
Charles Chand3baaba2017-08-08 15:13:37 -0700404 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800405 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700406 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800407 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800408 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
409 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800410 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
411 "forceUnprovision", "true");
Charles Chanef624ed2017-08-10 16:57:28 -0700412 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan73316522017-07-20 16:16:25 -0700413 "distributed", "true");
Charles Chan35a32322017-08-14 11:42:11 -0700414 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
415 "multihomingEnabled", "true");
Charles Chan807d87a2017-11-29 19:54:20 -0800416 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
417 "staleLinkAge", "15000");
418 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
419 "allowDuplicateIps", "false");
Charles Chan47933752017-11-30 15:37:50 -0800420 compCfgService.registerProperties(getClass());
421 modified(context);
Saurav Das80980c72016-03-23 11:22:49 -0700422
Charles Chanb8e10c82015-10-14 11:24:40 -0700423 processor = new InternalPacketProcessor();
424 linkListener = new InternalLinkListener();
425 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700426 appCfgHandler = new AppConfigHandler(this);
427 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700428 mcastHandler = new McastHandler(this);
429 hostHandler = new HostHandler(this);
Saurav Das45f48152018-01-18 12:07:33 -0800430 linkHandler = new LinkHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700431 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800432 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800433 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigi83f919b2018-02-15 16:33:08 +0100434 topologyHandler = new TopologyHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700435
Charles Chan3e783d02016-02-26 22:19:52 -0800436 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700437 cfgService.registerConfigFactory(deviceConfigFactory);
438 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700439 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700440 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase7f51012018-02-09 17:26:45 -0800441 log.info("Configuring network before adding listeners");
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200442
Charles Chan132393a2018-01-04 14:26:07 -0800443 cfgListener.configureNetwork();
444
Charles Chan5270ed02016-01-30 23:22:37 -0800445 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700446 packetService.addProcessor(processor, PacketProcessor.director(2));
447 linkService.addListener(linkListener);
448 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700449 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700450 routeService.addListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100451 topologyService.addListener(topologyListener);
Charles Chan50bb6ef2018-04-18 18:41:05 -0700452 mastershipService.addListener(mastershipListener);
Saurav Das201762d2018-04-21 17:19:48 -0700453 clusterService.addListener(clusterListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700454
Charles Chanb39777c2018-03-09 15:53:44 -0800455 linkHandler.init();
Andreas Pantelopoulos4a768c02018-01-11 07:53:48 -0800456 l2TunnelHandler.init();
457
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200458 networkConfigCompletion.whenComplete((value, ex) -> {
459 //setting to null for easier fall through
460 networkConfigCompletion = null;
461 //process all queued events
Charles Chan10b2fee2018-04-21 00:44:29 -0700462 queuedEvents.forEach(event -> {
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200463 mainEventExecutor.execute(new InternalEventHandler(event));
464 });
465 });
466
sanghob35a6192015-04-01 13:05:26 -0700467 log.info("Started");
468 }
469
Saurav Das45f48152018-01-18 12:07:33 -0800470 KryoNamespace.Builder createSerializer() {
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700471 return new KryoNamespace.Builder()
472 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700473 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800474 VlanNextObjectiveStoreKey.class,
475 DestinationSet.class,
476 NextNeighbors.class,
477 Tunnel.class,
478 DefaultTunnel.class,
479 Policy.class,
480 TunnelPolicy.class,
481 Policy.Type.class,
482 PortNextObjectiveStoreKey.class,
483 XConnectStoreKey.class,
484 L2Tunnel.class,
485 L2TunnelPolicy.class,
486 DefaultL2Tunnel.class,
487 DefaultL2TunnelPolicy.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700488 );
489 }
490
sanghob35a6192015-04-01 13:05:26 -0700491 @Deactivate
492 protected void deactivate() {
Charles Chan50bb6ef2018-04-18 18:41:05 -0700493 mainEventExecutor.shutdown();
494 hostEventExecutor.shutdown();
495 routeEventExecutor.shutdown();
496 mcastEventExecutor.shutdown();
497
Charles Chand6832882015-10-05 17:50:33 -0700498 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700499 cfgService.unregisterConfigFactory(deviceConfigFactory);
500 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700501 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700502 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan47933752017-11-30 15:37:50 -0800503 compCfgService.unregisterProperties(getClass(), false);
Charles Chand6832882015-10-05 17:50:33 -0700504
Charles Chanc7a8a682017-06-19 00:43:31 -0700505 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700506 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700507 linkService.removeListener(linkListener);
508 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700509 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700510 routeService.removeListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100511 topologyService.removeListener(topologyListener);
Charles Chan50bb6ef2018-04-18 18:41:05 -0700512 mastershipService.removeListener(mastershipListener);
Saurav Das201762d2018-04-21 17:19:48 -0700513 clusterService.removeListener(clusterListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700514
Charles Chan0aa674e2017-02-23 15:44:08 -0800515 neighbourResolutionService.unregisterNeighbourHandlers(appId);
516
sanghob35a6192015-04-01 13:05:26 -0700517 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700518 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700519 deviceListener = null;
Charles Chan6577f612018-04-19 13:10:01 -0700520 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chanb8e10c82015-10-14 11:24:40 -0700521 groupHandlerMap.clear();
522
Saurav Das7bcbe702017-06-13 15:35:54 -0700523 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800524 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700525 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700526 tunnelStore.destroy();
527 policyStore.destroy();
Pier Luigib72201b2018-01-25 16:16:02 +0100528
529 mcastHandler.terminate();
sanghob35a6192015-04-01 13:05:26 -0700530 log.info("Stopped");
531 }
532
Charles Chan47933752017-11-30 15:37:50 -0800533 @Modified
534 private void modified(ComponentContext context) {
535 Dictionary<?, ?> properties = context.getProperties();
536 if (properties == null) {
537 return;
538 }
539
540 String strActiveProving = Tools.get(properties, "activeProbing");
541 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
542
543 if (expectActiveProbing != activeProbing) {
544 activeProbing = expectActiveProbing;
545 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
546 }
547 }
548
sangho1e575652015-05-14 00:39:53 -0700549 @Override
550 public List<Tunnel> getTunnels() {
551 return tunnelHandler.getTunnels();
552 }
553
554 @Override
sangho71abe1b2015-06-29 14:58:47 -0700555 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
556 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700557 }
558
559 @Override
sangho71abe1b2015-06-29 14:58:47 -0700560 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700561 for (Policy policy: policyHandler.getPolicies()) {
562 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
563 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
564 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
565 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700566 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700567 }
568 }
569 }
sangho71abe1b2015-06-29 14:58:47 -0700570 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700571 }
572
573 @Override
sangho71abe1b2015-06-29 14:58:47 -0700574 public PolicyHandler.Result removePolicy(Policy policy) {
575 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700576 }
577
578 @Override
sangho71abe1b2015-06-29 14:58:47 -0700579 public PolicyHandler.Result createPolicy(Policy policy) {
580 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700581 }
582
583 @Override
584 public List<Policy> getPolicies() {
585 return policyHandler.getPolicies();
586 }
587
Saurav Das59232cf2016-04-27 18:35:50 -0700588 @Override
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700589 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
590 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700591 }
592
593 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800594 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700595 return l2TunnelHandler.getL2Tunnels();
596 }
597
598 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800599 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700600 return l2TunnelHandler.getL2Policies();
601 }
602
Saurav Das5a356042018-04-06 20:16:01 -0700603 @Override
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700604 @Deprecated
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700605 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
606
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700607 // get both added and pending pseudowires
608 List<L2TunnelDescription> pseudowires = new ArrayList<>();
609 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
610 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700611 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700612
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700613 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
614
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700615 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
616 L2TunnelHandler.Result res;
617 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
618 res = addPseudowire(pw);
619 if (res != L2TunnelHandler.Result.SUCCESS) {
620 log.error("Pseudowire with id {} can not be instantiated !", res);
621 retRes = res;
622 }
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700623 }
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700624
625 return retRes;
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700626 }
627
628 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800629 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700630 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700631 }
632
633 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800634 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700635 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700636 }
637
638 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700639 public void rerouteNetwork() {
640 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700641 }
642
Charles Chanc81c45b2016-10-20 17:02:44 -0700643 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800644 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
645 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800646 deviceConfiguration.getRouters().forEach(device ->
647 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700648 return deviceSubnetMap;
649 }
650
Saurav Dasc88d4662017-05-15 15:34:25 -0700651
652 @Override
653 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
654 if (defaultRoutingHandler != null) {
655 return defaultRoutingHandler.getCurrentEmcpSpgMap();
656 } else {
657 return null;
658 }
659 }
660
661 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700662 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
663 if (dsNextObjStore != null) {
664 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700665 } else {
666 return ImmutableMap.of();
667 }
668 }
669
Saurav Dasceccf242017-08-03 18:30:35 -0700670 @Override
671 public void verifyGroups(DeviceId id) {
672 DefaultGroupHandler gh = groupHandlerMap.get(id);
673 if (gh != null) {
674 gh.triggerBucketCorrector();
675 }
676 }
677
Saurav Dasc568c342018-01-25 09:49:01 -0800678 @Override
679 public ImmutableMap<Link, Boolean> getSeenLinks() {
680 return linkHandler.getSeenLinks();
681 }
682
683 @Override
684 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
685 return linkHandler.getDownedPorts();
686 }
687
Pier Luigib29144d2018-01-15 18:06:43 +0100688 @Override
689 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
690 return mcastHandler.getMcastNextIds(mcastIp);
691 }
692
693 @Override
Pier Luigi96fe0772018-02-28 12:10:50 +0100694 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigib29144d2018-01-15 18:06:43 +0100695 return mcastHandler.getMcastRoles(mcastIp);
696 }
697
698 @Override
699 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
700 return mcastHandler.getMcastPaths(mcastIp);
701 }
702
Pier96f63cb2018-04-17 16:29:56 +0200703 @Override
Pierb1fe7382018-04-17 17:25:22 +0200704 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
705 ConnectPoint sourcecp) {
706 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
707 }
708
709 @Override
Pier96f63cb2018-04-17 16:29:56 +0200710 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
711 return mcastHandler.getMcastLeaders(mcastIp);
712 }
713
Charles Chan8bc75ee2018-04-17 18:56:53 -0700714 @Override
715 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
716 return defaultRoutingHandler == null ? ImmutableMap.of() :
717 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
718 }
719
720 @Override
721 public Map<DeviceId, Boolean> getShouldProgramCache() {
722 return defaultRoutingHandler == null ? ImmutableMap.of() :
723 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
724 }
725
sanghof9d0bf12015-05-19 11:57:42 -0700726 /**
Ray Milkeye4afdb52017-04-05 09:42:04 -0700727 * Extracts the application ID from the manager.
728 *
729 * @return application ID
730 */
731 public ApplicationId appId() {
732 return appId;
733 }
734
735 /**
736 * Returns the device configuration.
737 *
738 * @return device configuration
739 */
740 public DeviceConfiguration deviceConfiguration() {
741 return deviceConfiguration;
742 }
743
744 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700745 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700746 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700747 *
748 * @return next objective ID store
749 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700750 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
751 dsNextObjStore() {
752 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700753 }
754
755 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700756 * Per device next objective ID store with (device id + vlanid) as key.
757 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700758 *
759 * @return vlan next object store
760 */
761 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
762 return vlanNextObjStore;
763 }
764
765 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700766 * Per device next objective ID store with (device id + port + treatment + meta) as key.
767 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700768 *
769 * @return port next object store.
770 */
771 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
772 return portNextObjStore;
773 }
774
775 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700776 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
777 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700778 *
779 * @return MPLS-ECMP value
780 */
781 public boolean getMplsEcmp() {
782 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
783 .getConfig(this.appId, SegmentRoutingAppConfig.class);
784 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
785 }
786
787 /**
sanghof9d0bf12015-05-19 11:57:42 -0700788 * Returns the tunnel object with the tunnel ID.
789 *
790 * @param tunnelId Tunnel ID
791 * @return Tunnel reference
792 */
sangho1e575652015-05-14 00:39:53 -0700793 public Tunnel getTunnel(String tunnelId) {
794 return tunnelHandler.getTunnel(tunnelId);
795 }
796
Charles Chan7ffd81f2017-02-08 15:52:08 -0800797 // TODO Consider moving these to InterfaceService
sanghob35a6192015-04-01 13:05:26 -0700798 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800799 * Returns untagged VLAN configured on given connect point.
Charles Chan7ffd81f2017-02-08 15:52:08 -0800800 * <p>
801 * Only returns the first match if there are multiple untagged VLAN configured
802 * on the connect point.
sanghob35a6192015-04-01 13:05:26 -0700803 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800804 * @param connectPoint connect point
805 * @return untagged VLAN or null if not configured
sanghob35a6192015-04-01 13:05:26 -0700806 */
Charles Chan65238242017-06-22 18:03:14 -0700807 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800808 return interfaceService.getInterfacesByPort(connectPoint).stream()
809 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
810 .map(Interface::vlanUntagged)
811 .findFirst().orElse(null);
sanghob35a6192015-04-01 13:05:26 -0700812 }
813
sangho1e575652015-05-14 00:39:53 -0700814 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800815 * Returns tagged VLAN configured on given connect point.
816 * <p>
817 * Returns all matches if there are multiple tagged VLAN configured
818 * on the connect point.
819 *
820 * @param connectPoint connect point
821 * @return tagged VLAN or empty set if not configured
822 */
Charles Chan65238242017-06-22 18:03:14 -0700823 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800824 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
825 return interfaces.stream()
826 .map(Interface::vlanTagged)
Charles Chan65238242017-06-22 18:03:14 -0700827 .flatMap(Set::stream)
Charles Chan7ffd81f2017-02-08 15:52:08 -0800828 .collect(Collectors.toSet());
829 }
830
831 /**
832 * Returns native VLAN configured on given connect point.
833 * <p>
834 * Only returns the first match if there are multiple native VLAN configured
835 * on the connect point.
836 *
837 * @param connectPoint connect point
838 * @return native VLAN or null if not configured
839 */
Charles Chan65238242017-06-22 18:03:14 -0700840 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800841 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
842 return interfaces.stream()
843 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
844 .map(Interface::vlanNative)
845 .findFirst()
846 .orElse(null);
847 }
848
849 /**
Charles Chanf9a52702017-06-16 15:19:24 -0700850 * Returns internal VLAN for untagged hosts on given connect point.
851 * <p>
852 * The internal VLAN is either vlan-untagged for an access port,
853 * or vlan-native for a trunk port.
854 *
855 * @param connectPoint connect point
856 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
857 */
Pier Luigi96fe0772018-02-28 12:10:50 +0100858 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chanf9a52702017-06-16 15:19:24 -0700859 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
860 VlanId nativeVlanId = getNativeVlanId(connectPoint);
861 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
862 }
863
864 /**
Charles Chan65238242017-06-22 18:03:14 -0700865 * Returns optional pair device ID of given device.
866 *
867 * @param deviceId device ID
868 * @return optional pair device ID. Might be empty if pair device is not configured
869 */
870 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
871 SegmentRoutingDeviceConfig deviceConfig =
872 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
873 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
874 }
875 /**
876 * Returns optional pair device local port of given device.
877 *
878 * @param deviceId device ID
879 * @return optional pair device ID. Might be empty if pair device is not configured
880 */
881 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
882 SegmentRoutingDeviceConfig deviceConfig =
883 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
884 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
885 }
886
887 /**
Charles Chan9640c812017-08-23 13:55:39 -0700888 * Returns locations of given resolved route.
889 *
890 * @param resolvedRoute resolved route
891 * @return locations of nexthop. Might be empty if next hop is not found
892 */
893 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
894 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
895 return Optional.ofNullable(hostService.getHost(hostId))
896 .map(Host::locations).orElse(Sets.newHashSet())
897 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
898 }
899
900 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800901 * Returns vlan port map of given device.
902 *
903 * @param deviceId device id
904 * @return vlan-port multimap
905 */
906 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
907 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
908
909 interfaceService.getInterfaces().stream()
910 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
911 .forEach(intf -> {
912 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -0700913 intf.vlanTagged().forEach(vlanTagged ->
914 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
915 );
Charles Chan7ffd81f2017-02-08 15:52:08 -0800916 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
917 });
918 vlanPortMap.removeAll(VlanId.NONE);
919
920 return vlanPortMap;
921 }
922
923 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800924 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800925 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700926 *
927 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800928 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800929 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700930 */
Charles Chan65238242017-06-22 18:03:14 -0700931 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700932 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800933 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
934 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700935 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800936 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800937 + "device {} not found", deviceId);
938 return -1;
939 }
940 }
941
942 /**
943 * Returns the next objective ID for the given portNumber, given the treatment.
944 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700945 * would result in different objectives. If the next object does not exist,
946 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800947 *
948 * @param deviceId Device ID
949 * @param portNum port number on device for which NextObjective is queried
950 * @param treatment the actions to apply on the packets (should include outport)
951 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700952 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700953 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800954 */
955 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
956 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700957 TrafficSelector meta,
958 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -0800959 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
960 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -0700961 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -0800962 } else {
Charles Chane849c192016-01-11 18:28:54 -0800963 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
964 + " not found", deviceId);
965 return -1;
966 }
967 }
968
Saurav Dasc88d4662017-05-15 15:34:25 -0700969 /**
970 * Returns the group handler object for the specified device id.
971 *
972 * @param devId the device identifier
973 * @return the groupHandler object for the device id, or null if not found
974 */
Charles Chan65238242017-06-22 18:03:14 -0700975 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700976 return groupHandlerMap.get(devId);
977 }
978
979 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700980 * Returns the default routing handler object.
981 *
982 * @return the default routing handler object
983 */
984 public DefaultRoutingHandler getRoutingHandler() {
985 return defaultRoutingHandler;
986 }
987
sanghob35a6192015-04-01 13:05:26 -0700988 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700989 @Override
990 public void process(PacketContext context) {
991
992 if (context.isHandled()) {
993 return;
994 }
995
996 InboundPacket pkt = context.inPacket();
997 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800998
999 if (ethernet == null) {
1000 return;
1001 }
1002
Saurav Das7bcbe702017-06-13 15:35:54 -07001003 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1004 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001005 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001006 log.warn("Received unexpected ARP packet on {}",
1007 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -07001008 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -08001009 return;
sanghob35a6192015-04-01 13:05:26 -07001010 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -08001011 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1012 //ipHandler.addToPacketBuffer(ipv4Packet);
1013 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1014 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001015 } else {
Charles Chan50035632017-01-13 17:20:44 -08001016 // NOTE: We don't support IP learning at this moment so this
1017 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -08001018 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001019 }
Pier Ventre10bd8d12016-11-26 21:05:22 -08001020 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1021 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -08001022 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -08001023 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -08001024 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1025 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1026 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1027 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1028 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1029 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -07001030 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -07001031 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -08001032 }
1033 } else {
1034 // NOTE: We don't support IP learning at this moment so this
1035 // is not necessary. Also it causes duplication of DHCPv6 packets.
1036 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1037 }
sanghob35a6192015-04-01 13:05:26 -07001038 }
1039 }
1040 }
1041
sanghob35a6192015-04-01 13:05:26 -07001042 private class InternalEventHandler implements Runnable {
Charles Chan9b7217c2018-04-05 16:31:15 -07001043 private Event event;
1044
1045 InternalEventHandler(Event event) {
1046 this.event = event;
1047 }
1048
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001049 @Override
sanghob35a6192015-04-01 13:05:26 -07001050 public void run() {
Charles Chan9b7217c2018-04-05 16:31:15 -07001051 try {
1052 // TODO We should also change SR routing and PW to listen to TopologyEvents
1053 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1054 event.type() == LinkEvent.Type.LINK_UPDATED) {
1055 linkHandler.processLinkAdded((Link) event.subject());
1056 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1057 linkHandler.processLinkRemoved((Link) event.subject());
1058 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1059 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1060 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1061 DeviceId deviceId = ((Device) event.subject()).id();
1062 if (deviceService.isAvailable(deviceId)) {
1063 log.info("** DEVICE UP Processing device event {} "
1064 + "for available device {}",
1065 event.type(), ((Device) event.subject()).id());
1066 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001067 } else {
Charles Chan9b7217c2018-04-05 16:31:15 -07001068 log.info(" ** DEVICE DOWN Processing device event {}"
1069 + " for unavailable device {}",
1070 event.type(), ((Device) event.subject()).id());
1071 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001072 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001073 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1074 // typically these calls come when device is added first time
1075 // so port filtering rules are handled at the device_added event.
1076 // port added calls represent all ports on the device,
1077 // enabled or not.
1078 log.trace("** PORT ADDED {}/{} -> {}",
1079 ((DeviceEvent) event).subject().id(),
1080 ((DeviceEvent) event).port().number(),
1081 event.type());
1082 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1083 // these calls happen for every subsequent event
1084 // ports enabled, disabled, switch goes away, comes back
1085 log.info("** PORT UPDATED {}/{} -> {}",
1086 event.subject(),
1087 ((DeviceEvent) event).port(),
1088 event.type());
1089 processPortUpdated(((Device) event.subject()),
1090 ((DeviceEvent) event).port());
1091 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1092 // Process topology event, needed for all modules relying on
1093 // topology service for path computation
1094 TopologyEvent topologyEvent = (TopologyEvent) event;
1095 log.info("Processing topology event {}, topology age {}, reasons {}",
1096 event.type(), topologyEvent.subject().time(),
1097 topologyEvent.reasons().size());
1098 topologyHandler.processTopologyChange(topologyEvent.reasons());
1099 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1100 hostHandler.processHostAddedEvent((HostEvent) event);
1101 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1102 hostHandler.processHostMovedEvent((HostEvent) event);
1103 routeHandler.processHostMovedEvent((HostEvent) event);
1104 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1105 hostHandler.processHostRemovedEvent((HostEvent) event);
1106 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1107 hostHandler.processHostUpdatedEvent((HostEvent) event);
1108 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1109 routeHandler.processRouteAdded((RouteEvent) event);
1110 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1111 routeHandler.processRouteUpdated((RouteEvent) event);
1112 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1113 routeHandler.processRouteRemoved((RouteEvent) event);
1114 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1115 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1116 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1117 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1118 event.type() == McastEvent.Type.SINKS_ADDED ||
1119 event.type() == McastEvent.Type.SINKS_REMOVED ||
1120 event.type() == McastEvent.Type.ROUTE_ADDED ||
1121 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1122 mcastHandler.processMcastEvent((McastEvent) event);
1123 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1124 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1125 Class configClass = netcfgEvent.configClass();
1126 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1127 appCfgHandler.processAppConfigAdded(netcfgEvent);
1128 log.info("App config event .. configuring network");
1129 cfgListener.configureNetwork();
1130 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1131 log.info("Segment Routing Device Config added for {}", event.subject());
1132 cfgListener.configureNetwork();
1133 } else if (configClass.equals(XConnectConfig.class)) {
1134 xConnectHandler.processXConnectConfigAdded(netcfgEvent);
1135 } else if (configClass.equals(InterfaceConfig.class)) {
1136 log.info("Interface Config added for {}", event.subject());
1137 cfgListener.configureNetwork();
1138 } else {
1139 log.error("Unhandled config class: {}", configClass);
1140 }
1141 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1142 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1143 Class configClass = netcfgEvent.configClass();
1144 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1145 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1146 log.info("App config event .. configuring network");
1147 cfgListener.configureNetwork();
1148 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1149 log.info("Segment Routing Device Config updated for {}", event.subject());
1150 createOrUpdateDeviceConfiguration();
1151 } else if (configClass.equals(XConnectConfig.class)) {
1152 xConnectHandler.processXConnectConfigUpdated(netcfgEvent);
1153 } else if (configClass.equals(InterfaceConfig.class)) {
1154 log.info("Interface Config updated for {}", event.subject());
1155 createOrUpdateDeviceConfiguration();
1156 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1157 (InterfaceConfig) netcfgEvent.prevConfig().get());
1158 } else {
1159 log.error("Unhandled config class: {}", configClass);
1160 }
1161 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1162 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1163 Class configClass = netcfgEvent.configClass();
1164 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1165 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1166 log.info("App config event .. configuring network");
1167 cfgListener.configureNetwork();
1168 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1169 // TODO Handle sr device config removal
1170 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
1171 } else if (configClass.equals(XConnectConfig.class)) {
1172 xConnectHandler.processXConnectConfigRemoved(netcfgEvent);
1173 } else if (configClass.equals(InterfaceConfig.class)) {
1174 // TODO Handle interface removal
1175 log.info("InterfaceConfig removal is not handled in current implementation");
1176 } else {
1177 log.error("Unhandled config class: {}", configClass);
1178 }
Saurav Das201762d2018-04-21 17:19:48 -07001179 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1180 MastershipEvent me = (MastershipEvent) event;
1181 DeviceId deviceId = me.subject();
1182 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1183 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1184 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1185 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1186 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1187 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan9b7217c2018-04-05 16:31:15 -07001188 } else {
1189 log.warn("Unhandled event type: {}", event.type());
sanghob35a6192015-04-01 13:05:26 -07001190 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001191 } catch (Exception e) {
1192 log.error("SegmentRouting event handler thread thrown an exception: {}",
1193 e.getMessage(), e);
sanghob35a6192015-04-01 13:05:26 -07001194 }
sanghob35a6192015-04-01 13:05:26 -07001195 }
1196 }
1197
Saurav Das45f48152018-01-18 12:07:33 -08001198 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001199 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001200
1201 // NOTE: Punt ARP/NDP even when the device is not configured.
1202 // Host learning without network config is required for CORD config generator.
1203 routingRulePopulator.populateIpPunts(device.id());
1204 routingRulePopulator.populateArpNdpPunts(device.id());
1205
Charles Chan0b4e6182015-11-03 10:42:14 -08001206 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001207 log.warn("Device configuration unavailable. Device {} will be "
1208 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001209 return;
1210 }
Charles Chan2199c302016-04-23 17:36:10 -07001211 processDeviceAddedInternal(device.id());
1212 }
1213
1214 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001215 // Irrespective of whether the local is a MASTER or not for this device,
1216 // we need to create a SR-group-handler instance. This is because in a
1217 // multi-instance setup, any instance can initiate forwarding/next-objectives
1218 // for any switch (even if this instance is a SLAVE or not even connected
1219 // to the switch). To handle this, a default-group-handler instance is necessary
1220 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001221 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1222 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001223 DefaultGroupHandler groupHandler;
1224 try {
1225 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001226 createGroupHandler(deviceId,
1227 appId,
1228 deviceConfiguration,
1229 linkService,
1230 flowObjectiveService,
1231 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001232 } catch (DeviceConfigNotFoundException e) {
1233 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1234 return;
1235 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001236 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001237 deviceId);
1238 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001239 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001240
Charles Chan2199c302016-04-23 17:36:10 -07001241 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001242 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001243 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001244 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001245 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001246 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001247 }
Charles Chan5270ed02016-01-30 23:22:37 -08001248
Charles Chan03a73e02016-10-24 14:52:01 -07001249 appCfgHandler.init(deviceId);
Charles Chan2ff1bac2018-03-29 16:03:41 -07001250 hostHandler.init(deviceId);
Charles Chan03a73e02016-10-24 14:52:01 -07001251 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001252 }
1253
Saurav Das80980c72016-03-23 11:22:49 -07001254 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001255 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001256 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan47933752017-11-30 15:37:50 -08001257 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001258 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001259 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001260 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001261 portNextObjStore.entrySet().stream()
1262 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001263 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Das45f48152018-01-18 12:07:33 -08001264 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001265
Saurav Dasceccf242017-08-03 18:30:35 -07001266 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1267 if (gh != null) {
1268 gh.shutdown();
1269 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001270 // Note that a switch going down is associated with all of its links
1271 // going down as well, but it is treated as a single switch down event
Saurav Das5a356042018-04-06 20:16:01 -07001272 // while the link-downs are ignored. We cannot rely on the ordering of
1273 // events - i.e we cannot expect all link-downs to come before the
1274 // switch down - so we purge all seen-links for the switch before
1275 // handling route-path changes for the switch-down
Saurav Dasc88d4662017-05-15 15:34:25 -07001276 defaultRoutingHandler
Saurav Das604ab3a2018-03-18 21:28:15 -07001277 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Dasc568c342018-01-25 09:49:01 -08001278 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001279 xConnectHandler.removeDevice(device.id());
Saurav Dasa4020382018-02-14 14:14:54 -08001280
1281 // Cleanup all internal groupHandler stores for this device. Should be
1282 // done after all rerouting or rehashing has been completed
1283 groupHandlerMap.entrySet()
1284 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Das80980c72016-03-23 11:22:49 -07001285 }
1286
Saurav Das201762d2018-04-21 17:19:48 -07001287 void purgeHashedNextObjectiveStore(DeviceId devId) {
1288 dsNextObjStore.entrySet().stream()
1289 .filter(entry -> entry.getKey().deviceId().equals(devId))
1290 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1291 }
1292
Saurav Das1a129a02016-11-18 15:21:57 -08001293 private void processPortUpdated(Device device, Port port) {
1294 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1295 log.warn("Device configuration uploading. Not handling port event for"
1296 + "dev: {} port: {}", device.id(), port.number());
1297 return;
1298 }
Saurav Das018605f2017-02-18 14:05:44 -08001299
1300 if (!mastershipService.isLocalMaster(device.id())) {
1301 log.debug("Not master for dev:{} .. not handling port updated event"
1302 + "for port {}", device.id(), port.number());
1303 return;
1304 }
1305
1306 // first we handle filtering rules associated with the port
1307 if (port.isEnabled()) {
1308 log.info("Switchport {}/{} enabled..programming filters",
1309 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001310 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001311 } else {
1312 log.info("Switchport {}/{} disabled..removing filters",
1313 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001314 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001315 }
Saurav Das1a129a02016-11-18 15:21:57 -08001316
1317 // portUpdated calls are for ports that have gone down or up. For switch
1318 // to switch ports, link-events should take care of any re-routing or
1319 // group editing necessary for port up/down. Here we only process edge ports
1320 // that are already configured.
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001321 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1322 VlanId untaggedVlan = getUntaggedVlanId(cp);
1323 VlanId nativeVlan = getNativeVlanId(cp);
1324 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001325
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001326 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001327 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das1a129a02016-11-18 15:21:57 -08001328 + "dev/port: {}/{}", device.id(), port.number());
1329 return;
1330 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001331 if (untaggedVlan != null) {
1332 processEdgePort(device, port, untaggedVlan, true);
1333 }
1334 if (nativeVlan != null) {
1335 processEdgePort(device, port, nativeVlan, true);
1336 }
1337 if (!taggedVlans.isEmpty()) {
1338 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1339 }
Saurav Das1a129a02016-11-18 15:21:57 -08001340 }
1341
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001342 private void processEdgePort(Device device, Port port, VlanId vlanId,
1343 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001344 boolean portUp = port.isEnabled();
1345 if (portUp) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001346 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001347 port.number(), vlanId);
Charles Chan47933752017-11-30 15:37:50 -08001348 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Das1a129a02016-11-18 15:21:57 -08001349 } else {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001350 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001351 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001352 }
1353
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001354 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001355 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001356 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001357 } else {
1358 log.warn("Group handler not found for dev:{}. Not handling edge port"
1359 + " {} event for port:{}", device.id(),
1360 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001361 }
1362 }
sangho1e575652015-05-14 00:39:53 -07001363
Charles Chan27fe1a52017-10-20 16:06:55 -07001364 private void createOrUpdateDeviceConfiguration() {
1365 if (deviceConfiguration == null) {
Saurav Dase7f51012018-02-09 17:26:45 -08001366 log.info("Creating new DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001367 deviceConfiguration = new DeviceConfiguration(this);
1368 } else {
Saurav Dase7f51012018-02-09 17:26:45 -08001369 log.info("Updating DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001370 deviceConfiguration.updateConfig();
1371 }
1372 }
1373
Charles Chan50bb6ef2018-04-18 18:41:05 -07001374 private void createOrUpdateDefaultRoutingHandler() {
1375 if (defaultRoutingHandler == null) {
1376 log.info("Creating new DefaultRoutingHandler");
1377 defaultRoutingHandler = new DefaultRoutingHandler(this);
1378 } else {
1379 log.info("Updating DefaultRoutingHandler");
1380 defaultRoutingHandler.update(this);
1381 }
1382 }
1383
Pier Ventre10bd8d12016-11-26 21:05:22 -08001384 /**
1385 * Registers the given connect point with the NRS, this is necessary
1386 * to receive the NDP and ARP packets from the NRS.
1387 *
1388 * @param portToRegister connect point to register
1389 */
1390 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001391 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001392 portToRegister,
1393 neighbourHandler,
1394 appId
1395 );
1396 }
1397
Charles Chand6832882015-10-05 17:50:33 -07001398 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001399 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001400 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001401
Charles Chane849c192016-01-11 18:28:54 -08001402 /**
1403 * Constructs the internal network config listener.
1404 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001405 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001406 */
Charles Chan65238242017-06-22 18:03:14 -07001407 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001408 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001409 }
1410
Charles Chane849c192016-01-11 18:28:54 -08001411 /**
1412 * Reads network config and initializes related data structure accordingly.
1413 */
Charles Chan47933752017-11-30 15:37:50 -08001414 void configureNetwork() {
Saurav Dase7f51012018-02-09 17:26:45 -08001415 log.info("Configuring network ...");
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001416
1417 // Setting handling of network configuration events completable future
1418 // The completable future is needed because of the async behaviour of the configureNetwork,
1419 // listener registration and event arrival
1420 // Enables us to buffer the events and execute them when the configure network is done.
1421 networkConfigCompletion = new CompletableFuture<>();
1422
1423 // add a small delay to absorb multiple network config added notifications
1424 if (!programmingScheduled.get()) {
1425 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1426 programmingScheduled.set(true);
1427 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1428 }
1429
Charles Chan27fe1a52017-10-20 16:06:55 -07001430 createOrUpdateDeviceConfiguration();
Charles Chan4636be02015-10-07 14:21:45 -07001431
Charles Chan2c15aca2016-11-09 20:51:44 -08001432 arpHandler = new ArpHandler(srManager);
1433 icmpHandler = new IcmpHandler(srManager);
1434 ipHandler = new IpHandler(srManager);
1435 routingRulePopulator = new RoutingRulePopulator(srManager);
Charles Chan50bb6ef2018-04-18 18:41:05 -07001436 createOrUpdateDefaultRoutingHandler();
Charles Chan4636be02015-10-07 14:21:45 -07001437
1438 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1439 groupHandlerMap, tunnelStore);
1440 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1441 flowObjectiveService,
1442 tunnelHandler, policyStore);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001443 networkConfigCompletion.complete(true);
1444
Charles Chan2199c302016-04-23 17:36:10 -07001445 mcastHandler.init();
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001446
Charles Chan4636be02015-10-07 14:21:45 -07001447 }
1448
Charles Chand6832882015-10-05 17:50:33 -07001449 @Override
1450 public void event(NetworkConfigEvent event) {
Charles Chan7a888e82018-03-21 16:57:47 -07001451 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
1452 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1453 switch (event.type()) {
1454 case CONFIG_ADDED:
1455 case CONFIG_UPDATED:
1456 case CONFIG_REMOVED:
1457 log.trace("Schedule Network Config event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001458 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1459 mainEventExecutor.execute(new InternalEventHandler(event));
1460 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001461 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001462 }
Charles Chan7a888e82018-03-21 16:57:47 -07001463 break;
1464 default:
1465 break;
Charles Chand6832882015-10-05 17:50:33 -07001466 }
1467 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001468
Charles Chan50443e82018-01-03 16:26:32 -08001469 @Override
1470 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase7f51012018-02-09 17:26:45 -08001471 if (event.type() == CONFIG_REGISTERED ||
1472 event.type() == CONFIG_UNREGISTERED) {
1473 log.debug("Ignore event {} due to type mismatch", event);
1474 return false;
Charles Chan50443e82018-01-03 16:26:32 -08001475 }
Saurav Dase7f51012018-02-09 17:26:45 -08001476
1477 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1478 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1479 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscd339592018-02-23 14:18:00 -08001480 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase7f51012018-02-09 17:26:45 -08001481 log.debug("Ignore event {} due to class mismatch", event);
1482 return false;
1483 }
1484
1485 return true;
Charles Chan50443e82018-01-03 16:26:32 -08001486 }
1487
Saurav Das7bcbe702017-06-13 15:35:54 -07001488 private final class ConfigChange implements Runnable {
1489 @Override
1490 public void run() {
1491 programmingScheduled.set(false);
Saurav Dase7f51012018-02-09 17:26:45 -08001492 log.info("Reacting to config changes after buffer delay");
Saurav Das7bcbe702017-06-13 15:35:54 -07001493 for (Device device : deviceService.getDevices()) {
1494 processDeviceAdded(device);
1495 }
1496 defaultRoutingHandler.startPopulationProcess();
1497 }
1498 }
Charles Chand6832882015-10-05 17:50:33 -07001499 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001500
Charles Chan7a888e82018-03-21 16:57:47 -07001501 private class InternalLinkListener implements LinkListener {
1502 @Override
1503 public void event(LinkEvent event) {
1504 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1505 event.type() == LinkEvent.Type.LINK_UPDATED ||
1506 event.type() == LinkEvent.Type.LINK_REMOVED) {
1507 log.trace("Schedule Link event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001508 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1509 mainEventExecutor.execute(new InternalEventHandler(event));
1510 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001511 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001512 }
Charles Chan7a888e82018-03-21 16:57:47 -07001513 }
1514 }
1515 }
1516
1517 private class InternalDeviceListener implements DeviceListener {
1518 @Override
1519 public void event(DeviceEvent event) {
1520 switch (event.type()) {
1521 case DEVICE_ADDED:
1522 case PORT_UPDATED:
1523 case PORT_ADDED:
1524 case DEVICE_UPDATED:
1525 case DEVICE_AVAILABILITY_CHANGED:
1526 log.trace("Schedule Device event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001527 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1528 mainEventExecutor.execute(new InternalEventHandler(event));
1529 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001530 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001531 }
Charles Chan7a888e82018-03-21 16:57:47 -07001532 break;
1533 default:
1534 }
1535 }
1536 }
1537
1538 private class InternalTopologyListener implements TopologyListener {
1539 @Override
1540 public void event(TopologyEvent event) {
1541 switch (event.type()) {
1542 case TOPOLOGY_CHANGED:
1543 log.trace("Schedule Topology event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001544 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1545 mainEventExecutor.execute(new InternalEventHandler(event));
1546 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001547 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001548 }
Charles Chan7a888e82018-03-21 16:57:47 -07001549 break;
1550 default:
1551 }
1552 }
1553 }
1554
Charles Chan68aa62d2015-11-09 16:37:23 -08001555 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001556 @Override
1557 public void event(HostEvent event) {
Charles Chan68aa62d2015-11-09 16:37:23 -08001558 switch (event.type()) {
1559 case HOST_ADDED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001560 case HOST_MOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001561 case HOST_REMOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001562 case HOST_UPDATED:
Charles Chan7a888e82018-03-21 16:57:47 -07001563 log.trace("Schedule Host event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001564 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chan68aa62d2015-11-09 16:37:23 -08001565 break;
1566 default:
1567 log.warn("Unsupported host event type: {}", event.type());
1568 break;
1569 }
1570 }
1571 }
1572
Charles Chand55e84d2016-03-30 17:54:24 -07001573 private class InternalMcastListener implements McastListener {
1574 @Override
1575 public void event(McastEvent event) {
1576 switch (event.type()) {
Pier3ee24552018-03-14 16:47:32 -07001577 case SOURCES_ADDED:
1578 case SOURCES_REMOVED:
1579 case SINKS_ADDED:
1580 case SINKS_REMOVED:
Charles Chand55e84d2016-03-30 17:54:24 -07001581 case ROUTE_REMOVED:
Pier96f63cb2018-04-17 16:29:56 +02001582 case ROUTE_ADDED:
Charles Chan7a888e82018-03-21 16:57:47 -07001583 log.trace("Schedule Mcast event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001584 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi9930da52018-02-02 16:19:11 +01001585 break;
Charles Chand55e84d2016-03-30 17:54:24 -07001586 default:
Charles Chan7a888e82018-03-21 16:57:47 -07001587 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chand55e84d2016-03-30 17:54:24 -07001588 break;
1589 }
1590 }
1591 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001592
Charles Chan03a73e02016-10-24 14:52:01 -07001593 private class InternalRouteEventListener implements RouteListener {
1594 @Override
1595 public void event(RouteEvent event) {
1596 switch (event.type()) {
1597 case ROUTE_ADDED:
Charles Chan03a73e02016-10-24 14:52:01 -07001598 case ROUTE_UPDATED:
Charles Chan03a73e02016-10-24 14:52:01 -07001599 case ROUTE_REMOVED:
Charles Chan2fde6d42017-08-23 14:46:43 -07001600 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan7a888e82018-03-21 16:57:47 -07001601 log.trace("Schedule Route event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001602 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan2fde6d42017-08-23 14:46:43 -07001603 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001604 default:
Charles Chan7a888e82018-03-21 16:57:47 -07001605 log.warn("Unsupported route event type: {}", event.type());
Charles Chan03a73e02016-10-24 14:52:01 -07001606 break;
1607 }
1608 }
1609 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001610
Charles Chan50bb6ef2018-04-18 18:41:05 -07001611 private class InternalMastershipListener implements MastershipListener {
1612 @Override
1613 public void event(MastershipEvent event) {
Charles Chan50bb6ef2018-04-18 18:41:05 -07001614 switch (event.type()) {
Saurav Das201762d2018-04-21 17:19:48 -07001615 case MASTER_CHANGED:
1616 log.debug("Mastership event: {}/{}", event.subject(),
1617 event.roleInfo());
1618 mainEventExecutor.execute(new InternalEventHandler(event));
1619 break;
1620 case BACKUPS_CHANGED:
1621 case SUSPENDED:
1622 default:
1623 log.debug("Mastership event type {} not handled", event.type());
1624 break;
Charles Chan50bb6ef2018-04-18 18:41:05 -07001625 }
1626 }
1627 }
1628
Saurav Das201762d2018-04-21 17:19:48 -07001629 class InternalClusterListener implements ClusterEventListener {
1630 private Instant lastClusterEvent = Instant.EPOCH;
1631
1632 long timeSinceLastClusterEvent() {
1633 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
1634 }
1635
1636 @Override
1637 public void event(ClusterEvent event) {
1638 switch (event.type()) {
1639 case INSTANCE_ACTIVATED:
1640 case INSTANCE_ADDED:
1641 case INSTANCE_READY:
1642 log.debug("Cluster event {} ignored", event.type());
1643 break;
1644 case INSTANCE_DEACTIVATED:
1645 case INSTANCE_REMOVED:
1646 log.info("** Cluster event {}", event.type());
1647 lastClusterEvent = Instant.now();
1648 break;
1649 default:
1650 break;
1651 }
1652
1653 }
1654
1655 }
1656
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001657 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1658 try {
1659 Set<Interface> intfs = conf.getInterfaces();
1660 Set<Interface> prevIntfs = prevConf.getInterfaces();
1661
1662 // Now we only handle one interface config at each port.
1663 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1664 log.warn("Interface update aborted - one at a time is allowed, " +
1665 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1666 return;
1667 }
1668
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001669 //The system is in an incoherent state, abort
1670 if (defaultRoutingHandler == null) {
1671 log.warn("Interface update aborted, defaultRoutingHandler is null");
1672 return;
1673 }
1674
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001675 Interface intf = intfs.stream().findFirst().get();
1676 Interface prevIntf = prevIntfs.stream().findFirst().get();
1677
1678 DeviceId deviceId = intf.connectPoint().deviceId();
1679 PortNumber portNum = intf.connectPoint().port();
1680
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001681 removeSubnetConfig(prevIntf.connectPoint(),
1682 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1683 new HashSet<>(intf.ipAddressesList())));
1684
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001685 if (!prevIntf.vlanNative().equals(VlanId.NONE)
1686 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
1687 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1688 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
1689 // Update filtering objective and L2IG group bucket
1690 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
1691 } else {
1692 // RemoveVlanNative
1693 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1694 }
1695 }
1696
1697 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
1698 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1699 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1700 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
1701 // Update filtering objective and L2IG group bucket
1702 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
1703 } else {
1704 // RemoveVlanUntagged
1705 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1706 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001707 }
1708
1709 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1710 // RemoveVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001711 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
1712 .filter(i -> !intf.vlanUntagged().equals(i))
1713 .filter(i -> !intf.vlanNative().equals(i))
1714 .forEach(vlanId -> updateVlanConfigInternal(
1715 deviceId, portNum, vlanId, false, false));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001716 }
1717
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001718 if (!intf.vlanNative().equals(VlanId.NONE)
1719 && !prevIntf.vlanNative().equals(intf.vlanNative())
1720 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1721 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
1722 // Update filtering objective and L2IG group bucket
1723 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
1724 } else {
1725 // AddVlanNative
1726 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1727 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001728 }
1729
1730 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1731 // AddVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001732 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
1733 .filter(i -> !prevIntf.vlanUntagged().equals(i))
1734 .filter(i -> !prevIntf.vlanNative().equals(i))
1735 .forEach(vlanId -> updateVlanConfigInternal(
1736 deviceId, portNum, vlanId, false, true)
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001737 );
1738 }
1739
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001740 if (!intf.vlanUntagged().equals(VlanId.NONE)
1741 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1742 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
1743 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
1744 // Update filtering objective and L2IG group bucket
1745 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
1746 } else {
1747 // AddVlanUntagged
1748 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1749 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001750 }
1751 addSubnetConfig(prevIntf.connectPoint(),
1752 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1753 new HashSet<>(prevIntf.ipAddressesList())));
1754 } catch (ConfigException e) {
1755 log.error("Error in configuration");
1756 }
1757 }
1758
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001759 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
1760 VlanId vlanId, boolean pushVlan) {
1761 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1762 if (grpHandler == null) {
1763 log.warn("Failed to retrieve group handler for device {}", deviceId);
1764 return;
1765 }
1766
1767 // Update filtering objective for a single port
1768 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
1769 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
1770
1771 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
1772 // Update L2IG bucket of the port
1773 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
1774 } else {
1775 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1776 }
1777 }
1778
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001779 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1780 VlanId vlanId, boolean pushVlan, boolean install) {
1781 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1782 if (grpHandler == null) {
1783 log.warn("Failed to retrieve group handler for device {}", deviceId);
1784 return;
1785 }
1786
1787 // Update filtering objective for a single port
1788 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1789
1790 // Update filtering objective for multicast ingress port
1791 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1792
1793 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1794
1795 if (nextId != -1 && !install) {
1796 // Update next objective for a single port as an output port
1797 // Remove a single port from L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001798 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001799 // Remove L2 Bridging rule and L3 Unicast rule to the host
1800 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1801 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1802 // only if there is no port configured on that VLAN ID
1803 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1804 // Remove broadcast forwarding rule for the VLAN
1805 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1806 // Remove L2FG for VLAN
1807 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1808 } else {
1809 // Remove L2IG of the port
1810 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1811 }
1812 } else if (install) {
1813 if (nextId != -1) {
1814 // Add a single port to L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001815 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001816 } else {
1817 // Create L2FG for VLAN
1818 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1819 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1820 }
1821 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1822 } else {
1823 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1824 }
1825 }
1826
1827 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1828 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1829 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1830
1831 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1832 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1833 .filter(intf -> !intf.connectPoint().equals(cp))
1834 .flatMap(intf -> intf.ipAddressesList().stream())
1835 .collect(Collectors.toSet());
1836 // 1. Partial subnet population
1837 // Remove routing rules for removed subnet from previous configuration,
1838 // which does not also exist in other interfaces in the same device
1839 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1840 .map(InterfaceIpAddress::subnetAddress)
1841 .collect(Collectors.toSet());
1842
1843 defaultRoutingHandler.revokeSubnet(
1844 ipPrefixSet.stream()
1845 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1846 .collect(Collectors.toSet()));
1847
1848 // 2. Interface IP punts
1849 // Remove IP punts for old Intf address
1850 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1851 .map(InterfaceIpAddress::ipAddress)
1852 .collect(Collectors.toSet());
1853 ipAddressSet.stream()
1854 .map(InterfaceIpAddress::ipAddress)
1855 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1856 .forEach(interfaceIpAddress ->
1857 routingRulePopulator.revokeSingleIpPunts(
1858 cp.deviceId(), interfaceIpAddress));
1859
1860 // 3. Host unicast routing rule
1861 // Remove unicast routing rule
1862 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1863 }
1864
1865 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1866 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1867 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1868
1869 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1870 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1871 .filter(intf -> !intf.connectPoint().equals(cp))
1872 .flatMap(intf -> intf.ipAddressesList().stream())
1873 .collect(Collectors.toSet());
1874 // 1. Partial subnet population
1875 // Add routing rules for newly added subnet, which does not also exist in
1876 // other interfaces in the same device
1877 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1878 .map(InterfaceIpAddress::subnetAddress)
1879 .collect(Collectors.toSet());
1880
1881 defaultRoutingHandler.populateSubnet(
1882 Collections.singleton(cp),
1883 ipPrefixSet.stream()
1884 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1885 .collect(Collectors.toSet()));
1886
1887 // 2. Interface IP punts
1888 // Add IP punts for new Intf address
1889 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1890 .map(InterfaceIpAddress::ipAddress)
1891 .collect(Collectors.toSet());
1892 ipAddressSet.stream()
1893 .map(InterfaceIpAddress::ipAddress)
1894 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1895 .forEach(interfaceIpAddress ->
1896 routingRulePopulator.populateSingleIpPunts(
1897 cp.deviceId(), interfaceIpAddress));
1898
1899 // 3. Host unicast routing rule
1900 // Add unicast routing rule
1901 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1902 }
sanghob35a6192015-04-01 13:05:26 -07001903}