blob: 032220123f1433f9ba03a56dbb1f3f763911429a [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sangho80f11cb2015-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 Hart61e24e12017-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 Chanf0ae41e2017-08-23 13:55:39 -070022import com.google.common.collect.Sets;
sangho80f11cb2015-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 Chan873661e2017-11-30 15:37:50 -080026import org.apache.felix.scr.annotations.Modified;
27import org.apache.felix.scr.annotations.Property;
sangho80f11cb2015-04-01 13:05:26 -070028import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho27462c62015-05-14 00:39:53 -070030import org.apache.felix.scr.annotations.Service;
sangho80f11cb2015-04-01 13:05:26 -070031import org.onlab.packet.Ethernet;
Pier Ventreb6b81d52016-12-02 08:16:05 -080032import org.onlab.packet.ICMP6;
Charles Chan77277672015-10-20 16:24:19 -070033import org.onlab.packet.IPv4;
Pier Ventreb6a7f342016-11-26 21:05:22 -080034import org.onlab.packet.IPv6;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070035import org.onlab.packet.IpAddress;
Charles Chan77277672015-10-20 16:24:19 -070036import org.onlab.packet.IpPrefix;
Jonathan Hart54541d12016-04-12 15:39:44 -070037import org.onlab.packet.VlanId;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070038import org.onlab.util.KryoNamespace;
Charles Chan873661e2017-11-30 15:37:50 -080039import org.onlab.util.Tools;
Saurav Dasc3604f12016-03-23 11:22:49 -070040import org.onosproject.cfg.ComponentConfigService;
Saurav Das00e553b2018-04-21 17:19:48 -070041import org.onosproject.cluster.ClusterEvent;
42import org.onosproject.cluster.ClusterEventListener;
Pier Luigi580fd8a2018-01-16 10:47:50 +010043import org.onosproject.cluster.ClusterService;
Pierdb27b8d2018-04-17 16:29:56 +020044import org.onosproject.cluster.NodeId;
sangho80f11cb2015-04-01 13:05:26 -070045import org.onosproject.core.ApplicationId;
46import org.onosproject.core.CoreService;
47import org.onosproject.event.Event;
Charles Chanfbcb8812018-04-18 18:41:05 -070048import org.onosproject.mastership.MastershipEvent;
49import org.onosproject.mastership.MastershipListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070050import org.onosproject.mastership.MastershipService;
Pier1f87aca2018-03-14 16:47:32 -070051import org.onosproject.mcast.api.McastEvent;
52import org.onosproject.mcast.api.McastListener;
53import org.onosproject.mcast.api.MulticastRouteService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080054import org.onosproject.net.ConnectPoint;
Jonathan Hart54541d12016-04-12 15:39:44 -070055import org.onosproject.net.Device;
56import org.onosproject.net.DeviceId;
Charles Chanf0ae41e2017-08-23 13:55:39 -070057import org.onosproject.net.Host;
58import org.onosproject.net.HostId;
Jonathan Hart54541d12016-04-12 15:39:44 -070059import org.onosproject.net.Link;
60import org.onosproject.net.Port;
Charles Chanf4586112015-11-09 16:37:23 -080061import org.onosproject.net.PortNumber;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070062import org.onosproject.net.config.ConfigException;
Charles Chan72f556a2015-10-05 17:50:33 -070063import org.onosproject.net.config.ConfigFactory;
64import org.onosproject.net.config.NetworkConfigEvent;
Charles Chan72f556a2015-10-05 17:50:33 -070065import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070066import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkeyae0068a2017-08-15 11:02:29 -070067import org.onosproject.net.config.basics.InterfaceConfig;
68import org.onosproject.net.config.basics.McastConfig;
Charles Chan72f556a2015-10-05 17:50:33 -070069import org.onosproject.net.config.basics.SubjectFactories;
Saurav Dase6c448a2018-01-18 12:07:33 -080070import org.onosproject.net.device.DeviceAdminService;
Jonathan Hart54541d12016-04-12 15:39:44 -070071import org.onosproject.net.device.DeviceEvent;
72import org.onosproject.net.device.DeviceListener;
73import org.onosproject.net.device.DeviceService;
Charles Chanf4586112015-11-09 16:37:23 -080074import org.onosproject.net.flow.TrafficSelector;
75import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart54541d12016-04-12 15:39:44 -070076import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chanf4586112015-11-09 16:37:23 -080077import org.onosproject.net.host.HostEvent;
78import org.onosproject.net.host.HostListener;
Charles Chan873661e2017-11-30 15:37:50 -080079import org.onosproject.net.host.HostLocationProbingService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080080import org.onosproject.net.host.HostService;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070081import org.onosproject.net.host.InterfaceIpAddress;
Pierdb27b8d2018-04-17 16:29:56 +020082import org.onosproject.net.intent.WorkPartitionService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070083import org.onosproject.net.intf.Interface;
84import org.onosproject.net.intf.InterfaceService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080085import org.onosproject.net.link.LinkEvent;
86import org.onosproject.net.link.LinkListener;
87import org.onosproject.net.link.LinkService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070088import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventreb6a7f342016-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 Luigid8a15162018-02-15 16:33:08 +010093import org.onosproject.net.topology.TopologyEvent;
94import org.onosproject.net.topology.TopologyListener;
Charles Chanc91c8782016-03-30 17:54:24 -070095import org.onosproject.net.topology.TopologyService;
Charles Chanf0ae41e2017-08-23 13:55:39 -070096import org.onosproject.routeservice.ResolvedRoute;
Ray Milkeyae0068a2017-08-15 11:02:29 -070097import org.onosproject.routeservice.RouteEvent;
98import org.onosproject.routeservice.RouteListener;
99import org.onosproject.routeservice.RouteService;
Charles Chanc91c8782016-03-30 17:54:24 -0700100import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
101import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventre6b19e482016-11-07 16:21:04 -0800102import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkeyae0068a2017-08-15 11:02:29 -0700103import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chan82f19972016-05-17 13:13:55 -0700104import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chanc91c8782016-03-30 17:54:24 -0700105import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -0700106import org.onosproject.segmentrouting.grouphandler.DestinationSet;
107import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Pier Luigi69f774d2018-02-28 12:10:50 +0100108import org.onosproject.segmentrouting.mcast.McastHandler;
109import org.onosproject.segmentrouting.mcast.McastRole;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700110import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700111import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800112import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700113import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800114
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800115import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkeyae0068a2017-08-15 11:02:29 -0700116import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800117import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800118import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
119
Saurav Das261c3002017-06-13 15:35:54 -0700120import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Pier Luigi0f9635b2018-01-15 18:06:43 +0100121import org.onosproject.segmentrouting.storekey.McastStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -0700122import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -0800123import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -0700124import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hart54541d12016-04-12 15:39:44 -0700125import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-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 Chan873661e2017-11-30 15:37:50 -0800130import org.osgi.service.component.ComponentContext;
sangho80f11cb2015-04-01 13:05:26 -0700131import org.slf4j.Logger;
132import org.slf4j.LoggerFactory;
133
Saurav Das00e553b2018-04-21 17:19:48 -0700134import java.time.Instant;
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700135import java.util.ArrayList;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800136import java.util.Collections;
Charles Chan873661e2017-11-30 15:37:50 -0800137import java.util.Dictionary;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800138import java.util.HashSet;
139import java.util.List;
140import java.util.Map;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800141import java.util.Optional;
142import java.util.Set;
Andrea Campanella060cad82018-04-17 12:09:34 +0200143import java.util.concurrent.CompletableFuture;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800144import java.util.concurrent.ConcurrentHashMap;
Andrea Campanella060cad82018-04-17 12:09:34 +0200145import java.util.concurrent.CopyOnWriteArrayList;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800146import java.util.concurrent.Executors;
147import java.util.concurrent.ScheduledExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800148import java.util.concurrent.TimeUnit;
149import java.util.concurrent.atomic.AtomicBoolean;
150import java.util.stream.Collectors;
sangho80f11cb2015-04-01 13:05:26 -0700151
Charles Chand6d25332016-02-26 22:19:52 -0800152import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800153import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700154import static org.onlab.util.Tools.groupedThreads;
Saurav Dase321cff2018-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 Chand6d25332016-02-26 22:19:52 -0800157
Charles Chanb7f75ac2016-01-11 18:28:54 -0800158/**
159 * Segment routing manager.
160 */
Jonathan Hart54541d12016-04-12 15:39:44 -0700161@Service
162@Component(immediate = true)
sangho27462c62015-05-14 00:39:53 -0700163public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700164
Charles Chan46fdfaf2016-11-09 20:51:44 -0800165 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan910be6a2017-08-23 14:46:43 -0700166 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sangho80f11cb2015-04-01 13:05:26 -0700167
168 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700169 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700170
171 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventreb6a7f342016-11-26 21:05:22 -0800172 private NeighbourResolutionService neighbourResolutionService;
173
174 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100175 public CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700176
177 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700178 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700179
180 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700181 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700182
183 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan873661e2017-11-30 15:37:50 -0800184 HostLocationProbingService probingService;
185
186 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100187 public DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700188
189 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Saurav Dase6c448a2018-01-18 12:07:33 -0800190 DeviceAdminService deviceAdminService;
191
192 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800193 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700194
195 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100196 public LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700197
Charles Chan82ab1932016-01-30 23:22:37 -0800198 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800199 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700200
201 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800202 public StorageService storageService;
Charles Chandebfea32016-10-24 14:52:01 -0700203
204 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100205 public MulticastRouteService multicastRouteService;
Charles Chandebfea32016-10-24 14:52:01 -0700206
207 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan83163812018-01-09 13:45:07 -0800208 public TopologyService topologyService;
Charles Chandebfea32016-10-24 14:52:01 -0700209
210 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700211 RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800212
213 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800214 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800215
Saurav Dasc3604f12016-03-23 11:22:49 -0700216 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800217 public InterfaceService interfaceService;
218
Pier Luigi580fd8a2018-01-16 10:47:50 +0100219 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
220 public ClusterService clusterService;
221
222 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pierdb27b8d2018-04-17 16:29:56 +0200223 public WorkPartitionService workPartitionService;
Pier Luigi580fd8a2018-01-16 10:47:50 +0100224
Charles Chan873661e2017-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 Chandebfea32016-10-24 14:52:01 -0700229 ArpHandler arpHandler = null;
230 IcmpHandler icmpHandler = null;
231 IpHandler ipHandler = null;
232 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700233 ApplicationId appId;
234 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700235
Charles Chandebfea32016-10-24 14:52:01 -0700236 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700237 private TunnelHandler tunnelHandler = null;
238 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700239 private InternalPacketProcessor processor = null;
240 private InternalLinkListener linkListener = null;
241 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700242 private AppConfigHandler appCfgHandler = null;
Pier Luigi69f774d2018-02-28 12:10:50 +0100243 public XConnectHandler xConnectHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800244 McastHandler mcastHandler = null;
245 HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800246 private RouteHandler routeHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800247 LinkHandler linkHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800248 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800249 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigid8a15162018-02-15 16:33:08 +0100250 private TopologyHandler topologyHandler = null;
Charles Chan82ab1932016-01-30 23:22:37 -0800251 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700252 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
253 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700254 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigid8a15162018-02-15 16:33:08 +0100255 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chanfbcb8812018-04-18 18:41:05 -0700256 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
Saurav Das00e553b2018-04-21 17:19:48 -0700257 final InternalClusterListener clusterListener = new InternalClusterListener();
Andrea Campanella060cad82018-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 Chan39b75522018-04-21 00:44:29 -0700260 private List<Event> queuedEvents = new CopyOnWriteArrayList<>();
sangho80f11cb2015-04-01 13:05:26 -0700261
Charles Chan52003922018-04-05 16:31:15 -0700262 // Handles device, link, topology and network config events
Charles Chanfbcb8812018-04-18 18:41:05 -0700263 private ScheduledExecutorService mainEventExecutor;
sangho80f11cb2015-04-01 13:05:26 -0700264
Charles Chanfbcb8812018-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 Chan52003922018-04-05 16:31:15 -0700269
270 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chanb7f75ac2016-01-11 18:28:54 -0800271 /**
Saurav Das261c3002017-06-13 15:35:54 -0700272 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700273 * Used to keep track on MPLS group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800274 */
Charles Chan873661e2017-11-30 15:37:50 -0800275 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das261c3002017-06-13 15:35:54 -0700276 dsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800277 /**
Charles Chan53de91f2017-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 Chanb7f75ac2016-01-11 18:28:54 -0800280 */
Charles Chan873661e2017-11-30 15:37:50 -0800281 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800282 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800283 /**
Charles Chan53de91f2017-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 Chanb7f75ac2016-01-11 18:28:54 -0800286 */
Charles Chan873661e2017-11-30 15:37:50 -0800287 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800288 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800289
Saurav Das2d94d312015-11-24 23:21:05 -0800290 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
291 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700292
Saurav Das261c3002017-06-13 15:35:54 -0700293 private AtomicBoolean programmingScheduled = new AtomicBoolean();
294
Charles Chanc91c8782016-03-30 17:54:24 -0700295 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700296 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
297 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700298 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700299 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800300 public SegmentRoutingDeviceConfig createConfig() {
301 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700302 }
303 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800304
Charles Chanc91c8782016-03-30 17:54:24 -0700305 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700306 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
307 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700308 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800309 @Override
310 public SegmentRoutingAppConfig createConfig() {
311 return new SegmentRoutingAppConfig();
312 }
313 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800314
Charles Chan82f19972016-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 Ventre6b19e482016-11-07 16:21:04 -0800324
Charles Chanc91c8782016-03-30 17:54:24 -0700325 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700326 new ConfigFactory<ApplicationId, McastConfig>(
327 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700328 McastConfig.class, "multicast") {
329 @Override
330 public McastConfig createConfig() {
331 return new McastConfig();
332 }
333 };
334
Charles Chan1963f4f2016-02-18 14:22:42 -0800335 /**
336 * Segment Routing App ID.
337 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800338 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan10b0fb72017-02-02 16:20:42 -0800339
Charles Chanb7f75ac2016-01-11 18:28:54 -0800340 /**
341 * The default VLAN ID assigned to the interfaces without subnet config.
342 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800343 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das7c305372015-10-28 12:39:42 -0700344
sangho80f11cb2015-04-01 13:05:26 -0700345 @Activate
Charles Chan873661e2017-11-30 15:37:50 -0800346 protected void activate(ComponentContext context) {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800347 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700348
Charles Chanfbcb8812018-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 Vavilapalli7cd16712015-05-04 09:48:09 -0700354 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700355 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700356 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700357 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700358 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700359 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700360 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700361 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700362 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700363
Charles Chan10b0fb72017-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 Hart54541d12016-04-12 15:39:44 -0700369 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700370 .withTimestampProvider((k, v) -> new WallClockTimestamp())
371 .build();
372
Saurav Das2d94d312015-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 Hart54541d12016-04-12 15:39:44 -0700378 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800379 .withTimestampProvider((k, v) -> new WallClockTimestamp())
380 .build();
381
sangho4a5c42a2015-05-20 22:16:38 -0700382 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
383 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700384 tunnelStore = tunnelMapBuilder
385 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700386 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700387 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700388 .build();
389
390 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
391 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700392 policyStore = policyMapBuilder
393 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700394 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700395 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700396 .build();
397
Saurav Dasc3604f12016-03-23 11:22:49 -0700398 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800399 "purgeOnDisconnection", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700400 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800401 "purgeOnDisconnection", "true");
Pier Luigib9632ba2017-01-12 18:14:58 -0800402 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
403 "requestInterceptsEnabled", "false");
Charles Chanc1909e12017-08-08 15:13:37 -0700404 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800405 "requestInterceptsEnabled", "false");
Charles Chan9597f8d2017-07-24 15:56:10 -0700406 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800407 "arpEnabled", "false");
Pier Luigi0ebeb312017-02-02 22:31:34 -0800408 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
409 "greedyLearningIpv6", "true");
Charles Chancf8ea472017-02-28 15:15:17 -0800410 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
411 "forceUnprovision", "true");
Charles Chanc7b73c72017-08-10 16:57:28 -0700412 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan4c95c0d2017-07-20 16:16:25 -0700413 "distributed", "true");
Charles Chan804772f2017-08-14 11:42:11 -0700414 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
415 "multihomingEnabled", "true");
Charles Chan0c9c19f2017-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 Chan873661e2017-11-30 15:37:50 -0800420 compCfgService.registerProperties(getClass());
421 modified(context);
Saurav Dasc3604f12016-03-23 11:22:49 -0700422
Charles Chan2b078ae2015-10-14 11:24:40 -0700423 processor = new InternalPacketProcessor();
424 linkListener = new InternalLinkListener();
425 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700426 appCfgHandler = new AppConfigHandler(this);
427 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700428 mcastHandler = new McastHandler(this);
429 hostHandler = new HostHandler(this);
Saurav Dase6c448a2018-01-18 12:07:33 -0800430 linkHandler = new LinkHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700431 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800432 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800433 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigid8a15162018-02-15 16:33:08 +0100434 topologyHandler = new TopologyHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700435
Charles Chand6d25332016-02-26 22:19:52 -0800436 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700437 cfgService.registerConfigFactory(deviceConfigFactory);
438 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700439 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700440 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase321cff2018-02-09 17:26:45 -0800441 log.info("Configuring network before adding listeners");
Andrea Campanella060cad82018-04-17 12:09:34 +0200442
Charles Chan6961f222018-01-04 14:26:07 -0800443 cfgListener.configureNetwork();
444
Charles Chan82ab1932016-01-30 23:22:37 -0800445 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700446 packetService.addProcessor(processor, PacketProcessor.director(2));
447 linkService.addListener(linkListener);
448 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700449 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700450 routeService.addListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100451 topologyService.addListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700452 mastershipService.addListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700453 clusterService.addListener(clusterListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700454
Charles Chanc12c3d02018-03-09 15:53:44 -0800455 linkHandler.init();
Andreas Pantelopoulos75eef062018-01-11 07:53:48 -0800456 l2TunnelHandler.init();
457
Andrea Campanella060cad82018-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 Chan39b75522018-04-21 00:44:29 -0700462 queuedEvents.forEach(event -> {
Andrea Campanella060cad82018-04-17 12:09:34 +0200463 mainEventExecutor.execute(new InternalEventHandler(event));
464 });
465 });
466
sangho80f11cb2015-04-01 13:05:26 -0700467 log.info("Started");
468 }
469
Saurav Dase6c448a2018-01-18 12:07:33 -0800470 KryoNamespace.Builder createSerializer() {
Jonathan Hart54541d12016-04-12 15:39:44 -0700471 return new KryoNamespace.Builder()
472 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700473 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulos4c7de132018-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 Hart54541d12016-04-12 15:39:44 -0700488 );
489 }
490
sangho80f11cb2015-04-01 13:05:26 -0700491 @Deactivate
492 protected void deactivate() {
Charles Chanfbcb8812018-04-18 18:41:05 -0700493 mainEventExecutor.shutdown();
494 hostEventExecutor.shutdown();
495 routeEventExecutor.shutdown();
496 mcastEventExecutor.shutdown();
497
Charles Chan72f556a2015-10-05 17:50:33 -0700498 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700499 cfgService.unregisterConfigFactory(deviceConfigFactory);
500 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700501 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700502 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan873661e2017-11-30 15:37:50 -0800503 compCfgService.unregisterProperties(getClass(), false);
Charles Chan72f556a2015-10-05 17:50:33 -0700504
Charles Chanfd48c222017-06-19 00:43:31 -0700505 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700506 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700507 linkService.removeListener(linkListener);
508 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700509 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700510 routeService.removeListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100511 topologyService.removeListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700512 mastershipService.removeListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700513 clusterService.removeListener(clusterListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700514
Charles Chan2e71ef32017-02-23 15:44:08 -0800515 neighbourResolutionService.unregisterNeighbourHandlers(appId);
516
sangho80f11cb2015-04-01 13:05:26 -0700517 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700518 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700519 deviceListener = null;
Charles Chan05bb1702018-04-19 13:10:01 -0700520 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chan2b078ae2015-10-14 11:24:40 -0700521 groupHandlerMap.clear();
Saurav Das49368392018-04-23 18:42:12 -0700522 defaultRoutingHandler.shutdown();
Charles Chan2b078ae2015-10-14 11:24:40 -0700523
Saurav Das261c3002017-06-13 15:35:54 -0700524 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800525 vlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700526 portNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700527 tunnelStore.destroy();
528 policyStore.destroy();
Pier Luigi35dab3f2018-01-25 16:16:02 +0100529
530 mcastHandler.terminate();
sangho80f11cb2015-04-01 13:05:26 -0700531 log.info("Stopped");
532 }
533
Charles Chan873661e2017-11-30 15:37:50 -0800534 @Modified
535 private void modified(ComponentContext context) {
536 Dictionary<?, ?> properties = context.getProperties();
537 if (properties == null) {
538 return;
539 }
540
541 String strActiveProving = Tools.get(properties, "activeProbing");
542 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
543
544 if (expectActiveProbing != activeProbing) {
545 activeProbing = expectActiveProbing;
546 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
547 }
548 }
549
sangho27462c62015-05-14 00:39:53 -0700550 @Override
551 public List<Tunnel> getTunnels() {
552 return tunnelHandler.getTunnels();
553 }
554
555 @Override
sanghobd812f82015-06-29 14:58:47 -0700556 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
557 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700558 }
559
560 @Override
sanghobd812f82015-06-29 14:58:47 -0700561 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700562 for (Policy policy: policyHandler.getPolicies()) {
563 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
564 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
565 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
566 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700567 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700568 }
569 }
570 }
sanghobd812f82015-06-29 14:58:47 -0700571 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700572 }
573
574 @Override
sanghobd812f82015-06-29 14:58:47 -0700575 public PolicyHandler.Result removePolicy(Policy policy) {
576 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700577 }
578
579 @Override
sanghobd812f82015-06-29 14:58:47 -0700580 public PolicyHandler.Result createPolicy(Policy policy) {
581 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700582 }
583
584 @Override
585 public List<Policy> getPolicies() {
586 return policyHandler.getPolicies();
587 }
588
Saurav Das07c74602016-04-27 18:35:50 -0700589 @Override
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700590 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
591 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700592 }
593
594 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800595 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700596 return l2TunnelHandler.getL2Tunnels();
597 }
598
599 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800600 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700601 return l2TunnelHandler.getL2Policies();
602 }
603
Saurav Dasdebcf882018-04-06 20:16:01 -0700604 @Override
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700605 @Deprecated
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700606 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
607
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700608 // get both added and pending pseudowires
609 List<L2TunnelDescription> pseudowires = new ArrayList<>();
610 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
611 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700612 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700613
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700614 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
615
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700616 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
617 L2TunnelHandler.Result res;
618 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
619 res = addPseudowire(pw);
620 if (res != L2TunnelHandler.Result.SUCCESS) {
621 log.error("Pseudowire with id {} can not be instantiated !", res);
622 retRes = res;
623 }
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700624 }
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700625
626 return retRes;
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700627 }
628
629 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800630 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700631 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700632 }
633
634 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800635 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700636 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700637 }
638
639 @Override
Saurav Das07c74602016-04-27 18:35:50 -0700640 public void rerouteNetwork() {
641 cfgListener.configureNetwork();
Saurav Das07c74602016-04-27 18:35:50 -0700642 }
643
Charles Chand7844e52016-10-20 17:02:44 -0700644 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800645 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
646 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart61e24e12017-11-30 18:23:42 -0800647 deviceConfiguration.getRouters().forEach(device ->
648 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chand7844e52016-10-20 17:02:44 -0700649 return deviceSubnetMap;
650 }
651
Saurav Das62ae6792017-05-15 15:34:25 -0700652
653 @Override
654 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
655 if (defaultRoutingHandler != null) {
656 return defaultRoutingHandler.getCurrentEmcpSpgMap();
657 } else {
658 return null;
659 }
660 }
661
662 @Override
Saurav Das261c3002017-06-13 15:35:54 -0700663 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
664 if (dsNextObjStore != null) {
665 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700666 } else {
667 return ImmutableMap.of();
668 }
669 }
670
Saurav Dasfbe74572017-08-03 18:30:35 -0700671 @Override
672 public void verifyGroups(DeviceId id) {
673 DefaultGroupHandler gh = groupHandlerMap.get(id);
674 if (gh != null) {
675 gh.triggerBucketCorrector();
676 }
677 }
678
Saurav Das6430f412018-01-25 09:49:01 -0800679 @Override
680 public ImmutableMap<Link, Boolean> getSeenLinks() {
681 return linkHandler.getSeenLinks();
682 }
683
684 @Override
685 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
686 return linkHandler.getDownedPorts();
687 }
688
Pier Luigi0f9635b2018-01-15 18:06:43 +0100689 @Override
690 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
691 return mcastHandler.getMcastNextIds(mcastIp);
692 }
693
694 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +0100695 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigi0f9635b2018-01-15 18:06:43 +0100696 return mcastHandler.getMcastRoles(mcastIp);
697 }
698
699 @Override
700 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
701 return mcastHandler.getMcastPaths(mcastIp);
702 }
703
Pierdb27b8d2018-04-17 16:29:56 +0200704 @Override
Pier71c55772018-04-17 17:25:22 +0200705 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
706 ConnectPoint sourcecp) {
707 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
708 }
709
710 @Override
Pierdb27b8d2018-04-17 16:29:56 +0200711 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
712 return mcastHandler.getMcastLeaders(mcastIp);
713 }
714
Charles Chanb13e0702018-04-17 18:56:53 -0700715 @Override
716 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
717 return defaultRoutingHandler == null ? ImmutableMap.of() :
718 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
719 }
720
721 @Override
722 public Map<DeviceId, Boolean> getShouldProgramCache() {
723 return defaultRoutingHandler == null ? ImmutableMap.of() :
724 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
725 }
726
sangho80f1f892015-05-19 11:57:42 -0700727 /**
Ray Milkeyb85de082017-04-05 09:42:04 -0700728 * Extracts the application ID from the manager.
729 *
730 * @return application ID
731 */
732 public ApplicationId appId() {
733 return appId;
734 }
735
736 /**
737 * Returns the device configuration.
738 *
739 * @return device configuration
740 */
741 public DeviceConfiguration deviceConfiguration() {
742 return deviceConfiguration;
743 }
744
745 /**
Saurav Das261c3002017-06-13 15:35:54 -0700746 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700747 * Used to keep track on MPLS group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700748 *
749 * @return next objective ID store
750 */
Saurav Das261c3002017-06-13 15:35:54 -0700751 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
752 dsNextObjStore() {
753 return dsNextObjStore;
Ray Milkeyb85de082017-04-05 09:42:04 -0700754 }
755
756 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700757 * Per device next objective ID store with (device id + vlanid) as key.
758 * Used to keep track on L2 flood group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700759 *
760 * @return vlan next object store
761 */
762 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
763 return vlanNextObjStore;
764 }
765
766 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700767 * Per device next objective ID store with (device id + port + treatment + meta) as key.
768 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700769 *
770 * @return port next object store.
771 */
772 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
773 return portNextObjStore;
774 }
775
776 /**
Saurav Das261c3002017-06-13 15:35:54 -0700777 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
778 * labeled packets should be programmed or not.
Pier Ventre7a78de22016-10-31 15:00:01 -0700779 *
780 * @return MPLS-ECMP value
781 */
782 public boolean getMplsEcmp() {
783 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
784 .getConfig(this.appId, SegmentRoutingAppConfig.class);
785 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
786 }
787
788 /**
sangho80f1f892015-05-19 11:57:42 -0700789 * Returns the tunnel object with the tunnel ID.
790 *
791 * @param tunnelId Tunnel ID
792 * @return Tunnel reference
793 */
sangho27462c62015-05-14 00:39:53 -0700794 public Tunnel getTunnel(String tunnelId) {
795 return tunnelHandler.getTunnel(tunnelId);
796 }
797
Charles Chan90772a72017-02-08 15:52:08 -0800798 // TODO Consider moving these to InterfaceService
sangho80f11cb2015-04-01 13:05:26 -0700799 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800800 * Returns untagged VLAN configured on given connect point.
Charles Chan90772a72017-02-08 15:52:08 -0800801 * <p>
802 * Only returns the first match if there are multiple untagged VLAN configured
803 * on the connect point.
sangho80f11cb2015-04-01 13:05:26 -0700804 *
Charles Chan10b0fb72017-02-02 16:20:42 -0800805 * @param connectPoint connect point
806 * @return untagged VLAN or null if not configured
sangho80f11cb2015-04-01 13:05:26 -0700807 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700808 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800809 return interfaceService.getInterfacesByPort(connectPoint).stream()
810 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
811 .map(Interface::vlanUntagged)
812 .findFirst().orElse(null);
sangho80f11cb2015-04-01 13:05:26 -0700813 }
814
sangho27462c62015-05-14 00:39:53 -0700815 /**
Charles Chan90772a72017-02-08 15:52:08 -0800816 * Returns tagged VLAN configured on given connect point.
817 * <p>
818 * Returns all matches if there are multiple tagged VLAN configured
819 * on the connect point.
820 *
821 * @param connectPoint connect point
822 * @return tagged VLAN or empty set if not configured
823 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700824 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan90772a72017-02-08 15:52:08 -0800825 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
826 return interfaces.stream()
827 .map(Interface::vlanTagged)
Charles Chan3ed34d82017-06-22 18:03:14 -0700828 .flatMap(Set::stream)
Charles Chan90772a72017-02-08 15:52:08 -0800829 .collect(Collectors.toSet());
830 }
831
832 /**
833 * Returns native VLAN configured on given connect point.
834 * <p>
835 * Only returns the first match if there are multiple native VLAN configured
836 * on the connect point.
837 *
838 * @param connectPoint connect point
839 * @return native VLAN or null if not configured
840 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700841 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan90772a72017-02-08 15:52:08 -0800842 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
843 return interfaces.stream()
844 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
845 .map(Interface::vlanNative)
846 .findFirst()
847 .orElse(null);
848 }
849
850 /**
Charles Chand9265a32017-06-16 15:19:24 -0700851 * Returns internal VLAN for untagged hosts on given connect point.
852 * <p>
853 * The internal VLAN is either vlan-untagged for an access port,
854 * or vlan-native for a trunk port.
855 *
856 * @param connectPoint connect point
857 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
858 */
Pier Luigi69f774d2018-02-28 12:10:50 +0100859 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chand9265a32017-06-16 15:19:24 -0700860 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
861 VlanId nativeVlanId = getNativeVlanId(connectPoint);
862 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
863 }
864
865 /**
Charles Chan3ed34d82017-06-22 18:03:14 -0700866 * Returns optional pair device ID of given device.
867 *
868 * @param deviceId device ID
869 * @return optional pair device ID. Might be empty if pair device is not configured
870 */
871 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
872 SegmentRoutingDeviceConfig deviceConfig =
873 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
874 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
875 }
876 /**
877 * Returns optional pair device local port of given device.
878 *
879 * @param deviceId device ID
880 * @return optional pair device ID. Might be empty if pair device is not configured
881 */
882 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
883 SegmentRoutingDeviceConfig deviceConfig =
884 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
885 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
886 }
887
888 /**
Charles Chanf0ae41e2017-08-23 13:55:39 -0700889 * Returns locations of given resolved route.
890 *
891 * @param resolvedRoute resolved route
892 * @return locations of nexthop. Might be empty if next hop is not found
893 */
894 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
895 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
896 return Optional.ofNullable(hostService.getHost(hostId))
897 .map(Host::locations).orElse(Sets.newHashSet())
898 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
899 }
900
901 /**
Charles Chan90772a72017-02-08 15:52:08 -0800902 * Returns vlan port map of given device.
903 *
904 * @param deviceId device id
905 * @return vlan-port multimap
906 */
907 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
908 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
909
910 interfaceService.getInterfaces().stream()
911 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
912 .forEach(intf -> {
913 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan3ed34d82017-06-22 18:03:14 -0700914 intf.vlanTagged().forEach(vlanTagged ->
915 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
916 );
Charles Chan90772a72017-02-08 15:52:08 -0800917 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
918 });
919 vlanPortMap.removeAll(VlanId.NONE);
920
921 return vlanPortMap;
922 }
923
924 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800925 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -0800926 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -0700927 *
928 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -0800929 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -0800930 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -0700931 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700932 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -0700933 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800934 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
935 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -0700936 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -0800937 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -0800938 + "device {} not found", deviceId);
939 return -1;
940 }
941 }
942
943 /**
944 * Returns the next objective ID for the given portNumber, given the treatment.
945 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -0700946 * would result in different objectives. If the next object does not exist,
947 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -0800948 *
949 * @param deviceId Device ID
950 * @param portNum port number on device for which NextObjective is queried
951 * @param treatment the actions to apply on the packets (should include outport)
952 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -0700953 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -0700954 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -0800955 */
956 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
957 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -0700958 TrafficSelector meta,
959 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -0800960 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
961 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -0700962 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -0800963 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800964 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
965 + " not found", deviceId);
966 return -1;
967 }
968 }
969
Saurav Das62ae6792017-05-15 15:34:25 -0700970 /**
971 * Returns the group handler object for the specified device id.
972 *
973 * @param devId the device identifier
974 * @return the groupHandler object for the device id, or null if not found
975 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700976 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Das62ae6792017-05-15 15:34:25 -0700977 return groupHandlerMap.get(devId);
978 }
979
980 /**
Saurav Dasfbe74572017-08-03 18:30:35 -0700981 * Returns the default routing handler object.
982 *
983 * @return the default routing handler object
984 */
985 public DefaultRoutingHandler getRoutingHandler() {
986 return defaultRoutingHandler;
987 }
988
sangho80f11cb2015-04-01 13:05:26 -0700989 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -0700990 @Override
991 public void process(PacketContext context) {
992
993 if (context.isHandled()) {
994 return;
995 }
996
997 InboundPacket pkt = context.inPacket();
998 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -0800999
1000 if (ethernet == null) {
1001 return;
1002 }
1003
Saurav Das261c3002017-06-13 15:35:54 -07001004 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1005 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001006 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -07001007 log.warn("Received unexpected ARP packet on {}",
1008 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -07001009 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001010 return;
sangho80f11cb2015-04-01 13:05:26 -07001011 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -08001012 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1013 //ipHandler.addToPacketBuffer(ipv4Packet);
1014 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1015 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001016 } else {
Charles Chand041ad82017-01-13 17:20:44 -08001017 // NOTE: We don't support IP learning at this moment so this
1018 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001019 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001020 }
Pier Ventreb6a7f342016-11-26 21:05:22 -08001021 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1022 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -08001023 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -08001024 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -08001025 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1026 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1027 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1028 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1029 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1030 } else {
Saurav Das62ae6792017-05-15 15:34:25 -07001031 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -07001032 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -08001033 }
1034 } else {
1035 // NOTE: We don't support IP learning at this moment so this
1036 // is not necessary. Also it causes duplication of DHCPv6 packets.
1037 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1038 }
sangho80f11cb2015-04-01 13:05:26 -07001039 }
1040 }
1041 }
1042
sangho80f11cb2015-04-01 13:05:26 -07001043 private class InternalEventHandler implements Runnable {
Charles Chan52003922018-04-05 16:31:15 -07001044 private Event event;
1045
1046 InternalEventHandler(Event event) {
1047 this.event = event;
1048 }
1049
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001050 @Override
sangho80f11cb2015-04-01 13:05:26 -07001051 public void run() {
Charles Chan52003922018-04-05 16:31:15 -07001052 try {
1053 // TODO We should also change SR routing and PW to listen to TopologyEvents
1054 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1055 event.type() == LinkEvent.Type.LINK_UPDATED) {
1056 linkHandler.processLinkAdded((Link) event.subject());
1057 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1058 linkHandler.processLinkRemoved((Link) event.subject());
1059 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1060 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1061 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1062 DeviceId deviceId = ((Device) event.subject()).id();
1063 if (deviceService.isAvailable(deviceId)) {
1064 log.info("** DEVICE UP Processing device event {} "
1065 + "for available device {}",
1066 event.type(), ((Device) event.subject()).id());
1067 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001068 } else {
Charles Chan52003922018-04-05 16:31:15 -07001069 log.info(" ** DEVICE DOWN Processing device event {}"
1070 + " for unavailable device {}",
1071 event.type(), ((Device) event.subject()).id());
1072 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001073 }
Charles Chan52003922018-04-05 16:31:15 -07001074 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1075 // typically these calls come when device is added first time
1076 // so port filtering rules are handled at the device_added event.
1077 // port added calls represent all ports on the device,
1078 // enabled or not.
1079 log.trace("** PORT ADDED {}/{} -> {}",
1080 ((DeviceEvent) event).subject().id(),
1081 ((DeviceEvent) event).port().number(),
1082 event.type());
1083 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1084 // these calls happen for every subsequent event
1085 // ports enabled, disabled, switch goes away, comes back
1086 log.info("** PORT UPDATED {}/{} -> {}",
1087 event.subject(),
1088 ((DeviceEvent) event).port(),
1089 event.type());
1090 processPortUpdated(((Device) event.subject()),
1091 ((DeviceEvent) event).port());
1092 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1093 // Process topology event, needed for all modules relying on
1094 // topology service for path computation
1095 TopologyEvent topologyEvent = (TopologyEvent) event;
1096 log.info("Processing topology event {}, topology age {}, reasons {}",
1097 event.type(), topologyEvent.subject().time(),
1098 topologyEvent.reasons().size());
1099 topologyHandler.processTopologyChange(topologyEvent.reasons());
1100 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1101 hostHandler.processHostAddedEvent((HostEvent) event);
1102 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1103 hostHandler.processHostMovedEvent((HostEvent) event);
1104 routeHandler.processHostMovedEvent((HostEvent) event);
1105 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1106 hostHandler.processHostRemovedEvent((HostEvent) event);
1107 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1108 hostHandler.processHostUpdatedEvent((HostEvent) event);
1109 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1110 routeHandler.processRouteAdded((RouteEvent) event);
1111 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1112 routeHandler.processRouteUpdated((RouteEvent) event);
1113 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1114 routeHandler.processRouteRemoved((RouteEvent) event);
1115 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1116 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1117 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1118 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1119 event.type() == McastEvent.Type.SINKS_ADDED ||
1120 event.type() == McastEvent.Type.SINKS_REMOVED ||
1121 event.type() == McastEvent.Type.ROUTE_ADDED ||
1122 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1123 mcastHandler.processMcastEvent((McastEvent) event);
1124 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1125 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1126 Class configClass = netcfgEvent.configClass();
1127 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1128 appCfgHandler.processAppConfigAdded(netcfgEvent);
1129 log.info("App config event .. configuring network");
1130 cfgListener.configureNetwork();
1131 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1132 log.info("Segment Routing Device Config added for {}", event.subject());
1133 cfgListener.configureNetwork();
1134 } else if (configClass.equals(XConnectConfig.class)) {
1135 xConnectHandler.processXConnectConfigAdded(netcfgEvent);
1136 } else if (configClass.equals(InterfaceConfig.class)) {
1137 log.info("Interface Config added for {}", event.subject());
1138 cfgListener.configureNetwork();
1139 } else {
1140 log.error("Unhandled config class: {}", configClass);
1141 }
1142 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1143 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1144 Class configClass = netcfgEvent.configClass();
1145 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1146 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1147 log.info("App config event .. configuring network");
1148 cfgListener.configureNetwork();
1149 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1150 log.info("Segment Routing Device Config updated for {}", event.subject());
1151 createOrUpdateDeviceConfiguration();
1152 } else if (configClass.equals(XConnectConfig.class)) {
1153 xConnectHandler.processXConnectConfigUpdated(netcfgEvent);
1154 } else if (configClass.equals(InterfaceConfig.class)) {
1155 log.info("Interface Config updated for {}", event.subject());
1156 createOrUpdateDeviceConfiguration();
1157 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1158 (InterfaceConfig) netcfgEvent.prevConfig().get());
1159 } else {
1160 log.error("Unhandled config class: {}", configClass);
1161 }
1162 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1163 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1164 Class configClass = netcfgEvent.configClass();
1165 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1166 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1167 log.info("App config event .. configuring network");
1168 cfgListener.configureNetwork();
1169 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1170 // TODO Handle sr device config removal
1171 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
1172 } else if (configClass.equals(XConnectConfig.class)) {
1173 xConnectHandler.processXConnectConfigRemoved(netcfgEvent);
1174 } else if (configClass.equals(InterfaceConfig.class)) {
1175 // TODO Handle interface removal
1176 log.info("InterfaceConfig removal is not handled in current implementation");
1177 } else {
1178 log.error("Unhandled config class: {}", configClass);
1179 }
Saurav Das00e553b2018-04-21 17:19:48 -07001180 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1181 MastershipEvent me = (MastershipEvent) event;
1182 DeviceId deviceId = me.subject();
1183 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1184 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1185 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1186 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1187 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1188 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan52003922018-04-05 16:31:15 -07001189 } else {
1190 log.warn("Unhandled event type: {}", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001191 }
Charles Chan52003922018-04-05 16:31:15 -07001192 } catch (Exception e) {
1193 log.error("SegmentRouting event handler thread thrown an exception: {}",
1194 e.getMessage(), e);
sangho80f11cb2015-04-01 13:05:26 -07001195 }
sangho80f11cb2015-04-01 13:05:26 -07001196 }
1197 }
1198
Saurav Dase6c448a2018-01-18 12:07:33 -08001199 void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001200 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001201
1202 // NOTE: Punt ARP/NDP even when the device is not configured.
1203 // Host learning without network config is required for CORD config generator.
1204 routingRulePopulator.populateIpPunts(device.id());
1205 routingRulePopulator.populateArpNdpPunts(device.id());
1206
Charles Chan319d1a22015-11-03 10:42:14 -08001207 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001208 log.warn("Device configuration unavailable. Device {} will be "
1209 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001210 return;
1211 }
Charles Chan72779502016-04-23 17:36:10 -07001212 processDeviceAddedInternal(device.id());
1213 }
1214
1215 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001216 // Irrespective of whether the local is a MASTER or not for this device,
1217 // we need to create a SR-group-handler instance. This is because in a
1218 // multi-instance setup, any instance can initiate forwarding/next-objectives
1219 // for any switch (even if this instance is a SLAVE or not even connected
1220 // to the switch). To handle this, a default-group-handler instance is necessary
1221 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001222 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1223 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001224 DefaultGroupHandler groupHandler;
1225 try {
1226 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001227 createGroupHandler(deviceId,
1228 appId,
1229 deviceConfiguration,
1230 linkService,
1231 flowObjectiveService,
1232 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001233 } catch (DeviceConfigNotFoundException e) {
1234 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1235 return;
1236 }
Saurav Das2b6a00f2017-12-05 15:00:23 -08001237 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan72779502016-04-23 17:36:10 -07001238 deviceId);
1239 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001240 }
Saurav Dasb149be12016-06-07 10:08:06 -07001241
Charles Chan72779502016-04-23 17:36:10 -07001242 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001243 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -07001244 xConnectHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001245 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001246 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001247 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001248 }
Charles Chan82ab1932016-01-30 23:22:37 -08001249
Charles Chandebfea32016-10-24 14:52:01 -07001250 appCfgHandler.init(deviceId);
Charles Chand66d6712018-03-29 16:03:41 -07001251 hostHandler.init(deviceId);
Charles Chandebfea32016-10-24 14:52:01 -07001252 routeHandler.init(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001253 }
1254
Saurav Dasc3604f12016-03-23 11:22:49 -07001255 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001256 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001257 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan873661e2017-11-30 15:37:50 -08001258 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan10b0fb72017-02-02 16:20:42 -08001259 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001260 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001261 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001262 portNextObjStore.entrySet().stream()
1263 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001264 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Dase6c448a2018-01-18 12:07:33 -08001265 linkHandler.processDeviceRemoved(device);
Charles Chan17d75d82017-06-15 00:44:51 -07001266
Saurav Dasfbe74572017-08-03 18:30:35 -07001267 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1268 if (gh != null) {
1269 gh.shutdown();
1270 }
Saurav Das62ae6792017-05-15 15:34:25 -07001271 // Note that a switch going down is associated with all of its links
1272 // going down as well, but it is treated as a single switch down event
Saurav Dasdebcf882018-04-06 20:16:01 -07001273 // while the link-downs are ignored. We cannot rely on the ordering of
1274 // events - i.e we cannot expect all link-downs to come before the
1275 // switch down - so we purge all seen-links for the switch before
1276 // handling route-path changes for the switch-down
Saurav Das62ae6792017-05-15 15:34:25 -07001277 defaultRoutingHandler
Saurav Dasdc7f2752018-03-18 21:28:15 -07001278 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Das6430f412018-01-25 09:49:01 -08001279 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan82f19972016-05-17 13:13:55 -07001280 xConnectHandler.removeDevice(device.id());
Saurav Das97241862018-02-14 14:14:54 -08001281
1282 // Cleanup all internal groupHandler stores for this device. Should be
1283 // done after all rerouting or rehashing has been completed
1284 groupHandlerMap.entrySet()
1285 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001286 }
1287
Saurav Das49368392018-04-23 18:42:12 -07001288 /**
1289 * Purge the destinationSet nextObjective store of entries with this device
1290 * as key. Erases app-level knowledge of hashed groups in this device.
1291 *
1292 * @param devId the device identifier
1293 */
Saurav Das00e553b2018-04-21 17:19:48 -07001294 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Das49368392018-04-23 18:42:12 -07001295 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Das00e553b2018-04-21 17:19:48 -07001296 dsNextObjStore.entrySet().stream()
1297 .filter(entry -> entry.getKey().deviceId().equals(devId))
1298 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1299 }
1300
Saurav Dasf0f592d2016-11-18 15:21:57 -08001301 private void processPortUpdated(Device device, Port port) {
1302 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1303 log.warn("Device configuration uploading. Not handling port event for"
1304 + "dev: {} port: {}", device.id(), port.number());
1305 return;
1306 }
Saurav Dasf9332192017-02-18 14:05:44 -08001307
1308 if (!mastershipService.isLocalMaster(device.id())) {
1309 log.debug("Not master for dev:{} .. not handling port updated event"
1310 + "for port {}", device.id(), port.number());
1311 return;
1312 }
1313
1314 // first we handle filtering rules associated with the port
1315 if (port.isEnabled()) {
1316 log.info("Switchport {}/{} enabled..programming filters",
1317 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001318 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001319 } else {
1320 log.info("Switchport {}/{} disabled..removing filters",
1321 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001322 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001323 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001324
1325 // portUpdated calls are for ports that have gone down or up. For switch
1326 // to switch ports, link-events should take care of any re-routing or
1327 // group editing necessary for port up/down. Here we only process edge ports
1328 // that are already configured.
Saurav Das3fb28272017-03-04 16:08:47 -08001329 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1330 VlanId untaggedVlan = getUntaggedVlanId(cp);
1331 VlanId nativeVlan = getNativeVlanId(cp);
1332 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001333
Saurav Das3fb28272017-03-04 16:08:47 -08001334 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001335 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasf0f592d2016-11-18 15:21:57 -08001336 + "dev/port: {}/{}", device.id(), port.number());
1337 return;
1338 }
Saurav Das3fb28272017-03-04 16:08:47 -08001339 if (untaggedVlan != null) {
1340 processEdgePort(device, port, untaggedVlan, true);
1341 }
1342 if (nativeVlan != null) {
1343 processEdgePort(device, port, nativeVlan, true);
1344 }
1345 if (!taggedVlans.isEmpty()) {
1346 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1347 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001348 }
1349
Saurav Das3fb28272017-03-04 16:08:47 -08001350 private void processEdgePort(Device device, Port port, VlanId vlanId,
1351 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001352 boolean portUp = port.isEnabled();
1353 if (portUp) {
Saurav Das3fb28272017-03-04 16:08:47 -08001354 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001355 port.number(), vlanId);
Charles Chan873661e2017-11-30 15:37:50 -08001356 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Dasf0f592d2016-11-18 15:21:57 -08001357 } else {
Saurav Das3fb28272017-03-04 16:08:47 -08001358 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001359 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001360 }
1361
Srikanth Vavilapalli64505482015-04-21 13:04:13 -07001362 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sangho80f11cb2015-04-01 13:05:26 -07001363 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001364 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001365 } else {
1366 log.warn("Group handler not found for dev:{}. Not handling edge port"
1367 + " {} event for port:{}", device.id(),
1368 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001369 }
1370 }
sangho27462c62015-05-14 00:39:53 -07001371
Charles Chan8ca5a122017-10-20 16:06:55 -07001372 private void createOrUpdateDeviceConfiguration() {
1373 if (deviceConfiguration == null) {
Saurav Dase321cff2018-02-09 17:26:45 -08001374 log.info("Creating new DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001375 deviceConfiguration = new DeviceConfiguration(this);
1376 } else {
Saurav Dase321cff2018-02-09 17:26:45 -08001377 log.info("Updating DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001378 deviceConfiguration.updateConfig();
1379 }
1380 }
1381
Charles Chanfbcb8812018-04-18 18:41:05 -07001382 private void createOrUpdateDefaultRoutingHandler() {
1383 if (defaultRoutingHandler == null) {
1384 log.info("Creating new DefaultRoutingHandler");
1385 defaultRoutingHandler = new DefaultRoutingHandler(this);
1386 } else {
1387 log.info("Updating DefaultRoutingHandler");
1388 defaultRoutingHandler.update(this);
1389 }
1390 }
1391
Pier Ventreb6a7f342016-11-26 21:05:22 -08001392 /**
1393 * Registers the given connect point with the NRS, this is necessary
1394 * to receive the NDP and ARP packets from the NRS.
1395 *
1396 * @param portToRegister connect point to register
1397 */
1398 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001399 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001400 portToRegister,
1401 neighbourHandler,
1402 appId
1403 );
1404 }
1405
Charles Chan72f556a2015-10-05 17:50:33 -07001406 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001407 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001408 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001409
Charles Chanb7f75ac2016-01-11 18:28:54 -08001410 /**
1411 * Constructs the internal network config listener.
1412 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001413 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001414 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001415 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001416 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001417 }
1418
Charles Chanb7f75ac2016-01-11 18:28:54 -08001419 /**
1420 * Reads network config and initializes related data structure accordingly.
1421 */
Charles Chan873661e2017-11-30 15:37:50 -08001422 void configureNetwork() {
Saurav Dase321cff2018-02-09 17:26:45 -08001423 log.info("Configuring network ...");
Andrea Campanella060cad82018-04-17 12:09:34 +02001424
1425 // Setting handling of network configuration events completable future
1426 // The completable future is needed because of the async behaviour of the configureNetwork,
1427 // listener registration and event arrival
1428 // Enables us to buffer the events and execute them when the configure network is done.
1429 networkConfigCompletion = new CompletableFuture<>();
1430
1431 // add a small delay to absorb multiple network config added notifications
1432 if (!programmingScheduled.get()) {
1433 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1434 programmingScheduled.set(true);
1435 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1436 }
1437
Charles Chan8ca5a122017-10-20 16:06:55 -07001438 createOrUpdateDeviceConfiguration();
Charles Chane7c61022015-10-07 14:21:45 -07001439
Charles Chan46fdfaf2016-11-09 20:51:44 -08001440 arpHandler = new ArpHandler(srManager);
1441 icmpHandler = new IcmpHandler(srManager);
1442 ipHandler = new IpHandler(srManager);
1443 routingRulePopulator = new RoutingRulePopulator(srManager);
Charles Chanfbcb8812018-04-18 18:41:05 -07001444 createOrUpdateDefaultRoutingHandler();
Charles Chane7c61022015-10-07 14:21:45 -07001445
1446 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1447 groupHandlerMap, tunnelStore);
1448 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1449 flowObjectiveService,
1450 tunnelHandler, policyStore);
Andrea Campanella060cad82018-04-17 12:09:34 +02001451 networkConfigCompletion.complete(true);
1452
Charles Chan72779502016-04-23 17:36:10 -07001453 mcastHandler.init();
Andrea Campanella060cad82018-04-17 12:09:34 +02001454
Charles Chane7c61022015-10-07 14:21:45 -07001455 }
1456
Charles Chan72f556a2015-10-05 17:50:33 -07001457 @Override
1458 public void event(NetworkConfigEvent event) {
Charles Chan3f4aca62018-03-21 16:57:47 -07001459 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
1460 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1461 switch (event.type()) {
1462 case CONFIG_ADDED:
1463 case CONFIG_UPDATED:
1464 case CONFIG_REMOVED:
1465 log.trace("Schedule Network Config event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001466 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1467 mainEventExecutor.execute(new InternalEventHandler(event));
1468 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001469 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001470 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001471 break;
1472 default:
1473 break;
Charles Chan72f556a2015-10-05 17:50:33 -07001474 }
1475 }
Saurav Das261c3002017-06-13 15:35:54 -07001476
Charles Chanc4a68c32018-01-03 16:26:32 -08001477 @Override
1478 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase321cff2018-02-09 17:26:45 -08001479 if (event.type() == CONFIG_REGISTERED ||
1480 event.type() == CONFIG_UNREGISTERED) {
1481 log.debug("Ignore event {} due to type mismatch", event);
1482 return false;
Charles Chanc4a68c32018-01-03 16:26:32 -08001483 }
Saurav Dase321cff2018-02-09 17:26:45 -08001484
1485 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1486 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1487 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -08001488 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase321cff2018-02-09 17:26:45 -08001489 log.debug("Ignore event {} due to class mismatch", event);
1490 return false;
1491 }
1492
1493 return true;
Charles Chanc4a68c32018-01-03 16:26:32 -08001494 }
1495
Saurav Das261c3002017-06-13 15:35:54 -07001496 private final class ConfigChange implements Runnable {
1497 @Override
1498 public void run() {
1499 programmingScheduled.set(false);
Saurav Dase321cff2018-02-09 17:26:45 -08001500 log.info("Reacting to config changes after buffer delay");
Saurav Das261c3002017-06-13 15:35:54 -07001501 for (Device device : deviceService.getDevices()) {
1502 processDeviceAdded(device);
1503 }
1504 defaultRoutingHandler.startPopulationProcess();
1505 }
1506 }
Charles Chan72f556a2015-10-05 17:50:33 -07001507 }
Charles Chanf4586112015-11-09 16:37:23 -08001508
Charles Chan3f4aca62018-03-21 16:57:47 -07001509 private class InternalLinkListener implements LinkListener {
1510 @Override
1511 public void event(LinkEvent event) {
1512 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1513 event.type() == LinkEvent.Type.LINK_UPDATED ||
1514 event.type() == LinkEvent.Type.LINK_REMOVED) {
1515 log.trace("Schedule Link event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001516 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1517 mainEventExecutor.execute(new InternalEventHandler(event));
1518 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001519 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001520 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001521 }
1522 }
1523 }
1524
1525 private class InternalDeviceListener implements DeviceListener {
1526 @Override
1527 public void event(DeviceEvent event) {
1528 switch (event.type()) {
1529 case DEVICE_ADDED:
1530 case PORT_UPDATED:
1531 case PORT_ADDED:
1532 case DEVICE_UPDATED:
1533 case DEVICE_AVAILABILITY_CHANGED:
1534 log.trace("Schedule Device event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001535 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1536 mainEventExecutor.execute(new InternalEventHandler(event));
1537 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001538 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001539 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001540 break;
1541 default:
1542 }
1543 }
1544 }
1545
1546 private class InternalTopologyListener implements TopologyListener {
1547 @Override
1548 public void event(TopologyEvent event) {
1549 switch (event.type()) {
1550 case TOPOLOGY_CHANGED:
1551 log.trace("Schedule Topology event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001552 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1553 mainEventExecutor.execute(new InternalEventHandler(event));
1554 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001555 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001556 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001557 break;
1558 default:
1559 }
1560 }
1561 }
1562
Charles Chanf4586112015-11-09 16:37:23 -08001563 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001564 @Override
1565 public void event(HostEvent event) {
Charles Chanf4586112015-11-09 16:37:23 -08001566 switch (event.type()) {
1567 case HOST_ADDED:
Charles Chanf4586112015-11-09 16:37:23 -08001568 case HOST_MOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001569 case HOST_REMOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001570 case HOST_UPDATED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001571 log.trace("Schedule Host event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001572 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chanf4586112015-11-09 16:37:23 -08001573 break;
1574 default:
1575 log.warn("Unsupported host event type: {}", event.type());
1576 break;
1577 }
1578 }
1579 }
1580
Charles Chanc91c8782016-03-30 17:54:24 -07001581 private class InternalMcastListener implements McastListener {
1582 @Override
1583 public void event(McastEvent event) {
1584 switch (event.type()) {
Pier1f87aca2018-03-14 16:47:32 -07001585 case SOURCES_ADDED:
1586 case SOURCES_REMOVED:
1587 case SINKS_ADDED:
1588 case SINKS_REMOVED:
Charles Chanc91c8782016-03-30 17:54:24 -07001589 case ROUTE_REMOVED:
Pierdb27b8d2018-04-17 16:29:56 +02001590 case ROUTE_ADDED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001591 log.trace("Schedule Mcast event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001592 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi6786b922018-02-02 16:19:11 +01001593 break;
Charles Chanc91c8782016-03-30 17:54:24 -07001594 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07001595 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chanc91c8782016-03-30 17:54:24 -07001596 break;
1597 }
1598 }
1599 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001600
Charles Chandebfea32016-10-24 14:52:01 -07001601 private class InternalRouteEventListener implements RouteListener {
1602 @Override
1603 public void event(RouteEvent event) {
1604 switch (event.type()) {
1605 case ROUTE_ADDED:
Charles Chandebfea32016-10-24 14:52:01 -07001606 case ROUTE_UPDATED:
Charles Chandebfea32016-10-24 14:52:01 -07001607 case ROUTE_REMOVED:
Charles Chan910be6a2017-08-23 14:46:43 -07001608 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001609 log.trace("Schedule Route event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001610 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan910be6a2017-08-23 14:46:43 -07001611 break;
Charles Chandebfea32016-10-24 14:52:01 -07001612 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07001613 log.warn("Unsupported route event type: {}", event.type());
Charles Chandebfea32016-10-24 14:52:01 -07001614 break;
1615 }
1616 }
1617 }
Saurav Das62ae6792017-05-15 15:34:25 -07001618
Charles Chanfbcb8812018-04-18 18:41:05 -07001619 private class InternalMastershipListener implements MastershipListener {
1620 @Override
1621 public void event(MastershipEvent event) {
Charles Chanfbcb8812018-04-18 18:41:05 -07001622 switch (event.type()) {
Saurav Das00e553b2018-04-21 17:19:48 -07001623 case MASTER_CHANGED:
1624 log.debug("Mastership event: {}/{}", event.subject(),
1625 event.roleInfo());
1626 mainEventExecutor.execute(new InternalEventHandler(event));
1627 break;
1628 case BACKUPS_CHANGED:
1629 case SUSPENDED:
1630 default:
1631 log.debug("Mastership event type {} not handled", event.type());
1632 break;
Charles Chanfbcb8812018-04-18 18:41:05 -07001633 }
1634 }
1635 }
1636
Saurav Das00e553b2018-04-21 17:19:48 -07001637 class InternalClusterListener implements ClusterEventListener {
1638 private Instant lastClusterEvent = Instant.EPOCH;
1639
1640 long timeSinceLastClusterEvent() {
1641 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
1642 }
1643
1644 @Override
1645 public void event(ClusterEvent event) {
1646 switch (event.type()) {
1647 case INSTANCE_ACTIVATED:
1648 case INSTANCE_ADDED:
1649 case INSTANCE_READY:
1650 log.debug("Cluster event {} ignored", event.type());
1651 break;
1652 case INSTANCE_DEACTIVATED:
1653 case INSTANCE_REMOVED:
1654 log.info("** Cluster event {}", event.type());
1655 lastClusterEvent = Instant.now();
1656 break;
1657 default:
1658 break;
1659 }
1660
1661 }
1662
1663 }
1664
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001665 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1666 try {
1667 Set<Interface> intfs = conf.getInterfaces();
1668 Set<Interface> prevIntfs = prevConf.getInterfaces();
1669
1670 // Now we only handle one interface config at each port.
1671 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1672 log.warn("Interface update aborted - one at a time is allowed, " +
1673 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1674 return;
1675 }
1676
Andrea Campanella060cad82018-04-17 12:09:34 +02001677 //The system is in an incoherent state, abort
1678 if (defaultRoutingHandler == null) {
1679 log.warn("Interface update aborted, defaultRoutingHandler is null");
1680 return;
1681 }
1682
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001683 Interface intf = intfs.stream().findFirst().get();
1684 Interface prevIntf = prevIntfs.stream().findFirst().get();
1685
1686 DeviceId deviceId = intf.connectPoint().deviceId();
1687 PortNumber portNum = intf.connectPoint().port();
1688
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001689 removeSubnetConfig(prevIntf.connectPoint(),
1690 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1691 new HashSet<>(intf.ipAddressesList())));
1692
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001693 if (!prevIntf.vlanNative().equals(VlanId.NONE)
1694 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
1695 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1696 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
1697 // Update filtering objective and L2IG group bucket
1698 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
1699 } else {
1700 // RemoveVlanNative
1701 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1702 }
1703 }
1704
1705 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
1706 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1707 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1708 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
1709 // Update filtering objective and L2IG group bucket
1710 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
1711 } else {
1712 // RemoveVlanUntagged
1713 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1714 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001715 }
1716
1717 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1718 // RemoveVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001719 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
1720 .filter(i -> !intf.vlanUntagged().equals(i))
1721 .filter(i -> !intf.vlanNative().equals(i))
1722 .forEach(vlanId -> updateVlanConfigInternal(
1723 deviceId, portNum, vlanId, false, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001724 }
1725
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001726 if (!intf.vlanNative().equals(VlanId.NONE)
1727 && !prevIntf.vlanNative().equals(intf.vlanNative())
1728 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1729 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
1730 // Update filtering objective and L2IG group bucket
1731 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
1732 } else {
1733 // AddVlanNative
1734 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1735 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001736 }
1737
1738 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1739 // AddVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001740 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
1741 .filter(i -> !prevIntf.vlanUntagged().equals(i))
1742 .filter(i -> !prevIntf.vlanNative().equals(i))
1743 .forEach(vlanId -> updateVlanConfigInternal(
1744 deviceId, portNum, vlanId, false, true)
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001745 );
1746 }
1747
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001748 if (!intf.vlanUntagged().equals(VlanId.NONE)
1749 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1750 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
1751 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
1752 // Update filtering objective and L2IG group bucket
1753 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
1754 } else {
1755 // AddVlanUntagged
1756 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1757 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001758 }
1759 addSubnetConfig(prevIntf.connectPoint(),
1760 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1761 new HashSet<>(prevIntf.ipAddressesList())));
1762 } catch (ConfigException e) {
1763 log.error("Error in configuration");
1764 }
1765 }
1766
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001767 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
1768 VlanId vlanId, boolean pushVlan) {
1769 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1770 if (grpHandler == null) {
1771 log.warn("Failed to retrieve group handler for device {}", deviceId);
1772 return;
1773 }
1774
1775 // Update filtering objective for a single port
1776 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
1777 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
1778
1779 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
1780 // Update L2IG bucket of the port
1781 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
1782 } else {
1783 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1784 }
1785 }
1786
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001787 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1788 VlanId vlanId, boolean pushVlan, boolean install) {
1789 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1790 if (grpHandler == null) {
1791 log.warn("Failed to retrieve group handler for device {}", deviceId);
1792 return;
1793 }
1794
1795 // Update filtering objective for a single port
1796 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1797
1798 // Update filtering objective for multicast ingress port
1799 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1800
1801 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1802
1803 if (nextId != -1 && !install) {
1804 // Update next objective for a single port as an output port
1805 // Remove a single port from L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001806 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001807 // Remove L2 Bridging rule and L3 Unicast rule to the host
1808 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1809 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1810 // only if there is no port configured on that VLAN ID
1811 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1812 // Remove broadcast forwarding rule for the VLAN
1813 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1814 // Remove L2FG for VLAN
1815 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1816 } else {
1817 // Remove L2IG of the port
1818 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1819 }
1820 } else if (install) {
1821 if (nextId != -1) {
1822 // Add a single port to L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001823 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001824 } else {
1825 // Create L2FG for VLAN
1826 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1827 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1828 }
1829 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1830 } else {
1831 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1832 }
1833 }
1834
1835 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1836 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1837 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1838
1839 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1840 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1841 .filter(intf -> !intf.connectPoint().equals(cp))
1842 .flatMap(intf -> intf.ipAddressesList().stream())
1843 .collect(Collectors.toSet());
1844 // 1. Partial subnet population
1845 // Remove routing rules for removed subnet from previous configuration,
1846 // which does not also exist in other interfaces in the same device
1847 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1848 .map(InterfaceIpAddress::subnetAddress)
1849 .collect(Collectors.toSet());
1850
1851 defaultRoutingHandler.revokeSubnet(
1852 ipPrefixSet.stream()
1853 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1854 .collect(Collectors.toSet()));
1855
1856 // 2. Interface IP punts
1857 // Remove IP punts for old Intf address
1858 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1859 .map(InterfaceIpAddress::ipAddress)
1860 .collect(Collectors.toSet());
1861 ipAddressSet.stream()
1862 .map(InterfaceIpAddress::ipAddress)
1863 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1864 .forEach(interfaceIpAddress ->
1865 routingRulePopulator.revokeSingleIpPunts(
1866 cp.deviceId(), interfaceIpAddress));
1867
1868 // 3. Host unicast routing rule
1869 // Remove unicast routing rule
1870 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1871 }
1872
1873 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1874 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1875 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1876
1877 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1878 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1879 .filter(intf -> !intf.connectPoint().equals(cp))
1880 .flatMap(intf -> intf.ipAddressesList().stream())
1881 .collect(Collectors.toSet());
1882 // 1. Partial subnet population
1883 // Add routing rules for newly added subnet, which does not also exist in
1884 // other interfaces in the same device
1885 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1886 .map(InterfaceIpAddress::subnetAddress)
1887 .collect(Collectors.toSet());
1888
1889 defaultRoutingHandler.populateSubnet(
1890 Collections.singleton(cp),
1891 ipPrefixSet.stream()
1892 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1893 .collect(Collectors.toSet()));
1894
1895 // 2. Interface IP punts
1896 // Add IP punts for new Intf address
1897 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1898 .map(InterfaceIpAddress::ipAddress)
1899 .collect(Collectors.toSet());
1900 ipAddressSet.stream()
1901 .map(InterfaceIpAddress::ipAddress)
1902 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1903 .forEach(interfaceIpAddress ->
1904 routingRulePopulator.populateSingleIpPunts(
1905 cp.deviceId(), interfaceIpAddress));
1906
1907 // 3. Host unicast routing rule
1908 // Add unicast routing rule
1909 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1910 }
sangho80f11cb2015-04-01 13:05:26 -07001911}