blob: 46599ea0f39d4504e14c353daf72cc60258a9fbc [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;
Pier Luigi580fd8a2018-01-16 10:47:50 +010041import org.onosproject.cluster.ClusterService;
Pierdb27b8d2018-04-17 16:29:56 +020042import org.onosproject.cluster.NodeId;
sangho80f11cb2015-04-01 13:05:26 -070043import org.onosproject.core.ApplicationId;
44import org.onosproject.core.CoreService;
45import org.onosproject.event.Event;
Charles Chanfbcb8812018-04-18 18:41:05 -070046import org.onosproject.mastership.MastershipEvent;
47import org.onosproject.mastership.MastershipListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070048import org.onosproject.mastership.MastershipService;
Pier1f87aca2018-03-14 16:47:32 -070049import org.onosproject.mcast.api.McastEvent;
50import org.onosproject.mcast.api.McastListener;
51import org.onosproject.mcast.api.MulticastRouteService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080052import org.onosproject.net.ConnectPoint;
Jonathan Hart54541d12016-04-12 15:39:44 -070053import org.onosproject.net.Device;
54import org.onosproject.net.DeviceId;
Charles Chanf0ae41e2017-08-23 13:55:39 -070055import org.onosproject.net.Host;
56import org.onosproject.net.HostId;
Jonathan Hart54541d12016-04-12 15:39:44 -070057import org.onosproject.net.Link;
58import org.onosproject.net.Port;
Charles Chanf4586112015-11-09 16:37:23 -080059import org.onosproject.net.PortNumber;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070060import org.onosproject.net.config.ConfigException;
Charles Chan72f556a2015-10-05 17:50:33 -070061import org.onosproject.net.config.ConfigFactory;
62import org.onosproject.net.config.NetworkConfigEvent;
Charles Chan72f556a2015-10-05 17:50:33 -070063import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070064import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkeyae0068a2017-08-15 11:02:29 -070065import org.onosproject.net.config.basics.InterfaceConfig;
66import org.onosproject.net.config.basics.McastConfig;
Charles Chan72f556a2015-10-05 17:50:33 -070067import org.onosproject.net.config.basics.SubjectFactories;
Saurav Dase6c448a2018-01-18 12:07:33 -080068import org.onosproject.net.device.DeviceAdminService;
Jonathan Hart54541d12016-04-12 15:39:44 -070069import org.onosproject.net.device.DeviceEvent;
70import org.onosproject.net.device.DeviceListener;
71import org.onosproject.net.device.DeviceService;
Charles Chanf4586112015-11-09 16:37:23 -080072import org.onosproject.net.flow.TrafficSelector;
73import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart54541d12016-04-12 15:39:44 -070074import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chanf4586112015-11-09 16:37:23 -080075import org.onosproject.net.host.HostEvent;
76import org.onosproject.net.host.HostListener;
Charles Chan873661e2017-11-30 15:37:50 -080077import org.onosproject.net.host.HostLocationProbingService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080078import org.onosproject.net.host.HostService;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070079import org.onosproject.net.host.InterfaceIpAddress;
Pierdb27b8d2018-04-17 16:29:56 +020080import org.onosproject.net.intent.WorkPartitionService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070081import org.onosproject.net.intf.Interface;
82import org.onosproject.net.intf.InterfaceService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080083import org.onosproject.net.link.LinkEvent;
84import org.onosproject.net.link.LinkListener;
85import org.onosproject.net.link.LinkService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070086import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080087import org.onosproject.net.packet.InboundPacket;
88import org.onosproject.net.packet.PacketContext;
89import org.onosproject.net.packet.PacketProcessor;
90import org.onosproject.net.packet.PacketService;
Pier Luigid8a15162018-02-15 16:33:08 +010091import org.onosproject.net.topology.TopologyEvent;
92import org.onosproject.net.topology.TopologyListener;
Charles Chanc91c8782016-03-30 17:54:24 -070093import org.onosproject.net.topology.TopologyService;
Charles Chanf0ae41e2017-08-23 13:55:39 -070094import org.onosproject.routeservice.ResolvedRoute;
Ray Milkeyae0068a2017-08-15 11:02:29 -070095import org.onosproject.routeservice.RouteEvent;
96import org.onosproject.routeservice.RouteListener;
97import org.onosproject.routeservice.RouteService;
Charles Chanc91c8782016-03-30 17:54:24 -070098import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
99import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventre6b19e482016-11-07 16:21:04 -0800100import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkeyae0068a2017-08-15 11:02:29 -0700101import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chan82f19972016-05-17 13:13:55 -0700102import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chanc91c8782016-03-30 17:54:24 -0700103import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -0700104import org.onosproject.segmentrouting.grouphandler.DestinationSet;
105import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Pier Luigi69f774d2018-02-28 12:10:50 +0100106import org.onosproject.segmentrouting.mcast.McastHandler;
107import org.onosproject.segmentrouting.mcast.McastRole;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700108import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700109import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800110import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700111import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800112
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800113import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkeyae0068a2017-08-15 11:02:29 -0700114import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800115import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800116import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
117
Saurav Das261c3002017-06-13 15:35:54 -0700118import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Pier Luigi0f9635b2018-01-15 18:06:43 +0100119import org.onosproject.segmentrouting.storekey.McastStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -0700120import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -0800121import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -0700122import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hart54541d12016-04-12 15:39:44 -0700123import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700124import org.onosproject.store.service.EventuallyConsistentMap;
125import org.onosproject.store.service.EventuallyConsistentMapBuilder;
126import org.onosproject.store.service.StorageService;
127import org.onosproject.store.service.WallClockTimestamp;
Charles Chan873661e2017-11-30 15:37:50 -0800128import org.osgi.service.component.ComponentContext;
sangho80f11cb2015-04-01 13:05:26 -0700129import org.slf4j.Logger;
130import org.slf4j.LoggerFactory;
131
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700132import java.util.ArrayList;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800133import java.util.Collections;
Charles Chan873661e2017-11-30 15:37:50 -0800134import java.util.Dictionary;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800135import java.util.HashSet;
136import java.util.List;
137import java.util.Map;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800138import java.util.Optional;
139import java.util.Set;
140import java.util.concurrent.ConcurrentHashMap;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800141import java.util.concurrent.Executors;
142import java.util.concurrent.ScheduledExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800143import java.util.concurrent.TimeUnit;
144import java.util.concurrent.atomic.AtomicBoolean;
145import java.util.stream.Collectors;
sangho80f11cb2015-04-01 13:05:26 -0700146
Charles Chand6d25332016-02-26 22:19:52 -0800147import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800148import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700149import static org.onlab.util.Tools.groupedThreads;
Saurav Dase321cff2018-02-09 17:26:45 -0800150import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
151import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Charles Chand6d25332016-02-26 22:19:52 -0800152
Charles Chanb7f75ac2016-01-11 18:28:54 -0800153/**
154 * Segment routing manager.
155 */
Jonathan Hart54541d12016-04-12 15:39:44 -0700156@Service
157@Component(immediate = true)
sangho27462c62015-05-14 00:39:53 -0700158public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700159
Charles Chan46fdfaf2016-11-09 20:51:44 -0800160 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan910be6a2017-08-23 14:46:43 -0700161 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sangho80f11cb2015-04-01 13:05:26 -0700162
163 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700164 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700165
166 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventreb6a7f342016-11-26 21:05:22 -0800167 private NeighbourResolutionService neighbourResolutionService;
168
169 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100170 public CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700171
172 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700173 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700174
175 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700176 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700177
178 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan873661e2017-11-30 15:37:50 -0800179 HostLocationProbingService probingService;
180
181 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100182 public DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700183
184 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Saurav Dase6c448a2018-01-18 12:07:33 -0800185 DeviceAdminService deviceAdminService;
186
187 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800188 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700189
190 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100191 public LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700192
Charles Chan82ab1932016-01-30 23:22:37 -0800193 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800194 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700195
196 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800197 public StorageService storageService;
Charles Chandebfea32016-10-24 14:52:01 -0700198
199 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100200 public MulticastRouteService multicastRouteService;
Charles Chandebfea32016-10-24 14:52:01 -0700201
202 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan83163812018-01-09 13:45:07 -0800203 public TopologyService topologyService;
Charles Chandebfea32016-10-24 14:52:01 -0700204
205 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700206 RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800207
208 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800209 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800210
Saurav Dasc3604f12016-03-23 11:22:49 -0700211 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800212 public InterfaceService interfaceService;
213
Pier Luigi580fd8a2018-01-16 10:47:50 +0100214 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
215 public ClusterService clusterService;
216
217 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pierdb27b8d2018-04-17 16:29:56 +0200218 public WorkPartitionService workPartitionService;
Pier Luigi580fd8a2018-01-16 10:47:50 +0100219
Charles Chan873661e2017-11-30 15:37:50 -0800220 @Property(name = "activeProbing", boolValue = true,
221 label = "Enable active probing to discover dual-homed hosts.")
222 boolean activeProbing = true;
223
Charles Chandebfea32016-10-24 14:52:01 -0700224 ArpHandler arpHandler = null;
225 IcmpHandler icmpHandler = null;
226 IpHandler ipHandler = null;
227 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700228 ApplicationId appId;
229 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700230
Charles Chandebfea32016-10-24 14:52:01 -0700231 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700232 private TunnelHandler tunnelHandler = null;
233 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700234 private InternalPacketProcessor processor = null;
235 private InternalLinkListener linkListener = null;
236 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700237 private AppConfigHandler appCfgHandler = null;
Pier Luigi69f774d2018-02-28 12:10:50 +0100238 public XConnectHandler xConnectHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800239 McastHandler mcastHandler = null;
240 HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800241 private RouteHandler routeHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800242 LinkHandler linkHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800243 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800244 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigid8a15162018-02-15 16:33:08 +0100245 private TopologyHandler topologyHandler = null;
Charles Chan82ab1932016-01-30 23:22:37 -0800246 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700247 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
248 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700249 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigid8a15162018-02-15 16:33:08 +0100250 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chanfbcb8812018-04-18 18:41:05 -0700251 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
sangho80f11cb2015-04-01 13:05:26 -0700252
Charles Chan52003922018-04-05 16:31:15 -0700253 // Handles device, link, topology and network config events
Charles Chanfbcb8812018-04-18 18:41:05 -0700254 private ScheduledExecutorService mainEventExecutor;
sangho80f11cb2015-04-01 13:05:26 -0700255
Charles Chanfbcb8812018-04-18 18:41:05 -0700256 // Handles host, route and mcast events respectively
257 private ScheduledExecutorService hostEventExecutor;
258 private ScheduledExecutorService routeEventExecutor;
259 private ScheduledExecutorService mcastEventExecutor;
Charles Chan52003922018-04-05 16:31:15 -0700260
261 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chanb7f75ac2016-01-11 18:28:54 -0800262 /**
Saurav Das261c3002017-06-13 15:35:54 -0700263 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700264 * Used to keep track on MPLS group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800265 */
Charles Chan873661e2017-11-30 15:37:50 -0800266 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das261c3002017-06-13 15:35:54 -0700267 dsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800268 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700269 * Per device next objective ID store with (device id + vlanid) as key.
270 * Used to keep track on L2 flood group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800271 */
Charles Chan873661e2017-11-30 15:37:50 -0800272 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800273 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800274 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700275 * Per device next objective ID store with (device id + port + treatment + meta) as key.
276 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800277 */
Charles Chan873661e2017-11-30 15:37:50 -0800278 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800279 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800280
Saurav Das2d94d312015-11-24 23:21:05 -0800281 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
282 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700283
Saurav Das261c3002017-06-13 15:35:54 -0700284 private AtomicBoolean programmingScheduled = new AtomicBoolean();
285
Charles Chanc91c8782016-03-30 17:54:24 -0700286 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700287 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
288 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700289 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700290 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800291 public SegmentRoutingDeviceConfig createConfig() {
292 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700293 }
294 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800295
Charles Chanc91c8782016-03-30 17:54:24 -0700296 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700297 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
298 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700299 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800300 @Override
301 public SegmentRoutingAppConfig createConfig() {
302 return new SegmentRoutingAppConfig();
303 }
304 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800305
Charles Chan82f19972016-05-17 13:13:55 -0700306 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
307 new ConfigFactory<ApplicationId, XConnectConfig>(
308 SubjectFactories.APP_SUBJECT_FACTORY,
309 XConnectConfig.class, "xconnect") {
310 @Override
311 public XConnectConfig createConfig() {
312 return new XConnectConfig();
313 }
314 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800315
Charles Chanc91c8782016-03-30 17:54:24 -0700316 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700317 new ConfigFactory<ApplicationId, McastConfig>(
318 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700319 McastConfig.class, "multicast") {
320 @Override
321 public McastConfig createConfig() {
322 return new McastConfig();
323 }
324 };
325
Charles Chan1963f4f2016-02-18 14:22:42 -0800326 /**
327 * Segment Routing App ID.
328 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800329 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan10b0fb72017-02-02 16:20:42 -0800330
Charles Chanb7f75ac2016-01-11 18:28:54 -0800331 /**
332 * The default VLAN ID assigned to the interfaces without subnet config.
333 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800334 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das7c305372015-10-28 12:39:42 -0700335
sangho80f11cb2015-04-01 13:05:26 -0700336 @Activate
Charles Chan873661e2017-11-30 15:37:50 -0800337 protected void activate(ComponentContext context) {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800338 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700339
Charles Chanfbcb8812018-04-18 18:41:05 -0700340 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-main", "%d", log));
341 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-host", "%d", log));
342 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-route", "%d", log));
343 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-mcast", "%d", log));
344
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700345 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700346 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700347 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700348 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700349 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700350 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700351 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700352 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700353 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700354
Charles Chan10b0fb72017-02-02 16:20:42 -0800355 log.debug("Creating EC map vlannextobjectivestore");
356 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
357 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
358 vlanNextObjStore = vlanNextObjMapBuilder
359 .withName("vlannextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700360 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700361 .withTimestampProvider((k, v) -> new WallClockTimestamp())
362 .build();
363
Saurav Das2d94d312015-11-24 23:21:05 -0800364 log.debug("Creating EC map subnetnextobjectivestore");
365 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
366 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
367 portNextObjStore = portNextObjMapBuilder
368 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700369 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800370 .withTimestampProvider((k, v) -> new WallClockTimestamp())
371 .build();
372
sangho4a5c42a2015-05-20 22:16:38 -0700373 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
374 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700375 tunnelStore = tunnelMapBuilder
376 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700377 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700378 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700379 .build();
380
381 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
382 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700383 policyStore = policyMapBuilder
384 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700385 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700386 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700387 .build();
388
Saurav Dasc3604f12016-03-23 11:22:49 -0700389 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800390 "purgeOnDisconnection", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700391 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800392 "purgeOnDisconnection", "true");
Pier Luigib9632ba2017-01-12 18:14:58 -0800393 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
394 "requestInterceptsEnabled", "false");
Charles Chanc1909e12017-08-08 15:13:37 -0700395 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800396 "requestInterceptsEnabled", "false");
Charles Chan9597f8d2017-07-24 15:56:10 -0700397 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800398 "arpEnabled", "false");
Pier Luigi0ebeb312017-02-02 22:31:34 -0800399 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
400 "greedyLearningIpv6", "true");
Charles Chancf8ea472017-02-28 15:15:17 -0800401 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
402 "forceUnprovision", "true");
Charles Chanc7b73c72017-08-10 16:57:28 -0700403 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan4c95c0d2017-07-20 16:16:25 -0700404 "distributed", "true");
Charles Chan804772f2017-08-14 11:42:11 -0700405 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
406 "multihomingEnabled", "true");
Charles Chan0c9c19f2017-11-29 19:54:20 -0800407 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
408 "staleLinkAge", "15000");
409 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
410 "allowDuplicateIps", "false");
Charles Chan873661e2017-11-30 15:37:50 -0800411 compCfgService.registerProperties(getClass());
412 modified(context);
Saurav Dasc3604f12016-03-23 11:22:49 -0700413
Charles Chan2b078ae2015-10-14 11:24:40 -0700414 processor = new InternalPacketProcessor();
415 linkListener = new InternalLinkListener();
416 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700417 appCfgHandler = new AppConfigHandler(this);
418 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700419 mcastHandler = new McastHandler(this);
420 hostHandler = new HostHandler(this);
Saurav Dase6c448a2018-01-18 12:07:33 -0800421 linkHandler = new LinkHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700422 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800423 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800424 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigid8a15162018-02-15 16:33:08 +0100425 topologyHandler = new TopologyHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700426
Charles Chand6d25332016-02-26 22:19:52 -0800427 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700428 cfgService.registerConfigFactory(deviceConfigFactory);
429 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700430 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700431 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase321cff2018-02-09 17:26:45 -0800432 log.info("Configuring network before adding listeners");
Charles Chan6961f222018-01-04 14:26:07 -0800433 cfgListener.configureNetwork();
434
Charles Chan82ab1932016-01-30 23:22:37 -0800435 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700436 packetService.addProcessor(processor, PacketProcessor.director(2));
437 linkService.addListener(linkListener);
438 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700439 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700440 routeService.addListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100441 topologyService.addListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700442 mastershipService.addListener(mastershipListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700443
Charles Chanc12c3d02018-03-09 15:53:44 -0800444 linkHandler.init();
Andreas Pantelopoulos75eef062018-01-11 07:53:48 -0800445 l2TunnelHandler.init();
446
sangho80f11cb2015-04-01 13:05:26 -0700447 log.info("Started");
448 }
449
Saurav Dase6c448a2018-01-18 12:07:33 -0800450 KryoNamespace.Builder createSerializer() {
Jonathan Hart54541d12016-04-12 15:39:44 -0700451 return new KryoNamespace.Builder()
452 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700453 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800454 VlanNextObjectiveStoreKey.class,
455 DestinationSet.class,
456 NextNeighbors.class,
457 Tunnel.class,
458 DefaultTunnel.class,
459 Policy.class,
460 TunnelPolicy.class,
461 Policy.Type.class,
462 PortNextObjectiveStoreKey.class,
463 XConnectStoreKey.class,
464 L2Tunnel.class,
465 L2TunnelPolicy.class,
466 DefaultL2Tunnel.class,
467 DefaultL2TunnelPolicy.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700468 );
469 }
470
sangho80f11cb2015-04-01 13:05:26 -0700471 @Deactivate
472 protected void deactivate() {
Charles Chanfbcb8812018-04-18 18:41:05 -0700473 mainEventExecutor.shutdown();
474 hostEventExecutor.shutdown();
475 routeEventExecutor.shutdown();
476 mcastEventExecutor.shutdown();
477
Charles Chan72f556a2015-10-05 17:50:33 -0700478 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700479 cfgService.unregisterConfigFactory(deviceConfigFactory);
480 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700481 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700482 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan873661e2017-11-30 15:37:50 -0800483 compCfgService.unregisterProperties(getClass(), false);
Charles Chan72f556a2015-10-05 17:50:33 -0700484
Charles Chanfd48c222017-06-19 00:43:31 -0700485 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700486 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700487 linkService.removeListener(linkListener);
488 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700489 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700490 routeService.removeListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100491 topologyService.removeListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700492 mastershipService.removeListener(mastershipListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700493
Charles Chan2e71ef32017-02-23 15:44:08 -0800494 neighbourResolutionService.unregisterNeighbourHandlers(appId);
495
sangho80f11cb2015-04-01 13:05:26 -0700496 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700497 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700498 deviceListener = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700499 groupHandlerMap.clear();
500
Saurav Das261c3002017-06-13 15:35:54 -0700501 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800502 vlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700503 portNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700504 tunnelStore.destroy();
505 policyStore.destroy();
Pier Luigi35dab3f2018-01-25 16:16:02 +0100506
507 mcastHandler.terminate();
sangho80f11cb2015-04-01 13:05:26 -0700508 log.info("Stopped");
509 }
510
Charles Chan873661e2017-11-30 15:37:50 -0800511 @Modified
512 private void modified(ComponentContext context) {
513 Dictionary<?, ?> properties = context.getProperties();
514 if (properties == null) {
515 return;
516 }
517
518 String strActiveProving = Tools.get(properties, "activeProbing");
519 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
520
521 if (expectActiveProbing != activeProbing) {
522 activeProbing = expectActiveProbing;
523 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
524 }
525 }
526
sangho27462c62015-05-14 00:39:53 -0700527 @Override
528 public List<Tunnel> getTunnels() {
529 return tunnelHandler.getTunnels();
530 }
531
532 @Override
sanghobd812f82015-06-29 14:58:47 -0700533 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
534 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700535 }
536
537 @Override
sanghobd812f82015-06-29 14:58:47 -0700538 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700539 for (Policy policy: policyHandler.getPolicies()) {
540 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
541 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
542 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
543 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700544 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700545 }
546 }
547 }
sanghobd812f82015-06-29 14:58:47 -0700548 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700549 }
550
551 @Override
sanghobd812f82015-06-29 14:58:47 -0700552 public PolicyHandler.Result removePolicy(Policy policy) {
553 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700554 }
555
556 @Override
sanghobd812f82015-06-29 14:58:47 -0700557 public PolicyHandler.Result createPolicy(Policy policy) {
558 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700559 }
560
561 @Override
562 public List<Policy> getPolicies() {
563 return policyHandler.getPolicies();
564 }
565
Saurav Das07c74602016-04-27 18:35:50 -0700566 @Override
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700567 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
568 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700569 }
570
571 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800572 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700573 return l2TunnelHandler.getL2Tunnels();
574 }
575
576 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800577 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700578 return l2TunnelHandler.getL2Policies();
579 }
580
Saurav Dasdebcf882018-04-06 20:16:01 -0700581 @Override
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700582 @Deprecated
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700583 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
584
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700585 // get both added and pending pseudowires
586 List<L2TunnelDescription> pseudowires = new ArrayList<>();
587 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
588 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700589 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700590
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700591 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
592
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700593 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
594 L2TunnelHandler.Result res;
595 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
596 res = addPseudowire(pw);
597 if (res != L2TunnelHandler.Result.SUCCESS) {
598 log.error("Pseudowire with id {} can not be instantiated !", res);
599 retRes = res;
600 }
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700601 }
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700602
603 return retRes;
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700604 }
605
606 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800607 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700608 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700609 }
610
611 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800612 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700613 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700614 }
615
616 @Override
Saurav Das07c74602016-04-27 18:35:50 -0700617 public void rerouteNetwork() {
618 cfgListener.configureNetwork();
Saurav Das07c74602016-04-27 18:35:50 -0700619 }
620
Charles Chand7844e52016-10-20 17:02:44 -0700621 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800622 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
623 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart61e24e12017-11-30 18:23:42 -0800624 deviceConfiguration.getRouters().forEach(device ->
625 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chand7844e52016-10-20 17:02:44 -0700626 return deviceSubnetMap;
627 }
628
Saurav Das62ae6792017-05-15 15:34:25 -0700629
630 @Override
631 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
632 if (defaultRoutingHandler != null) {
633 return defaultRoutingHandler.getCurrentEmcpSpgMap();
634 } else {
635 return null;
636 }
637 }
638
639 @Override
Saurav Das261c3002017-06-13 15:35:54 -0700640 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
641 if (dsNextObjStore != null) {
642 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700643 } else {
644 return ImmutableMap.of();
645 }
646 }
647
Saurav Dasfbe74572017-08-03 18:30:35 -0700648 @Override
649 public void verifyGroups(DeviceId id) {
650 DefaultGroupHandler gh = groupHandlerMap.get(id);
651 if (gh != null) {
652 gh.triggerBucketCorrector();
653 }
654 }
655
Saurav Das6430f412018-01-25 09:49:01 -0800656 @Override
657 public ImmutableMap<Link, Boolean> getSeenLinks() {
658 return linkHandler.getSeenLinks();
659 }
660
661 @Override
662 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
663 return linkHandler.getDownedPorts();
664 }
665
Pier Luigi0f9635b2018-01-15 18:06:43 +0100666 @Override
667 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
668 return mcastHandler.getMcastNextIds(mcastIp);
669 }
670
671 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +0100672 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigi0f9635b2018-01-15 18:06:43 +0100673 return mcastHandler.getMcastRoles(mcastIp);
674 }
675
676 @Override
677 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
678 return mcastHandler.getMcastPaths(mcastIp);
679 }
680
Pierdb27b8d2018-04-17 16:29:56 +0200681 @Override
Pier71c55772018-04-17 17:25:22 +0200682 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
683 ConnectPoint sourcecp) {
684 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
685 }
686
687 @Override
Pierdb27b8d2018-04-17 16:29:56 +0200688 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
689 return mcastHandler.getMcastLeaders(mcastIp);
690 }
691
sangho80f1f892015-05-19 11:57:42 -0700692 /**
Ray Milkeyb85de082017-04-05 09:42:04 -0700693 * Extracts the application ID from the manager.
694 *
695 * @return application ID
696 */
697 public ApplicationId appId() {
698 return appId;
699 }
700
701 /**
702 * Returns the device configuration.
703 *
704 * @return device configuration
705 */
706 public DeviceConfiguration deviceConfiguration() {
707 return deviceConfiguration;
708 }
709
710 /**
Saurav Das261c3002017-06-13 15:35:54 -0700711 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700712 * Used to keep track on MPLS group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700713 *
714 * @return next objective ID store
715 */
Saurav Das261c3002017-06-13 15:35:54 -0700716 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
717 dsNextObjStore() {
718 return dsNextObjStore;
Ray Milkeyb85de082017-04-05 09:42:04 -0700719 }
720
721 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700722 * Per device next objective ID store with (device id + vlanid) as key.
723 * Used to keep track on L2 flood group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700724 *
725 * @return vlan next object store
726 */
727 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
728 return vlanNextObjStore;
729 }
730
731 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700732 * Per device next objective ID store with (device id + port + treatment + meta) as key.
733 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700734 *
735 * @return port next object store.
736 */
737 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
738 return portNextObjStore;
739 }
740
741 /**
Saurav Das261c3002017-06-13 15:35:54 -0700742 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
743 * labeled packets should be programmed or not.
Pier Ventre7a78de22016-10-31 15:00:01 -0700744 *
745 * @return MPLS-ECMP value
746 */
747 public boolean getMplsEcmp() {
748 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
749 .getConfig(this.appId, SegmentRoutingAppConfig.class);
750 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
751 }
752
753 /**
sangho80f1f892015-05-19 11:57:42 -0700754 * Returns the tunnel object with the tunnel ID.
755 *
756 * @param tunnelId Tunnel ID
757 * @return Tunnel reference
758 */
sangho27462c62015-05-14 00:39:53 -0700759 public Tunnel getTunnel(String tunnelId) {
760 return tunnelHandler.getTunnel(tunnelId);
761 }
762
Charles Chan90772a72017-02-08 15:52:08 -0800763 // TODO Consider moving these to InterfaceService
sangho80f11cb2015-04-01 13:05:26 -0700764 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800765 * Returns untagged VLAN configured on given connect point.
Charles Chan90772a72017-02-08 15:52:08 -0800766 * <p>
767 * Only returns the first match if there are multiple untagged VLAN configured
768 * on the connect point.
sangho80f11cb2015-04-01 13:05:26 -0700769 *
Charles Chan10b0fb72017-02-02 16:20:42 -0800770 * @param connectPoint connect point
771 * @return untagged VLAN or null if not configured
sangho80f11cb2015-04-01 13:05:26 -0700772 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700773 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800774 return interfaceService.getInterfacesByPort(connectPoint).stream()
775 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
776 .map(Interface::vlanUntagged)
777 .findFirst().orElse(null);
sangho80f11cb2015-04-01 13:05:26 -0700778 }
779
sangho27462c62015-05-14 00:39:53 -0700780 /**
Charles Chan90772a72017-02-08 15:52:08 -0800781 * Returns tagged VLAN configured on given connect point.
782 * <p>
783 * Returns all matches if there are multiple tagged VLAN configured
784 * on the connect point.
785 *
786 * @param connectPoint connect point
787 * @return tagged VLAN or empty set if not configured
788 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700789 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan90772a72017-02-08 15:52:08 -0800790 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
791 return interfaces.stream()
792 .map(Interface::vlanTagged)
Charles Chan3ed34d82017-06-22 18:03:14 -0700793 .flatMap(Set::stream)
Charles Chan90772a72017-02-08 15:52:08 -0800794 .collect(Collectors.toSet());
795 }
796
797 /**
798 * Returns native VLAN configured on given connect point.
799 * <p>
800 * Only returns the first match if there are multiple native VLAN configured
801 * on the connect point.
802 *
803 * @param connectPoint connect point
804 * @return native VLAN or null if not configured
805 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700806 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan90772a72017-02-08 15:52:08 -0800807 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
808 return interfaces.stream()
809 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
810 .map(Interface::vlanNative)
811 .findFirst()
812 .orElse(null);
813 }
814
815 /**
Charles Chand9265a32017-06-16 15:19:24 -0700816 * Returns internal VLAN for untagged hosts on given connect point.
817 * <p>
818 * The internal VLAN is either vlan-untagged for an access port,
819 * or vlan-native for a trunk port.
820 *
821 * @param connectPoint connect point
822 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
823 */
Pier Luigi69f774d2018-02-28 12:10:50 +0100824 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chand9265a32017-06-16 15:19:24 -0700825 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
826 VlanId nativeVlanId = getNativeVlanId(connectPoint);
827 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
828 }
829
830 /**
Charles Chan3ed34d82017-06-22 18:03:14 -0700831 * Returns optional pair device ID of given device.
832 *
833 * @param deviceId device ID
834 * @return optional pair device ID. Might be empty if pair device is not configured
835 */
836 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
837 SegmentRoutingDeviceConfig deviceConfig =
838 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
839 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
840 }
841 /**
842 * Returns optional pair device local port of given device.
843 *
844 * @param deviceId device ID
845 * @return optional pair device ID. Might be empty if pair device is not configured
846 */
847 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
848 SegmentRoutingDeviceConfig deviceConfig =
849 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
850 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
851 }
852
853 /**
Charles Chanf0ae41e2017-08-23 13:55:39 -0700854 * Returns locations of given resolved route.
855 *
856 * @param resolvedRoute resolved route
857 * @return locations of nexthop. Might be empty if next hop is not found
858 */
859 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
860 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
861 return Optional.ofNullable(hostService.getHost(hostId))
862 .map(Host::locations).orElse(Sets.newHashSet())
863 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
864 }
865
866 /**
Charles Chan90772a72017-02-08 15:52:08 -0800867 * Returns vlan port map of given device.
868 *
869 * @param deviceId device id
870 * @return vlan-port multimap
871 */
872 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
873 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
874
875 interfaceService.getInterfaces().stream()
876 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
877 .forEach(intf -> {
878 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan3ed34d82017-06-22 18:03:14 -0700879 intf.vlanTagged().forEach(vlanTagged ->
880 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
881 );
Charles Chan90772a72017-02-08 15:52:08 -0800882 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
883 });
884 vlanPortMap.removeAll(VlanId.NONE);
885
886 return vlanPortMap;
887 }
888
889 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800890 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -0800891 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -0700892 *
893 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -0800894 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -0800895 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -0700896 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700897 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -0700898 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800899 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
900 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -0700901 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -0800902 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -0800903 + "device {} not found", deviceId);
904 return -1;
905 }
906 }
907
908 /**
909 * Returns the next objective ID for the given portNumber, given the treatment.
910 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -0700911 * would result in different objectives. If the next object does not exist,
912 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -0800913 *
914 * @param deviceId Device ID
915 * @param portNum port number on device for which NextObjective is queried
916 * @param treatment the actions to apply on the packets (should include outport)
917 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -0700918 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -0700919 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -0800920 */
921 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
922 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -0700923 TrafficSelector meta,
924 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -0800925 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
926 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -0700927 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -0800928 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800929 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
930 + " not found", deviceId);
931 return -1;
932 }
933 }
934
Saurav Das62ae6792017-05-15 15:34:25 -0700935 /**
936 * Returns the group handler object for the specified device id.
937 *
938 * @param devId the device identifier
939 * @return the groupHandler object for the device id, or null if not found
940 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700941 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Das62ae6792017-05-15 15:34:25 -0700942 return groupHandlerMap.get(devId);
943 }
944
945 /**
Saurav Dasfbe74572017-08-03 18:30:35 -0700946 * Returns the default routing handler object.
947 *
948 * @return the default routing handler object
949 */
950 public DefaultRoutingHandler getRoutingHandler() {
951 return defaultRoutingHandler;
952 }
953
sangho80f11cb2015-04-01 13:05:26 -0700954 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -0700955 @Override
956 public void process(PacketContext context) {
957
958 if (context.isHandled()) {
959 return;
960 }
961
962 InboundPacket pkt = context.inPacket();
963 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -0800964
965 if (ethernet == null) {
966 return;
967 }
968
Saurav Das261c3002017-06-13 15:35:54 -0700969 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
970 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800971 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -0700972 log.warn("Received unexpected ARP packet on {}",
973 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -0700974 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800975 return;
sangho80f11cb2015-04-01 13:05:26 -0700976 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -0800977 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
978 //ipHandler.addToPacketBuffer(ipv4Packet);
979 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
980 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700981 } else {
Charles Chand041ad82017-01-13 17:20:44 -0800982 // NOTE: We don't support IP learning at this moment so this
983 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800984 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700985 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800986 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
987 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -0800988 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -0800989 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -0800990 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
991 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
992 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
993 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
994 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
995 } else {
Saurav Das62ae6792017-05-15 15:34:25 -0700996 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -0700997 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -0800998 }
999 } else {
1000 // NOTE: We don't support IP learning at this moment so this
1001 // is not necessary. Also it causes duplication of DHCPv6 packets.
1002 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1003 }
sangho80f11cb2015-04-01 13:05:26 -07001004 }
1005 }
1006 }
1007
sangho80f11cb2015-04-01 13:05:26 -07001008 private class InternalEventHandler implements Runnable {
Charles Chan52003922018-04-05 16:31:15 -07001009 private Event event;
1010
1011 InternalEventHandler(Event event) {
1012 this.event = event;
1013 }
1014
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001015 @Override
sangho80f11cb2015-04-01 13:05:26 -07001016 public void run() {
Charles Chan52003922018-04-05 16:31:15 -07001017 try {
1018 // TODO We should also change SR routing and PW to listen to TopologyEvents
1019 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1020 event.type() == LinkEvent.Type.LINK_UPDATED) {
1021 linkHandler.processLinkAdded((Link) event.subject());
1022 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1023 linkHandler.processLinkRemoved((Link) event.subject());
1024 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1025 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1026 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1027 DeviceId deviceId = ((Device) event.subject()).id();
1028 if (deviceService.isAvailable(deviceId)) {
1029 log.info("** DEVICE UP Processing device event {} "
1030 + "for available device {}",
1031 event.type(), ((Device) event.subject()).id());
1032 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001033 } else {
Charles Chan52003922018-04-05 16:31:15 -07001034 log.info(" ** DEVICE DOWN Processing device event {}"
1035 + " for unavailable device {}",
1036 event.type(), ((Device) event.subject()).id());
1037 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001038 }
Charles Chan52003922018-04-05 16:31:15 -07001039 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1040 // typically these calls come when device is added first time
1041 // so port filtering rules are handled at the device_added event.
1042 // port added calls represent all ports on the device,
1043 // enabled or not.
1044 log.trace("** PORT ADDED {}/{} -> {}",
1045 ((DeviceEvent) event).subject().id(),
1046 ((DeviceEvent) event).port().number(),
1047 event.type());
1048 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1049 // these calls happen for every subsequent event
1050 // ports enabled, disabled, switch goes away, comes back
1051 log.info("** PORT UPDATED {}/{} -> {}",
1052 event.subject(),
1053 ((DeviceEvent) event).port(),
1054 event.type());
1055 processPortUpdated(((Device) event.subject()),
1056 ((DeviceEvent) event).port());
1057 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1058 // Process topology event, needed for all modules relying on
1059 // topology service for path computation
1060 TopologyEvent topologyEvent = (TopologyEvent) event;
1061 log.info("Processing topology event {}, topology age {}, reasons {}",
1062 event.type(), topologyEvent.subject().time(),
1063 topologyEvent.reasons().size());
1064 topologyHandler.processTopologyChange(topologyEvent.reasons());
1065 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1066 hostHandler.processHostAddedEvent((HostEvent) event);
1067 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1068 hostHandler.processHostMovedEvent((HostEvent) event);
1069 routeHandler.processHostMovedEvent((HostEvent) event);
1070 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1071 hostHandler.processHostRemovedEvent((HostEvent) event);
1072 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1073 hostHandler.processHostUpdatedEvent((HostEvent) event);
1074 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1075 routeHandler.processRouteAdded((RouteEvent) event);
1076 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1077 routeHandler.processRouteUpdated((RouteEvent) event);
1078 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1079 routeHandler.processRouteRemoved((RouteEvent) event);
1080 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1081 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1082 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1083 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1084 event.type() == McastEvent.Type.SINKS_ADDED ||
1085 event.type() == McastEvent.Type.SINKS_REMOVED ||
1086 event.type() == McastEvent.Type.ROUTE_ADDED ||
1087 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1088 mcastHandler.processMcastEvent((McastEvent) event);
1089 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1090 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1091 Class configClass = netcfgEvent.configClass();
1092 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1093 appCfgHandler.processAppConfigAdded(netcfgEvent);
1094 log.info("App config event .. configuring network");
1095 cfgListener.configureNetwork();
1096 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1097 log.info("Segment Routing Device Config added for {}", event.subject());
1098 cfgListener.configureNetwork();
1099 } else if (configClass.equals(XConnectConfig.class)) {
1100 xConnectHandler.processXConnectConfigAdded(netcfgEvent);
1101 } else if (configClass.equals(InterfaceConfig.class)) {
1102 log.info("Interface Config added for {}", event.subject());
1103 cfgListener.configureNetwork();
1104 } else {
1105 log.error("Unhandled config class: {}", configClass);
1106 }
1107 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1108 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1109 Class configClass = netcfgEvent.configClass();
1110 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1111 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1112 log.info("App config event .. configuring network");
1113 cfgListener.configureNetwork();
1114 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1115 log.info("Segment Routing Device Config updated for {}", event.subject());
1116 createOrUpdateDeviceConfiguration();
1117 } else if (configClass.equals(XConnectConfig.class)) {
1118 xConnectHandler.processXConnectConfigUpdated(netcfgEvent);
1119 } else if (configClass.equals(InterfaceConfig.class)) {
1120 log.info("Interface Config updated for {}", event.subject());
1121 createOrUpdateDeviceConfiguration();
1122 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1123 (InterfaceConfig) netcfgEvent.prevConfig().get());
1124 } else {
1125 log.error("Unhandled config class: {}", configClass);
1126 }
1127 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1128 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1129 Class configClass = netcfgEvent.configClass();
1130 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1131 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1132 log.info("App config event .. configuring network");
1133 cfgListener.configureNetwork();
1134 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1135 // TODO Handle sr device config removal
1136 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
1137 } else if (configClass.equals(XConnectConfig.class)) {
1138 xConnectHandler.processXConnectConfigRemoved(netcfgEvent);
1139 } else if (configClass.equals(InterfaceConfig.class)) {
1140 // TODO Handle interface removal
1141 log.info("InterfaceConfig removal is not handled in current implementation");
1142 } else {
1143 log.error("Unhandled config class: {}", configClass);
1144 }
1145 } else {
1146 log.warn("Unhandled event type: {}", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001147 }
Charles Chan52003922018-04-05 16:31:15 -07001148 } catch (Exception e) {
1149 log.error("SegmentRouting event handler thread thrown an exception: {}",
1150 e.getMessage(), e);
sangho80f11cb2015-04-01 13:05:26 -07001151 }
sangho80f11cb2015-04-01 13:05:26 -07001152 }
1153 }
1154
Saurav Dase6c448a2018-01-18 12:07:33 -08001155 void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001156 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001157
1158 // NOTE: Punt ARP/NDP even when the device is not configured.
1159 // Host learning without network config is required for CORD config generator.
1160 routingRulePopulator.populateIpPunts(device.id());
1161 routingRulePopulator.populateArpNdpPunts(device.id());
1162
Charles Chan319d1a22015-11-03 10:42:14 -08001163 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001164 log.warn("Device configuration unavailable. Device {} will be "
1165 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001166 return;
1167 }
Charles Chan72779502016-04-23 17:36:10 -07001168 processDeviceAddedInternal(device.id());
1169 }
1170
1171 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001172 // Irrespective of whether the local is a MASTER or not for this device,
1173 // we need to create a SR-group-handler instance. This is because in a
1174 // multi-instance setup, any instance can initiate forwarding/next-objectives
1175 // for any switch (even if this instance is a SLAVE or not even connected
1176 // to the switch). To handle this, a default-group-handler instance is necessary
1177 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001178 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1179 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001180 DefaultGroupHandler groupHandler;
1181 try {
1182 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001183 createGroupHandler(deviceId,
1184 appId,
1185 deviceConfiguration,
1186 linkService,
1187 flowObjectiveService,
1188 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001189 } catch (DeviceConfigNotFoundException e) {
1190 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1191 return;
1192 }
Saurav Das2b6a00f2017-12-05 15:00:23 -08001193 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan72779502016-04-23 17:36:10 -07001194 deviceId);
1195 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001196 }
Saurav Dasb149be12016-06-07 10:08:06 -07001197
Charles Chan72779502016-04-23 17:36:10 -07001198 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001199 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -07001200 xConnectHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001201 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001202 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001203 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001204 }
Charles Chan82ab1932016-01-30 23:22:37 -08001205
Charles Chandebfea32016-10-24 14:52:01 -07001206 appCfgHandler.init(deviceId);
Charles Chand66d6712018-03-29 16:03:41 -07001207 hostHandler.init(deviceId);
Charles Chandebfea32016-10-24 14:52:01 -07001208 routeHandler.init(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001209 }
1210
Saurav Dasc3604f12016-03-23 11:22:49 -07001211 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001212 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001213 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan873661e2017-11-30 15:37:50 -08001214 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan10b0fb72017-02-02 16:20:42 -08001215 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001216 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001217 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001218 portNextObjStore.entrySet().stream()
1219 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001220 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Dase6c448a2018-01-18 12:07:33 -08001221 linkHandler.processDeviceRemoved(device);
Charles Chan17d75d82017-06-15 00:44:51 -07001222
Saurav Dasfbe74572017-08-03 18:30:35 -07001223 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1224 if (gh != null) {
1225 gh.shutdown();
1226 }
Saurav Das62ae6792017-05-15 15:34:25 -07001227 // Note that a switch going down is associated with all of its links
1228 // going down as well, but it is treated as a single switch down event
Saurav Dasdebcf882018-04-06 20:16:01 -07001229 // while the link-downs are ignored. We cannot rely on the ordering of
1230 // events - i.e we cannot expect all link-downs to come before the
1231 // switch down - so we purge all seen-links for the switch before
1232 // handling route-path changes for the switch-down
Saurav Das62ae6792017-05-15 15:34:25 -07001233 defaultRoutingHandler
Saurav Dasdc7f2752018-03-18 21:28:15 -07001234 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Das6430f412018-01-25 09:49:01 -08001235 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan82f19972016-05-17 13:13:55 -07001236 xConnectHandler.removeDevice(device.id());
Saurav Das97241862018-02-14 14:14:54 -08001237
1238 // Cleanup all internal groupHandler stores for this device. Should be
1239 // done after all rerouting or rehashing has been completed
1240 groupHandlerMap.entrySet()
1241 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001242 }
1243
Saurav Dasf0f592d2016-11-18 15:21:57 -08001244 private void processPortUpdated(Device device, Port port) {
1245 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1246 log.warn("Device configuration uploading. Not handling port event for"
1247 + "dev: {} port: {}", device.id(), port.number());
1248 return;
1249 }
Saurav Dasf9332192017-02-18 14:05:44 -08001250
1251 if (!mastershipService.isLocalMaster(device.id())) {
1252 log.debug("Not master for dev:{} .. not handling port updated event"
1253 + "for port {}", device.id(), port.number());
1254 return;
1255 }
1256
1257 // first we handle filtering rules associated with the port
1258 if (port.isEnabled()) {
1259 log.info("Switchport {}/{} enabled..programming filters",
1260 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001261 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001262 } else {
1263 log.info("Switchport {}/{} disabled..removing filters",
1264 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001265 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001266 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001267
1268 // portUpdated calls are for ports that have gone down or up. For switch
1269 // to switch ports, link-events should take care of any re-routing or
1270 // group editing necessary for port up/down. Here we only process edge ports
1271 // that are already configured.
Saurav Das3fb28272017-03-04 16:08:47 -08001272 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1273 VlanId untaggedVlan = getUntaggedVlanId(cp);
1274 VlanId nativeVlan = getNativeVlanId(cp);
1275 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001276
Saurav Das3fb28272017-03-04 16:08:47 -08001277 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001278 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasf0f592d2016-11-18 15:21:57 -08001279 + "dev/port: {}/{}", device.id(), port.number());
1280 return;
1281 }
Saurav Das3fb28272017-03-04 16:08:47 -08001282 if (untaggedVlan != null) {
1283 processEdgePort(device, port, untaggedVlan, true);
1284 }
1285 if (nativeVlan != null) {
1286 processEdgePort(device, port, nativeVlan, true);
1287 }
1288 if (!taggedVlans.isEmpty()) {
1289 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1290 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001291 }
1292
Saurav Das3fb28272017-03-04 16:08:47 -08001293 private void processEdgePort(Device device, Port port, VlanId vlanId,
1294 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001295 boolean portUp = port.isEnabled();
1296 if (portUp) {
Saurav Das3fb28272017-03-04 16:08:47 -08001297 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001298 port.number(), vlanId);
Charles Chan873661e2017-11-30 15:37:50 -08001299 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Dasf0f592d2016-11-18 15:21:57 -08001300 } else {
Saurav Das3fb28272017-03-04 16:08:47 -08001301 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001302 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001303 }
1304
Srikanth Vavilapalli64505482015-04-21 13:04:13 -07001305 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sangho80f11cb2015-04-01 13:05:26 -07001306 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001307 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001308 } else {
1309 log.warn("Group handler not found for dev:{}. Not handling edge port"
1310 + " {} event for port:{}", device.id(),
1311 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001312 }
1313 }
sangho27462c62015-05-14 00:39:53 -07001314
Charles Chan8ca5a122017-10-20 16:06:55 -07001315 private void createOrUpdateDeviceConfiguration() {
1316 if (deviceConfiguration == null) {
Saurav Dase321cff2018-02-09 17:26:45 -08001317 log.info("Creating new DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001318 deviceConfiguration = new DeviceConfiguration(this);
1319 } else {
Saurav Dase321cff2018-02-09 17:26:45 -08001320 log.info("Updating DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001321 deviceConfiguration.updateConfig();
1322 }
1323 }
1324
Charles Chanfbcb8812018-04-18 18:41:05 -07001325 private void createOrUpdateDefaultRoutingHandler() {
1326 if (defaultRoutingHandler == null) {
1327 log.info("Creating new DefaultRoutingHandler");
1328 defaultRoutingHandler = new DefaultRoutingHandler(this);
1329 } else {
1330 log.info("Updating DefaultRoutingHandler");
1331 defaultRoutingHandler.update(this);
1332 }
1333 }
1334
Pier Ventreb6a7f342016-11-26 21:05:22 -08001335 /**
1336 * Registers the given connect point with the NRS, this is necessary
1337 * to receive the NDP and ARP packets from the NRS.
1338 *
1339 * @param portToRegister connect point to register
1340 */
1341 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001342 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001343 portToRegister,
1344 neighbourHandler,
1345 appId
1346 );
1347 }
1348
Charles Chan72f556a2015-10-05 17:50:33 -07001349 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001350 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001351 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001352
Charles Chanb7f75ac2016-01-11 18:28:54 -08001353 /**
1354 * Constructs the internal network config listener.
1355 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001356 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001357 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001358 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001359 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001360 }
1361
Charles Chanb7f75ac2016-01-11 18:28:54 -08001362 /**
1363 * Reads network config and initializes related data structure accordingly.
1364 */
Charles Chan873661e2017-11-30 15:37:50 -08001365 void configureNetwork() {
Saurav Dase321cff2018-02-09 17:26:45 -08001366 log.info("Configuring network ...");
Charles Chan8ca5a122017-10-20 16:06:55 -07001367 createOrUpdateDeviceConfiguration();
Charles Chane7c61022015-10-07 14:21:45 -07001368
Charles Chan46fdfaf2016-11-09 20:51:44 -08001369 arpHandler = new ArpHandler(srManager);
1370 icmpHandler = new IcmpHandler(srManager);
1371 ipHandler = new IpHandler(srManager);
1372 routingRulePopulator = new RoutingRulePopulator(srManager);
Charles Chanfbcb8812018-04-18 18:41:05 -07001373 createOrUpdateDefaultRoutingHandler();
Charles Chane7c61022015-10-07 14:21:45 -07001374
1375 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1376 groupHandlerMap, tunnelStore);
1377 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1378 flowObjectiveService,
1379 tunnelHandler, policyStore);
Saurav Das261c3002017-06-13 15:35:54 -07001380 // add a small delay to absorb multiple network config added notifications
1381 if (!programmingScheduled.get()) {
Saurav Dase321cff2018-02-09 17:26:45 -08001382 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
Saurav Das261c3002017-06-13 15:35:54 -07001383 programmingScheduled.set(true);
Charles Chan52003922018-04-05 16:31:15 -07001384 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
Charles Chane7c61022015-10-07 14:21:45 -07001385 }
Charles Chan72779502016-04-23 17:36:10 -07001386 mcastHandler.init();
Charles Chane7c61022015-10-07 14:21:45 -07001387 }
1388
Charles Chan72f556a2015-10-05 17:50:33 -07001389 @Override
1390 public void event(NetworkConfigEvent event) {
Charles Chan3f4aca62018-03-21 16:57:47 -07001391 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
1392 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1393 switch (event.type()) {
1394 case CONFIG_ADDED:
1395 case CONFIG_UPDATED:
1396 case CONFIG_REMOVED:
1397 log.trace("Schedule Network Config event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001398 mainEventExecutor.execute(new InternalEventHandler(event));
Charles Chan3f4aca62018-03-21 16:57:47 -07001399 break;
1400 default:
1401 break;
Charles Chan72f556a2015-10-05 17:50:33 -07001402 }
1403 }
Saurav Das261c3002017-06-13 15:35:54 -07001404
Charles Chanc4a68c32018-01-03 16:26:32 -08001405 @Override
1406 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase321cff2018-02-09 17:26:45 -08001407 if (event.type() == CONFIG_REGISTERED ||
1408 event.type() == CONFIG_UNREGISTERED) {
1409 log.debug("Ignore event {} due to type mismatch", event);
1410 return false;
Charles Chanc4a68c32018-01-03 16:26:32 -08001411 }
Saurav Dase321cff2018-02-09 17:26:45 -08001412
1413 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1414 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1415 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -08001416 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase321cff2018-02-09 17:26:45 -08001417 log.debug("Ignore event {} due to class mismatch", event);
1418 return false;
1419 }
1420
1421 return true;
Charles Chanc4a68c32018-01-03 16:26:32 -08001422 }
1423
Saurav Das261c3002017-06-13 15:35:54 -07001424 private final class ConfigChange implements Runnable {
1425 @Override
1426 public void run() {
1427 programmingScheduled.set(false);
Saurav Dase321cff2018-02-09 17:26:45 -08001428 log.info("Reacting to config changes after buffer delay");
Saurav Das261c3002017-06-13 15:35:54 -07001429 for (Device device : deviceService.getDevices()) {
1430 processDeviceAdded(device);
1431 }
1432 defaultRoutingHandler.startPopulationProcess();
1433 }
1434 }
Charles Chan72f556a2015-10-05 17:50:33 -07001435 }
Charles Chanf4586112015-11-09 16:37:23 -08001436
Charles Chan3f4aca62018-03-21 16:57:47 -07001437 private class InternalLinkListener implements LinkListener {
1438 @Override
1439 public void event(LinkEvent event) {
1440 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1441 event.type() == LinkEvent.Type.LINK_UPDATED ||
1442 event.type() == LinkEvent.Type.LINK_REMOVED) {
1443 log.trace("Schedule Link event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001444 mainEventExecutor.execute(new InternalEventHandler(event));
Charles Chan3f4aca62018-03-21 16:57:47 -07001445 }
1446 }
1447 }
1448
1449 private class InternalDeviceListener implements DeviceListener {
1450 @Override
1451 public void event(DeviceEvent event) {
1452 switch (event.type()) {
1453 case DEVICE_ADDED:
1454 case PORT_UPDATED:
1455 case PORT_ADDED:
1456 case DEVICE_UPDATED:
1457 case DEVICE_AVAILABILITY_CHANGED:
1458 log.trace("Schedule Device event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001459 mainEventExecutor.execute(new InternalEventHandler(event));
Charles Chan3f4aca62018-03-21 16:57:47 -07001460 break;
1461 default:
1462 }
1463 }
1464 }
1465
1466 private class InternalTopologyListener implements TopologyListener {
1467 @Override
1468 public void event(TopologyEvent event) {
1469 switch (event.type()) {
1470 case TOPOLOGY_CHANGED:
1471 log.trace("Schedule Topology event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001472 mainEventExecutor.execute(new InternalEventHandler(event));
Charles Chan3f4aca62018-03-21 16:57:47 -07001473 break;
1474 default:
1475 }
1476 }
1477 }
1478
Charles Chanf4586112015-11-09 16:37:23 -08001479 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001480 @Override
1481 public void event(HostEvent event) {
Charles Chanf4586112015-11-09 16:37:23 -08001482 switch (event.type()) {
1483 case HOST_ADDED:
Charles Chanf4586112015-11-09 16:37:23 -08001484 case HOST_MOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001485 case HOST_REMOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001486 case HOST_UPDATED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001487 log.trace("Schedule Host event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001488 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chanf4586112015-11-09 16:37:23 -08001489 break;
1490 default:
1491 log.warn("Unsupported host event type: {}", event.type());
1492 break;
1493 }
1494 }
1495 }
1496
Charles Chanc91c8782016-03-30 17:54:24 -07001497 private class InternalMcastListener implements McastListener {
1498 @Override
1499 public void event(McastEvent event) {
1500 switch (event.type()) {
Pier1f87aca2018-03-14 16:47:32 -07001501 case SOURCES_ADDED:
1502 case SOURCES_REMOVED:
1503 case SINKS_ADDED:
1504 case SINKS_REMOVED:
Charles Chanc91c8782016-03-30 17:54:24 -07001505 case ROUTE_REMOVED:
Pierdb27b8d2018-04-17 16:29:56 +02001506 case ROUTE_ADDED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001507 log.trace("Schedule Mcast event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001508 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi6786b922018-02-02 16:19:11 +01001509 break;
Charles Chanc91c8782016-03-30 17:54:24 -07001510 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07001511 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chanc91c8782016-03-30 17:54:24 -07001512 break;
1513 }
1514 }
1515 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001516
Charles Chandebfea32016-10-24 14:52:01 -07001517 private class InternalRouteEventListener implements RouteListener {
1518 @Override
1519 public void event(RouteEvent event) {
1520 switch (event.type()) {
1521 case ROUTE_ADDED:
Charles Chandebfea32016-10-24 14:52:01 -07001522 case ROUTE_UPDATED:
Charles Chandebfea32016-10-24 14:52:01 -07001523 case ROUTE_REMOVED:
Charles Chan910be6a2017-08-23 14:46:43 -07001524 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001525 log.trace("Schedule Route event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001526 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan910be6a2017-08-23 14:46:43 -07001527 break;
Charles Chandebfea32016-10-24 14:52:01 -07001528 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07001529 log.warn("Unsupported route event type: {}", event.type());
Charles Chandebfea32016-10-24 14:52:01 -07001530 break;
1531 }
1532 }
1533 }
Saurav Das62ae6792017-05-15 15:34:25 -07001534
Charles Chanfbcb8812018-04-18 18:41:05 -07001535 private class InternalMastershipListener implements MastershipListener {
1536 @Override
1537 public void event(MastershipEvent event) {
1538 DeviceId deviceId = event.subject();
1539 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1540
1541 switch (event.type()) {
1542 case MASTER_CHANGED:
1543 log.info("Invalidating shouldProgram cache for {}/pair={} due to mastership change",
1544 deviceId, pairDeviceId);
1545 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1546 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1547 break;
1548 case BACKUPS_CHANGED:
1549 case SUSPENDED:
1550 default:
1551 break;
1552 }
1553 }
1554 }
1555
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001556 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1557 try {
1558 Set<Interface> intfs = conf.getInterfaces();
1559 Set<Interface> prevIntfs = prevConf.getInterfaces();
1560
1561 // Now we only handle one interface config at each port.
1562 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1563 log.warn("Interface update aborted - one at a time is allowed, " +
1564 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1565 return;
1566 }
1567
1568 Interface intf = intfs.stream().findFirst().get();
1569 Interface prevIntf = prevIntfs.stream().findFirst().get();
1570
1571 DeviceId deviceId = intf.connectPoint().deviceId();
1572 PortNumber portNum = intf.connectPoint().port();
1573
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001574 removeSubnetConfig(prevIntf.connectPoint(),
1575 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1576 new HashSet<>(intf.ipAddressesList())));
1577
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001578 if (!prevIntf.vlanNative().equals(VlanId.NONE)
1579 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
1580 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1581 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
1582 // Update filtering objective and L2IG group bucket
1583 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
1584 } else {
1585 // RemoveVlanNative
1586 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1587 }
1588 }
1589
1590 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
1591 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1592 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1593 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
1594 // Update filtering objective and L2IG group bucket
1595 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
1596 } else {
1597 // RemoveVlanUntagged
1598 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1599 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001600 }
1601
1602 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1603 // RemoveVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001604 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
1605 .filter(i -> !intf.vlanUntagged().equals(i))
1606 .filter(i -> !intf.vlanNative().equals(i))
1607 .forEach(vlanId -> updateVlanConfigInternal(
1608 deviceId, portNum, vlanId, false, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001609 }
1610
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001611 if (!intf.vlanNative().equals(VlanId.NONE)
1612 && !prevIntf.vlanNative().equals(intf.vlanNative())
1613 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1614 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
1615 // Update filtering objective and L2IG group bucket
1616 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
1617 } else {
1618 // AddVlanNative
1619 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1620 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001621 }
1622
1623 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1624 // AddVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001625 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
1626 .filter(i -> !prevIntf.vlanUntagged().equals(i))
1627 .filter(i -> !prevIntf.vlanNative().equals(i))
1628 .forEach(vlanId -> updateVlanConfigInternal(
1629 deviceId, portNum, vlanId, false, true)
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001630 );
1631 }
1632
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001633 if (!intf.vlanUntagged().equals(VlanId.NONE)
1634 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1635 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
1636 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
1637 // Update filtering objective and L2IG group bucket
1638 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
1639 } else {
1640 // AddVlanUntagged
1641 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1642 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001643 }
1644 addSubnetConfig(prevIntf.connectPoint(),
1645 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1646 new HashSet<>(prevIntf.ipAddressesList())));
1647 } catch (ConfigException e) {
1648 log.error("Error in configuration");
1649 }
1650 }
1651
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001652 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
1653 VlanId vlanId, boolean pushVlan) {
1654 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1655 if (grpHandler == null) {
1656 log.warn("Failed to retrieve group handler for device {}", deviceId);
1657 return;
1658 }
1659
1660 // Update filtering objective for a single port
1661 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
1662 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
1663
1664 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
1665 // Update L2IG bucket of the port
1666 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
1667 } else {
1668 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1669 }
1670 }
1671
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001672 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1673 VlanId vlanId, boolean pushVlan, boolean install) {
1674 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1675 if (grpHandler == null) {
1676 log.warn("Failed to retrieve group handler for device {}", deviceId);
1677 return;
1678 }
1679
1680 // Update filtering objective for a single port
1681 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1682
1683 // Update filtering objective for multicast ingress port
1684 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1685
1686 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1687
1688 if (nextId != -1 && !install) {
1689 // Update next objective for a single port as an output port
1690 // Remove a single port from L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001691 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001692 // Remove L2 Bridging rule and L3 Unicast rule to the host
1693 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1694 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1695 // only if there is no port configured on that VLAN ID
1696 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1697 // Remove broadcast forwarding rule for the VLAN
1698 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1699 // Remove L2FG for VLAN
1700 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1701 } else {
1702 // Remove L2IG of the port
1703 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1704 }
1705 } else if (install) {
1706 if (nextId != -1) {
1707 // Add a single port to L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001708 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001709 } else {
1710 // Create L2FG for VLAN
1711 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1712 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1713 }
1714 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1715 } else {
1716 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1717 }
1718 }
1719
1720 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1721 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1722 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1723
1724 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1725 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1726 .filter(intf -> !intf.connectPoint().equals(cp))
1727 .flatMap(intf -> intf.ipAddressesList().stream())
1728 .collect(Collectors.toSet());
1729 // 1. Partial subnet population
1730 // Remove routing rules for removed subnet from previous configuration,
1731 // which does not also exist in other interfaces in the same device
1732 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1733 .map(InterfaceIpAddress::subnetAddress)
1734 .collect(Collectors.toSet());
1735
1736 defaultRoutingHandler.revokeSubnet(
1737 ipPrefixSet.stream()
1738 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1739 .collect(Collectors.toSet()));
1740
1741 // 2. Interface IP punts
1742 // Remove IP punts for old Intf address
1743 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1744 .map(InterfaceIpAddress::ipAddress)
1745 .collect(Collectors.toSet());
1746 ipAddressSet.stream()
1747 .map(InterfaceIpAddress::ipAddress)
1748 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1749 .forEach(interfaceIpAddress ->
1750 routingRulePopulator.revokeSingleIpPunts(
1751 cp.deviceId(), interfaceIpAddress));
1752
1753 // 3. Host unicast routing rule
1754 // Remove unicast routing rule
1755 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1756 }
1757
1758 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1759 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1760 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1761
1762 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1763 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1764 .filter(intf -> !intf.connectPoint().equals(cp))
1765 .flatMap(intf -> intf.ipAddressesList().stream())
1766 .collect(Collectors.toSet());
1767 // 1. Partial subnet population
1768 // Add routing rules for newly added subnet, which does not also exist in
1769 // other interfaces in the same device
1770 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1771 .map(InterfaceIpAddress::subnetAddress)
1772 .collect(Collectors.toSet());
1773
1774 defaultRoutingHandler.populateSubnet(
1775 Collections.singleton(cp),
1776 ipPrefixSet.stream()
1777 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1778 .collect(Collectors.toSet()));
1779
1780 // 2. Interface IP punts
1781 // Add IP punts for new Intf address
1782 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1783 .map(InterfaceIpAddress::ipAddress)
1784 .collect(Collectors.toSet());
1785 ipAddressSet.stream()
1786 .map(InterfaceIpAddress::ipAddress)
1787 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1788 .forEach(interfaceIpAddress ->
1789 routingRulePopulator.populateSingleIpPunts(
1790 cp.deviceId(), interfaceIpAddress));
1791
1792 // 3. Host unicast routing rule
1793 // Add unicast routing rule
1794 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1795 }
sangho80f11cb2015-04-01 13:05:26 -07001796}