blob: 79bd8d3806b17e4f18e13f3df20f7ecc9601b783 [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
Ray Milkeyae0068a2017-08-15 11:02:29 -070018import java.util.List;
19import java.util.Map;
20import java.util.Map.Entry;
21import java.util.Optional;
22import java.util.Set;
23import java.util.concurrent.ConcurrentHashMap;
24import java.util.concurrent.ConcurrentLinkedQueue;
25import java.util.concurrent.Executors;
26import java.util.concurrent.ScheduledExecutorService;
27import java.util.concurrent.ScheduledFuture;
28import java.util.concurrent.TimeUnit;
29import java.util.concurrent.atomic.AtomicBoolean;
30import java.util.stream.Collectors;
31
Charles Chanf0ae41e2017-08-23 13:55:39 -070032import com.google.common.collect.Sets;
sangho80f11cb2015-04-01 13:05:26 -070033import org.apache.felix.scr.annotations.Activate;
34import org.apache.felix.scr.annotations.Component;
35import org.apache.felix.scr.annotations.Deactivate;
36import org.apache.felix.scr.annotations.Reference;
37import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho27462c62015-05-14 00:39:53 -070038import org.apache.felix.scr.annotations.Service;
sangho80f11cb2015-04-01 13:05:26 -070039import org.onlab.packet.Ethernet;
Pier Ventreb6b81d52016-12-02 08:16:05 -080040import org.onlab.packet.ICMP6;
Charles Chan77277672015-10-20 16:24:19 -070041import org.onlab.packet.IPv4;
Pier Ventreb6a7f342016-11-26 21:05:22 -080042import org.onlab.packet.IPv6;
Charles Chan77277672015-10-20 16:24:19 -070043import org.onlab.packet.IpPrefix;
Jonathan Hart54541d12016-04-12 15:39:44 -070044import org.onlab.packet.VlanId;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070045import org.onlab.util.KryoNamespace;
Saurav Dasc3604f12016-03-23 11:22:49 -070046import org.onosproject.cfg.ComponentConfigService;
sangho80f11cb2015-04-01 13:05:26 -070047import org.onosproject.core.ApplicationId;
48import org.onosproject.core.CoreService;
49import org.onosproject.event.Event;
Jonathan Hart54541d12016-04-12 15:39:44 -070050import org.onosproject.mastership.MastershipService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080051import org.onosproject.net.ConnectPoint;
Jonathan Hart54541d12016-04-12 15:39:44 -070052import org.onosproject.net.Device;
53import org.onosproject.net.DeviceId;
Charles Chanf0ae41e2017-08-23 13:55:39 -070054import org.onosproject.net.Host;
55import org.onosproject.net.HostId;
Jonathan Hart54541d12016-04-12 15:39:44 -070056import org.onosproject.net.Link;
57import org.onosproject.net.Port;
Charles Chanf4586112015-11-09 16:37:23 -080058import org.onosproject.net.PortNumber;
Charles Chan72f556a2015-10-05 17:50:33 -070059import org.onosproject.net.config.ConfigFactory;
60import org.onosproject.net.config.NetworkConfigEvent;
Charles Chan72f556a2015-10-05 17:50:33 -070061import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070062import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkeyae0068a2017-08-15 11:02:29 -070063import org.onosproject.net.config.basics.InterfaceConfig;
64import org.onosproject.net.config.basics.McastConfig;
Charles Chan72f556a2015-10-05 17:50:33 -070065import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hart54541d12016-04-12 15:39:44 -070066import org.onosproject.net.device.DeviceEvent;
67import org.onosproject.net.device.DeviceListener;
68import org.onosproject.net.device.DeviceService;
Charles Chanf4586112015-11-09 16:37:23 -080069import org.onosproject.net.flow.TrafficSelector;
70import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart54541d12016-04-12 15:39:44 -070071import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chanf4586112015-11-09 16:37:23 -080072import org.onosproject.net.host.HostEvent;
73import org.onosproject.net.host.HostListener;
Pier Ventreb6a7f342016-11-26 21:05:22 -080074import org.onosproject.net.host.HostService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070075import org.onosproject.net.intf.Interface;
76import org.onosproject.net.intf.InterfaceService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080077import org.onosproject.net.link.LinkEvent;
78import org.onosproject.net.link.LinkListener;
79import org.onosproject.net.link.LinkService;
Charles Chanc91c8782016-03-30 17:54:24 -070080import org.onosproject.net.mcast.McastEvent;
81import org.onosproject.net.mcast.McastListener;
82import org.onosproject.net.mcast.MulticastRouteService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070083import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080084import org.onosproject.net.packet.InboundPacket;
85import org.onosproject.net.packet.PacketContext;
86import org.onosproject.net.packet.PacketProcessor;
87import org.onosproject.net.packet.PacketService;
Pier Ventref3cf5b92016-11-09 14:17:26 -080088import org.onosproject.net.topology.PathService;
Charles Chanc91c8782016-03-30 17:54:24 -070089import org.onosproject.net.topology.TopologyService;
Charles Chanf0ae41e2017-08-23 13:55:39 -070090import org.onosproject.routeservice.ResolvedRoute;
Ray Milkeyae0068a2017-08-15 11:02:29 -070091import org.onosproject.routeservice.RouteEvent;
92import org.onosproject.routeservice.RouteListener;
93import org.onosproject.routeservice.RouteService;
Charles Chanc91c8782016-03-30 17:54:24 -070094import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
95import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventre6b19e482016-11-07 16:21:04 -080096import org.onosproject.segmentrouting.config.PwaasConfig;
Pier Ventre6b19e482016-11-07 16:21:04 -080097import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkeyae0068a2017-08-15 11:02:29 -070098import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chan82f19972016-05-17 13:13:55 -070099import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chanc91c8782016-03-30 17:54:24 -0700100import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -0700101import org.onosproject.segmentrouting.grouphandler.DestinationSet;
102import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Ray Milkeyae0068a2017-08-15 11:02:29 -0700103import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Saurav Das261c3002017-06-13 15:35:54 -0700104import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -0700105import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -0800106import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -0700107import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hart54541d12016-04-12 15:39:44 -0700108import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700109import org.onosproject.store.service.EventuallyConsistentMap;
110import org.onosproject.store.service.EventuallyConsistentMapBuilder;
111import org.onosproject.store.service.StorageService;
112import org.onosproject.store.service.WallClockTimestamp;
sangho80f11cb2015-04-01 13:05:26 -0700113import org.slf4j.Logger;
114import org.slf4j.LoggerFactory;
115
Ray Milkeyae0068a2017-08-15 11:02:29 -0700116import com.google.common.collect.HashMultimap;
117import com.google.common.collect.ImmutableMap;
118import com.google.common.collect.Maps;
119import com.google.common.collect.Multimap;
sangho80f11cb2015-04-01 13:05:26 -0700120
Charles Chand6d25332016-02-26 22:19:52 -0800121import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800122import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700123import static org.onlab.util.Tools.groupedThreads;
Charles Chand6d25332016-02-26 22:19:52 -0800124
Charles Chanb7f75ac2016-01-11 18:28:54 -0800125/**
126 * Segment routing manager.
127 */
Jonathan Hart54541d12016-04-12 15:39:44 -0700128@Service
129@Component(immediate = true)
sangho27462c62015-05-14 00:39:53 -0700130public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700131
Charles Chan46fdfaf2016-11-09 20:51:44 -0800132 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan910be6a2017-08-23 14:46:43 -0700133 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sangho80f11cb2015-04-01 13:05:26 -0700134
135 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700136 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700137
138 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventreb6a7f342016-11-26 21:05:22 -0800139 private NeighbourResolutionService neighbourResolutionService;
140
141 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800142 public PathService pathService;
143
144 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700145 CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700146
147 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700148 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700149
150 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700151 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700152
153 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700154 DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700155
156 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800157 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700158
159 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700160 LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700161
Charles Chan82ab1932016-01-30 23:22:37 -0800162 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800163 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700164
165 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800166 public StorageService storageService;
Charles Chandebfea32016-10-24 14:52:01 -0700167
168 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
169 MulticastRouteService multicastRouteService;
170
171 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
172 TopologyService topologyService;
173
174 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700175 RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800176
177 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800178 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800179
Saurav Dasc3604f12016-03-23 11:22:49 -0700180 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800181 public InterfaceService interfaceService;
182
Charles Chandebfea32016-10-24 14:52:01 -0700183 ArpHandler arpHandler = null;
184 IcmpHandler icmpHandler = null;
185 IpHandler ipHandler = null;
186 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700187 ApplicationId appId;
188 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700189
Charles Chandebfea32016-10-24 14:52:01 -0700190 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700191 private TunnelHandler tunnelHandler = null;
192 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700193 private InternalPacketProcessor processor = null;
194 private InternalLinkListener linkListener = null;
195 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700196 private AppConfigHandler appCfgHandler = null;
Charles Chandebfea32016-10-24 14:52:01 -0700197 XConnectHandler xConnectHandler = null;
Charles Chan1eaf4802016-04-18 13:44:03 -0700198 private McastHandler mcastHandler = null;
Charles Chan3ed34d82017-06-22 18:03:14 -0700199 private HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800200 private RouteHandler routeHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800201 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventre6b19e482016-11-07 16:21:04 -0800202 private L2TunnelHandler l2TunnelHandler = null;
sangho80f11cb2015-04-01 13:05:26 -0700203 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan82ab1932016-01-30 23:22:37 -0800204 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700205 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
206 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700207 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sangho80f11cb2015-04-01 13:05:26 -0700208
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700209 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700210 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sangho80f11cb2015-04-01 13:05:26 -0700211
Saurav Das2d94d312015-11-24 23:21:05 -0800212 @SuppressWarnings("unused")
sangho80f11cb2015-04-01 13:05:26 -0700213 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das2d94d312015-11-24 23:21:05 -0800214 @SuppressWarnings("rawtypes")
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700215 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chanf4586112015-11-09 16:37:23 -0800216 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chanb7f75ac2016-01-11 18:28:54 -0800217 new ConcurrentHashMap<>();
218 /**
Saurav Das261c3002017-06-13 15:35:54 -0700219 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700220 * Used to keep track on MPLS group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800221 */
Saurav Das261c3002017-06-13 15:35:54 -0700222 EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
223 dsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800224 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700225 * Per device next objective ID store with (device id + vlanid) as key.
226 * Used to keep track on L2 flood group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800227 */
Ray Milkeyb85de082017-04-05 09:42:04 -0700228 EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800229 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800230 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700231 * Per device next objective ID store with (device id + port + treatment + meta) as key.
232 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800233 */
Ray Milkeyb85de082017-04-05 09:42:04 -0700234 EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800235 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800236
Saurav Das62ae6792017-05-15 15:34:25 -0700237 // Local store for all links seen and their present status, used for
238 // optimized routing. The existence of the link in the keys is enough to know
239 // if the link has been "seen-before" by this instance of the controller.
240 // The boolean value indicates if the link is currently up or not.
241 // XXX Currently the optimized routing logic depends on "forgetting" a link
242 // when a switch goes down, but "remembering" it when only the link goes down.
243 // Consider changing this logic so we can use the Link Service instead of
244 // a local cache.
245 private Map<Link, Boolean> seenLinks = new ConcurrentHashMap<>();
246
Saurav Das2d94d312015-11-24 23:21:05 -0800247 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
248 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700249
Saurav Das261c3002017-06-13 15:35:54 -0700250 private AtomicBoolean programmingScheduled = new AtomicBoolean();
251
Charles Chanc91c8782016-03-30 17:54:24 -0700252 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700253 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
254 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700255 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700256 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800257 public SegmentRoutingDeviceConfig createConfig() {
258 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700259 }
260 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800261
Charles Chanc91c8782016-03-30 17:54:24 -0700262 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700263 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
264 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700265 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800266 @Override
267 public SegmentRoutingAppConfig createConfig() {
268 return new SegmentRoutingAppConfig();
269 }
270 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800271
Charles Chan82f19972016-05-17 13:13:55 -0700272 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
273 new ConfigFactory<ApplicationId, XConnectConfig>(
274 SubjectFactories.APP_SUBJECT_FACTORY,
275 XConnectConfig.class, "xconnect") {
276 @Override
277 public XConnectConfig createConfig() {
278 return new XConnectConfig();
279 }
280 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800281
Charles Chanc91c8782016-03-30 17:54:24 -0700282 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700283 new ConfigFactory<ApplicationId, McastConfig>(
284 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700285 McastConfig.class, "multicast") {
286 @Override
287 public McastConfig createConfig() {
288 return new McastConfig();
289 }
290 };
291
Pier Ventre6b19e482016-11-07 16:21:04 -0800292 private final ConfigFactory<ApplicationId, PwaasConfig> pwaasConfigFactory =
293 new ConfigFactory<ApplicationId, PwaasConfig>(
294 SubjectFactories.APP_SUBJECT_FACTORY,
295 PwaasConfig.class, "pwaas") {
296 @Override
297 public PwaasConfig createConfig() {
298 return new PwaasConfig();
299 }
300 };
301
Charles Chan3ed34d82017-06-22 18:03:14 -0700302 private static final Object THREAD_SCHED_LOCK = new Object();
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700303 private static int numOfEventsQueued = 0;
304 private static int numOfEventsExecuted = 0;
sangho80f11cb2015-04-01 13:05:26 -0700305 private static int numOfHandlerExecution = 0;
306 private static int numOfHandlerScheduled = 0;
307
Charles Chan1963f4f2016-02-18 14:22:42 -0800308 /**
309 * Segment Routing App ID.
310 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800311 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan10b0fb72017-02-02 16:20:42 -0800312
Charles Chanb7f75ac2016-01-11 18:28:54 -0800313 /**
314 * The default VLAN ID assigned to the interfaces without subnet config.
315 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800316 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das7c305372015-10-28 12:39:42 -0700317
sangho80f11cb2015-04-01 13:05:26 -0700318 @Activate
319 protected void activate() {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800320 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700321
322 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700323 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700324 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700325 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700326 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700327 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700328 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700329 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700330 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700331
Charles Chan10b0fb72017-02-02 16:20:42 -0800332 log.debug("Creating EC map vlannextobjectivestore");
333 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
334 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
335 vlanNextObjStore = vlanNextObjMapBuilder
336 .withName("vlannextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700337 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700338 .withTimestampProvider((k, v) -> new WallClockTimestamp())
339 .build();
340
Saurav Das2d94d312015-11-24 23:21:05 -0800341 log.debug("Creating EC map subnetnextobjectivestore");
342 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
343 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
344 portNextObjStore = portNextObjMapBuilder
345 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700346 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800347 .withTimestampProvider((k, v) -> new WallClockTimestamp())
348 .build();
349
sangho4a5c42a2015-05-20 22:16:38 -0700350 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
351 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700352 tunnelStore = tunnelMapBuilder
353 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700354 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700355 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700356 .build();
357
358 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
359 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700360 policyStore = policyMapBuilder
361 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700362 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700363 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700364 .build();
365
Saurav Dasc3604f12016-03-23 11:22:49 -0700366 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800367 "purgeOnDisconnection", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700368 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800369 "purgeOnDisconnection", "true");
Pier Luigib9632ba2017-01-12 18:14:58 -0800370 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
371 "requestInterceptsEnabled", "false");
Charles Chanc1909e12017-08-08 15:13:37 -0700372 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800373 "requestInterceptsEnabled", "false");
Charles Chan9597f8d2017-07-24 15:56:10 -0700374 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800375 "arpEnabled", "false");
Pier Luigi0ebeb312017-02-02 22:31:34 -0800376 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
377 "greedyLearningIpv6", "true");
Charles Chancf8ea472017-02-28 15:15:17 -0800378 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
379 "forceUnprovision", "true");
Charles Chanc7b73c72017-08-10 16:57:28 -0700380 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan4c95c0d2017-07-20 16:16:25 -0700381 "distributed", "true");
Charles Chan804772f2017-08-14 11:42:11 -0700382 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
383 "multihomingEnabled", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700384
Charles Chan2b078ae2015-10-14 11:24:40 -0700385 processor = new InternalPacketProcessor();
386 linkListener = new InternalLinkListener();
387 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700388 appCfgHandler = new AppConfigHandler(this);
389 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700390 mcastHandler = new McastHandler(this);
391 hostHandler = new HostHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700392 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800393 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventre6b19e482016-11-07 16:21:04 -0800394 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700395
Charles Chand6d25332016-02-26 22:19:52 -0800396 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700397 cfgService.registerConfigFactory(deviceConfigFactory);
398 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700399 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700400 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventre6b19e482016-11-07 16:21:04 -0800401 cfgService.registerConfigFactory(pwaasConfigFactory);
Charles Chan82ab1932016-01-30 23:22:37 -0800402 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700403 packetService.addProcessor(processor, PacketProcessor.director(2));
404 linkService.addListener(linkListener);
405 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700406 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700407 routeService.addListener(routeListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700408
409 cfgListener.configureNetwork();
410
sangho80f11cb2015-04-01 13:05:26 -0700411 log.info("Started");
412 }
413
Jonathan Hart54541d12016-04-12 15:39:44 -0700414 private KryoNamespace.Builder createSerializer() {
415 return new KryoNamespace.Builder()
416 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700417 .register(DestinationSetNextObjectiveStoreKey.class,
Charles Chan10b0fb72017-02-02 16:20:42 -0800418 VlanNextObjectiveStoreKey.class,
Saurav Das261c3002017-06-13 15:35:54 -0700419 DestinationSet.class,
420 NextNeighbors.class,
Jonathan Hart54541d12016-04-12 15:39:44 -0700421 Tunnel.class,
422 DefaultTunnel.class,
423 Policy.class,
424 TunnelPolicy.class,
425 Policy.Type.class,
426 PortNextObjectiveStoreKey.class,
Charles Chan82f19972016-05-17 13:13:55 -0700427 XConnectStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700428 );
429 }
430
sangho80f11cb2015-04-01 13:05:26 -0700431 @Deactivate
432 protected void deactivate() {
Charles Chan72f556a2015-10-05 17:50:33 -0700433 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700434 cfgService.unregisterConfigFactory(deviceConfigFactory);
435 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700436 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700437 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventre6b19e482016-11-07 16:21:04 -0800438 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chan72f556a2015-10-05 17:50:33 -0700439
Charles Chanfd48c222017-06-19 00:43:31 -0700440 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700441 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700442 linkService.removeListener(linkListener);
443 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700444 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700445 routeService.removeListener(routeListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700446
Charles Chan2e71ef32017-02-23 15:44:08 -0800447 neighbourResolutionService.unregisterNeighbourHandlers(appId);
448
sangho80f11cb2015-04-01 13:05:26 -0700449 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700450 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700451 deviceListener = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700452 groupHandlerMap.clear();
453
Saurav Das261c3002017-06-13 15:35:54 -0700454 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800455 vlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700456 portNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700457 tunnelStore.destroy();
458 policyStore.destroy();
sangho80f11cb2015-04-01 13:05:26 -0700459 log.info("Stopped");
460 }
461
sangho27462c62015-05-14 00:39:53 -0700462 @Override
463 public List<Tunnel> getTunnels() {
464 return tunnelHandler.getTunnels();
465 }
466
467 @Override
sanghobd812f82015-06-29 14:58:47 -0700468 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
469 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700470 }
471
472 @Override
sanghobd812f82015-06-29 14:58:47 -0700473 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700474 for (Policy policy: policyHandler.getPolicies()) {
475 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
476 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
477 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
478 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700479 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700480 }
481 }
482 }
sanghobd812f82015-06-29 14:58:47 -0700483 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700484 }
485
486 @Override
sanghobd812f82015-06-29 14:58:47 -0700487 public PolicyHandler.Result removePolicy(Policy policy) {
488 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700489 }
490
491 @Override
sanghobd812f82015-06-29 14:58:47 -0700492 public PolicyHandler.Result createPolicy(Policy policy) {
493 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700494 }
495
496 @Override
497 public List<Policy> getPolicies() {
498 return policyHandler.getPolicies();
499 }
500
Saurav Das07c74602016-04-27 18:35:50 -0700501 @Override
502 public void rerouteNetwork() {
503 cfgListener.configureNetwork();
Saurav Das07c74602016-04-27 18:35:50 -0700504 }
505
Charles Chand7844e52016-10-20 17:02:44 -0700506 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800507 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
508 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Charles Chand7844e52016-10-20 17:02:44 -0700509 deviceService.getAvailableDevices().forEach(device -> {
510 deviceSubnetMap.put(device.id(), deviceConfiguration.getSubnets(device.id()));
511 });
512 return deviceSubnetMap;
513 }
514
Saurav Das62ae6792017-05-15 15:34:25 -0700515
516 @Override
517 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
518 if (defaultRoutingHandler != null) {
519 return defaultRoutingHandler.getCurrentEmcpSpgMap();
520 } else {
521 return null;
522 }
523 }
524
525 @Override
Saurav Das261c3002017-06-13 15:35:54 -0700526 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
527 if (dsNextObjStore != null) {
528 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700529 } else {
530 return ImmutableMap.of();
531 }
532 }
533
Saurav Dasfbe74572017-08-03 18:30:35 -0700534 @Override
535 public void verifyGroups(DeviceId id) {
536 DefaultGroupHandler gh = groupHandlerMap.get(id);
537 if (gh != null) {
538 gh.triggerBucketCorrector();
539 }
540 }
541
sangho80f1f892015-05-19 11:57:42 -0700542 /**
Ray Milkeyb85de082017-04-05 09:42:04 -0700543 * Extracts the application ID from the manager.
544 *
545 * @return application ID
546 */
547 public ApplicationId appId() {
548 return appId;
549 }
550
551 /**
552 * Returns the device configuration.
553 *
554 * @return device configuration
555 */
556 public DeviceConfiguration deviceConfiguration() {
557 return deviceConfiguration;
558 }
559
560 /**
Saurav Das261c3002017-06-13 15:35:54 -0700561 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700562 * Used to keep track on MPLS group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700563 *
564 * @return next objective ID store
565 */
Saurav Das261c3002017-06-13 15:35:54 -0700566 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
567 dsNextObjStore() {
568 return dsNextObjStore;
Ray Milkeyb85de082017-04-05 09:42:04 -0700569 }
570
571 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700572 * Per device next objective ID store with (device id + vlanid) as key.
573 * Used to keep track on L2 flood group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700574 *
575 * @return vlan next object store
576 */
577 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
578 return vlanNextObjStore;
579 }
580
581 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700582 * Per device next objective ID store with (device id + port + treatment + meta) as key.
583 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700584 *
585 * @return port next object store.
586 */
587 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
588 return portNextObjStore;
589 }
590
591 /**
Saurav Das261c3002017-06-13 15:35:54 -0700592 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
593 * labeled packets should be programmed or not.
Pier Ventre7a78de22016-10-31 15:00:01 -0700594 *
595 * @return MPLS-ECMP value
596 */
597 public boolean getMplsEcmp() {
598 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
599 .getConfig(this.appId, SegmentRoutingAppConfig.class);
600 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
601 }
602
603 /**
sangho80f1f892015-05-19 11:57:42 -0700604 * Returns the tunnel object with the tunnel ID.
605 *
606 * @param tunnelId Tunnel ID
607 * @return Tunnel reference
608 */
sangho27462c62015-05-14 00:39:53 -0700609 public Tunnel getTunnel(String tunnelId) {
610 return tunnelHandler.getTunnel(tunnelId);
611 }
612
Charles Chan90772a72017-02-08 15:52:08 -0800613 // TODO Consider moving these to InterfaceService
sangho80f11cb2015-04-01 13:05:26 -0700614 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800615 * Returns untagged VLAN configured on given connect point.
Charles Chan90772a72017-02-08 15:52:08 -0800616 * <p>
617 * Only returns the first match if there are multiple untagged VLAN configured
618 * on the connect point.
sangho80f11cb2015-04-01 13:05:26 -0700619 *
Charles Chan10b0fb72017-02-02 16:20:42 -0800620 * @param connectPoint connect point
621 * @return untagged VLAN or null if not configured
sangho80f11cb2015-04-01 13:05:26 -0700622 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700623 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800624 return interfaceService.getInterfacesByPort(connectPoint).stream()
625 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
626 .map(Interface::vlanUntagged)
627 .findFirst().orElse(null);
sangho80f11cb2015-04-01 13:05:26 -0700628 }
629
sangho27462c62015-05-14 00:39:53 -0700630 /**
Charles Chan90772a72017-02-08 15:52:08 -0800631 * Returns tagged VLAN configured on given connect point.
632 * <p>
633 * Returns all matches if there are multiple tagged VLAN configured
634 * on the connect point.
635 *
636 * @param connectPoint connect point
637 * @return tagged VLAN or empty set if not configured
638 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700639 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan90772a72017-02-08 15:52:08 -0800640 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
641 return interfaces.stream()
642 .map(Interface::vlanTagged)
Charles Chan3ed34d82017-06-22 18:03:14 -0700643 .flatMap(Set::stream)
Charles Chan90772a72017-02-08 15:52:08 -0800644 .collect(Collectors.toSet());
645 }
646
647 /**
648 * Returns native VLAN configured on given connect point.
649 * <p>
650 * Only returns the first match if there are multiple native VLAN configured
651 * on the connect point.
652 *
653 * @param connectPoint connect point
654 * @return native VLAN or null if not configured
655 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700656 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan90772a72017-02-08 15:52:08 -0800657 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
658 return interfaces.stream()
659 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
660 .map(Interface::vlanNative)
661 .findFirst()
662 .orElse(null);
663 }
664
665 /**
Charles Chand9265a32017-06-16 15:19:24 -0700666 * Returns internal VLAN for untagged hosts on given connect point.
667 * <p>
668 * The internal VLAN is either vlan-untagged for an access port,
669 * or vlan-native for a trunk port.
670 *
671 * @param connectPoint connect point
672 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
673 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700674 VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chand9265a32017-06-16 15:19:24 -0700675 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
676 VlanId nativeVlanId = getNativeVlanId(connectPoint);
677 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
678 }
679
680 /**
Charles Chan3ed34d82017-06-22 18:03:14 -0700681 * Returns optional pair device ID of given device.
682 *
683 * @param deviceId device ID
684 * @return optional pair device ID. Might be empty if pair device is not configured
685 */
686 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
687 SegmentRoutingDeviceConfig deviceConfig =
688 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
689 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
690 }
691 /**
692 * Returns optional pair device local port of given device.
693 *
694 * @param deviceId device ID
695 * @return optional pair device ID. Might be empty if pair device is not configured
696 */
697 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
698 SegmentRoutingDeviceConfig deviceConfig =
699 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
700 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
701 }
702
703 /**
Charles Chan910be6a2017-08-23 14:46:43 -0700704 * Determine if current instance is the master of given connect point.
705 *
706 * @param cp connect point
707 * @return true if current instance is the master of given connect point
708 */
709 boolean isMasterOf(ConnectPoint cp) {
710 boolean isMaster = mastershipService.isLocalMaster(cp.deviceId());
711 if (!isMaster) {
712 log.debug(NOT_MASTER, cp);
713 }
714 return isMaster;
715 }
716
717 /**
Charles Chanf0ae41e2017-08-23 13:55:39 -0700718 * Returns locations of given resolved route.
719 *
720 * @param resolvedRoute resolved route
721 * @return locations of nexthop. Might be empty if next hop is not found
722 */
723 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
724 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
725 return Optional.ofNullable(hostService.getHost(hostId))
726 .map(Host::locations).orElse(Sets.newHashSet())
727 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
728 }
729
730 /**
Charles Chan90772a72017-02-08 15:52:08 -0800731 * Returns vlan port map of given device.
732 *
733 * @param deviceId device id
734 * @return vlan-port multimap
735 */
736 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
737 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
738
739 interfaceService.getInterfaces().stream()
740 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
741 .forEach(intf -> {
742 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan3ed34d82017-06-22 18:03:14 -0700743 intf.vlanTagged().forEach(vlanTagged ->
744 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
745 );
Charles Chan90772a72017-02-08 15:52:08 -0800746 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
747 });
748 vlanPortMap.removeAll(VlanId.NONE);
749
750 return vlanPortMap;
751 }
752
753 /**
Saurav Das2d94d312015-11-24 23:21:05 -0800754 * Returns the next objective ID for the given subnet prefix. It is expected
Charles Chan10b0fb72017-02-02 16:20:42 -0800755 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -0800756 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -0700757 *
758 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -0800759 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -0800760 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -0700761 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700762 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -0700763 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800764 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
765 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -0700766 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -0800767 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -0800768 + "device {} not found", deviceId);
769 return -1;
770 }
771 }
772
773 /**
774 * Returns the next objective ID for the given portNumber, given the treatment.
775 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -0700776 * would result in different objectives. If the next object does not exist,
777 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -0800778 *
779 * @param deviceId Device ID
780 * @param portNum port number on device for which NextObjective is queried
781 * @param treatment the actions to apply on the packets (should include outport)
782 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -0700783 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -0700784 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -0800785 */
786 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
787 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -0700788 TrafficSelector meta,
789 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -0800790 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
791 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -0700792 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -0800793 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800794 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
795 + " not found", deviceId);
796 return -1;
797 }
798 }
799
Saurav Das62ae6792017-05-15 15:34:25 -0700800 /**
801 * Returns the group handler object for the specified device id.
802 *
803 * @param devId the device identifier
804 * @return the groupHandler object for the device id, or null if not found
805 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700806 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Das62ae6792017-05-15 15:34:25 -0700807 return groupHandlerMap.get(devId);
808 }
809
810 /**
Saurav Dasfbe74572017-08-03 18:30:35 -0700811 * Returns the default routing handler object.
812 *
813 * @return the default routing handler object
814 */
815 public DefaultRoutingHandler getRoutingHandler() {
816 return defaultRoutingHandler;
817 }
818
819 /**
Saurav Das62ae6792017-05-15 15:34:25 -0700820 * Returns true if this controller instance has seen this link before. The
821 * link may not be currently up, but as long as the link had been seen before
822 * this method will return true. The one exception is when the link was
823 * indeed seen before, but this controller instance was forced to forget it
824 * by a call to purgeSeenLink method.
825 *
826 * @param link the infrastructure link being queried
827 * @return true if this controller instance has seen this link before
828 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700829 boolean isSeenLink(Link link) {
Saurav Das62ae6792017-05-15 15:34:25 -0700830 return seenLinks.containsKey(link);
831 }
832
833 /**
834 * Updates the seen link store. Updates can be for links that are currently
835 * available or not.
836 *
837 * @param link the link to update in the seen-link local store
838 * @param up the status of the link, true if up, false if down
839 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700840 void updateSeenLink(Link link, boolean up) {
Saurav Das62ae6792017-05-15 15:34:25 -0700841 seenLinks.put(link, up);
842 }
843
844 /**
845 * Returns the status of a seen-link (up or down). If the link has not
846 * been seen-before, a null object is returned.
847 *
848 * @param link the infrastructure link being queried
849 * @return null if the link was not seen-before;
850 * true if the seen-link is up;
851 * false if the seen-link is down
852 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700853 Boolean isSeenLinkUp(Link link) {
Saurav Das62ae6792017-05-15 15:34:25 -0700854 return seenLinks.get(link);
855 }
856
857 /**
858 * Makes this controller instance forget a previously seen before link.
859 *
860 * @param link the infrastructure link to purge
861 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700862 private void purgeSeenLink(Link link) {
Saurav Das62ae6792017-05-15 15:34:25 -0700863 seenLinks.remove(link);
864 }
865
866 /**
867 * Returns the status of a link as parallel link. A parallel link
868 * is defined as a link which has common src and dst switches as another
869 * seen-link that is currently enabled. It is not necessary for the link being
870 * queried to be a seen-link.
871 *
872 * @param link the infrastructure link being queried
873 * @return true if a seen-link exists that is up, and shares the
874 * same src and dst switches as the link being queried
875 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700876 private boolean isParallelLink(Link link) {
Saurav Das62ae6792017-05-15 15:34:25 -0700877 for (Entry<Link, Boolean> seen : seenLinks.entrySet()) {
878 Link seenLink = seen.getKey();
879 if (seenLink.equals(link)) {
880 continue;
881 }
882 if (seenLink.src().deviceId().equals(link.src().deviceId()) &&
883 seenLink.dst().deviceId().equals(link.dst().deviceId()) &&
884 seen.getValue()) {
885 return true;
886 }
887 }
888 return false;
889 }
890
Saurav Das261c3002017-06-13 15:35:54 -0700891 /**
892 * Returns true if the link being queried is a bidirectional link. A bidi
893 * link is defined as a link, whose reverse link - ie. the link in the reverse
894 * direction - has been seen-before and is up. It is not necessary for the link
895 * being queried to be a seen-link.
896 *
897 * @param link the infrastructure link being queried
898 * @return true if another unidirectional link exists in the reverse direction,
899 * has been seen-before and is up
900 */
901 public boolean isBidirectional(Link link) {
902 Link reverseLink = linkService.getLink(link.dst(), link.src());
903 if (reverseLink == null) {
904 return false;
905 }
906 Boolean result = isSeenLinkUp(reverseLink);
907 if (result == null) {
908 return false;
909 }
910 return result.booleanValue();
911 }
912
913 /**
914 * Determines if the given link should be avoided in routing calculations
915 * by policy or design.
916 *
917 * @param link the infrastructure link being queried
918 * @return true if link should be avoided
919 */
920 public boolean avoidLink(Link link) {
921 // XXX currently only avoids all pair-links. In the future can be
922 // extended to avoid any generic link
923 DeviceId src = link.src().deviceId();
924 PortNumber srcPort = link.src().port();
925 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(src)) {
926 log.warn("Device {} not configured..cannot avoid link {}", src, link);
927 return false;
928 }
929 DeviceId pairDev;
930 PortNumber pairLocalPort, pairRemotePort = null;
931 try {
932 pairDev = deviceConfiguration.getPairDeviceId(src);
933 pairLocalPort = deviceConfiguration.getPairLocalPort(src);
934 if (pairDev != null) {
935 pairRemotePort = deviceConfiguration.getPairLocalPort(pairDev);
936 }
937 } catch (DeviceConfigNotFoundException e) {
938 log.warn("Pair dev for dev {} not configured..cannot avoid link {}",
939 src, link);
940 return false;
941 }
942
943 if (srcPort.equals(pairLocalPort) &&
944 link.dst().deviceId().equals(pairDev) &&
945 link.dst().port().equals(pairRemotePort)) {
946 return true;
947 }
948 return false;
949 }
950
sangho80f11cb2015-04-01 13:05:26 -0700951 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -0700952 @Override
953 public void process(PacketContext context) {
954
955 if (context.isHandled()) {
956 return;
957 }
958
959 InboundPacket pkt = context.inPacket();
960 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -0800961
962 if (ethernet == null) {
963 return;
964 }
965
Saurav Das261c3002017-06-13 15:35:54 -0700966 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
967 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800968 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -0700969 log.warn("Received unexpected ARP packet on {}",
970 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -0700971 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800972 return;
sangho80f11cb2015-04-01 13:05:26 -0700973 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -0800974 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
975 //ipHandler.addToPacketBuffer(ipv4Packet);
976 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
977 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700978 } else {
Charles Chand041ad82017-01-13 17:20:44 -0800979 // NOTE: We don't support IP learning at this moment so this
980 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800981 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700982 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800983 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
984 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -0800985 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -0800986 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -0800987 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
988 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
989 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
990 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
991 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
992 } else {
Saurav Das62ae6792017-05-15 15:34:25 -0700993 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -0700994 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -0800995 }
996 } else {
997 // NOTE: We don't support IP learning at this moment so this
998 // is not necessary. Also it causes duplication of DHCPv6 packets.
999 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1000 }
sangho80f11cb2015-04-01 13:05:26 -07001001 }
1002 }
1003 }
1004
1005 private class InternalLinkListener implements LinkListener {
1006 @Override
1007 public void event(LinkEvent event) {
Charles Chan0cff8aa2017-03-29 16:39:05 -07001008 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1009 event.type() == LinkEvent.Type.LINK_UPDATED ||
1010 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001011 log.debug("Event {} received from Link Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001012 scheduleEventHandlerIfNotScheduled(event);
1013 }
1014 }
1015 }
1016
1017 private class InternalDeviceListener implements DeviceListener {
sangho80f11cb2015-04-01 13:05:26 -07001018 @Override
1019 public void event(DeviceEvent event) {
sangho80f11cb2015-04-01 13:05:26 -07001020 switch (event.type()) {
1021 case DEVICE_ADDED:
Saurav Dasf0f592d2016-11-18 15:21:57 -08001022 case PORT_UPDATED:
1023 case PORT_ADDED:
sanghofb7c7292015-04-13 15:15:58 -07001024 case DEVICE_UPDATED:
1025 case DEVICE_AVAILABILITY_CHANGED:
Saurav Das62ae6792017-05-15 15:34:25 -07001026 log.trace("Event {} received from Device Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001027 scheduleEventHandlerIfNotScheduled(event);
1028 break;
1029 default:
1030 }
1031 }
1032 }
1033
Saurav Das2d94d312015-11-24 23:21:05 -08001034 @SuppressWarnings("rawtypes")
sangho80f11cb2015-04-01 13:05:26 -07001035 private void scheduleEventHandlerIfNotScheduled(Event event) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001036 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001037 eventQueue.add(event);
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001038 numOfEventsQueued++;
1039
1040 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
1041 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001042 eventHandlerFuture = executorService
1043 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
1044 numOfHandlerScheduled++;
1045 }
Jonathan Hart54541d12016-04-12 15:39:44 -07001046 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001047 numOfEventsQueued,
1048 numOfHandlerScheduled);
sangho80f11cb2015-04-01 13:05:26 -07001049 }
sangho80f11cb2015-04-01 13:05:26 -07001050 }
1051
1052 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001053 @Override
sangho80f11cb2015-04-01 13:05:26 -07001054 public void run() {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001055 try {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001056 while (true) {
Saurav Das2d94d312015-11-24 23:21:05 -08001057 @SuppressWarnings("rawtypes")
Charles Chan3ed34d82017-06-22 18:03:14 -07001058 Event event;
1059 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001060 if (!eventQueue.isEmpty()) {
1061 event = eventQueue.poll();
1062 numOfEventsExecuted++;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001063 } else {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001064 numOfHandlerExecution++;
1065 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
1066 numOfHandlerExecution, numOfEventsExecuted);
1067 break;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001068 }
sanghofb7c7292015-04-13 15:15:58 -07001069 }
Charles Chan0cff8aa2017-03-29 16:39:05 -07001070 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1071 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Das62ae6792017-05-15 15:34:25 -07001072 // Note: do not update seenLinks here, otherwise every
1073 // link, even one seen for the first time, will be appear
1074 // to be a previously seen link
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001075 processLinkAdded((Link) event.subject());
1076 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre6d593892016-09-13 21:33:40 -07001077 Link linkRemoved = (Link) event.subject();
Saurav Das62ae6792017-05-15 15:34:25 -07001078 if (linkRemoved.type() == Link.Type.DIRECT) {
1079 updateSeenLink(linkRemoved, false);
1080 }
1081 // device availability check helps to ensure that
1082 // multiple link-removed events are actually treated as a
1083 // single switch removed event. purgeSeenLink is necessary
1084 // so we do rerouting (instead of rehashing) when switch
1085 // comes back.
Pier Ventre6d593892016-09-13 21:33:40 -07001086 if (linkRemoved.src().elementId() instanceof DeviceId &&
1087 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
Saurav Das62ae6792017-05-15 15:34:25 -07001088 purgeSeenLink(linkRemoved);
Pier Ventre6d593892016-09-13 21:33:40 -07001089 continue;
1090 }
1091 if (linkRemoved.dst().elementId() instanceof DeviceId &&
1092 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
Saurav Das62ae6792017-05-15 15:34:25 -07001093 purgeSeenLink(linkRemoved);
Pier Ventre6d593892016-09-13 21:33:40 -07001094 continue;
1095 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001096 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001097 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1098 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1099 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das62af8802015-12-04 10:52:59 -08001100 DeviceId deviceId = ((Device) event.subject()).id();
1101 if (deviceService.isAvailable(deviceId)) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001102 log.info("Processing device event {} for available device {}",
1103 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001104 processDeviceAdded((Device) event.subject());
Saurav Dasc3604f12016-03-23 11:22:49 -07001105 } else {
1106 log.info("Processing device event {} for unavailable device {}",
1107 event.type(), ((Device) event.subject()).id());
1108 processDeviceRemoved((Device) event.subject());
1109 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001110 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd1872b02016-12-02 15:43:47 -08001111 // typically these calls come when device is added first time
1112 // so port filtering rules are handled at the device_added event.
1113 // port added calls represent all ports on the device,
1114 // enabled or not.
Saurav Das62ae6792017-05-15 15:34:25 -07001115 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd1872b02016-12-02 15:43:47 -08001116 ((DeviceEvent) event).subject().id(),
1117 ((DeviceEvent) event).port().number(),
1118 event.type());
Saurav Dasf0f592d2016-11-18 15:21:57 -08001119 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd1872b02016-12-02 15:43:47 -08001120 // these calls happen for every subsequent event
1121 // ports enabled, disabled, switch goes away, comes back
Saurav Dasf0f592d2016-11-18 15:21:57 -08001122 log.info("** PORT UPDATED {}/{} -> {}",
1123 event.subject(),
1124 ((DeviceEvent) event).port(),
1125 event.type());
1126 processPortUpdated(((Device) event.subject()),
1127 ((DeviceEvent) event).port());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001128 } else {
1129 log.warn("Unhandled event type: {}", event.type());
1130 }
sangho80f11cb2015-04-01 13:05:26 -07001131 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001132 } catch (Exception e) {
1133 log.error("SegmentRouting event handler "
1134 + "thread thrown an exception: {}", e);
sangho80f11cb2015-04-01 13:05:26 -07001135 }
sangho80f11cb2015-04-01 13:05:26 -07001136 }
1137 }
1138
sangho80f11cb2015-04-01 13:05:26 -07001139 private void processLinkAdded(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -07001140 log.info("** LINK ADDED {}", link.toString());
Saurav Das62ae6792017-05-15 15:34:25 -07001141 if (link.type() != Link.Type.DIRECT) {
1142 // NOTE: A DIRECT link might be transiently marked as INDIRECT
1143 // if BDDP is received before LLDP. We can safely ignore that
1144 // until the LLDP is received and the link is marked as DIRECT.
1145 log.info("Ignore link {}->{}. Link type is {} instead of DIRECT.",
1146 link.src(), link.dst(), link.type());
1147 return;
1148 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001149 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
1150 updateSeenLink(link, true);
1151 // XXX revisit - what about devicePortMap
1152 log.warn("Source device of this link is not configured.. "
1153 + "not processing further");
1154 return;
1155 }
Saurav Das62ae6792017-05-15 15:34:25 -07001156
1157 //Irrespective of whether the local is a MASTER or not for this device,
1158 //create group handler instance and push default TTP flow rules if needed,
1159 //as in a multi-instance setup, instances can initiate groups for any device.
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001160 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
1161 .deviceId());
1162 if (groupHandler != null) {
Saurav Das62ae6792017-05-15 15:34:25 -07001163 groupHandler.portUpForLink(link);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001164 } else {
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001165 // XXX revisit/cleanup
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001166 Device device = deviceService.getDevice(link.src().deviceId());
1167 if (device != null) {
1168 log.warn("processLinkAdded: Link Added "
1169 + "Notification without Device Added "
1170 + "event, still handling it");
1171 processDeviceAdded(device);
1172 groupHandler = groupHandlerMap.get(link.src()
1173 .deviceId());
Saurav Das62ae6792017-05-15 15:34:25 -07001174 groupHandler.portUpForLink(link);
sangho80f11cb2015-04-01 13:05:26 -07001175 }
1176 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001177
Saurav Das261c3002017-06-13 15:35:54 -07001178 /*// process link only if it is bidirectional
1179 if (!isBidirectional(link)) {
1180 log.debug("Link not bidirectional.. waiting for other direction "
1181 + "src {} --> dst {} ", link.dst(), link.src());
1182 // note that if we are not processing for routing, it should at least
1183 // be considered a seen-link
1184 updateSeenLink(link, true);
1185 return;
1186 }
1187 TO DO this ensure that rehash is still done correctly even if link is
1188 not processed for rerouting - perhaps rehash in both directions when
1189 it ultimately becomes bidi?
1190 */
1191
1192 log.debug("Starting optimized route population process for link "
1193 + "{} --> {}", link.src(), link.dst());
Saurav Das62ae6792017-05-15 15:34:25 -07001194 boolean seenBefore = isSeenLink(link);
1195 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null, link, null);
Saurav Das261c3002017-06-13 15:35:54 -07001196
1197 // It's possible that linkUp causes no route-path change as ECMP graph does
1198 // not change if the link is a parallel link (same src-dst as another link.
1199 // However we still need to update ECMP hash groups to include new buckets
1200 // for the link that has come up.
Saurav Das62ae6792017-05-15 15:34:25 -07001201 if (mastershipService.isLocalMaster(link.src().deviceId())) {
Saurav Das261c3002017-06-13 15:35:54 -07001202 if (!seenBefore && isParallelLink(link)) {
Saurav Das62ae6792017-05-15 15:34:25 -07001203 // if link seen first time, we need to ensure hash-groups have all ports
Saurav Das261c3002017-06-13 15:35:54 -07001204 log.debug("Attempting retryHash for paralled first-time link {}", link);
Saurav Das62ae6792017-05-15 15:34:25 -07001205 groupHandler.retryHash(link, false, true);
1206 } else {
1207 //seen before-link
1208 if (isParallelLink(link)) {
Saurav Das261c3002017-06-13 15:35:54 -07001209 log.debug("Attempting retryHash for paralled seen-before "
1210 + "link {}", link);
Saurav Das62ae6792017-05-15 15:34:25 -07001211 groupHandler.retryHash(link, false, false);
1212 }
1213 }
1214 }
Charles Chan72779502016-04-23 17:36:10 -07001215
1216 mcastHandler.init();
sangho80f11cb2015-04-01 13:05:26 -07001217 }
1218
1219 private void processLinkRemoved(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -07001220 log.info("** LINK REMOVED {}", link.toString());
Saurav Das62ae6792017-05-15 15:34:25 -07001221 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link, null, null);
1222
1223 // update local groupHandler stores
sangho2165d222015-05-01 09:38:25 -07001224 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
1225 if (groupHandler != null) {
Saurav Das62ae6792017-05-15 15:34:25 -07001226 if (mastershipService.isLocalMaster(link.src().deviceId()) &&
1227 isParallelLink(link)) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001228 log.debug("* retrying hash for parallel link removed:{}", link);
Saurav Das62ae6792017-05-15 15:34:25 -07001229 groupHandler.retryHash(link, true, false);
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001230 } else {
1231 log.debug("Not attempting retry-hash for link removed: {} .. {}", link,
1232 (mastershipService.isLocalMaster(link.src().deviceId()))
1233 ? "not parallel" : "not master");
Saurav Das62ae6792017-05-15 15:34:25 -07001234 }
1235 // ensure local stores are updated
1236 groupHandler.portDown(link.src().port());
1237 } else {
1238 log.warn("group handler not found for dev:{} when removing link: {}",
1239 link.src().deviceId(), link);
sangho2165d222015-05-01 09:38:25 -07001240 }
Charles Chan72779502016-04-23 17:36:10 -07001241
1242 mcastHandler.processLinkDown(link);
sangho80f11cb2015-04-01 13:05:26 -07001243 }
1244
1245 private void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001246 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001247
1248 // NOTE: Punt ARP/NDP even when the device is not configured.
1249 // Host learning without network config is required for CORD config generator.
1250 routingRulePopulator.populateIpPunts(device.id());
1251 routingRulePopulator.populateArpNdpPunts(device.id());
1252
Charles Chan319d1a22015-11-03 10:42:14 -08001253 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001254 log.warn("Device configuration unavailable. Device {} will be "
1255 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001256 return;
1257 }
Charles Chan72779502016-04-23 17:36:10 -07001258 processDeviceAddedInternal(device.id());
1259 }
1260
1261 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001262 // Irrespective of whether the local is a MASTER or not for this device,
1263 // we need to create a SR-group-handler instance. This is because in a
1264 // multi-instance setup, any instance can initiate forwarding/next-objectives
1265 // for any switch (even if this instance is a SLAVE or not even connected
1266 // to the switch). To handle this, a default-group-handler instance is necessary
1267 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001268 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1269 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001270 DefaultGroupHandler groupHandler;
1271 try {
1272 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001273 createGroupHandler(deviceId,
1274 appId,
1275 deviceConfiguration,
1276 linkService,
1277 flowObjectiveService,
1278 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001279 } catch (DeviceConfigNotFoundException e) {
1280 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1281 return;
1282 }
Charles Chan72779502016-04-23 17:36:10 -07001283 log.debug("updating groupHandlerMap with new config for device: {}",
1284 deviceId);
1285 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001286 }
Saurav Dasb149be12016-06-07 10:08:06 -07001287
Charles Chan72779502016-04-23 17:36:10 -07001288 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001289 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chandebfea32016-10-24 14:52:01 -07001290 hostHandler.init(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -07001291 xConnectHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001292 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001293 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001294 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001295 }
Charles Chan82ab1932016-01-30 23:22:37 -08001296
Charles Chandebfea32016-10-24 14:52:01 -07001297 appCfgHandler.init(deviceId);
1298 routeHandler.init(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001299 }
1300
Saurav Dasc3604f12016-03-23 11:22:49 -07001301 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001302 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001303 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1304 .forEach(entry -> {
Saurav Das261c3002017-06-13 15:35:54 -07001305 dsNextObjStore.remove(entry.getKey());
Saurav Dasc3604f12016-03-23 11:22:49 -07001306 });
Charles Chan10b0fb72017-02-02 16:20:42 -08001307 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001308 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001309 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001310 portNextObjStore.entrySet().stream()
1311 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001312 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Charles Chan17d75d82017-06-15 00:44:51 -07001313
1314 seenLinks.keySet().removeIf(key -> key.src().deviceId().equals(device.id()) ||
1315 key.dst().deviceId().equals(device.id()));
1316
Saurav Dasfbe74572017-08-03 18:30:35 -07001317 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1318 if (gh != null) {
1319 gh.shutdown();
1320 }
Saurav Dasc3604f12016-03-23 11:22:49 -07001321 defaultRoutingHandler.purgeEcmpGraph(device.id());
Saurav Das62ae6792017-05-15 15:34:25 -07001322 // Note that a switch going down is associated with all of its links
1323 // going down as well, but it is treated as a single switch down event
1324 // while the link-downs are ignored.
1325 defaultRoutingHandler
1326 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Charles Chan72779502016-04-23 17:36:10 -07001327 mcastHandler.removeDevice(device.id());
Charles Chan82f19972016-05-17 13:13:55 -07001328 xConnectHandler.removeDevice(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -07001329 }
1330
Saurav Dasf0f592d2016-11-18 15:21:57 -08001331 private void processPortUpdated(Device device, Port port) {
1332 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1333 log.warn("Device configuration uploading. Not handling port event for"
1334 + "dev: {} port: {}", device.id(), port.number());
1335 return;
1336 }
Saurav Dasf9332192017-02-18 14:05:44 -08001337
1338 if (!mastershipService.isLocalMaster(device.id())) {
1339 log.debug("Not master for dev:{} .. not handling port updated event"
1340 + "for port {}", device.id(), port.number());
1341 return;
1342 }
1343
1344 // first we handle filtering rules associated with the port
1345 if (port.isEnabled()) {
1346 log.info("Switchport {}/{} enabled..programming filters",
1347 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001348 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001349 } else {
1350 log.info("Switchport {}/{} disabled..removing filters",
1351 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001352 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001353 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001354
1355 // portUpdated calls are for ports that have gone down or up. For switch
1356 // to switch ports, link-events should take care of any re-routing or
1357 // group editing necessary for port up/down. Here we only process edge ports
1358 // that are already configured.
Saurav Das3fb28272017-03-04 16:08:47 -08001359 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1360 VlanId untaggedVlan = getUntaggedVlanId(cp);
1361 VlanId nativeVlan = getNativeVlanId(cp);
1362 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001363
Saurav Das3fb28272017-03-04 16:08:47 -08001364 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001365 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasf0f592d2016-11-18 15:21:57 -08001366 + "dev/port: {}/{}", device.id(), port.number());
1367 return;
1368 }
Saurav Das3fb28272017-03-04 16:08:47 -08001369 if (untaggedVlan != null) {
1370 processEdgePort(device, port, untaggedVlan, true);
1371 }
1372 if (nativeVlan != null) {
1373 processEdgePort(device, port, nativeVlan, true);
1374 }
1375 if (!taggedVlans.isEmpty()) {
1376 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1377 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001378 }
1379
Saurav Das3fb28272017-03-04 16:08:47 -08001380 private void processEdgePort(Device device, Port port, VlanId vlanId,
1381 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001382 boolean portUp = port.isEnabled();
1383 if (portUp) {
Saurav Das3fb28272017-03-04 16:08:47 -08001384 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001385 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001386 } else {
Saurav Das3fb28272017-03-04 16:08:47 -08001387 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001388 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001389 }
1390
Srikanth Vavilapalli64505482015-04-21 13:04:13 -07001391 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sangho80f11cb2015-04-01 13:05:26 -07001392 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001393 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001394 } else {
1395 log.warn("Group handler not found for dev:{}. Not handling edge port"
1396 + " {} event for port:{}", device.id(),
1397 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001398 }
1399 }
sangho27462c62015-05-14 00:39:53 -07001400
Pier Ventreb6a7f342016-11-26 21:05:22 -08001401 /**
1402 * Registers the given connect point with the NRS, this is necessary
1403 * to receive the NDP and ARP packets from the NRS.
1404 *
1405 * @param portToRegister connect point to register
1406 */
1407 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001408 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001409 portToRegister,
1410 neighbourHandler,
1411 appId
1412 );
1413 }
1414
Charles Chan72f556a2015-10-05 17:50:33 -07001415 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001416 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001417 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001418
Charles Chanb7f75ac2016-01-11 18:28:54 -08001419 /**
1420 * Constructs the internal network config listener.
1421 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001422 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001423 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001424 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001425 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001426 }
1427
Charles Chanb7f75ac2016-01-11 18:28:54 -08001428 /**
1429 * Reads network config and initializes related data structure accordingly.
1430 */
Charles Chane7c61022015-10-07 14:21:45 -07001431 public void configureNetwork() {
Saurav Das261c3002017-06-13 15:35:54 -07001432 if (deviceConfiguration == null) {
1433 deviceConfiguration = new DeviceConfiguration(srManager);
1434 } else {
1435 deviceConfiguration.updateConfig();
1436 }
Charles Chane7c61022015-10-07 14:21:45 -07001437
Charles Chan46fdfaf2016-11-09 20:51:44 -08001438 arpHandler = new ArpHandler(srManager);
1439 icmpHandler = new IcmpHandler(srManager);
1440 ipHandler = new IpHandler(srManager);
1441 routingRulePopulator = new RoutingRulePopulator(srManager);
1442 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chane7c61022015-10-07 14:21:45 -07001443
1444 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1445 groupHandlerMap, tunnelStore);
1446 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1447 flowObjectiveService,
1448 tunnelHandler, policyStore);
Saurav Das261c3002017-06-13 15:35:54 -07001449 // add a small delay to absorb multiple network config added notifications
1450 if (!programmingScheduled.get()) {
1451 programmingScheduled.set(true);
1452 executorService.schedule(new ConfigChange(), PROGRAM_DELAY,
1453 TimeUnit.SECONDS);
Charles Chane7c61022015-10-07 14:21:45 -07001454 }
Charles Chan72779502016-04-23 17:36:10 -07001455 mcastHandler.init();
Charles Chane7c61022015-10-07 14:21:45 -07001456 }
1457
Charles Chan72f556a2015-10-05 17:50:33 -07001458 @Override
1459 public void event(NetworkConfigEvent event) {
Charles Chan82ab1932016-01-30 23:22:37 -08001460 // TODO move this part to NetworkConfigEventHandler
1461 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1462 switch (event.type()) {
1463 case CONFIG_ADDED:
Charles Chan68363b12017-06-26 15:25:09 -07001464 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan82ab1932016-01-30 23:22:37 -08001465 configureNetwork();
1466 break;
1467 case CONFIG_UPDATED:
Charles Chan68363b12017-06-26 15:25:09 -07001468 log.info("Segment Routing Config updated for {}", event.subject());
1469 // TODO support dynamic configuration
1470 break;
1471 default:
1472 break;
1473 }
1474 } else if (event.configClass().equals(InterfaceConfig.class)) {
1475 switch (event.type()) {
1476 case CONFIG_ADDED:
1477 log.info("Interface Config added for {}", event.subject());
1478 configureNetwork();
1479 break;
1480 case CONFIG_UPDATED:
1481 log.info("Interface Config updated for {}", event.subject());
Charles Chan82ab1932016-01-30 23:22:37 -08001482 // TODO support dynamic configuration
1483 break;
1484 default:
1485 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001486 }
Charles Chan82ab1932016-01-30 23:22:37 -08001487 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chan82f19972016-05-17 13:13:55 -07001488 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan82ab1932016-01-30 23:22:37 -08001489 switch (event.type()) {
1490 case CONFIG_ADDED:
Charles Chan82f19972016-05-17 13:13:55 -07001491 appCfgHandler.processAppConfigAdded(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001492 break;
1493 case CONFIG_UPDATED:
Charles Chan82f19972016-05-17 13:13:55 -07001494 appCfgHandler.processAppConfigUpdated(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001495 break;
1496 case CONFIG_REMOVED:
Charles Chan82f19972016-05-17 13:13:55 -07001497 appCfgHandler.processAppConfigRemoved(event);
1498 break;
1499 default:
1500 break;
1501 }
Charles Chandebfea32016-10-24 14:52:01 -07001502 configureNetwork();
Charles Chan82f19972016-05-17 13:13:55 -07001503 } else if (event.configClass().equals(XConnectConfig.class)) {
1504 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1505 switch (event.type()) {
1506 case CONFIG_ADDED:
1507 xConnectHandler.processXConnectConfigAdded(event);
1508 break;
1509 case CONFIG_UPDATED:
1510 xConnectHandler.processXConnectConfigUpdated(event);
1511 break;
1512 case CONFIG_REMOVED:
1513 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001514 break;
1515 default:
1516 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001517 }
Pier Ventre6b19e482016-11-07 16:21:04 -08001518 } else if (event.configClass().equals(PwaasConfig.class)) {
1519 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1520 switch (event.type()) {
1521 case CONFIG_ADDED:
1522 l2TunnelHandler.processPwaasConfigAdded(event);
1523 break;
1524 case CONFIG_UPDATED:
1525 l2TunnelHandler.processPwaasConfigUpdated(event);
1526 break;
1527 case CONFIG_REMOVED:
1528 l2TunnelHandler.processPwaasConfigRemoved(event);
1529 break;
1530 default:
1531 break;
1532 }
Charles Chan72f556a2015-10-05 17:50:33 -07001533 }
1534 }
Saurav Das261c3002017-06-13 15:35:54 -07001535
1536 private final class ConfigChange implements Runnable {
1537 @Override
1538 public void run() {
1539 programmingScheduled.set(false);
1540 for (Device device : deviceService.getDevices()) {
1541 processDeviceAdded(device);
1542 }
1543 defaultRoutingHandler.startPopulationProcess();
1544 }
1545 }
Charles Chan72f556a2015-10-05 17:50:33 -07001546 }
Charles Chanf4586112015-11-09 16:37:23 -08001547
1548 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001549 @Override
1550 public void event(HostEvent event) {
Charles Chanf4586112015-11-09 16:37:23 -08001551 switch (event.type()) {
1552 case HOST_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001553 hostHandler.processHostAddedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001554 break;
1555 case HOST_MOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001556 hostHandler.processHostMovedEvent(event);
Charles Chan910be6a2017-08-23 14:46:43 -07001557 routeHandler.processHostMovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001558 break;
1559 case HOST_REMOVED:
Charles Chand9265a32017-06-16 15:19:24 -07001560 hostHandler.processHostRemovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001561 break;
1562 case HOST_UPDATED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001563 hostHandler.processHostUpdatedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001564 break;
1565 default:
1566 log.warn("Unsupported host event type: {}", event.type());
1567 break;
1568 }
1569 }
1570 }
1571
Charles Chanc91c8782016-03-30 17:54:24 -07001572 private class InternalMcastListener implements McastListener {
1573 @Override
1574 public void event(McastEvent event) {
1575 switch (event.type()) {
1576 case SOURCE_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001577 mcastHandler.processSourceAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001578 break;
1579 case SINK_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001580 mcastHandler.processSinkAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001581 break;
1582 case SINK_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001583 mcastHandler.processSinkRemoved(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001584 break;
1585 case ROUTE_ADDED:
1586 case ROUTE_REMOVED:
1587 default:
1588 break;
1589 }
1590 }
1591 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001592
Charles Chandebfea32016-10-24 14:52:01 -07001593 private class InternalRouteEventListener implements RouteListener {
1594 @Override
1595 public void event(RouteEvent event) {
1596 switch (event.type()) {
1597 case ROUTE_ADDED:
1598 routeHandler.processRouteAdded(event);
1599 break;
1600 case ROUTE_UPDATED:
1601 routeHandler.processRouteUpdated(event);
1602 break;
1603 case ROUTE_REMOVED:
1604 routeHandler.processRouteRemoved(event);
1605 break;
Charles Chan910be6a2017-08-23 14:46:43 -07001606 case ALTERNATIVE_ROUTES_CHANGED:
1607 routeHandler.processAlternativeRoutesChanged(event);
1608 break;
Charles Chandebfea32016-10-24 14:52:01 -07001609 default:
1610 break;
1611 }
1612 }
1613 }
Saurav Das62ae6792017-05-15 15:34:25 -07001614
sangho80f11cb2015-04-01 13:05:26 -07001615}