blob: 6be675a57e414c3bf0367e88c468de828b7d4035 [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);
sangho80f11cb2015-04-01 13:05:26 -0700133
134 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700135 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700136
137 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventreb6a7f342016-11-26 21:05:22 -0800138 private NeighbourResolutionService neighbourResolutionService;
139
140 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800141 public PathService pathService;
142
143 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700144 CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700145
146 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700147 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700148
149 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700150 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700151
152 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700153 DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700154
155 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800156 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700157
158 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700159 LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700160
Charles Chan82ab1932016-01-30 23:22:37 -0800161 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800162 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700163
164 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800165 public StorageService storageService;
Charles Chandebfea32016-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 Chandebfea32016-10-24 14:52:01 -0700174 RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800175
176 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800177 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800178
Saurav Dasc3604f12016-03-23 11:22:49 -0700179 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800180 public InterfaceService interfaceService;
181
Charles Chandebfea32016-10-24 14:52:01 -0700182 ArpHandler arpHandler = null;
183 IcmpHandler icmpHandler = null;
184 IpHandler ipHandler = null;
185 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700186 ApplicationId appId;
187 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700188
Charles Chandebfea32016-10-24 14:52:01 -0700189 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700190 private TunnelHandler tunnelHandler = null;
191 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700192 private InternalPacketProcessor processor = null;
193 private InternalLinkListener linkListener = null;
194 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700195 private AppConfigHandler appCfgHandler = null;
Charles Chandebfea32016-10-24 14:52:01 -0700196 XConnectHandler xConnectHandler = null;
Charles Chan1eaf4802016-04-18 13:44:03 -0700197 private McastHandler mcastHandler = null;
Charles Chan3ed34d82017-06-22 18:03:14 -0700198 private HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800199 private RouteHandler routeHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800200 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventre6b19e482016-11-07 16:21:04 -0800201 private L2TunnelHandler l2TunnelHandler = null;
sangho80f11cb2015-04-01 13:05:26 -0700202 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan82ab1932016-01-30 23:22:37 -0800203 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700204 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
205 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700206 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sangho80f11cb2015-04-01 13:05:26 -0700207
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700208 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700209 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sangho80f11cb2015-04-01 13:05:26 -0700210
Saurav Das2d94d312015-11-24 23:21:05 -0800211 @SuppressWarnings("unused")
sangho80f11cb2015-04-01 13:05:26 -0700212 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das2d94d312015-11-24 23:21:05 -0800213 @SuppressWarnings("rawtypes")
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700214 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chanf4586112015-11-09 16:37:23 -0800215 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chanb7f75ac2016-01-11 18:28:54 -0800216 new ConcurrentHashMap<>();
217 /**
Saurav Das261c3002017-06-13 15:35:54 -0700218 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700219 * Used to keep track on MPLS group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800220 */
Saurav Das261c3002017-06-13 15:35:54 -0700221 EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
222 dsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800223 /**
Charles Chan53de91f2017-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 Chanb7f75ac2016-01-11 18:28:54 -0800226 */
Ray Milkeyb85de082017-04-05 09:42:04 -0700227 EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800228 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800229 /**
Charles Chan53de91f2017-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 Chanb7f75ac2016-01-11 18:28:54 -0800232 */
Ray Milkeyb85de082017-04-05 09:42:04 -0700233 EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800234 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800235
Saurav Das62ae6792017-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 Das2d94d312015-11-24 23:21:05 -0800246 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
247 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700248
Saurav Das261c3002017-06-13 15:35:54 -0700249 private AtomicBoolean programmingScheduled = new AtomicBoolean();
250
Charles Chanc91c8782016-03-30 17:54:24 -0700251 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700252 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
253 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700254 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700255 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800256 public SegmentRoutingDeviceConfig createConfig() {
257 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700258 }
259 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800260
Charles Chanc91c8782016-03-30 17:54:24 -0700261 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700262 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
263 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700264 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800265 @Override
266 public SegmentRoutingAppConfig createConfig() {
267 return new SegmentRoutingAppConfig();
268 }
269 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800270
Charles Chan82f19972016-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 Ventre6b19e482016-11-07 16:21:04 -0800280
Charles Chanc91c8782016-03-30 17:54:24 -0700281 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700282 new ConfigFactory<ApplicationId, McastConfig>(
283 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700284 McastConfig.class, "multicast") {
285 @Override
286 public McastConfig createConfig() {
287 return new McastConfig();
288 }
289 };
290
Pier Ventre6b19e482016-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 Chan3ed34d82017-06-22 18:03:14 -0700301 private static final Object THREAD_SCHED_LOCK = new Object();
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700302 private static int numOfEventsQueued = 0;
303 private static int numOfEventsExecuted = 0;
sangho80f11cb2015-04-01 13:05:26 -0700304 private static int numOfHandlerExecution = 0;
305 private static int numOfHandlerScheduled = 0;
306
Charles Chan1963f4f2016-02-18 14:22:42 -0800307 /**
308 * Segment Routing App ID.
309 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800310 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan10b0fb72017-02-02 16:20:42 -0800311
Charles Chanb7f75ac2016-01-11 18:28:54 -0800312 /**
313 * The default VLAN ID assigned to the interfaces without subnet config.
314 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800315 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das7c305372015-10-28 12:39:42 -0700316
sangho80f11cb2015-04-01 13:05:26 -0700317 @Activate
318 protected void activate() {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800319 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700320
321 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700322 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700323 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700324 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700325 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700326 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700327 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700328 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700329 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700330
Charles Chan10b0fb72017-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 Hart54541d12016-04-12 15:39:44 -0700336 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700337 .withTimestampProvider((k, v) -> new WallClockTimestamp())
338 .build();
339
Saurav Das2d94d312015-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 Hart54541d12016-04-12 15:39:44 -0700345 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800346 .withTimestampProvider((k, v) -> new WallClockTimestamp())
347 .build();
348
sangho4a5c42a2015-05-20 22:16:38 -0700349 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
350 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700351 tunnelStore = tunnelMapBuilder
352 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700353 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700354 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700355 .build();
356
357 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
358 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700359 policyStore = policyMapBuilder
360 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700361 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700362 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700363 .build();
364
Saurav Dasc3604f12016-03-23 11:22:49 -0700365 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800366 "purgeOnDisconnection", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700367 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800368 "purgeOnDisconnection", "true");
Pier Luigib9632ba2017-01-12 18:14:58 -0800369 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
370 "requestInterceptsEnabled", "false");
Charles Chanc1909e12017-08-08 15:13:37 -0700371 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800372 "requestInterceptsEnabled", "false");
Charles Chan9597f8d2017-07-24 15:56:10 -0700373 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800374 "arpEnabled", "false");
Pier Luigi0ebeb312017-02-02 22:31:34 -0800375 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
376 "greedyLearningIpv6", "true");
Charles Chancf8ea472017-02-28 15:15:17 -0800377 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
378 "forceUnprovision", "true");
Charles Chanc7b73c72017-08-10 16:57:28 -0700379 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan4c95c0d2017-07-20 16:16:25 -0700380 "distributed", "true");
Charles Chan804772f2017-08-14 11:42:11 -0700381 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
382 "multihomingEnabled", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700383
Charles Chan2b078ae2015-10-14 11:24:40 -0700384 processor = new InternalPacketProcessor();
385 linkListener = new InternalLinkListener();
386 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700387 appCfgHandler = new AppConfigHandler(this);
388 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700389 mcastHandler = new McastHandler(this);
390 hostHandler = new HostHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700391 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800392 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventre6b19e482016-11-07 16:21:04 -0800393 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700394
Charles Chand6d25332016-02-26 22:19:52 -0800395 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700396 cfgService.registerConfigFactory(deviceConfigFactory);
397 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700398 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700399 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventre6b19e482016-11-07 16:21:04 -0800400 cfgService.registerConfigFactory(pwaasConfigFactory);
Charles Chan82ab1932016-01-30 23:22:37 -0800401 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700402 packetService.addProcessor(processor, PacketProcessor.director(2));
403 linkService.addListener(linkListener);
404 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700405 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700406 routeService.addListener(routeListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700407
408 cfgListener.configureNetwork();
409
sangho80f11cb2015-04-01 13:05:26 -0700410 log.info("Started");
411 }
412
Jonathan Hart54541d12016-04-12 15:39:44 -0700413 private KryoNamespace.Builder createSerializer() {
414 return new KryoNamespace.Builder()
415 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700416 .register(DestinationSetNextObjectiveStoreKey.class,
Charles Chan10b0fb72017-02-02 16:20:42 -0800417 VlanNextObjectiveStoreKey.class,
Saurav Das261c3002017-06-13 15:35:54 -0700418 DestinationSet.class,
419 NextNeighbors.class,
Jonathan Hart54541d12016-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 Chan82f19972016-05-17 13:13:55 -0700426 XConnectStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700427 );
428 }
429
sangho80f11cb2015-04-01 13:05:26 -0700430 @Deactivate
431 protected void deactivate() {
Charles Chan72f556a2015-10-05 17:50:33 -0700432 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700433 cfgService.unregisterConfigFactory(deviceConfigFactory);
434 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700435 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700436 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventre6b19e482016-11-07 16:21:04 -0800437 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chan72f556a2015-10-05 17:50:33 -0700438
Charles Chanfd48c222017-06-19 00:43:31 -0700439 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700440 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700441 linkService.removeListener(linkListener);
442 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700443 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700444 routeService.removeListener(routeListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700445
Charles Chan2e71ef32017-02-23 15:44:08 -0800446 neighbourResolutionService.unregisterNeighbourHandlers(appId);
447
sangho80f11cb2015-04-01 13:05:26 -0700448 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700449 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700450 deviceListener = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700451 groupHandlerMap.clear();
452
Saurav Das261c3002017-06-13 15:35:54 -0700453 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800454 vlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700455 portNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700456 tunnelStore.destroy();
457 policyStore.destroy();
sangho80f11cb2015-04-01 13:05:26 -0700458 log.info("Stopped");
459 }
460
sangho27462c62015-05-14 00:39:53 -0700461 @Override
462 public List<Tunnel> getTunnels() {
463 return tunnelHandler.getTunnels();
464 }
465
466 @Override
sanghobd812f82015-06-29 14:58:47 -0700467 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
468 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700469 }
470
471 @Override
sanghobd812f82015-06-29 14:58:47 -0700472 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-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");
sanghobd812f82015-06-29 14:58:47 -0700478 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700479 }
480 }
481 }
sanghobd812f82015-06-29 14:58:47 -0700482 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700483 }
484
485 @Override
sanghobd812f82015-06-29 14:58:47 -0700486 public PolicyHandler.Result removePolicy(Policy policy) {
487 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700488 }
489
490 @Override
sanghobd812f82015-06-29 14:58:47 -0700491 public PolicyHandler.Result createPolicy(Policy policy) {
492 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700493 }
494
495 @Override
496 public List<Policy> getPolicies() {
497 return policyHandler.getPolicies();
498 }
499
Saurav Das07c74602016-04-27 18:35:50 -0700500 @Override
501 public void rerouteNetwork() {
502 cfgListener.configureNetwork();
Saurav Das07c74602016-04-27 18:35:50 -0700503 }
504
Charles Chand7844e52016-10-20 17:02:44 -0700505 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800506 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
507 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Charles Chand7844e52016-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 Das62ae6792017-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 Das261c3002017-06-13 15:35:54 -0700525 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
526 if (dsNextObjStore != null) {
527 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700528 } else {
529 return ImmutableMap.of();
530 }
531 }
532
Saurav Dasfbe74572017-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
sangho80f1f892015-05-19 11:57:42 -0700541 /**
Ray Milkeyb85de082017-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 Das261c3002017-06-13 15:35:54 -0700560 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700561 * Used to keep track on MPLS group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700562 *
563 * @return next objective ID store
564 */
Saurav Das261c3002017-06-13 15:35:54 -0700565 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
566 dsNextObjStore() {
567 return dsNextObjStore;
Ray Milkeyb85de082017-04-05 09:42:04 -0700568 }
569
570 /**
Charles Chan53de91f2017-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 Milkeyb85de082017-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 Chan53de91f2017-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 Milkeyb85de082017-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 Das261c3002017-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 Ventre7a78de22016-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 /**
sangho80f1f892015-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 */
sangho27462c62015-05-14 00:39:53 -0700608 public Tunnel getTunnel(String tunnelId) {
609 return tunnelHandler.getTunnel(tunnelId);
610 }
611
Charles Chan90772a72017-02-08 15:52:08 -0800612 // TODO Consider moving these to InterfaceService
sangho80f11cb2015-04-01 13:05:26 -0700613 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800614 * Returns untagged VLAN configured on given connect point.
Charles Chan90772a72017-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.
sangho80f11cb2015-04-01 13:05:26 -0700618 *
Charles Chan10b0fb72017-02-02 16:20:42 -0800619 * @param connectPoint connect point
620 * @return untagged VLAN or null if not configured
sangho80f11cb2015-04-01 13:05:26 -0700621 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700622 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan10b0fb72017-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);
sangho80f11cb2015-04-01 13:05:26 -0700627 }
628
sangho27462c62015-05-14 00:39:53 -0700629 /**
Charles Chan90772a72017-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 Chan3ed34d82017-06-22 18:03:14 -0700638 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan90772a72017-02-08 15:52:08 -0800639 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
640 return interfaces.stream()
641 .map(Interface::vlanTagged)
Charles Chan3ed34d82017-06-22 18:03:14 -0700642 .flatMap(Set::stream)
Charles Chan90772a72017-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 Chan3ed34d82017-06-22 18:03:14 -0700655 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan90772a72017-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 Chand9265a32017-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 Chan3ed34d82017-06-22 18:03:14 -0700673 VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chand9265a32017-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 Chan3ed34d82017-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 Chanf0ae41e2017-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 Chan90772a72017-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 Chan3ed34d82017-06-22 18:03:14 -0700728 intf.vlanTagged().forEach(vlanTagged ->
729 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
730 );
Charles Chan90772a72017-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 Das2d94d312015-11-24 23:21:05 -0800739 * Returns the next objective ID for the given subnet prefix. It is expected
Charles Chan10b0fb72017-02-02 16:20:42 -0800740 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -0800741 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -0700742 *
743 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -0800744 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -0800745 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -0700746 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700747 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -0700748 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800749 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
750 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -0700751 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -0800752 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-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 Das2cb38292017-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 Das2d94d312015-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 Das2cb38292017-03-29 19:09:17 -0700768 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -0700769 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -0800770 */
771 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
772 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -0700773 TrafficSelector meta,
774 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -0800775 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
776 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -0700777 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -0800778 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800779 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
780 + " not found", deviceId);
781 return -1;
782 }
783 }
784
Saurav Das62ae6792017-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 Chan3ed34d82017-06-22 18:03:14 -0700791 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Das62ae6792017-05-15 15:34:25 -0700792 return groupHandlerMap.get(devId);
793 }
794
795 /**
Saurav Dasfbe74572017-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 Das62ae6792017-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 Chan3ed34d82017-06-22 18:03:14 -0700814 boolean isSeenLink(Link link) {
Saurav Das62ae6792017-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 Chan3ed34d82017-06-22 18:03:14 -0700825 void updateSeenLink(Link link, boolean up) {
Saurav Das62ae6792017-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 Chan3ed34d82017-06-22 18:03:14 -0700838 Boolean isSeenLinkUp(Link link) {
Saurav Das62ae6792017-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 Chan3ed34d82017-06-22 18:03:14 -0700847 private void purgeSeenLink(Link link) {
Saurav Das62ae6792017-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 Chan3ed34d82017-06-22 18:03:14 -0700861 private boolean isParallelLink(Link link) {
Saurav Das62ae6792017-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 Das261c3002017-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
sangho80f11cb2015-04-01 13:05:26 -0700936 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-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 Luigi37a35432017-02-01 13:50:04 -0800946
947 if (ethernet == null) {
948 return;
949 }
950
Saurav Das261c3002017-06-13 15:35:54 -0700951 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
952 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800953 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -0700954 log.warn("Received unexpected ARP packet on {}",
955 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -0700956 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800957 return;
sangho80f11cb2015-04-01 13:05:26 -0700958 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-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());
sangho80f11cb2015-04-01 13:05:26 -0700963 } else {
Charles Chand041ad82017-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 Ventre6b2c1b32016-12-09 17:26:04 -0800966 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700967 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800968 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
969 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -0800970 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -0800971 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-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 Das62ae6792017-05-15 15:34:25 -0700978 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -0700979 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-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 }
sangho80f11cb2015-04-01 13:05:26 -0700986 }
987 }
988 }
989
990 private class InternalLinkListener implements LinkListener {
991 @Override
992 public void event(LinkEvent event) {
Charles Chan0cff8aa2017-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 Vavilapalli64d96c12015-05-14 20:22:47 -0700996 log.debug("Event {} received from Link Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -0700997 scheduleEventHandlerIfNotScheduled(event);
998 }
999 }
1000 }
1001
1002 private class InternalDeviceListener implements DeviceListener {
sangho80f11cb2015-04-01 13:05:26 -07001003 @Override
1004 public void event(DeviceEvent event) {
sangho80f11cb2015-04-01 13:05:26 -07001005 switch (event.type()) {
1006 case DEVICE_ADDED:
Saurav Dasf0f592d2016-11-18 15:21:57 -08001007 case PORT_UPDATED:
1008 case PORT_ADDED:
sanghofb7c7292015-04-13 15:15:58 -07001009 case DEVICE_UPDATED:
1010 case DEVICE_AVAILABILITY_CHANGED:
Saurav Das62ae6792017-05-15 15:34:25 -07001011 log.trace("Event {} received from Device Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001012 scheduleEventHandlerIfNotScheduled(event);
1013 break;
1014 default:
1015 }
1016 }
1017 }
1018
Saurav Das2d94d312015-11-24 23:21:05 -08001019 @SuppressWarnings("rawtypes")
sangho80f11cb2015-04-01 13:05:26 -07001020 private void scheduleEventHandlerIfNotScheduled(Event event) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001021 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001022 eventQueue.add(event);
Srikanth Vavilapalli64d96c12015-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 Vavilapalli7cd16712015-05-04 09:48:09 -07001027 eventHandlerFuture = executorService
1028 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
1029 numOfHandlerScheduled++;
1030 }
Jonathan Hart54541d12016-04-12 15:39:44 -07001031 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001032 numOfEventsQueued,
1033 numOfHandlerScheduled);
sangho80f11cb2015-04-01 13:05:26 -07001034 }
sangho80f11cb2015-04-01 13:05:26 -07001035 }
1036
1037 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001038 @Override
sangho80f11cb2015-04-01 13:05:26 -07001039 public void run() {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001040 try {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001041 while (true) {
Saurav Das2d94d312015-11-24 23:21:05 -08001042 @SuppressWarnings("rawtypes")
Charles Chan3ed34d82017-06-22 18:03:14 -07001043 Event event;
1044 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001045 if (!eventQueue.isEmpty()) {
1046 event = eventQueue.poll();
1047 numOfEventsExecuted++;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001048 } else {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001049 numOfHandlerExecution++;
1050 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
1051 numOfHandlerExecution, numOfEventsExecuted);
1052 break;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001053 }
sanghofb7c7292015-04-13 15:15:58 -07001054 }
Charles Chan0cff8aa2017-03-29 16:39:05 -07001055 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1056 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Das62ae6792017-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 Vavilapalli64d96c12015-05-14 20:22:47 -07001060 processLinkAdded((Link) event.subject());
1061 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre6d593892016-09-13 21:33:40 -07001062 Link linkRemoved = (Link) event.subject();
Saurav Das62ae6792017-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 Ventre6d593892016-09-13 21:33:40 -07001071 if (linkRemoved.src().elementId() instanceof DeviceId &&
1072 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
Saurav Das62ae6792017-05-15 15:34:25 -07001073 purgeSeenLink(linkRemoved);
Pier Ventre6d593892016-09-13 21:33:40 -07001074 continue;
1075 }
1076 if (linkRemoved.dst().elementId() instanceof DeviceId &&
1077 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
Saurav Das62ae6792017-05-15 15:34:25 -07001078 purgeSeenLink(linkRemoved);
Pier Ventre6d593892016-09-13 21:33:40 -07001079 continue;
1080 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001081 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli64d96c12015-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 Das62af8802015-12-04 10:52:59 -08001085 DeviceId deviceId = ((Device) event.subject()).id();
1086 if (deviceService.isAvailable(deviceId)) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001087 log.info("Processing device event {} for available device {}",
1088 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001089 processDeviceAdded((Device) event.subject());
Saurav Dasc3604f12016-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 Dasf0f592d2016-11-18 15:21:57 -08001095 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd1872b02016-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 Das62ae6792017-05-15 15:34:25 -07001100 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd1872b02016-12-02 15:43:47 -08001101 ((DeviceEvent) event).subject().id(),
1102 ((DeviceEvent) event).port().number(),
1103 event.type());
Saurav Dasf0f592d2016-11-18 15:21:57 -08001104 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd1872b02016-12-02 15:43:47 -08001105 // these calls happen for every subsequent event
1106 // ports enabled, disabled, switch goes away, comes back
Saurav Dasf0f592d2016-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 Vavilapalli64d96c12015-05-14 20:22:47 -07001113 } else {
1114 log.warn("Unhandled event type: {}", event.type());
1115 }
sangho80f11cb2015-04-01 13:05:26 -07001116 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001117 } catch (Exception e) {
1118 log.error("SegmentRouting event handler "
1119 + "thread thrown an exception: {}", e);
sangho80f11cb2015-04-01 13:05:26 -07001120 }
sangho80f11cb2015-04-01 13:05:26 -07001121 }
1122 }
1123
sangho80f11cb2015-04-01 13:05:26 -07001124 private void processLinkAdded(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -07001125 log.info("** LINK ADDED {}", link.toString());
Saurav Das62ae6792017-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 Dasfe0b05e2017-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 Das62ae6792017-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 Vavilapalli7cd16712015-05-04 09:48:09 -07001145 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
1146 .deviceId());
1147 if (groupHandler != null) {
Saurav Das62ae6792017-05-15 15:34:25 -07001148 groupHandler.portUpForLink(link);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001149 } else {
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001150 // XXX revisit/cleanup
Srikanth Vavilapalli7cd16712015-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 Das62ae6792017-05-15 15:34:25 -07001159 groupHandler.portUpForLink(link);
sangho80f11cb2015-04-01 13:05:26 -07001160 }
1161 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001162
Saurav Das261c3002017-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 Das62ae6792017-05-15 15:34:25 -07001179 boolean seenBefore = isSeenLink(link);
1180 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null, link, null);
Saurav Das261c3002017-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 Das62ae6792017-05-15 15:34:25 -07001186 if (mastershipService.isLocalMaster(link.src().deviceId())) {
Saurav Das261c3002017-06-13 15:35:54 -07001187 if (!seenBefore && isParallelLink(link)) {
Saurav Das62ae6792017-05-15 15:34:25 -07001188 // if link seen first time, we need to ensure hash-groups have all ports
Saurav Das261c3002017-06-13 15:35:54 -07001189 log.debug("Attempting retryHash for paralled first-time link {}", link);
Saurav Das62ae6792017-05-15 15:34:25 -07001190 groupHandler.retryHash(link, false, true);
1191 } else {
1192 //seen before-link
1193 if (isParallelLink(link)) {
Saurav Das261c3002017-06-13 15:35:54 -07001194 log.debug("Attempting retryHash for paralled seen-before "
1195 + "link {}", link);
Saurav Das62ae6792017-05-15 15:34:25 -07001196 groupHandler.retryHash(link, false, false);
1197 }
1198 }
1199 }
Charles Chan72779502016-04-23 17:36:10 -07001200
1201 mcastHandler.init();
sangho80f11cb2015-04-01 13:05:26 -07001202 }
1203
1204 private void processLinkRemoved(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -07001205 log.info("** LINK REMOVED {}", link.toString());
Saurav Das62ae6792017-05-15 15:34:25 -07001206 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link, null, null);
1207
1208 // update local groupHandler stores
sangho2165d222015-05-01 09:38:25 -07001209 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
1210 if (groupHandler != null) {
Saurav Das62ae6792017-05-15 15:34:25 -07001211 if (mastershipService.isLocalMaster(link.src().deviceId()) &&
1212 isParallelLink(link)) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001213 log.debug("* retrying hash for parallel link removed:{}", link);
Saurav Das62ae6792017-05-15 15:34:25 -07001214 groupHandler.retryHash(link, true, false);
Saurav Dasfe0b05e2017-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 Das62ae6792017-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);
sangho2165d222015-05-01 09:38:25 -07001225 }
Charles Chan72779502016-04-23 17:36:10 -07001226
1227 mcastHandler.processLinkDown(link);
sangho80f11cb2015-04-01 13:05:26 -07001228 }
1229
1230 private void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001231 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-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 Chan319d1a22015-11-03 10:42:14 -08001238 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001239 log.warn("Device configuration unavailable. Device {} will be "
1240 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001241 return;
1242 }
Charles Chan72779502016-04-23 17:36:10 -07001243 processDeviceAddedInternal(device.id());
1244 }
1245
1246 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-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 Chan72779502016-04-23 17:36:10 -07001253 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1254 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001255 DefaultGroupHandler groupHandler;
1256 try {
1257 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001258 createGroupHandler(deviceId,
1259 appId,
1260 deviceConfiguration,
1261 linkService,
1262 flowObjectiveService,
1263 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001264 } catch (DeviceConfigNotFoundException e) {
1265 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1266 return;
1267 }
Charles Chan72779502016-04-23 17:36:10 -07001268 log.debug("updating groupHandlerMap with new config for device: {}",
1269 deviceId);
1270 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001271 }
Saurav Dasb149be12016-06-07 10:08:06 -07001272
Charles Chan72779502016-04-23 17:36:10 -07001273 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001274 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chandebfea32016-10-24 14:52:01 -07001275 hostHandler.init(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -07001276 xConnectHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001277 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001278 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001279 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001280 }
Charles Chan82ab1932016-01-30 23:22:37 -08001281
Charles Chandebfea32016-10-24 14:52:01 -07001282 appCfgHandler.init(deviceId);
1283 routeHandler.init(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001284 }
1285
Saurav Dasc3604f12016-03-23 11:22:49 -07001286 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001287 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001288 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1289 .forEach(entry -> {
Saurav Das261c3002017-06-13 15:35:54 -07001290 dsNextObjStore.remove(entry.getKey());
Saurav Dasc3604f12016-03-23 11:22:49 -07001291 });
Charles Chan10b0fb72017-02-02 16:20:42 -08001292 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001293 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001294 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001295 portNextObjStore.entrySet().stream()
1296 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001297 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Charles Chan17d75d82017-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 Dasfbe74572017-08-03 18:30:35 -07001302 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1303 if (gh != null) {
1304 gh.shutdown();
1305 }
Saurav Dasc3604f12016-03-23 11:22:49 -07001306 defaultRoutingHandler.purgeEcmpGraph(device.id());
Saurav Das62ae6792017-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 Chan72779502016-04-23 17:36:10 -07001312 mcastHandler.removeDevice(device.id());
Charles Chan82f19972016-05-17 13:13:55 -07001313 xConnectHandler.removeDevice(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -07001314 }
1315
Saurav Dasf0f592d2016-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 Dasf9332192017-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 Chan43be46b2017-02-26 22:59:35 -08001333 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001334 } else {
1335 log.info("Switchport {}/{} disabled..removing filters",
1336 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001337 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001338 }
Saurav Dasf0f592d2016-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 Das3fb28272017-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 Chan10b0fb72017-02-02 16:20:42 -08001348
Saurav Das3fb28272017-03-04 16:08:47 -08001349 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001350 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasf0f592d2016-11-18 15:21:57 -08001351 + "dev/port: {}/{}", device.id(), port.number());
1352 return;
1353 }
Saurav Das3fb28272017-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 Dasf0f592d2016-11-18 15:21:57 -08001363 }
1364
Saurav Das3fb28272017-03-04 16:08:47 -08001365 private void processEdgePort(Device device, Port port, VlanId vlanId,
1366 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001367 boolean portUp = port.isEnabled();
1368 if (portUp) {
Saurav Das3fb28272017-03-04 16:08:47 -08001369 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001370 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001371 } else {
Saurav Das3fb28272017-03-04 16:08:47 -08001372 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001373 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001374 }
1375
Srikanth Vavilapalli64505482015-04-21 13:04:13 -07001376 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sangho80f11cb2015-04-01 13:05:26 -07001377 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001378 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-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());
sangho80f11cb2015-04-01 13:05:26 -07001383 }
1384 }
sangho27462c62015-05-14 00:39:53 -07001385
Pier Ventreb6a7f342016-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 Chan2e71ef32017-02-23 15:44:08 -08001393 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001394 portToRegister,
1395 neighbourHandler,
1396 appId
1397 );
1398 }
1399
Charles Chan72f556a2015-10-05 17:50:33 -07001400 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001401 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001402 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001403
Charles Chanb7f75ac2016-01-11 18:28:54 -08001404 /**
1405 * Constructs the internal network config listener.
1406 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001407 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001408 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001409 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001410 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001411 }
1412
Charles Chanb7f75ac2016-01-11 18:28:54 -08001413 /**
1414 * Reads network config and initializes related data structure accordingly.
1415 */
Charles Chane7c61022015-10-07 14:21:45 -07001416 public void configureNetwork() {
Saurav Das261c3002017-06-13 15:35:54 -07001417 if (deviceConfiguration == null) {
1418 deviceConfiguration = new DeviceConfiguration(srManager);
1419 } else {
1420 deviceConfiguration.updateConfig();
1421 }
Charles Chane7c61022015-10-07 14:21:45 -07001422
Charles Chan46fdfaf2016-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 Chane7c61022015-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 Das261c3002017-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 Chane7c61022015-10-07 14:21:45 -07001439 }
Charles Chan72779502016-04-23 17:36:10 -07001440 mcastHandler.init();
Charles Chane7c61022015-10-07 14:21:45 -07001441 }
1442
Charles Chan72f556a2015-10-05 17:50:33 -07001443 @Override
1444 public void event(NetworkConfigEvent event) {
Charles Chan82ab1932016-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 Chan68363b12017-06-26 15:25:09 -07001449 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan82ab1932016-01-30 23:22:37 -08001450 configureNetwork();
1451 break;
1452 case CONFIG_UPDATED:
Charles Chan68363b12017-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 Chan82ab1932016-01-30 23:22:37 -08001467 // TODO support dynamic configuration
1468 break;
1469 default:
1470 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001471 }
Charles Chan82ab1932016-01-30 23:22:37 -08001472 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chan82f19972016-05-17 13:13:55 -07001473 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan82ab1932016-01-30 23:22:37 -08001474 switch (event.type()) {
1475 case CONFIG_ADDED:
Charles Chan82f19972016-05-17 13:13:55 -07001476 appCfgHandler.processAppConfigAdded(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001477 break;
1478 case CONFIG_UPDATED:
Charles Chan82f19972016-05-17 13:13:55 -07001479 appCfgHandler.processAppConfigUpdated(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001480 break;
1481 case CONFIG_REMOVED:
Charles Chan82f19972016-05-17 13:13:55 -07001482 appCfgHandler.processAppConfigRemoved(event);
1483 break;
1484 default:
1485 break;
1486 }
Charles Chandebfea32016-10-24 14:52:01 -07001487 configureNetwork();
Charles Chan82f19972016-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 Chan82ab1932016-01-30 23:22:37 -08001499 break;
1500 default:
1501 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001502 }
Pier Ventre6b19e482016-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 Chan72f556a2015-10-05 17:50:33 -07001518 }
1519 }
Saurav Das261c3002017-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 Chan72f556a2015-10-05 17:50:33 -07001531 }
Charles Chanf4586112015-11-09 16:37:23 -08001532
1533 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001534 @Override
1535 public void event(HostEvent event) {
Charles Chanf4586112015-11-09 16:37:23 -08001536 switch (event.type()) {
1537 case HOST_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001538 hostHandler.processHostAddedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001539 break;
1540 case HOST_MOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001541 hostHandler.processHostMovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001542 break;
1543 case HOST_REMOVED:
Charles Chand9265a32017-06-16 15:19:24 -07001544 hostHandler.processHostRemovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001545 break;
1546 case HOST_UPDATED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001547 hostHandler.processHostUpdatedEvent(event);
Charles Chanf4586112015-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 Chanc91c8782016-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 Chan1eaf4802016-04-18 13:44:03 -07001561 mcastHandler.processSourceAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001562 break;
1563 case SINK_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001564 mcastHandler.processSinkAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001565 break;
1566 case SINK_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001567 mcastHandler.processSinkRemoved(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001568 break;
1569 case ROUTE_ADDED:
1570 case ROUTE_REMOVED:
1571 default:
1572 break;
1573 }
1574 }
1575 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001576
Charles Chandebfea32016-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 Das62ae6792017-05-15 15:34:25 -07001595
sangho80f11cb2015-04-01 13:05:26 -07001596}