blob: 6be675a57e414c3bf0367e88c468de828b7d4035 [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sanghob35a6192015-04-01 13:05:26 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.segmentrouting;
17
Ray Milkey6c1f0f02017-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 Chan9640c812017-08-23 13:55:39 -070032import com.google.common.collect.Sets;
sanghob35a6192015-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;
sangho1e575652015-05-14 00:39:53 -070038import org.apache.felix.scr.annotations.Service;
sanghob35a6192015-04-01 13:05:26 -070039import org.onlab.packet.Ethernet;
Pier Ventre735b8c82016-12-02 08:16:05 -080040import org.onlab.packet.ICMP6;
Charles Chanc42e84e2015-10-20 16:24:19 -070041import org.onlab.packet.IPv4;
Pier Ventre10bd8d12016-11-26 21:05:22 -080042import org.onlab.packet.IPv6;
Charles Chanc42e84e2015-10-20 16:24:19 -070043import org.onlab.packet.IpPrefix;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070044import org.onlab.packet.VlanId;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070045import org.onlab.util.KryoNamespace;
Saurav Das80980c72016-03-23 11:22:49 -070046import org.onosproject.cfg.ComponentConfigService;
sanghob35a6192015-04-01 13:05:26 -070047import org.onosproject.core.ApplicationId;
48import org.onosproject.core.CoreService;
49import org.onosproject.event.Event;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070050import org.onosproject.mastership.MastershipService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080051import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070052import org.onosproject.net.Device;
53import org.onosproject.net.DeviceId;
Charles Chan9640c812017-08-23 13:55:39 -070054import org.onosproject.net.Host;
55import org.onosproject.net.HostId;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070056import org.onosproject.net.Link;
57import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080058import org.onosproject.net.PortNumber;
Charles Chand6832882015-10-05 17:50:33 -070059import org.onosproject.net.config.ConfigFactory;
60import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070061import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070062import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070063import org.onosproject.net.config.basics.InterfaceConfig;
64import org.onosproject.net.config.basics.McastConfig;
Charles Chand6832882015-10-05 17:50:33 -070065import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070066import org.onosproject.net.device.DeviceEvent;
67import org.onosproject.net.device.DeviceListener;
68import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080069import org.onosproject.net.flow.TrafficSelector;
70import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070071import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan68aa62d2015-11-09 16:37:23 -080072import org.onosproject.net.host.HostEvent;
73import org.onosproject.net.host.HostListener;
Pier Ventre10bd8d12016-11-26 21:05:22 -080074import org.onosproject.net.host.HostService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070075import org.onosproject.net.intf.Interface;
76import org.onosproject.net.intf.InterfaceService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080077import org.onosproject.net.link.LinkEvent;
78import org.onosproject.net.link.LinkListener;
79import org.onosproject.net.link.LinkService;
Charles Chand55e84d2016-03-30 17:54:24 -070080import org.onosproject.net.mcast.McastEvent;
81import org.onosproject.net.mcast.McastListener;
82import org.onosproject.net.mcast.MulticastRouteService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070083import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventre10bd8d12016-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 Ventre42287df2016-11-09 14:17:26 -080088import org.onosproject.net.topology.PathService;
Charles Chand55e84d2016-03-30 17:54:24 -070089import org.onosproject.net.topology.TopologyService;
Charles Chan9640c812017-08-23 13:55:39 -070090import org.onosproject.routeservice.ResolvedRoute;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070091import org.onosproject.routeservice.RouteEvent;
92import org.onosproject.routeservice.RouteListener;
93import org.onosproject.routeservice.RouteService;
Charles Chand55e84d2016-03-30 17:54:24 -070094import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
95import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventref34966c2016-11-07 16:21:04 -080096import org.onosproject.segmentrouting.config.PwaasConfig;
Pier Ventref34966c2016-11-07 16:21:04 -080097import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070098import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -070099import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -0700100import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700101import org.onosproject.segmentrouting.grouphandler.DestinationSet;
102import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700103import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700104import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Charles Chand2990362016-04-18 13:44:03 -0700105import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -0800106import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -0700107import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700108import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-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;
sanghob35a6192015-04-01 13:05:26 -0700113import org.slf4j.Logger;
114import org.slf4j.LoggerFactory;
115
Ray Milkey6c1f0f02017-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;
sanghob35a6192015-04-01 13:05:26 -0700120
Charles Chan3e783d02016-02-26 22:19:52 -0800121import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800122import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700123import static org.onlab.util.Tools.groupedThreads;
Charles Chan3e783d02016-02-26 22:19:52 -0800124
Charles Chane849c192016-01-11 18:28:54 -0800125/**
126 * Segment routing manager.
127 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700128@Service
129@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700130public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700131
Charles Chan2c15aca2016-11-09 20:51:44 -0800132 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
sanghob35a6192015-04-01 13:05:26 -0700133
134 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700135 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700136
137 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800138 private NeighbourResolutionService neighbourResolutionService;
139
140 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800141 public PathService pathService;
142
143 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700144 CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700145
146 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700147 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700148
149 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700150 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700151
152 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700153 DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700154
155 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800156 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700157
158 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700159 LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700160
Charles Chan5270ed02016-01-30 23:22:37 -0800161 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800162 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700163
164 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800165 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700166
167 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
168 MulticastRouteService multicastRouteService;
169
170 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
171 TopologyService topologyService;
172
173 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700174 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800175
176 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800177 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800178
Saurav Das80980c72016-03-23 11:22:49 -0700179 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800180 public InterfaceService interfaceService;
181
Charles Chan03a73e02016-10-24 14:52:01 -0700182 ArpHandler arpHandler = null;
183 IcmpHandler icmpHandler = null;
184 IpHandler ipHandler = null;
185 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700186 ApplicationId appId;
187 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700188
Charles Chan03a73e02016-10-24 14:52:01 -0700189 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700190 private TunnelHandler tunnelHandler = null;
191 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700192 private InternalPacketProcessor processor = null;
193 private InternalLinkListener linkListener = null;
194 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700195 private AppConfigHandler appCfgHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700196 XConnectHandler xConnectHandler = null;
Charles Chand2990362016-04-18 13:44:03 -0700197 private McastHandler mcastHandler = null;
Charles Chan65238242017-06-22 18:03:14 -0700198 private HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800199 private RouteHandler routeHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800200 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventref34966c2016-11-07 16:21:04 -0800201 private L2TunnelHandler l2TunnelHandler = null;
sanghob35a6192015-04-01 13:05:26 -0700202 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan5270ed02016-01-30 23:22:37 -0800203 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700204 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
205 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700206 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sanghob35a6192015-04-01 13:05:26 -0700207
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700208 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700209 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sanghob35a6192015-04-01 13:05:26 -0700210
Saurav Das4ce45962015-11-24 23:21:05 -0800211 @SuppressWarnings("unused")
sanghob35a6192015-04-01 13:05:26 -0700212 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800213 @SuppressWarnings("rawtypes")
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700214 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chan68aa62d2015-11-09 16:37:23 -0800215 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chane849c192016-01-11 18:28:54 -0800216 new ConcurrentHashMap<>();
217 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700218 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700219 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800220 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700221 EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
222 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800223 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700224 * Per device next objective ID store with (device id + vlanid) as key.
225 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800226 */
Ray Milkeye4afdb52017-04-05 09:42:04 -0700227 EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800228 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800229 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700230 * Per device next objective ID store with (device id + port + treatment + meta) as key.
231 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chane849c192016-01-11 18:28:54 -0800232 */
Ray Milkeye4afdb52017-04-05 09:42:04 -0700233 EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800234 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800235
Saurav Dasc88d4662017-05-15 15:34:25 -0700236 // Local store for all links seen and their present status, used for
237 // optimized routing. The existence of the link in the keys is enough to know
238 // if the link has been "seen-before" by this instance of the controller.
239 // The boolean value indicates if the link is currently up or not.
240 // XXX Currently the optimized routing logic depends on "forgetting" a link
241 // when a switch goes down, but "remembering" it when only the link goes down.
242 // Consider changing this logic so we can use the Link Service instead of
243 // a local cache.
244 private Map<Link, Boolean> seenLinks = new ConcurrentHashMap<>();
245
Saurav Das4ce45962015-11-24 23:21:05 -0800246 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
247 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700248
Saurav Das7bcbe702017-06-13 15:35:54 -0700249 private AtomicBoolean programmingScheduled = new AtomicBoolean();
250
Charles Chand55e84d2016-03-30 17:54:24 -0700251 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700252 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
253 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700254 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700255 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800256 public SegmentRoutingDeviceConfig createConfig() {
257 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700258 }
259 };
Pier Ventref34966c2016-11-07 16:21:04 -0800260
Charles Chand55e84d2016-03-30 17:54:24 -0700261 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700262 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
263 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700264 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800265 @Override
266 public SegmentRoutingAppConfig createConfig() {
267 return new SegmentRoutingAppConfig();
268 }
269 };
Pier Ventref34966c2016-11-07 16:21:04 -0800270
Charles Chanfc5c7802016-05-17 13:13:55 -0700271 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
272 new ConfigFactory<ApplicationId, XConnectConfig>(
273 SubjectFactories.APP_SUBJECT_FACTORY,
274 XConnectConfig.class, "xconnect") {
275 @Override
276 public XConnectConfig createConfig() {
277 return new XConnectConfig();
278 }
279 };
Pier Ventref34966c2016-11-07 16:21:04 -0800280
Charles Chand55e84d2016-03-30 17:54:24 -0700281 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700282 new ConfigFactory<ApplicationId, McastConfig>(
283 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700284 McastConfig.class, "multicast") {
285 @Override
286 public McastConfig createConfig() {
287 return new McastConfig();
288 }
289 };
290
Pier Ventref34966c2016-11-07 16:21:04 -0800291 private final ConfigFactory<ApplicationId, PwaasConfig> pwaasConfigFactory =
292 new ConfigFactory<ApplicationId, PwaasConfig>(
293 SubjectFactories.APP_SUBJECT_FACTORY,
294 PwaasConfig.class, "pwaas") {
295 @Override
296 public PwaasConfig createConfig() {
297 return new PwaasConfig();
298 }
299 };
300
Charles Chan65238242017-06-22 18:03:14 -0700301 private static final Object THREAD_SCHED_LOCK = new Object();
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700302 private static int numOfEventsQueued = 0;
303 private static int numOfEventsExecuted = 0;
sanghob35a6192015-04-01 13:05:26 -0700304 private static int numOfHandlerExecution = 0;
305 private static int numOfHandlerScheduled = 0;
306
Charles Chan116188d2016-02-18 14:22:42 -0800307 /**
308 * Segment Routing App ID.
309 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800310 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan59cc16d2017-02-02 16:20:42 -0800311
Charles Chane849c192016-01-11 18:28:54 -0800312 /**
313 * The default VLAN ID assigned to the interfaces without subnet config.
314 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800315 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700316
sanghob35a6192015-04-01 13:05:26 -0700317 @Activate
318 protected void activate() {
Charles Chan2c15aca2016-11-09 20:51:44 -0800319 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700320
321 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700322 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700323 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700324 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700325 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700326 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700327 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700328 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700329 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700330
Charles Chan59cc16d2017-02-02 16:20:42 -0800331 log.debug("Creating EC map vlannextobjectivestore");
332 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
333 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
334 vlanNextObjStore = vlanNextObjMapBuilder
335 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700336 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700337 .withTimestampProvider((k, v) -> new WallClockTimestamp())
338 .build();
339
Saurav Das4ce45962015-11-24 23:21:05 -0800340 log.debug("Creating EC map subnetnextobjectivestore");
341 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
342 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
343 portNextObjStore = portNextObjMapBuilder
344 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700345 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800346 .withTimestampProvider((k, v) -> new WallClockTimestamp())
347 .build();
348
sangho0b2b6d12015-05-20 22:16:38 -0700349 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
350 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700351 tunnelStore = tunnelMapBuilder
352 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700353 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700354 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700355 .build();
356
357 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
358 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700359 policyStore = policyMapBuilder
360 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700361 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700362 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700363 .build();
364
Saurav Das80980c72016-03-23 11:22:49 -0700365 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800366 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700367 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800368 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800369 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
370 "requestInterceptsEnabled", "false");
Charles Chand3baaba2017-08-08 15:13:37 -0700371 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800372 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700373 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800374 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800375 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
376 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800377 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
378 "forceUnprovision", "true");
Charles Chanef624ed2017-08-10 16:57:28 -0700379 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan73316522017-07-20 16:16:25 -0700380 "distributed", "true");
Charles Chan35a32322017-08-14 11:42:11 -0700381 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
382 "multihomingEnabled", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700383
Charles Chanb8e10c82015-10-14 11:24:40 -0700384 processor = new InternalPacketProcessor();
385 linkListener = new InternalLinkListener();
386 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700387 appCfgHandler = new AppConfigHandler(this);
388 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700389 mcastHandler = new McastHandler(this);
390 hostHandler = new HostHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700391 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800392 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventref34966c2016-11-07 16:21:04 -0800393 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700394
Charles Chan3e783d02016-02-26 22:19:52 -0800395 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700396 cfgService.registerConfigFactory(deviceConfigFactory);
397 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700398 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700399 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800400 cfgService.registerConfigFactory(pwaasConfigFactory);
Charles Chan5270ed02016-01-30 23:22:37 -0800401 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700402 packetService.addProcessor(processor, PacketProcessor.director(2));
403 linkService.addListener(linkListener);
404 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700405 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700406 routeService.addListener(routeListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700407
408 cfgListener.configureNetwork();
409
sanghob35a6192015-04-01 13:05:26 -0700410 log.info("Started");
411 }
412
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700413 private KryoNamespace.Builder createSerializer() {
414 return new KryoNamespace.Builder()
415 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700416 .register(DestinationSetNextObjectiveStoreKey.class,
Charles Chan59cc16d2017-02-02 16:20:42 -0800417 VlanNextObjectiveStoreKey.class,
Saurav Das7bcbe702017-06-13 15:35:54 -0700418 DestinationSet.class,
419 NextNeighbors.class,
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700420 Tunnel.class,
421 DefaultTunnel.class,
422 Policy.class,
423 TunnelPolicy.class,
424 Policy.Type.class,
425 PortNextObjectiveStoreKey.class,
Charles Chanfc5c7802016-05-17 13:13:55 -0700426 XConnectStoreKey.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700427 );
428 }
429
sanghob35a6192015-04-01 13:05:26 -0700430 @Deactivate
431 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700432 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700433 cfgService.unregisterConfigFactory(deviceConfigFactory);
434 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700435 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700436 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800437 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chand6832882015-10-05 17:50:33 -0700438
Charles Chanc7a8a682017-06-19 00:43:31 -0700439 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700440 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700441 linkService.removeListener(linkListener);
442 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700443 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700444 routeService.removeListener(routeListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700445
Charles Chan0aa674e2017-02-23 15:44:08 -0800446 neighbourResolutionService.unregisterNeighbourHandlers(appId);
447
sanghob35a6192015-04-01 13:05:26 -0700448 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700449 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700450 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700451 groupHandlerMap.clear();
452
Saurav Das7bcbe702017-06-13 15:35:54 -0700453 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800454 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700455 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700456 tunnelStore.destroy();
457 policyStore.destroy();
sanghob35a6192015-04-01 13:05:26 -0700458 log.info("Stopped");
459 }
460
sangho1e575652015-05-14 00:39:53 -0700461 @Override
462 public List<Tunnel> getTunnels() {
463 return tunnelHandler.getTunnels();
464 }
465
466 @Override
sangho71abe1b2015-06-29 14:58:47 -0700467 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
468 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700469 }
470
471 @Override
sangho71abe1b2015-06-29 14:58:47 -0700472 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700473 for (Policy policy: policyHandler.getPolicies()) {
474 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
475 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
476 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
477 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700478 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700479 }
480 }
481 }
sangho71abe1b2015-06-29 14:58:47 -0700482 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700483 }
484
485 @Override
sangho71abe1b2015-06-29 14:58:47 -0700486 public PolicyHandler.Result removePolicy(Policy policy) {
487 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700488 }
489
490 @Override
sangho71abe1b2015-06-29 14:58:47 -0700491 public PolicyHandler.Result createPolicy(Policy policy) {
492 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700493 }
494
495 @Override
496 public List<Policy> getPolicies() {
497 return policyHandler.getPolicies();
498 }
499
Saurav Das59232cf2016-04-27 18:35:50 -0700500 @Override
501 public void rerouteNetwork() {
502 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700503 }
504
Charles Chanc81c45b2016-10-20 17:02:44 -0700505 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800506 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
507 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Charles Chanc81c45b2016-10-20 17:02:44 -0700508 deviceService.getAvailableDevices().forEach(device -> {
509 deviceSubnetMap.put(device.id(), deviceConfiguration.getSubnets(device.id()));
510 });
511 return deviceSubnetMap;
512 }
513
Saurav Dasc88d4662017-05-15 15:34:25 -0700514
515 @Override
516 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
517 if (defaultRoutingHandler != null) {
518 return defaultRoutingHandler.getCurrentEmcpSpgMap();
519 } else {
520 return null;
521 }
522 }
523
524 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700525 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
526 if (dsNextObjStore != null) {
527 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700528 } else {
529 return ImmutableMap.of();
530 }
531 }
532
Saurav Dasceccf242017-08-03 18:30:35 -0700533 @Override
534 public void verifyGroups(DeviceId id) {
535 DefaultGroupHandler gh = groupHandlerMap.get(id);
536 if (gh != null) {
537 gh.triggerBucketCorrector();
538 }
539 }
540
sanghof9d0bf12015-05-19 11:57:42 -0700541 /**
Ray Milkeye4afdb52017-04-05 09:42:04 -0700542 * Extracts the application ID from the manager.
543 *
544 * @return application ID
545 */
546 public ApplicationId appId() {
547 return appId;
548 }
549
550 /**
551 * Returns the device configuration.
552 *
553 * @return device configuration
554 */
555 public DeviceConfiguration deviceConfiguration() {
556 return deviceConfiguration;
557 }
558
559 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700560 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700561 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700562 *
563 * @return next objective ID store
564 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700565 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
566 dsNextObjStore() {
567 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700568 }
569
570 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700571 * Per device next objective ID store with (device id + vlanid) as key.
572 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700573 *
574 * @return vlan next object store
575 */
576 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
577 return vlanNextObjStore;
578 }
579
580 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700581 * Per device next objective ID store with (device id + port + treatment + meta) as key.
582 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700583 *
584 * @return port next object store.
585 */
586 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
587 return portNextObjStore;
588 }
589
590 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700591 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
592 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700593 *
594 * @return MPLS-ECMP value
595 */
596 public boolean getMplsEcmp() {
597 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
598 .getConfig(this.appId, SegmentRoutingAppConfig.class);
599 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
600 }
601
602 /**
sanghof9d0bf12015-05-19 11:57:42 -0700603 * Returns the tunnel object with the tunnel ID.
604 *
605 * @param tunnelId Tunnel ID
606 * @return Tunnel reference
607 */
sangho1e575652015-05-14 00:39:53 -0700608 public Tunnel getTunnel(String tunnelId) {
609 return tunnelHandler.getTunnel(tunnelId);
610 }
611
Charles Chan7ffd81f2017-02-08 15:52:08 -0800612 // TODO Consider moving these to InterfaceService
sanghob35a6192015-04-01 13:05:26 -0700613 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800614 * Returns untagged VLAN configured on given connect point.
Charles Chan7ffd81f2017-02-08 15:52:08 -0800615 * <p>
616 * Only returns the first match if there are multiple untagged VLAN configured
617 * on the connect point.
sanghob35a6192015-04-01 13:05:26 -0700618 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800619 * @param connectPoint connect point
620 * @return untagged VLAN or null if not configured
sanghob35a6192015-04-01 13:05:26 -0700621 */
Charles Chan65238242017-06-22 18:03:14 -0700622 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800623 return interfaceService.getInterfacesByPort(connectPoint).stream()
624 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
625 .map(Interface::vlanUntagged)
626 .findFirst().orElse(null);
sanghob35a6192015-04-01 13:05:26 -0700627 }
628
sangho1e575652015-05-14 00:39:53 -0700629 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800630 * Returns tagged VLAN configured on given connect point.
631 * <p>
632 * Returns all matches if there are multiple tagged VLAN configured
633 * on the connect point.
634 *
635 * @param connectPoint connect point
636 * @return tagged VLAN or empty set if not configured
637 */
Charles Chan65238242017-06-22 18:03:14 -0700638 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800639 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
640 return interfaces.stream()
641 .map(Interface::vlanTagged)
Charles Chan65238242017-06-22 18:03:14 -0700642 .flatMap(Set::stream)
Charles Chan7ffd81f2017-02-08 15:52:08 -0800643 .collect(Collectors.toSet());
644 }
645
646 /**
647 * Returns native VLAN configured on given connect point.
648 * <p>
649 * Only returns the first match if there are multiple native VLAN configured
650 * on the connect point.
651 *
652 * @param connectPoint connect point
653 * @return native VLAN or null if not configured
654 */
Charles Chan65238242017-06-22 18:03:14 -0700655 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800656 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
657 return interfaces.stream()
658 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
659 .map(Interface::vlanNative)
660 .findFirst()
661 .orElse(null);
662 }
663
664 /**
Charles Chanf9a52702017-06-16 15:19:24 -0700665 * Returns internal VLAN for untagged hosts on given connect point.
666 * <p>
667 * The internal VLAN is either vlan-untagged for an access port,
668 * or vlan-native for a trunk port.
669 *
670 * @param connectPoint connect point
671 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
672 */
Charles Chan65238242017-06-22 18:03:14 -0700673 VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chanf9a52702017-06-16 15:19:24 -0700674 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
675 VlanId nativeVlanId = getNativeVlanId(connectPoint);
676 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
677 }
678
679 /**
Charles Chan65238242017-06-22 18:03:14 -0700680 * Returns optional pair device ID of given device.
681 *
682 * @param deviceId device ID
683 * @return optional pair device ID. Might be empty if pair device is not configured
684 */
685 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
686 SegmentRoutingDeviceConfig deviceConfig =
687 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
688 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
689 }
690 /**
691 * Returns optional pair device local port of given device.
692 *
693 * @param deviceId device ID
694 * @return optional pair device ID. Might be empty if pair device is not configured
695 */
696 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
697 SegmentRoutingDeviceConfig deviceConfig =
698 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
699 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
700 }
701
702 /**
Charles Chan9640c812017-08-23 13:55:39 -0700703 * Returns locations of given resolved route.
704 *
705 * @param resolvedRoute resolved route
706 * @return locations of nexthop. Might be empty if next hop is not found
707 */
708 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
709 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
710 return Optional.ofNullable(hostService.getHost(hostId))
711 .map(Host::locations).orElse(Sets.newHashSet())
712 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
713 }
714
715 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800716 * Returns vlan port map of given device.
717 *
718 * @param deviceId device id
719 * @return vlan-port multimap
720 */
721 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
722 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
723
724 interfaceService.getInterfaces().stream()
725 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
726 .forEach(intf -> {
727 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -0700728 intf.vlanTagged().forEach(vlanTagged ->
729 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
730 );
Charles Chan7ffd81f2017-02-08 15:52:08 -0800731 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
732 });
733 vlanPortMap.removeAll(VlanId.NONE);
734
735 return vlanPortMap;
736 }
737
738 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800739 * Returns the next objective ID for the given subnet prefix. It is expected
Charles Chan59cc16d2017-02-02 16:20:42 -0800740 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800741 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700742 *
743 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800744 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800745 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700746 */
Charles Chan65238242017-06-22 18:03:14 -0700747 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700748 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800749 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
750 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700751 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800752 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800753 + "device {} not found", deviceId);
754 return -1;
755 }
756 }
757
758 /**
759 * Returns the next objective ID for the given portNumber, given the treatment.
760 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700761 * would result in different objectives. If the next object does not exist,
762 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800763 *
764 * @param deviceId Device ID
765 * @param portNum port number on device for which NextObjective is queried
766 * @param treatment the actions to apply on the packets (should include outport)
767 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700768 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700769 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800770 */
771 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
772 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700773 TrafficSelector meta,
774 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -0800775 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
776 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -0700777 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -0800778 } else {
Charles Chane849c192016-01-11 18:28:54 -0800779 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
780 + " not found", deviceId);
781 return -1;
782 }
783 }
784
Saurav Dasc88d4662017-05-15 15:34:25 -0700785 /**
786 * Returns the group handler object for the specified device id.
787 *
788 * @param devId the device identifier
789 * @return the groupHandler object for the device id, or null if not found
790 */
Charles Chan65238242017-06-22 18:03:14 -0700791 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700792 return groupHandlerMap.get(devId);
793 }
794
795 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700796 * Returns the default routing handler object.
797 *
798 * @return the default routing handler object
799 */
800 public DefaultRoutingHandler getRoutingHandler() {
801 return defaultRoutingHandler;
802 }
803
804 /**
Saurav Dasc88d4662017-05-15 15:34:25 -0700805 * Returns true if this controller instance has seen this link before. The
806 * link may not be currently up, but as long as the link had been seen before
807 * this method will return true. The one exception is when the link was
808 * indeed seen before, but this controller instance was forced to forget it
809 * by a call to purgeSeenLink method.
810 *
811 * @param link the infrastructure link being queried
812 * @return true if this controller instance has seen this link before
813 */
Charles Chan65238242017-06-22 18:03:14 -0700814 boolean isSeenLink(Link link) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700815 return seenLinks.containsKey(link);
816 }
817
818 /**
819 * Updates the seen link store. Updates can be for links that are currently
820 * available or not.
821 *
822 * @param link the link to update in the seen-link local store
823 * @param up the status of the link, true if up, false if down
824 */
Charles Chan65238242017-06-22 18:03:14 -0700825 void updateSeenLink(Link link, boolean up) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700826 seenLinks.put(link, up);
827 }
828
829 /**
830 * Returns the status of a seen-link (up or down). If the link has not
831 * been seen-before, a null object is returned.
832 *
833 * @param link the infrastructure link being queried
834 * @return null if the link was not seen-before;
835 * true if the seen-link is up;
836 * false if the seen-link is down
837 */
Charles Chan65238242017-06-22 18:03:14 -0700838 Boolean isSeenLinkUp(Link link) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700839 return seenLinks.get(link);
840 }
841
842 /**
843 * Makes this controller instance forget a previously seen before link.
844 *
845 * @param link the infrastructure link to purge
846 */
Charles Chan65238242017-06-22 18:03:14 -0700847 private void purgeSeenLink(Link link) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700848 seenLinks.remove(link);
849 }
850
851 /**
852 * Returns the status of a link as parallel link. A parallel link
853 * is defined as a link which has common src and dst switches as another
854 * seen-link that is currently enabled. It is not necessary for the link being
855 * queried to be a seen-link.
856 *
857 * @param link the infrastructure link being queried
858 * @return true if a seen-link exists that is up, and shares the
859 * same src and dst switches as the link being queried
860 */
Charles Chan65238242017-06-22 18:03:14 -0700861 private boolean isParallelLink(Link link) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700862 for (Entry<Link, Boolean> seen : seenLinks.entrySet()) {
863 Link seenLink = seen.getKey();
864 if (seenLink.equals(link)) {
865 continue;
866 }
867 if (seenLink.src().deviceId().equals(link.src().deviceId()) &&
868 seenLink.dst().deviceId().equals(link.dst().deviceId()) &&
869 seen.getValue()) {
870 return true;
871 }
872 }
873 return false;
874 }
875
Saurav Das7bcbe702017-06-13 15:35:54 -0700876 /**
877 * Returns true if the link being queried is a bidirectional link. A bidi
878 * link is defined as a link, whose reverse link - ie. the link in the reverse
879 * direction - has been seen-before and is up. It is not necessary for the link
880 * being queried to be a seen-link.
881 *
882 * @param link the infrastructure link being queried
883 * @return true if another unidirectional link exists in the reverse direction,
884 * has been seen-before and is up
885 */
886 public boolean isBidirectional(Link link) {
887 Link reverseLink = linkService.getLink(link.dst(), link.src());
888 if (reverseLink == null) {
889 return false;
890 }
891 Boolean result = isSeenLinkUp(reverseLink);
892 if (result == null) {
893 return false;
894 }
895 return result.booleanValue();
896 }
897
898 /**
899 * Determines if the given link should be avoided in routing calculations
900 * by policy or design.
901 *
902 * @param link the infrastructure link being queried
903 * @return true if link should be avoided
904 */
905 public boolean avoidLink(Link link) {
906 // XXX currently only avoids all pair-links. In the future can be
907 // extended to avoid any generic link
908 DeviceId src = link.src().deviceId();
909 PortNumber srcPort = link.src().port();
910 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(src)) {
911 log.warn("Device {} not configured..cannot avoid link {}", src, link);
912 return false;
913 }
914 DeviceId pairDev;
915 PortNumber pairLocalPort, pairRemotePort = null;
916 try {
917 pairDev = deviceConfiguration.getPairDeviceId(src);
918 pairLocalPort = deviceConfiguration.getPairLocalPort(src);
919 if (pairDev != null) {
920 pairRemotePort = deviceConfiguration.getPairLocalPort(pairDev);
921 }
922 } catch (DeviceConfigNotFoundException e) {
923 log.warn("Pair dev for dev {} not configured..cannot avoid link {}",
924 src, link);
925 return false;
926 }
927
928 if (srcPort.equals(pairLocalPort) &&
929 link.dst().deviceId().equals(pairDev) &&
930 link.dst().port().equals(pairRemotePort)) {
931 return true;
932 }
933 return false;
934 }
935
sanghob35a6192015-04-01 13:05:26 -0700936 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700937 @Override
938 public void process(PacketContext context) {
939
940 if (context.isHandled()) {
941 return;
942 }
943
944 InboundPacket pkt = context.inPacket();
945 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800946
947 if (ethernet == null) {
948 return;
949 }
950
Saurav Das7bcbe702017-06-13 15:35:54 -0700951 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
952 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800953 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700954 log.warn("Received unexpected ARP packet on {}",
955 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -0700956 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800957 return;
sanghob35a6192015-04-01 13:05:26 -0700958 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800959 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
960 //ipHandler.addToPacketBuffer(ipv4Packet);
961 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
962 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700963 } else {
Charles Chan50035632017-01-13 17:20:44 -0800964 // NOTE: We don't support IP learning at this moment so this
965 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800966 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700967 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800968 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
969 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -0800970 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -0800971 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -0800972 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
973 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
974 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
975 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
976 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
977 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -0700978 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -0700979 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -0800980 }
981 } else {
982 // NOTE: We don't support IP learning at this moment so this
983 // is not necessary. Also it causes duplication of DHCPv6 packets.
984 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
985 }
sanghob35a6192015-04-01 13:05:26 -0700986 }
987 }
988 }
989
990 private class InternalLinkListener implements LinkListener {
991 @Override
992 public void event(LinkEvent event) {
Charles Chanb1f8c762017-03-29 16:39:05 -0700993 if (event.type() == LinkEvent.Type.LINK_ADDED ||
994 event.type() == LinkEvent.Type.LINK_UPDATED ||
995 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700996 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700997 scheduleEventHandlerIfNotScheduled(event);
998 }
999 }
1000 }
1001
1002 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -07001003 @Override
1004 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -07001005 switch (event.type()) {
1006 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -08001007 case PORT_UPDATED:
1008 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -07001009 case DEVICE_UPDATED:
1010 case DEVICE_AVAILABILITY_CHANGED:
Saurav Dasc88d4662017-05-15 15:34:25 -07001011 log.trace("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -07001012 scheduleEventHandlerIfNotScheduled(event);
1013 break;
1014 default:
1015 }
1016 }
1017 }
1018
Saurav Das4ce45962015-11-24 23:21:05 -08001019 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -07001020 private void scheduleEventHandlerIfNotScheduled(Event event) {
Charles Chan65238242017-06-22 18:03:14 -07001021 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001022 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001023 numOfEventsQueued++;
1024
1025 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
1026 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001027 eventHandlerFuture = executorService
1028 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
1029 numOfHandlerScheduled++;
1030 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -07001031 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001032 numOfEventsQueued,
1033 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -07001034 }
sanghob35a6192015-04-01 13:05:26 -07001035 }
1036
1037 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001038 @Override
sanghob35a6192015-04-01 13:05:26 -07001039 public void run() {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001040 try {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001041 while (true) {
Saurav Das4ce45962015-11-24 23:21:05 -08001042 @SuppressWarnings("rawtypes")
Charles Chan65238242017-06-22 18:03:14 -07001043 Event event;
1044 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001045 if (!eventQueue.isEmpty()) {
1046 event = eventQueue.poll();
1047 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001048 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001049 numOfHandlerExecution++;
1050 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
1051 numOfHandlerExecution, numOfEventsExecuted);
1052 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001053 }
sangho20eff1d2015-04-13 15:15:58 -07001054 }
Charles Chanb1f8c762017-03-29 16:39:05 -07001055 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1056 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001057 // Note: do not update seenLinks here, otherwise every
1058 // link, even one seen for the first time, will be appear
1059 // to be a previously seen link
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001060 processLinkAdded((Link) event.subject());
1061 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre2c515312016-09-13 21:33:40 -07001062 Link linkRemoved = (Link) event.subject();
Saurav Dasc88d4662017-05-15 15:34:25 -07001063 if (linkRemoved.type() == Link.Type.DIRECT) {
1064 updateSeenLink(linkRemoved, false);
1065 }
1066 // device availability check helps to ensure that
1067 // multiple link-removed events are actually treated as a
1068 // single switch removed event. purgeSeenLink is necessary
1069 // so we do rerouting (instead of rehashing) when switch
1070 // comes back.
Pier Ventre2c515312016-09-13 21:33:40 -07001071 if (linkRemoved.src().elementId() instanceof DeviceId &&
1072 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001073 purgeSeenLink(linkRemoved);
Pier Ventre2c515312016-09-13 21:33:40 -07001074 continue;
1075 }
1076 if (linkRemoved.dst().elementId() instanceof DeviceId &&
1077 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001078 purgeSeenLink(linkRemoved);
Pier Ventre2c515312016-09-13 21:33:40 -07001079 continue;
1080 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001081 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001082 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1083 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1084 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001085 DeviceId deviceId = ((Device) event.subject()).id();
1086 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001087 log.info("Processing device event {} for available device {}",
1088 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001089 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -07001090 } else {
1091 log.info("Processing device event {} for unavailable device {}",
1092 event.type(), ((Device) event.subject()).id());
1093 processDeviceRemoved((Device) event.subject());
1094 }
Saurav Das1a129a02016-11-18 15:21:57 -08001095 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001096 // typically these calls come when device is added first time
1097 // so port filtering rules are handled at the device_added event.
1098 // port added calls represent all ports on the device,
1099 // enabled or not.
Saurav Dasc88d4662017-05-15 15:34:25 -07001100 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001101 ((DeviceEvent) event).subject().id(),
1102 ((DeviceEvent) event).port().number(),
1103 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -08001104 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001105 // these calls happen for every subsequent event
1106 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -08001107 log.info("** PORT UPDATED {}/{} -> {}",
1108 event.subject(),
1109 ((DeviceEvent) event).port(),
1110 event.type());
1111 processPortUpdated(((Device) event.subject()),
1112 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001113 } else {
1114 log.warn("Unhandled event type: {}", event.type());
1115 }
sanghob35a6192015-04-01 13:05:26 -07001116 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001117 } catch (Exception e) {
1118 log.error("SegmentRouting event handler "
1119 + "thread thrown an exception: {}", e);
sanghob35a6192015-04-01 13:05:26 -07001120 }
sanghob35a6192015-04-01 13:05:26 -07001121 }
1122 }
1123
sanghob35a6192015-04-01 13:05:26 -07001124 private void processLinkAdded(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001125 log.info("** LINK ADDED {}", link.toString());
Saurav Dasc88d4662017-05-15 15:34:25 -07001126 if (link.type() != Link.Type.DIRECT) {
1127 // NOTE: A DIRECT link might be transiently marked as INDIRECT
1128 // if BDDP is received before LLDP. We can safely ignore that
1129 // until the LLDP is received and the link is marked as DIRECT.
1130 log.info("Ignore link {}->{}. Link type is {} instead of DIRECT.",
1131 link.src(), link.dst(), link.type());
1132 return;
1133 }
Saurav Das9df5b7c2017-08-14 16:44:43 -07001134 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
1135 updateSeenLink(link, true);
1136 // XXX revisit - what about devicePortMap
1137 log.warn("Source device of this link is not configured.. "
1138 + "not processing further");
1139 return;
1140 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001141
1142 //Irrespective of whether the local is a MASTER or not for this device,
1143 //create group handler instance and push default TTP flow rules if needed,
1144 //as in a multi-instance setup, instances can initiate groups for any device.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001145 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
1146 .deviceId());
1147 if (groupHandler != null) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001148 groupHandler.portUpForLink(link);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001149 } else {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001150 // XXX revisit/cleanup
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001151 Device device = deviceService.getDevice(link.src().deviceId());
1152 if (device != null) {
1153 log.warn("processLinkAdded: Link Added "
1154 + "Notification without Device Added "
1155 + "event, still handling it");
1156 processDeviceAdded(device);
1157 groupHandler = groupHandlerMap.get(link.src()
1158 .deviceId());
Saurav Dasc88d4662017-05-15 15:34:25 -07001159 groupHandler.portUpForLink(link);
sanghob35a6192015-04-01 13:05:26 -07001160 }
1161 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001162
Saurav Das7bcbe702017-06-13 15:35:54 -07001163 /*// process link only if it is bidirectional
1164 if (!isBidirectional(link)) {
1165 log.debug("Link not bidirectional.. waiting for other direction "
1166 + "src {} --> dst {} ", link.dst(), link.src());
1167 // note that if we are not processing for routing, it should at least
1168 // be considered a seen-link
1169 updateSeenLink(link, true);
1170 return;
1171 }
1172 TO DO this ensure that rehash is still done correctly even if link is
1173 not processed for rerouting - perhaps rehash in both directions when
1174 it ultimately becomes bidi?
1175 */
1176
1177 log.debug("Starting optimized route population process for link "
1178 + "{} --> {}", link.src(), link.dst());
Saurav Dasc88d4662017-05-15 15:34:25 -07001179 boolean seenBefore = isSeenLink(link);
1180 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null, link, null);
Saurav Das7bcbe702017-06-13 15:35:54 -07001181
1182 // It's possible that linkUp causes no route-path change as ECMP graph does
1183 // not change if the link is a parallel link (same src-dst as another link.
1184 // However we still need to update ECMP hash groups to include new buckets
1185 // for the link that has come up.
Saurav Dasc88d4662017-05-15 15:34:25 -07001186 if (mastershipService.isLocalMaster(link.src().deviceId())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001187 if (!seenBefore && isParallelLink(link)) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001188 // if link seen first time, we need to ensure hash-groups have all ports
Saurav Das7bcbe702017-06-13 15:35:54 -07001189 log.debug("Attempting retryHash for paralled first-time link {}", link);
Saurav Dasc88d4662017-05-15 15:34:25 -07001190 groupHandler.retryHash(link, false, true);
1191 } else {
1192 //seen before-link
1193 if (isParallelLink(link)) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001194 log.debug("Attempting retryHash for paralled seen-before "
1195 + "link {}", link);
Saurav Dasc88d4662017-05-15 15:34:25 -07001196 groupHandler.retryHash(link, false, false);
1197 }
1198 }
1199 }
Charles Chan2199c302016-04-23 17:36:10 -07001200
1201 mcastHandler.init();
sanghob35a6192015-04-01 13:05:26 -07001202 }
1203
1204 private void processLinkRemoved(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001205 log.info("** LINK REMOVED {}", link.toString());
Saurav Dasc88d4662017-05-15 15:34:25 -07001206 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link, null, null);
1207
1208 // update local groupHandler stores
sangho834e4b02015-05-01 09:38:25 -07001209 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
1210 if (groupHandler != null) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001211 if (mastershipService.isLocalMaster(link.src().deviceId()) &&
1212 isParallelLink(link)) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001213 log.debug("* retrying hash for parallel link removed:{}", link);
Saurav Dasc88d4662017-05-15 15:34:25 -07001214 groupHandler.retryHash(link, true, false);
Saurav Das9df5b7c2017-08-14 16:44:43 -07001215 } else {
1216 log.debug("Not attempting retry-hash for link removed: {} .. {}", link,
1217 (mastershipService.isLocalMaster(link.src().deviceId()))
1218 ? "not parallel" : "not master");
Saurav Dasc88d4662017-05-15 15:34:25 -07001219 }
1220 // ensure local stores are updated
1221 groupHandler.portDown(link.src().port());
1222 } else {
1223 log.warn("group handler not found for dev:{} when removing link: {}",
1224 link.src().deviceId(), link);
sangho834e4b02015-05-01 09:38:25 -07001225 }
Charles Chan2199c302016-04-23 17:36:10 -07001226
1227 mcastHandler.processLinkDown(link);
sanghob35a6192015-04-01 13:05:26 -07001228 }
1229
1230 private void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001231 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001232
1233 // NOTE: Punt ARP/NDP even when the device is not configured.
1234 // Host learning without network config is required for CORD config generator.
1235 routingRulePopulator.populateIpPunts(device.id());
1236 routingRulePopulator.populateArpNdpPunts(device.id());
1237
Charles Chan0b4e6182015-11-03 10:42:14 -08001238 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001239 log.warn("Device configuration unavailable. Device {} will be "
1240 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001241 return;
1242 }
Charles Chan2199c302016-04-23 17:36:10 -07001243 processDeviceAddedInternal(device.id());
1244 }
1245
1246 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001247 // Irrespective of whether the local is a MASTER or not for this device,
1248 // we need to create a SR-group-handler instance. This is because in a
1249 // multi-instance setup, any instance can initiate forwarding/next-objectives
1250 // for any switch (even if this instance is a SLAVE or not even connected
1251 // to the switch). To handle this, a default-group-handler instance is necessary
1252 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001253 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1254 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001255 DefaultGroupHandler groupHandler;
1256 try {
1257 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001258 createGroupHandler(deviceId,
1259 appId,
1260 deviceConfiguration,
1261 linkService,
1262 flowObjectiveService,
1263 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001264 } catch (DeviceConfigNotFoundException e) {
1265 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1266 return;
1267 }
Charles Chan2199c302016-04-23 17:36:10 -07001268 log.debug("updating groupHandlerMap with new config for device: {}",
1269 deviceId);
1270 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001271 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001272
Charles Chan2199c302016-04-23 17:36:10 -07001273 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001274 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan03a73e02016-10-24 14:52:01 -07001275 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001276 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001277 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001278 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001279 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001280 }
Charles Chan5270ed02016-01-30 23:22:37 -08001281
Charles Chan03a73e02016-10-24 14:52:01 -07001282 appCfgHandler.init(deviceId);
1283 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001284 }
1285
Saurav Das80980c72016-03-23 11:22:49 -07001286 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001287 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001288 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1289 .forEach(entry -> {
Saurav Das7bcbe702017-06-13 15:35:54 -07001290 dsNextObjStore.remove(entry.getKey());
Saurav Das80980c72016-03-23 11:22:49 -07001291 });
Charles Chan59cc16d2017-02-02 16:20:42 -08001292 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001293 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001294 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001295 portNextObjStore.entrySet().stream()
1296 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001297 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Charles Chaned3742352017-06-15 00:44:51 -07001298
1299 seenLinks.keySet().removeIf(key -> key.src().deviceId().equals(device.id()) ||
1300 key.dst().deviceId().equals(device.id()));
1301
Saurav Dasceccf242017-08-03 18:30:35 -07001302 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1303 if (gh != null) {
1304 gh.shutdown();
1305 }
Saurav Das80980c72016-03-23 11:22:49 -07001306 defaultRoutingHandler.purgeEcmpGraph(device.id());
Saurav Dasc88d4662017-05-15 15:34:25 -07001307 // Note that a switch going down is associated with all of its links
1308 // going down as well, but it is treated as a single switch down event
1309 // while the link-downs are ignored.
1310 defaultRoutingHandler
1311 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Charles Chan2199c302016-04-23 17:36:10 -07001312 mcastHandler.removeDevice(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001313 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -07001314 }
1315
Saurav Das1a129a02016-11-18 15:21:57 -08001316 private void processPortUpdated(Device device, Port port) {
1317 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1318 log.warn("Device configuration uploading. Not handling port event for"
1319 + "dev: {} port: {}", device.id(), port.number());
1320 return;
1321 }
Saurav Das018605f2017-02-18 14:05:44 -08001322
1323 if (!mastershipService.isLocalMaster(device.id())) {
1324 log.debug("Not master for dev:{} .. not handling port updated event"
1325 + "for port {}", device.id(), port.number());
1326 return;
1327 }
1328
1329 // first we handle filtering rules associated with the port
1330 if (port.isEnabled()) {
1331 log.info("Switchport {}/{} enabled..programming filters",
1332 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001333 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001334 } else {
1335 log.info("Switchport {}/{} disabled..removing filters",
1336 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001337 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001338 }
Saurav Das1a129a02016-11-18 15:21:57 -08001339
1340 // portUpdated calls are for ports that have gone down or up. For switch
1341 // to switch ports, link-events should take care of any re-routing or
1342 // group editing necessary for port up/down. Here we only process edge ports
1343 // that are already configured.
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001344 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1345 VlanId untaggedVlan = getUntaggedVlanId(cp);
1346 VlanId nativeVlan = getNativeVlanId(cp);
1347 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001348
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001349 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001350 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das1a129a02016-11-18 15:21:57 -08001351 + "dev/port: {}/{}", device.id(), port.number());
1352 return;
1353 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001354 if (untaggedVlan != null) {
1355 processEdgePort(device, port, untaggedVlan, true);
1356 }
1357 if (nativeVlan != null) {
1358 processEdgePort(device, port, nativeVlan, true);
1359 }
1360 if (!taggedVlans.isEmpty()) {
1361 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1362 }
Saurav Das1a129a02016-11-18 15:21:57 -08001363 }
1364
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001365 private void processEdgePort(Device device, Port port, VlanId vlanId,
1366 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001367 boolean portUp = port.isEnabled();
1368 if (portUp) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001369 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001370 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001371 } else {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001372 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001373 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001374 }
1375
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001376 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001377 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001378 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001379 } else {
1380 log.warn("Group handler not found for dev:{}. Not handling edge port"
1381 + " {} event for port:{}", device.id(),
1382 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001383 }
1384 }
sangho1e575652015-05-14 00:39:53 -07001385
Pier Ventre10bd8d12016-11-26 21:05:22 -08001386 /**
1387 * Registers the given connect point with the NRS, this is necessary
1388 * to receive the NDP and ARP packets from the NRS.
1389 *
1390 * @param portToRegister connect point to register
1391 */
1392 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001393 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001394 portToRegister,
1395 neighbourHandler,
1396 appId
1397 );
1398 }
1399
Charles Chand6832882015-10-05 17:50:33 -07001400 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001401 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001402 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001403
Charles Chane849c192016-01-11 18:28:54 -08001404 /**
1405 * Constructs the internal network config listener.
1406 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001407 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001408 */
Charles Chan65238242017-06-22 18:03:14 -07001409 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001410 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001411 }
1412
Charles Chane849c192016-01-11 18:28:54 -08001413 /**
1414 * Reads network config and initializes related data structure accordingly.
1415 */
Charles Chan4636be02015-10-07 14:21:45 -07001416 public void configureNetwork() {
Saurav Das7bcbe702017-06-13 15:35:54 -07001417 if (deviceConfiguration == null) {
1418 deviceConfiguration = new DeviceConfiguration(srManager);
1419 } else {
1420 deviceConfiguration.updateConfig();
1421 }
Charles Chan4636be02015-10-07 14:21:45 -07001422
Charles Chan2c15aca2016-11-09 20:51:44 -08001423 arpHandler = new ArpHandler(srManager);
1424 icmpHandler = new IcmpHandler(srManager);
1425 ipHandler = new IpHandler(srManager);
1426 routingRulePopulator = new RoutingRulePopulator(srManager);
1427 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001428
1429 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1430 groupHandlerMap, tunnelStore);
1431 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1432 flowObjectiveService,
1433 tunnelHandler, policyStore);
Saurav Das7bcbe702017-06-13 15:35:54 -07001434 // add a small delay to absorb multiple network config added notifications
1435 if (!programmingScheduled.get()) {
1436 programmingScheduled.set(true);
1437 executorService.schedule(new ConfigChange(), PROGRAM_DELAY,
1438 TimeUnit.SECONDS);
Charles Chan4636be02015-10-07 14:21:45 -07001439 }
Charles Chan2199c302016-04-23 17:36:10 -07001440 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001441 }
1442
Charles Chand6832882015-10-05 17:50:33 -07001443 @Override
1444 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001445 // TODO move this part to NetworkConfigEventHandler
1446 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1447 switch (event.type()) {
1448 case CONFIG_ADDED:
Charles Chan278ce832017-06-26 15:25:09 -07001449 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan5270ed02016-01-30 23:22:37 -08001450 configureNetwork();
1451 break;
1452 case CONFIG_UPDATED:
Charles Chan278ce832017-06-26 15:25:09 -07001453 log.info("Segment Routing Config updated for {}", event.subject());
1454 // TODO support dynamic configuration
1455 break;
1456 default:
1457 break;
1458 }
1459 } else if (event.configClass().equals(InterfaceConfig.class)) {
1460 switch (event.type()) {
1461 case CONFIG_ADDED:
1462 log.info("Interface Config added for {}", event.subject());
1463 configureNetwork();
1464 break;
1465 case CONFIG_UPDATED:
1466 log.info("Interface Config updated for {}", event.subject());
Charles Chan5270ed02016-01-30 23:22:37 -08001467 // TODO support dynamic configuration
1468 break;
1469 default:
1470 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001471 }
Charles Chan5270ed02016-01-30 23:22:37 -08001472 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001473 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001474 switch (event.type()) {
1475 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001476 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001477 break;
1478 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001479 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001480 break;
1481 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001482 appCfgHandler.processAppConfigRemoved(event);
1483 break;
1484 default:
1485 break;
1486 }
Charles Chan03a73e02016-10-24 14:52:01 -07001487 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001488 } else if (event.configClass().equals(XConnectConfig.class)) {
1489 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1490 switch (event.type()) {
1491 case CONFIG_ADDED:
1492 xConnectHandler.processXConnectConfigAdded(event);
1493 break;
1494 case CONFIG_UPDATED:
1495 xConnectHandler.processXConnectConfigUpdated(event);
1496 break;
1497 case CONFIG_REMOVED:
1498 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001499 break;
1500 default:
1501 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001502 }
Pier Ventref34966c2016-11-07 16:21:04 -08001503 } else if (event.configClass().equals(PwaasConfig.class)) {
1504 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1505 switch (event.type()) {
1506 case CONFIG_ADDED:
1507 l2TunnelHandler.processPwaasConfigAdded(event);
1508 break;
1509 case CONFIG_UPDATED:
1510 l2TunnelHandler.processPwaasConfigUpdated(event);
1511 break;
1512 case CONFIG_REMOVED:
1513 l2TunnelHandler.processPwaasConfigRemoved(event);
1514 break;
1515 default:
1516 break;
1517 }
Charles Chand6832882015-10-05 17:50:33 -07001518 }
1519 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001520
1521 private final class ConfigChange implements Runnable {
1522 @Override
1523 public void run() {
1524 programmingScheduled.set(false);
1525 for (Device device : deviceService.getDevices()) {
1526 processDeviceAdded(device);
1527 }
1528 defaultRoutingHandler.startPopulationProcess();
1529 }
1530 }
Charles Chand6832882015-10-05 17:50:33 -07001531 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001532
1533 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001534 @Override
1535 public void event(HostEvent event) {
Charles Chan68aa62d2015-11-09 16:37:23 -08001536 switch (event.type()) {
1537 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001538 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001539 break;
1540 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001541 hostHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001542 break;
1543 case HOST_REMOVED:
Charles Chanf9a52702017-06-16 15:19:24 -07001544 hostHandler.processHostRemovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001545 break;
1546 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001547 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001548 break;
1549 default:
1550 log.warn("Unsupported host event type: {}", event.type());
1551 break;
1552 }
1553 }
1554 }
1555
Charles Chand55e84d2016-03-30 17:54:24 -07001556 private class InternalMcastListener implements McastListener {
1557 @Override
1558 public void event(McastEvent event) {
1559 switch (event.type()) {
1560 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001561 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001562 break;
1563 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001564 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001565 break;
1566 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001567 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001568 break;
1569 case ROUTE_ADDED:
1570 case ROUTE_REMOVED:
1571 default:
1572 break;
1573 }
1574 }
1575 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001576
Charles Chan03a73e02016-10-24 14:52:01 -07001577 private class InternalRouteEventListener implements RouteListener {
1578 @Override
1579 public void event(RouteEvent event) {
1580 switch (event.type()) {
1581 case ROUTE_ADDED:
1582 routeHandler.processRouteAdded(event);
1583 break;
1584 case ROUTE_UPDATED:
1585 routeHandler.processRouteUpdated(event);
1586 break;
1587 case ROUTE_REMOVED:
1588 routeHandler.processRouteRemoved(event);
1589 break;
1590 default:
1591 break;
1592 }
1593 }
1594 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001595
sanghob35a6192015-04-01 13:05:26 -07001596}