blob: d57a681f45865069d1defeafc71b7f95651e168c [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
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -070018import com.fasterxml.jackson.databind.node.ObjectNode;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -080019import com.google.common.collect.HashMultimap;
20import com.google.common.collect.ImmutableMap;
21import com.google.common.collect.Maps;
22import com.google.common.collect.Multimap;
Charles Chan9640c812017-08-23 13:55:39 -070023import com.google.common.collect.Sets;
sanghob35a6192015-04-01 13:05:26 -070024import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
27import org.apache.felix.scr.annotations.Reference;
28import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho1e575652015-05-14 00:39:53 -070029import org.apache.felix.scr.annotations.Service;
sanghob35a6192015-04-01 13:05:26 -070030import org.onlab.packet.Ethernet;
Pier Ventre735b8c82016-12-02 08:16:05 -080031import org.onlab.packet.ICMP6;
Charles Chanc42e84e2015-10-20 16:24:19 -070032import org.onlab.packet.IPv4;
Pier Ventre10bd8d12016-11-26 21:05:22 -080033import org.onlab.packet.IPv6;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070034import org.onlab.packet.IpAddress;
Charles Chanc42e84e2015-10-20 16:24:19 -070035import org.onlab.packet.IpPrefix;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070036import org.onlab.packet.VlanId;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070037import org.onlab.util.KryoNamespace;
Saurav Das80980c72016-03-23 11:22:49 -070038import org.onosproject.cfg.ComponentConfigService;
sanghob35a6192015-04-01 13:05:26 -070039import org.onosproject.core.ApplicationId;
40import org.onosproject.core.CoreService;
41import org.onosproject.event.Event;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070042import org.onosproject.mastership.MastershipService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080043import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070044import org.onosproject.net.Device;
45import org.onosproject.net.DeviceId;
Charles Chan9640c812017-08-23 13:55:39 -070046import org.onosproject.net.Host;
47import org.onosproject.net.HostId;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070048import org.onosproject.net.Link;
49import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080050import org.onosproject.net.PortNumber;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070051import org.onosproject.net.config.ConfigException;
Charles Chand6832882015-10-05 17:50:33 -070052import org.onosproject.net.config.ConfigFactory;
53import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070054import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070055import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070056import org.onosproject.net.config.basics.InterfaceConfig;
57import org.onosproject.net.config.basics.McastConfig;
Charles Chand6832882015-10-05 17:50:33 -070058import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070059import org.onosproject.net.device.DeviceEvent;
60import org.onosproject.net.device.DeviceListener;
61import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080062import org.onosproject.net.flow.TrafficSelector;
63import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070064import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan68aa62d2015-11-09 16:37:23 -080065import org.onosproject.net.host.HostEvent;
66import org.onosproject.net.host.HostListener;
Pier Ventre10bd8d12016-11-26 21:05:22 -080067import org.onosproject.net.host.HostService;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070068import org.onosproject.net.host.InterfaceIpAddress;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070069import org.onosproject.net.intf.Interface;
70import org.onosproject.net.intf.InterfaceService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080071import org.onosproject.net.link.LinkEvent;
72import org.onosproject.net.link.LinkListener;
73import org.onosproject.net.link.LinkService;
Charles Chand55e84d2016-03-30 17:54:24 -070074import org.onosproject.net.mcast.McastEvent;
75import org.onosproject.net.mcast.McastListener;
76import org.onosproject.net.mcast.MulticastRouteService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070077import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080078import org.onosproject.net.packet.InboundPacket;
79import org.onosproject.net.packet.PacketContext;
80import org.onosproject.net.packet.PacketProcessor;
81import org.onosproject.net.packet.PacketService;
Pier Ventre42287df2016-11-09 14:17:26 -080082import org.onosproject.net.topology.PathService;
Charles Chand55e84d2016-03-30 17:54:24 -070083import org.onosproject.net.topology.TopologyService;
Charles Chan9640c812017-08-23 13:55:39 -070084import org.onosproject.routeservice.ResolvedRoute;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070085import org.onosproject.routeservice.RouteEvent;
86import org.onosproject.routeservice.RouteListener;
87import org.onosproject.routeservice.RouteService;
Charles Chand55e84d2016-03-30 17:54:24 -070088import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
89import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventref34966c2016-11-07 16:21:04 -080090import org.onosproject.segmentrouting.config.PwaasConfig;
Pier Ventref34966c2016-11-07 16:21:04 -080091import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070092import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -070093import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -070094import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -070095import org.onosproject.segmentrouting.grouphandler.DestinationSet;
96import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -070097import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
98import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070099import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700100import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Charles Chand2990362016-04-18 13:44:03 -0700101import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -0800102import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -0700103import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700104import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700105import org.onosproject.store.service.EventuallyConsistentMap;
106import org.onosproject.store.service.EventuallyConsistentMapBuilder;
107import org.onosproject.store.service.StorageService;
108import org.onosproject.store.service.WallClockTimestamp;
sanghob35a6192015-04-01 13:05:26 -0700109import org.slf4j.Logger;
110import org.slf4j.LoggerFactory;
111
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800112import java.util.Collections;
113import java.util.HashSet;
114import java.util.List;
115import java.util.Map;
116import java.util.Map.Entry;
117import java.util.Optional;
118import java.util.Set;
119import java.util.concurrent.ConcurrentHashMap;
120import java.util.concurrent.ConcurrentLinkedQueue;
121import java.util.concurrent.Executors;
122import java.util.concurrent.ScheduledExecutorService;
123import java.util.concurrent.ScheduledFuture;
124import java.util.concurrent.TimeUnit;
125import java.util.concurrent.atomic.AtomicBoolean;
126import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -0700127
Charles Chan3e783d02016-02-26 22:19:52 -0800128import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800129import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700130import static org.onlab.util.Tools.groupedThreads;
Charles Chan3e783d02016-02-26 22:19:52 -0800131
Charles Chane849c192016-01-11 18:28:54 -0800132/**
133 * Segment routing manager.
134 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700135@Service
136@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700137public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700138
Charles Chan2c15aca2016-11-09 20:51:44 -0800139 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan2fde6d42017-08-23 14:46:43 -0700140 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sanghob35a6192015-04-01 13:05:26 -0700141
142 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700143 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700144
145 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800146 private NeighbourResolutionService neighbourResolutionService;
147
148 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800149 public PathService pathService;
150
151 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700152 CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700153
154 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700155 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700156
157 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700158 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700159
160 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700161 DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700162
163 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800164 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700165
166 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700167 LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700168
Charles Chan5270ed02016-01-30 23:22:37 -0800169 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800170 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700171
172 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800173 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700174
175 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
176 MulticastRouteService multicastRouteService;
177
178 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
179 TopologyService topologyService;
180
181 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700182 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800183
184 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800185 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800186
Saurav Das80980c72016-03-23 11:22:49 -0700187 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800188 public InterfaceService interfaceService;
189
Charles Chan03a73e02016-10-24 14:52:01 -0700190 ArpHandler arpHandler = null;
191 IcmpHandler icmpHandler = null;
192 IpHandler ipHandler = null;
193 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700194 ApplicationId appId;
195 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700196
Charles Chan03a73e02016-10-24 14:52:01 -0700197 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700198 private TunnelHandler tunnelHandler = null;
199 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700200 private InternalPacketProcessor processor = null;
201 private InternalLinkListener linkListener = null;
202 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700203 private AppConfigHandler appCfgHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700204 XConnectHandler xConnectHandler = null;
Charles Chand2990362016-04-18 13:44:03 -0700205 private McastHandler mcastHandler = null;
Charles Chan65238242017-06-22 18:03:14 -0700206 private HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800207 private RouteHandler routeHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800208 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventref34966c2016-11-07 16:21:04 -0800209 private L2TunnelHandler l2TunnelHandler = null;
sanghob35a6192015-04-01 13:05:26 -0700210 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan5270ed02016-01-30 23:22:37 -0800211 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700212 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
213 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700214 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sanghob35a6192015-04-01 13:05:26 -0700215
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700216 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700217 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sanghob35a6192015-04-01 13:05:26 -0700218
Saurav Das4ce45962015-11-24 23:21:05 -0800219 @SuppressWarnings("unused")
sanghob35a6192015-04-01 13:05:26 -0700220 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800221 @SuppressWarnings("rawtypes")
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700222 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chan68aa62d2015-11-09 16:37:23 -0800223 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chane849c192016-01-11 18:28:54 -0800224 new ConcurrentHashMap<>();
225 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700226 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700227 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800228 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700229 EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
230 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800231 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700232 * Per device next objective ID store with (device id + vlanid) as key.
233 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800234 */
Ray Milkeye4afdb52017-04-05 09:42:04 -0700235 EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800236 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800237 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700238 * Per device next objective ID store with (device id + port + treatment + meta) as key.
239 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chane849c192016-01-11 18:28:54 -0800240 */
Ray Milkeye4afdb52017-04-05 09:42:04 -0700241 EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800242 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800243
Saurav Dasc88d4662017-05-15 15:34:25 -0700244 // Local store for all links seen and their present status, used for
245 // optimized routing. The existence of the link in the keys is enough to know
246 // if the link has been "seen-before" by this instance of the controller.
247 // The boolean value indicates if the link is currently up or not.
248 // XXX Currently the optimized routing logic depends on "forgetting" a link
249 // when a switch goes down, but "remembering" it when only the link goes down.
250 // Consider changing this logic so we can use the Link Service instead of
251 // a local cache.
252 private Map<Link, Boolean> seenLinks = new ConcurrentHashMap<>();
253
Saurav Das4ce45962015-11-24 23:21:05 -0800254 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
255 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700256
Saurav Das7bcbe702017-06-13 15:35:54 -0700257 private AtomicBoolean programmingScheduled = new AtomicBoolean();
258
Charles Chand55e84d2016-03-30 17:54:24 -0700259 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700260 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
261 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700262 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700263 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800264 public SegmentRoutingDeviceConfig createConfig() {
265 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700266 }
267 };
Pier Ventref34966c2016-11-07 16:21:04 -0800268
Charles Chand55e84d2016-03-30 17:54:24 -0700269 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700270 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
271 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700272 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800273 @Override
274 public SegmentRoutingAppConfig createConfig() {
275 return new SegmentRoutingAppConfig();
276 }
277 };
Pier Ventref34966c2016-11-07 16:21:04 -0800278
Charles Chanfc5c7802016-05-17 13:13:55 -0700279 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
280 new ConfigFactory<ApplicationId, XConnectConfig>(
281 SubjectFactories.APP_SUBJECT_FACTORY,
282 XConnectConfig.class, "xconnect") {
283 @Override
284 public XConnectConfig createConfig() {
285 return new XConnectConfig();
286 }
287 };
Pier Ventref34966c2016-11-07 16:21:04 -0800288
Charles Chand55e84d2016-03-30 17:54:24 -0700289 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700290 new ConfigFactory<ApplicationId, McastConfig>(
291 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700292 McastConfig.class, "multicast") {
293 @Override
294 public McastConfig createConfig() {
295 return new McastConfig();
296 }
297 };
298
Pier Ventref34966c2016-11-07 16:21:04 -0800299 private final ConfigFactory<ApplicationId, PwaasConfig> pwaasConfigFactory =
300 new ConfigFactory<ApplicationId, PwaasConfig>(
301 SubjectFactories.APP_SUBJECT_FACTORY,
302 PwaasConfig.class, "pwaas") {
303 @Override
304 public PwaasConfig createConfig() {
305 return new PwaasConfig();
306 }
307 };
308
Charles Chan65238242017-06-22 18:03:14 -0700309 private static final Object THREAD_SCHED_LOCK = new Object();
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700310 private static int numOfEventsQueued = 0;
311 private static int numOfEventsExecuted = 0;
sanghob35a6192015-04-01 13:05:26 -0700312 private static int numOfHandlerExecution = 0;
313 private static int numOfHandlerScheduled = 0;
314
Charles Chan116188d2016-02-18 14:22:42 -0800315 /**
316 * Segment Routing App ID.
317 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800318 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan59cc16d2017-02-02 16:20:42 -0800319
Charles Chane849c192016-01-11 18:28:54 -0800320 /**
321 * The default VLAN ID assigned to the interfaces without subnet config.
322 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800323 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700324
sanghob35a6192015-04-01 13:05:26 -0700325 @Activate
326 protected void activate() {
Charles Chan2c15aca2016-11-09 20:51:44 -0800327 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700328
329 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700330 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700331 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700332 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700333 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700334 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700335 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700336 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700337 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700338
Charles Chan59cc16d2017-02-02 16:20:42 -0800339 log.debug("Creating EC map vlannextobjectivestore");
340 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
341 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
342 vlanNextObjStore = vlanNextObjMapBuilder
343 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700344 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700345 .withTimestampProvider((k, v) -> new WallClockTimestamp())
346 .build();
347
Saurav Das4ce45962015-11-24 23:21:05 -0800348 log.debug("Creating EC map subnetnextobjectivestore");
349 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
350 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
351 portNextObjStore = portNextObjMapBuilder
352 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700353 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800354 .withTimestampProvider((k, v) -> new WallClockTimestamp())
355 .build();
356
sangho0b2b6d12015-05-20 22:16:38 -0700357 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
358 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700359 tunnelStore = tunnelMapBuilder
360 .withName("tunnelstore")
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
365 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
366 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700367 policyStore = policyMapBuilder
368 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700369 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700370 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700371 .build();
372
Saurav Das80980c72016-03-23 11:22:49 -0700373 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800374 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700375 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800376 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800377 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
378 "requestInterceptsEnabled", "false");
Charles Chand3baaba2017-08-08 15:13:37 -0700379 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800380 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700381 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800382 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800383 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
384 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800385 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
386 "forceUnprovision", "true");
Charles Chanef624ed2017-08-10 16:57:28 -0700387 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan73316522017-07-20 16:16:25 -0700388 "distributed", "true");
Charles Chan35a32322017-08-14 11:42:11 -0700389 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
390 "multihomingEnabled", "true");
Charles Chan807d87a2017-11-29 19:54:20 -0800391 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
392 "staleLinkAge", "15000");
393 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
394 "allowDuplicateIps", "false");
Saurav Das80980c72016-03-23 11:22:49 -0700395
Charles Chanb8e10c82015-10-14 11:24:40 -0700396 processor = new InternalPacketProcessor();
397 linkListener = new InternalLinkListener();
398 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700399 appCfgHandler = new AppConfigHandler(this);
400 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700401 mcastHandler = new McastHandler(this);
402 hostHandler = new HostHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700403 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800404 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventref34966c2016-11-07 16:21:04 -0800405 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700406
Charles Chan3e783d02016-02-26 22:19:52 -0800407 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700408 cfgService.registerConfigFactory(deviceConfigFactory);
409 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700410 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700411 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800412 cfgService.registerConfigFactory(pwaasConfigFactory);
Charles Chan5270ed02016-01-30 23:22:37 -0800413 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700414 packetService.addProcessor(processor, PacketProcessor.director(2));
415 linkService.addListener(linkListener);
416 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700417 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700418 routeService.addListener(routeListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700419
420 cfgListener.configureNetwork();
421
sanghob35a6192015-04-01 13:05:26 -0700422 log.info("Started");
423 }
424
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700425 private KryoNamespace.Builder createSerializer() {
426 return new KryoNamespace.Builder()
427 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700428 .register(DestinationSetNextObjectiveStoreKey.class,
Charles Chan59cc16d2017-02-02 16:20:42 -0800429 VlanNextObjectiveStoreKey.class,
Saurav Das7bcbe702017-06-13 15:35:54 -0700430 DestinationSet.class,
431 NextNeighbors.class,
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700432 Tunnel.class,
433 DefaultTunnel.class,
434 Policy.class,
435 TunnelPolicy.class,
436 Policy.Type.class,
437 PortNextObjectiveStoreKey.class,
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700438 XConnectStoreKey.class,
439 DefaultL2Tunnel.class,
440 DefaultL2TunnelPolicy.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700441 );
442 }
443
sanghob35a6192015-04-01 13:05:26 -0700444 @Deactivate
445 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700446 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700447 cfgService.unregisterConfigFactory(deviceConfigFactory);
448 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700449 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700450 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800451 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chand6832882015-10-05 17:50:33 -0700452
Charles Chanc7a8a682017-06-19 00:43:31 -0700453 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700454 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700455 linkService.removeListener(linkListener);
456 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700457 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700458 routeService.removeListener(routeListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700459
Charles Chan0aa674e2017-02-23 15:44:08 -0800460 neighbourResolutionService.unregisterNeighbourHandlers(appId);
461
sanghob35a6192015-04-01 13:05:26 -0700462 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700463 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700464 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700465 groupHandlerMap.clear();
466
Saurav Das7bcbe702017-06-13 15:35:54 -0700467 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800468 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700469 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700470 tunnelStore.destroy();
471 policyStore.destroy();
sanghob35a6192015-04-01 13:05:26 -0700472 log.info("Stopped");
473 }
474
sangho1e575652015-05-14 00:39:53 -0700475 @Override
476 public List<Tunnel> getTunnels() {
477 return tunnelHandler.getTunnels();
478 }
479
480 @Override
sangho71abe1b2015-06-29 14:58:47 -0700481 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
482 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700483 }
484
485 @Override
sangho71abe1b2015-06-29 14:58:47 -0700486 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700487 for (Policy policy: policyHandler.getPolicies()) {
488 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
489 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
490 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
491 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700492 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700493 }
494 }
495 }
sangho71abe1b2015-06-29 14:58:47 -0700496 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700497 }
498
499 @Override
sangho71abe1b2015-06-29 14:58:47 -0700500 public PolicyHandler.Result removePolicy(Policy policy) {
501 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700502 }
503
504 @Override
sangho71abe1b2015-06-29 14:58:47 -0700505 public PolicyHandler.Result createPolicy(Policy policy) {
506 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700507 }
508
509 @Override
510 public List<Policy> getPolicies() {
511 return policyHandler.getPolicies();
512 }
513
Saurav Das59232cf2016-04-27 18:35:50 -0700514 @Override
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700515 public List<DefaultL2Tunnel> getL2Tunnels() {
516 return l2TunnelHandler.getL2Tunnels();
517 }
518
519 @Override
520 public List<DefaultL2TunnelPolicy> getL2Policies() {
521 return l2TunnelHandler.getL2Policies();
522 }
523
524 @Override
525 public L2TunnelHandler.Result addPseudowire(String tunnelId, String pwLabel, String cP1,
526 String cP1InnerVlan, String cP1OuterVlan, String cP2,
527 String cP2InnerVlan, String cP2OuterVlan,
528 String mode, String sdTag) {
529
530 PwaasConfig config = cfgService.getConfig(appId(), PwaasConfig.class);
531 if (config == null) {
532 log.warn("Configuration for Pwaas class could not be found!");
533 return L2TunnelHandler.Result.CONFIG_NOT_FOUND;
534 }
535
536 ObjectNode object = config.addPseudowire(tunnelId, pwLabel,
537 cP1, cP1InnerVlan, cP1OuterVlan,
538 cP2, cP2InnerVlan, cP2OuterVlan,
539 mode, sdTag);
540 if (object == null) {
541 log.warn("Could not add pseudowire to the configuration!");
542 return L2TunnelHandler.Result.ADDITION_ERROR;
543 }
544
545 // inform everyone about the valid change in the pw configuration
546 cfgService.applyConfig(appId(), PwaasConfig.class, object);
547 return L2TunnelHandler.Result.SUCCESS;
548 }
549
550 @Override
551 public L2TunnelHandler.Result removePseudowire(String pwId) {
552
553 PwaasConfig config = cfgService.getConfig(appId(), PwaasConfig.class);
554 if (config == null) {
555 log.warn("Configuration for Pwaas class could not be found!");
556 return L2TunnelHandler.Result.CONFIG_NOT_FOUND;
557 }
558
559 ObjectNode object = config.removePseudowire(pwId);
560 if (object == null) {
561 log.warn("Could not delete pseudowire from configuration!");
562 return L2TunnelHandler.Result.REMOVAL_ERROR;
563 }
564
565 // sanity check, this should never fail since we removed a pw
566 // and we always check when we update the configuration
567 config.isValid();
568
569 // inform everyone
570 cfgService.applyConfig(appId(), PwaasConfig.class, object);
571
572 return L2TunnelHandler.Result.SUCCESS;
573 }
574
575 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700576 public void rerouteNetwork() {
577 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700578 }
579
Charles Chanc81c45b2016-10-20 17:02:44 -0700580 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800581 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
582 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800583 deviceConfiguration.getRouters().forEach(device ->
584 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700585 return deviceSubnetMap;
586 }
587
Saurav Dasc88d4662017-05-15 15:34:25 -0700588
589 @Override
590 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
591 if (defaultRoutingHandler != null) {
592 return defaultRoutingHandler.getCurrentEmcpSpgMap();
593 } else {
594 return null;
595 }
596 }
597
598 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700599 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
600 if (dsNextObjStore != null) {
601 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700602 } else {
603 return ImmutableMap.of();
604 }
605 }
606
Saurav Dasceccf242017-08-03 18:30:35 -0700607 @Override
608 public void verifyGroups(DeviceId id) {
609 DefaultGroupHandler gh = groupHandlerMap.get(id);
610 if (gh != null) {
611 gh.triggerBucketCorrector();
612 }
613 }
614
sanghof9d0bf12015-05-19 11:57:42 -0700615 /**
Ray Milkeye4afdb52017-04-05 09:42:04 -0700616 * Extracts the application ID from the manager.
617 *
618 * @return application ID
619 */
620 public ApplicationId appId() {
621 return appId;
622 }
623
624 /**
625 * Returns the device configuration.
626 *
627 * @return device configuration
628 */
629 public DeviceConfiguration deviceConfiguration() {
630 return deviceConfiguration;
631 }
632
633 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700634 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700635 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700636 *
637 * @return next objective ID store
638 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700639 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
640 dsNextObjStore() {
641 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700642 }
643
644 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700645 * Per device next objective ID store with (device id + vlanid) as key.
646 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700647 *
648 * @return vlan next object store
649 */
650 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
651 return vlanNextObjStore;
652 }
653
654 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700655 * Per device next objective ID store with (device id + port + treatment + meta) as key.
656 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700657 *
658 * @return port next object store.
659 */
660 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
661 return portNextObjStore;
662 }
663
664 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700665 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
666 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700667 *
668 * @return MPLS-ECMP value
669 */
670 public boolean getMplsEcmp() {
671 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
672 .getConfig(this.appId, SegmentRoutingAppConfig.class);
673 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
674 }
675
676 /**
sanghof9d0bf12015-05-19 11:57:42 -0700677 * Returns the tunnel object with the tunnel ID.
678 *
679 * @param tunnelId Tunnel ID
680 * @return Tunnel reference
681 */
sangho1e575652015-05-14 00:39:53 -0700682 public Tunnel getTunnel(String tunnelId) {
683 return tunnelHandler.getTunnel(tunnelId);
684 }
685
Charles Chan7ffd81f2017-02-08 15:52:08 -0800686 // TODO Consider moving these to InterfaceService
sanghob35a6192015-04-01 13:05:26 -0700687 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800688 * Returns untagged VLAN configured on given connect point.
Charles Chan7ffd81f2017-02-08 15:52:08 -0800689 * <p>
690 * Only returns the first match if there are multiple untagged VLAN configured
691 * on the connect point.
sanghob35a6192015-04-01 13:05:26 -0700692 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800693 * @param connectPoint connect point
694 * @return untagged VLAN or null if not configured
sanghob35a6192015-04-01 13:05:26 -0700695 */
Charles Chan65238242017-06-22 18:03:14 -0700696 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800697 return interfaceService.getInterfacesByPort(connectPoint).stream()
698 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
699 .map(Interface::vlanUntagged)
700 .findFirst().orElse(null);
sanghob35a6192015-04-01 13:05:26 -0700701 }
702
sangho1e575652015-05-14 00:39:53 -0700703 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800704 * Returns tagged VLAN configured on given connect point.
705 * <p>
706 * Returns all matches if there are multiple tagged VLAN configured
707 * on the connect point.
708 *
709 * @param connectPoint connect point
710 * @return tagged VLAN or empty set if not configured
711 */
Charles Chan65238242017-06-22 18:03:14 -0700712 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800713 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
714 return interfaces.stream()
715 .map(Interface::vlanTagged)
Charles Chan65238242017-06-22 18:03:14 -0700716 .flatMap(Set::stream)
Charles Chan7ffd81f2017-02-08 15:52:08 -0800717 .collect(Collectors.toSet());
718 }
719
720 /**
721 * Returns native VLAN configured on given connect point.
722 * <p>
723 * Only returns the first match if there are multiple native VLAN configured
724 * on the connect point.
725 *
726 * @param connectPoint connect point
727 * @return native VLAN or null if not configured
728 */
Charles Chan65238242017-06-22 18:03:14 -0700729 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800730 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
731 return interfaces.stream()
732 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
733 .map(Interface::vlanNative)
734 .findFirst()
735 .orElse(null);
736 }
737
738 /**
Charles Chanf9a52702017-06-16 15:19:24 -0700739 * Returns internal VLAN for untagged hosts on given connect point.
740 * <p>
741 * The internal VLAN is either vlan-untagged for an access port,
742 * or vlan-native for a trunk port.
743 *
744 * @param connectPoint connect point
745 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
746 */
Charles Chan65238242017-06-22 18:03:14 -0700747 VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chanf9a52702017-06-16 15:19:24 -0700748 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
749 VlanId nativeVlanId = getNativeVlanId(connectPoint);
750 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
751 }
752
753 /**
Charles Chan65238242017-06-22 18:03:14 -0700754 * Returns optional pair device ID of given device.
755 *
756 * @param deviceId device ID
757 * @return optional pair device ID. Might be empty if pair device is not configured
758 */
759 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
760 SegmentRoutingDeviceConfig deviceConfig =
761 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
762 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
763 }
764 /**
765 * Returns optional pair device local port of given device.
766 *
767 * @param deviceId device ID
768 * @return optional pair device ID. Might be empty if pair device is not configured
769 */
770 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
771 SegmentRoutingDeviceConfig deviceConfig =
772 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
773 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
774 }
775
776 /**
Charles Chan2fde6d42017-08-23 14:46:43 -0700777 * Determine if current instance is the master of given connect point.
778 *
779 * @param cp connect point
780 * @return true if current instance is the master of given connect point
781 */
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700782 public boolean isMasterOf(ConnectPoint cp) {
Charles Chan2fde6d42017-08-23 14:46:43 -0700783 boolean isMaster = mastershipService.isLocalMaster(cp.deviceId());
784 if (!isMaster) {
785 log.debug(NOT_MASTER, cp);
786 }
787 return isMaster;
788 }
789
790 /**
Charles Chan9640c812017-08-23 13:55:39 -0700791 * Returns locations of given resolved route.
792 *
793 * @param resolvedRoute resolved route
794 * @return locations of nexthop. Might be empty if next hop is not found
795 */
796 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
797 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
798 return Optional.ofNullable(hostService.getHost(hostId))
799 .map(Host::locations).orElse(Sets.newHashSet())
800 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
801 }
802
803 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800804 * Returns vlan port map of given device.
805 *
806 * @param deviceId device id
807 * @return vlan-port multimap
808 */
809 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
810 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
811
812 interfaceService.getInterfaces().stream()
813 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
814 .forEach(intf -> {
815 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -0700816 intf.vlanTagged().forEach(vlanTagged ->
817 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
818 );
Charles Chan7ffd81f2017-02-08 15:52:08 -0800819 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
820 });
821 vlanPortMap.removeAll(VlanId.NONE);
822
823 return vlanPortMap;
824 }
825
826 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800827 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800828 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700829 *
830 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800831 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800832 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700833 */
Charles Chan65238242017-06-22 18:03:14 -0700834 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700835 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800836 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
837 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700838 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800839 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800840 + "device {} not found", deviceId);
841 return -1;
842 }
843 }
844
845 /**
846 * Returns the next objective ID for the given portNumber, given the treatment.
847 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700848 * would result in different objectives. If the next object does not exist,
849 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800850 *
851 * @param deviceId Device ID
852 * @param portNum port number on device for which NextObjective is queried
853 * @param treatment the actions to apply on the packets (should include outport)
854 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700855 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700856 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800857 */
858 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
859 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700860 TrafficSelector meta,
861 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -0800862 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
863 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -0700864 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -0800865 } else {
Charles Chane849c192016-01-11 18:28:54 -0800866 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
867 + " not found", deviceId);
868 return -1;
869 }
870 }
871
Saurav Dasc88d4662017-05-15 15:34:25 -0700872 /**
873 * Returns the group handler object for the specified device id.
874 *
875 * @param devId the device identifier
876 * @return the groupHandler object for the device id, or null if not found
877 */
Charles Chan65238242017-06-22 18:03:14 -0700878 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700879 return groupHandlerMap.get(devId);
880 }
881
882 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700883 * Returns the default routing handler object.
884 *
885 * @return the default routing handler object
886 */
887 public DefaultRoutingHandler getRoutingHandler() {
888 return defaultRoutingHandler;
889 }
890
891 /**
Saurav Dasc88d4662017-05-15 15:34:25 -0700892 * Returns true if this controller instance has seen this link before. The
893 * link may not be currently up, but as long as the link had been seen before
894 * this method will return true. The one exception is when the link was
895 * indeed seen before, but this controller instance was forced to forget it
896 * by a call to purgeSeenLink method.
897 *
898 * @param link the infrastructure link being queried
899 * @return true if this controller instance has seen this link before
900 */
Charles Chan65238242017-06-22 18:03:14 -0700901 boolean isSeenLink(Link link) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700902 return seenLinks.containsKey(link);
903 }
904
905 /**
906 * Updates the seen link store. Updates can be for links that are currently
907 * available or not.
908 *
909 * @param link the link to update in the seen-link local store
910 * @param up the status of the link, true if up, false if down
911 */
Charles Chan65238242017-06-22 18:03:14 -0700912 void updateSeenLink(Link link, boolean up) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700913 seenLinks.put(link, up);
914 }
915
916 /**
917 * Returns the status of a seen-link (up or down). If the link has not
918 * been seen-before, a null object is returned.
919 *
920 * @param link the infrastructure link being queried
921 * @return null if the link was not seen-before;
922 * true if the seen-link is up;
923 * false if the seen-link is down
924 */
Charles Chan65238242017-06-22 18:03:14 -0700925 Boolean isSeenLinkUp(Link link) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700926 return seenLinks.get(link);
927 }
928
929 /**
930 * Makes this controller instance forget a previously seen before link.
931 *
932 * @param link the infrastructure link to purge
933 */
Charles Chan65238242017-06-22 18:03:14 -0700934 private void purgeSeenLink(Link link) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700935 seenLinks.remove(link);
936 }
937
938 /**
939 * Returns the status of a link as parallel link. A parallel link
940 * is defined as a link which has common src and dst switches as another
941 * seen-link that is currently enabled. It is not necessary for the link being
942 * queried to be a seen-link.
943 *
944 * @param link the infrastructure link being queried
945 * @return true if a seen-link exists that is up, and shares the
946 * same src and dst switches as the link being queried
947 */
Charles Chan65238242017-06-22 18:03:14 -0700948 private boolean isParallelLink(Link link) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700949 for (Entry<Link, Boolean> seen : seenLinks.entrySet()) {
950 Link seenLink = seen.getKey();
951 if (seenLink.equals(link)) {
952 continue;
953 }
954 if (seenLink.src().deviceId().equals(link.src().deviceId()) &&
955 seenLink.dst().deviceId().equals(link.dst().deviceId()) &&
956 seen.getValue()) {
957 return true;
958 }
959 }
960 return false;
961 }
962
Saurav Das7bcbe702017-06-13 15:35:54 -0700963 /**
964 * Returns true if the link being queried is a bidirectional link. A bidi
965 * link is defined as a link, whose reverse link - ie. the link in the reverse
966 * direction - has been seen-before and is up. It is not necessary for the link
967 * being queried to be a seen-link.
968 *
969 * @param link the infrastructure link being queried
970 * @return true if another unidirectional link exists in the reverse direction,
971 * has been seen-before and is up
972 */
973 public boolean isBidirectional(Link link) {
974 Link reverseLink = linkService.getLink(link.dst(), link.src());
975 if (reverseLink == null) {
976 return false;
977 }
978 Boolean result = isSeenLinkUp(reverseLink);
979 if (result == null) {
980 return false;
981 }
982 return result.booleanValue();
983 }
984
985 /**
986 * Determines if the given link should be avoided in routing calculations
987 * by policy or design.
988 *
989 * @param link the infrastructure link being queried
990 * @return true if link should be avoided
991 */
992 public boolean avoidLink(Link link) {
993 // XXX currently only avoids all pair-links. In the future can be
994 // extended to avoid any generic link
995 DeviceId src = link.src().deviceId();
996 PortNumber srcPort = link.src().port();
997 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(src)) {
998 log.warn("Device {} not configured..cannot avoid link {}", src, link);
999 return false;
1000 }
1001 DeviceId pairDev;
1002 PortNumber pairLocalPort, pairRemotePort = null;
1003 try {
1004 pairDev = deviceConfiguration.getPairDeviceId(src);
1005 pairLocalPort = deviceConfiguration.getPairLocalPort(src);
1006 if (pairDev != null) {
1007 pairRemotePort = deviceConfiguration.getPairLocalPort(pairDev);
1008 }
1009 } catch (DeviceConfigNotFoundException e) {
1010 log.warn("Pair dev for dev {} not configured..cannot avoid link {}",
1011 src, link);
1012 return false;
1013 }
1014
1015 if (srcPort.equals(pairLocalPort) &&
1016 link.dst().deviceId().equals(pairDev) &&
1017 link.dst().port().equals(pairRemotePort)) {
1018 return true;
1019 }
1020 return false;
1021 }
1022
sanghob35a6192015-04-01 13:05:26 -07001023 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -07001024 @Override
1025 public void process(PacketContext context) {
1026
1027 if (context.isHandled()) {
1028 return;
1029 }
1030
1031 InboundPacket pkt = context.inPacket();
1032 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -08001033
1034 if (ethernet == null) {
1035 return;
1036 }
1037
Saurav Das7bcbe702017-06-13 15:35:54 -07001038 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1039 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001040 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001041 log.warn("Received unexpected ARP packet on {}",
1042 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -07001043 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -08001044 return;
sanghob35a6192015-04-01 13:05:26 -07001045 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -08001046 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1047 //ipHandler.addToPacketBuffer(ipv4Packet);
1048 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1049 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001050 } else {
Charles Chan50035632017-01-13 17:20:44 -08001051 // NOTE: We don't support IP learning at this moment so this
1052 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -08001053 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001054 }
Pier Ventre10bd8d12016-11-26 21:05:22 -08001055 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1056 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -08001057 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -08001058 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -08001059 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1060 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1061 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1062 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1063 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1064 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -07001065 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -07001066 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -08001067 }
1068 } else {
1069 // NOTE: We don't support IP learning at this moment so this
1070 // is not necessary. Also it causes duplication of DHCPv6 packets.
1071 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1072 }
sanghob35a6192015-04-01 13:05:26 -07001073 }
1074 }
1075 }
1076
1077 private class InternalLinkListener implements LinkListener {
1078 @Override
1079 public void event(LinkEvent event) {
Charles Chanb1f8c762017-03-29 16:39:05 -07001080 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1081 event.type() == LinkEvent.Type.LINK_UPDATED ||
1082 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001083 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -07001084 scheduleEventHandlerIfNotScheduled(event);
1085 }
1086 }
1087 }
1088
1089 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -07001090 @Override
1091 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -07001092 switch (event.type()) {
1093 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -08001094 case PORT_UPDATED:
1095 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -07001096 case DEVICE_UPDATED:
1097 case DEVICE_AVAILABILITY_CHANGED:
Saurav Dasc88d4662017-05-15 15:34:25 -07001098 log.trace("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -07001099 scheduleEventHandlerIfNotScheduled(event);
1100 break;
1101 default:
1102 }
1103 }
1104 }
1105
Saurav Das4ce45962015-11-24 23:21:05 -08001106 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -07001107 private void scheduleEventHandlerIfNotScheduled(Event event) {
Charles Chan65238242017-06-22 18:03:14 -07001108 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001109 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001110 numOfEventsQueued++;
1111
1112 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
1113 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001114 eventHandlerFuture = executorService
1115 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
1116 numOfHandlerScheduled++;
1117 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -07001118 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001119 numOfEventsQueued,
1120 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -07001121 }
sanghob35a6192015-04-01 13:05:26 -07001122 }
1123
1124 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001125 @Override
sanghob35a6192015-04-01 13:05:26 -07001126 public void run() {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001127 try {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001128 while (true) {
Saurav Das4ce45962015-11-24 23:21:05 -08001129 @SuppressWarnings("rawtypes")
Charles Chan65238242017-06-22 18:03:14 -07001130 Event event;
1131 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001132 if (!eventQueue.isEmpty()) {
1133 event = eventQueue.poll();
1134 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001135 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001136 numOfHandlerExecution++;
1137 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
1138 numOfHandlerExecution, numOfEventsExecuted);
1139 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001140 }
sangho20eff1d2015-04-13 15:15:58 -07001141 }
Charles Chanb1f8c762017-03-29 16:39:05 -07001142 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1143 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001144 // Note: do not update seenLinks here, otherwise every
1145 // link, even one seen for the first time, will be appear
1146 // to be a previously seen link
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001147 processLinkAdded((Link) event.subject());
1148 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre2c515312016-09-13 21:33:40 -07001149 Link linkRemoved = (Link) event.subject();
Saurav Dasc88d4662017-05-15 15:34:25 -07001150 if (linkRemoved.type() == Link.Type.DIRECT) {
1151 updateSeenLink(linkRemoved, false);
1152 }
1153 // device availability check helps to ensure that
1154 // multiple link-removed events are actually treated as a
1155 // single switch removed event. purgeSeenLink is necessary
1156 // so we do rerouting (instead of rehashing) when switch
1157 // comes back.
Pier Ventre2c515312016-09-13 21:33:40 -07001158 if (linkRemoved.src().elementId() instanceof DeviceId &&
1159 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001160 purgeSeenLink(linkRemoved);
Pier Ventre2c515312016-09-13 21:33:40 -07001161 continue;
1162 }
1163 if (linkRemoved.dst().elementId() instanceof DeviceId &&
1164 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001165 purgeSeenLink(linkRemoved);
Pier Ventre2c515312016-09-13 21:33:40 -07001166 continue;
1167 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001168 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001169 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1170 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1171 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001172 DeviceId deviceId = ((Device) event.subject()).id();
1173 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001174 log.info("Processing device event {} for available device {}",
1175 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001176 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -07001177 } else {
1178 log.info("Processing device event {} for unavailable device {}",
1179 event.type(), ((Device) event.subject()).id());
1180 processDeviceRemoved((Device) event.subject());
1181 }
Saurav Das1a129a02016-11-18 15:21:57 -08001182 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001183 // typically these calls come when device is added first time
1184 // so port filtering rules are handled at the device_added event.
1185 // port added calls represent all ports on the device,
1186 // enabled or not.
Saurav Dasc88d4662017-05-15 15:34:25 -07001187 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001188 ((DeviceEvent) event).subject().id(),
1189 ((DeviceEvent) event).port().number(),
1190 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -08001191 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001192 // these calls happen for every subsequent event
1193 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -08001194 log.info("** PORT UPDATED {}/{} -> {}",
1195 event.subject(),
1196 ((DeviceEvent) event).port(),
1197 event.type());
1198 processPortUpdated(((Device) event.subject()),
1199 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001200 } else {
1201 log.warn("Unhandled event type: {}", event.type());
1202 }
sanghob35a6192015-04-01 13:05:26 -07001203 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001204 } catch (Exception e) {
1205 log.error("SegmentRouting event handler "
1206 + "thread thrown an exception: {}", e);
sanghob35a6192015-04-01 13:05:26 -07001207 }
sanghob35a6192015-04-01 13:05:26 -07001208 }
1209 }
1210
sanghob35a6192015-04-01 13:05:26 -07001211 private void processLinkAdded(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001212 log.info("** LINK ADDED {}", link.toString());
Saurav Dasc88d4662017-05-15 15:34:25 -07001213 if (link.type() != Link.Type.DIRECT) {
1214 // NOTE: A DIRECT link might be transiently marked as INDIRECT
1215 // if BDDP is received before LLDP. We can safely ignore that
1216 // until the LLDP is received and the link is marked as DIRECT.
1217 log.info("Ignore link {}->{}. Link type is {} instead of DIRECT.",
1218 link.src(), link.dst(), link.type());
1219 return;
1220 }
Saurav Das9df5b7c2017-08-14 16:44:43 -07001221 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
1222 updateSeenLink(link, true);
1223 // XXX revisit - what about devicePortMap
1224 log.warn("Source device of this link is not configured.. "
1225 + "not processing further");
1226 return;
1227 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001228
1229 //Irrespective of whether the local is a MASTER or not for this device,
1230 //create group handler instance and push default TTP flow rules if needed,
1231 //as in a multi-instance setup, instances can initiate groups for any device.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001232 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
1233 .deviceId());
1234 if (groupHandler != null) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001235 groupHandler.portUpForLink(link);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001236 } else {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001237 // XXX revisit/cleanup
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001238 Device device = deviceService.getDevice(link.src().deviceId());
1239 if (device != null) {
1240 log.warn("processLinkAdded: Link Added "
1241 + "Notification without Device Added "
1242 + "event, still handling it");
1243 processDeviceAdded(device);
1244 groupHandler = groupHandlerMap.get(link.src()
1245 .deviceId());
Saurav Dasc88d4662017-05-15 15:34:25 -07001246 groupHandler.portUpForLink(link);
sanghob35a6192015-04-01 13:05:26 -07001247 }
1248 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001249
Saurav Das7bcbe702017-06-13 15:35:54 -07001250 /*// process link only if it is bidirectional
1251 if (!isBidirectional(link)) {
1252 log.debug("Link not bidirectional.. waiting for other direction "
1253 + "src {} --> dst {} ", link.dst(), link.src());
1254 // note that if we are not processing for routing, it should at least
1255 // be considered a seen-link
1256 updateSeenLink(link, true);
1257 return;
1258 }
1259 TO DO this ensure that rehash is still done correctly even if link is
1260 not processed for rerouting - perhaps rehash in both directions when
1261 it ultimately becomes bidi?
1262 */
1263
1264 log.debug("Starting optimized route population process for link "
1265 + "{} --> {}", link.src(), link.dst());
Saurav Dasc88d4662017-05-15 15:34:25 -07001266 boolean seenBefore = isSeenLink(link);
1267 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null, link, null);
Saurav Das7bcbe702017-06-13 15:35:54 -07001268
1269 // It's possible that linkUp causes no route-path change as ECMP graph does
1270 // not change if the link is a parallel link (same src-dst as another link.
1271 // However we still need to update ECMP hash groups to include new buckets
1272 // for the link that has come up.
Saurav Dasc88d4662017-05-15 15:34:25 -07001273 if (mastershipService.isLocalMaster(link.src().deviceId())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001274 if (!seenBefore && isParallelLink(link)) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001275 // if link seen first time, we need to ensure hash-groups have all ports
Saurav Das7bcbe702017-06-13 15:35:54 -07001276 log.debug("Attempting retryHash for paralled first-time link {}", link);
Saurav Dasc88d4662017-05-15 15:34:25 -07001277 groupHandler.retryHash(link, false, true);
1278 } else {
1279 //seen before-link
1280 if (isParallelLink(link)) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001281 log.debug("Attempting retryHash for paralled seen-before "
1282 + "link {}", link);
Saurav Dasc88d4662017-05-15 15:34:25 -07001283 groupHandler.retryHash(link, false, false);
1284 }
1285 }
1286 }
Charles Chan2199c302016-04-23 17:36:10 -07001287
1288 mcastHandler.init();
sanghob35a6192015-04-01 13:05:26 -07001289 }
1290
1291 private void processLinkRemoved(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001292 log.info("** LINK REMOVED {}", link.toString());
Saurav Dasc88d4662017-05-15 15:34:25 -07001293 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link, null, null);
1294
1295 // update local groupHandler stores
sangho834e4b02015-05-01 09:38:25 -07001296 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
1297 if (groupHandler != null) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001298 if (mastershipService.isLocalMaster(link.src().deviceId()) &&
1299 isParallelLink(link)) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001300 log.debug("* retrying hash for parallel link removed:{}", link);
Saurav Dasc88d4662017-05-15 15:34:25 -07001301 groupHandler.retryHash(link, true, false);
Saurav Das9df5b7c2017-08-14 16:44:43 -07001302 } else {
1303 log.debug("Not attempting retry-hash for link removed: {} .. {}", link,
1304 (mastershipService.isLocalMaster(link.src().deviceId()))
1305 ? "not parallel" : "not master");
Saurav Dasc88d4662017-05-15 15:34:25 -07001306 }
1307 // ensure local stores are updated
1308 groupHandler.portDown(link.src().port());
1309 } else {
1310 log.warn("group handler not found for dev:{} when removing link: {}",
1311 link.src().deviceId(), link);
sangho834e4b02015-05-01 09:38:25 -07001312 }
Charles Chan2199c302016-04-23 17:36:10 -07001313
1314 mcastHandler.processLinkDown(link);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001315 l2TunnelHandler.processLinkDown(link);
sanghob35a6192015-04-01 13:05:26 -07001316 }
1317
1318 private void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001319 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001320
1321 // NOTE: Punt ARP/NDP even when the device is not configured.
1322 // Host learning without network config is required for CORD config generator.
1323 routingRulePopulator.populateIpPunts(device.id());
1324 routingRulePopulator.populateArpNdpPunts(device.id());
1325
Charles Chan0b4e6182015-11-03 10:42:14 -08001326 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001327 log.warn("Device configuration unavailable. Device {} will be "
1328 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001329 return;
1330 }
Charles Chan2199c302016-04-23 17:36:10 -07001331 processDeviceAddedInternal(device.id());
1332 }
1333
1334 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001335 // Irrespective of whether the local is a MASTER or not for this device,
1336 // we need to create a SR-group-handler instance. This is because in a
1337 // multi-instance setup, any instance can initiate forwarding/next-objectives
1338 // for any switch (even if this instance is a SLAVE or not even connected
1339 // to the switch). To handle this, a default-group-handler instance is necessary
1340 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001341 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1342 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001343 DefaultGroupHandler groupHandler;
1344 try {
1345 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001346 createGroupHandler(deviceId,
1347 appId,
1348 deviceConfiguration,
1349 linkService,
1350 flowObjectiveService,
1351 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001352 } catch (DeviceConfigNotFoundException e) {
1353 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1354 return;
1355 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001356 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001357 deviceId);
1358 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001359 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001360
Charles Chan2199c302016-04-23 17:36:10 -07001361 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001362 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan03a73e02016-10-24 14:52:01 -07001363 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001364 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001365 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001366 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001367 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001368 }
Charles Chan5270ed02016-01-30 23:22:37 -08001369
Charles Chan03a73e02016-10-24 14:52:01 -07001370 appCfgHandler.init(deviceId);
1371 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001372 }
1373
Saurav Das80980c72016-03-23 11:22:49 -07001374 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001375 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001376 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1377 .forEach(entry -> {
Saurav Das7bcbe702017-06-13 15:35:54 -07001378 dsNextObjStore.remove(entry.getKey());
Saurav Das80980c72016-03-23 11:22:49 -07001379 });
Charles Chan59cc16d2017-02-02 16:20:42 -08001380 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001381 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001382 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001383 portNextObjStore.entrySet().stream()
1384 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001385 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Charles Chaned3742352017-06-15 00:44:51 -07001386
1387 seenLinks.keySet().removeIf(key -> key.src().deviceId().equals(device.id()) ||
1388 key.dst().deviceId().equals(device.id()));
1389
Saurav Dasceccf242017-08-03 18:30:35 -07001390 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1391 if (gh != null) {
1392 gh.shutdown();
1393 }
Saurav Das80980c72016-03-23 11:22:49 -07001394 defaultRoutingHandler.purgeEcmpGraph(device.id());
Saurav Dasc88d4662017-05-15 15:34:25 -07001395 // Note that a switch going down is associated with all of its links
1396 // going down as well, but it is treated as a single switch down event
1397 // while the link-downs are ignored.
1398 defaultRoutingHandler
1399 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Charles Chan2199c302016-04-23 17:36:10 -07001400 mcastHandler.removeDevice(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001401 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -07001402 }
1403
Saurav Das1a129a02016-11-18 15:21:57 -08001404 private void processPortUpdated(Device device, Port port) {
1405 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1406 log.warn("Device configuration uploading. Not handling port event for"
1407 + "dev: {} port: {}", device.id(), port.number());
1408 return;
1409 }
Saurav Das018605f2017-02-18 14:05:44 -08001410
1411 if (!mastershipService.isLocalMaster(device.id())) {
1412 log.debug("Not master for dev:{} .. not handling port updated event"
1413 + "for port {}", device.id(), port.number());
1414 return;
1415 }
1416
1417 // first we handle filtering rules associated with the port
1418 if (port.isEnabled()) {
1419 log.info("Switchport {}/{} enabled..programming filters",
1420 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001421 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001422 } else {
1423 log.info("Switchport {}/{} disabled..removing filters",
1424 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001425 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001426 }
Saurav Das1a129a02016-11-18 15:21:57 -08001427
1428 // portUpdated calls are for ports that have gone down or up. For switch
1429 // to switch ports, link-events should take care of any re-routing or
1430 // group editing necessary for port up/down. Here we only process edge ports
1431 // that are already configured.
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001432 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1433 VlanId untaggedVlan = getUntaggedVlanId(cp);
1434 VlanId nativeVlan = getNativeVlanId(cp);
1435 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001436
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001437 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001438 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das1a129a02016-11-18 15:21:57 -08001439 + "dev/port: {}/{}", device.id(), port.number());
1440 return;
1441 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001442 if (untaggedVlan != null) {
1443 processEdgePort(device, port, untaggedVlan, true);
1444 }
1445 if (nativeVlan != null) {
1446 processEdgePort(device, port, nativeVlan, true);
1447 }
1448 if (!taggedVlans.isEmpty()) {
1449 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1450 }
Saurav Das1a129a02016-11-18 15:21:57 -08001451 }
1452
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001453 private void processEdgePort(Device device, Port port, VlanId vlanId,
1454 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001455 boolean portUp = port.isEnabled();
1456 if (portUp) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001457 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001458 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001459 } else {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001460 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001461 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001462 }
1463
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001464 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001465 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001466 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001467 } else {
1468 log.warn("Group handler not found for dev:{}. Not handling edge port"
1469 + " {} event for port:{}", device.id(),
1470 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001471 }
1472 }
sangho1e575652015-05-14 00:39:53 -07001473
Charles Chan27fe1a52017-10-20 16:06:55 -07001474 private void createOrUpdateDeviceConfiguration() {
1475 if (deviceConfiguration == null) {
1476 deviceConfiguration = new DeviceConfiguration(this);
1477 } else {
1478 deviceConfiguration.updateConfig();
1479 }
1480 }
1481
Pier Ventre10bd8d12016-11-26 21:05:22 -08001482 /**
1483 * Registers the given connect point with the NRS, this is necessary
1484 * to receive the NDP and ARP packets from the NRS.
1485 *
1486 * @param portToRegister connect point to register
1487 */
1488 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001489 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001490 portToRegister,
1491 neighbourHandler,
1492 appId
1493 );
1494 }
1495
Charles Chand6832882015-10-05 17:50:33 -07001496 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001497 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001498 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001499
Charles Chane849c192016-01-11 18:28:54 -08001500 /**
1501 * Constructs the internal network config listener.
1502 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001503 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001504 */
Charles Chan65238242017-06-22 18:03:14 -07001505 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001506 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001507 }
1508
Charles Chane849c192016-01-11 18:28:54 -08001509 /**
1510 * Reads network config and initializes related data structure accordingly.
1511 */
Charles Chan4636be02015-10-07 14:21:45 -07001512 public void configureNetwork() {
Charles Chan27fe1a52017-10-20 16:06:55 -07001513 createOrUpdateDeviceConfiguration();
Charles Chan4636be02015-10-07 14:21:45 -07001514
Charles Chan2c15aca2016-11-09 20:51:44 -08001515 arpHandler = new ArpHandler(srManager);
1516 icmpHandler = new IcmpHandler(srManager);
1517 ipHandler = new IpHandler(srManager);
1518 routingRulePopulator = new RoutingRulePopulator(srManager);
1519 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001520
1521 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1522 groupHandlerMap, tunnelStore);
1523 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1524 flowObjectiveService,
1525 tunnelHandler, policyStore);
Saurav Das7bcbe702017-06-13 15:35:54 -07001526 // add a small delay to absorb multiple network config added notifications
1527 if (!programmingScheduled.get()) {
1528 programmingScheduled.set(true);
1529 executorService.schedule(new ConfigChange(), PROGRAM_DELAY,
1530 TimeUnit.SECONDS);
Charles Chan4636be02015-10-07 14:21:45 -07001531 }
Charles Chan2199c302016-04-23 17:36:10 -07001532 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001533 }
1534
Charles Chand6832882015-10-05 17:50:33 -07001535 @Override
1536 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001537 // TODO move this part to NetworkConfigEventHandler
1538 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1539 switch (event.type()) {
1540 case CONFIG_ADDED:
Charles Chan278ce832017-06-26 15:25:09 -07001541 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan5270ed02016-01-30 23:22:37 -08001542 configureNetwork();
1543 break;
1544 case CONFIG_UPDATED:
Charles Chan278ce832017-06-26 15:25:09 -07001545 log.info("Segment Routing Config updated for {}", event.subject());
Charles Chan27fe1a52017-10-20 16:06:55 -07001546 createOrUpdateDeviceConfiguration();
Charles Chan278ce832017-06-26 15:25:09 -07001547 // TODO support dynamic configuration
1548 break;
1549 default:
1550 break;
1551 }
1552 } else if (event.configClass().equals(InterfaceConfig.class)) {
1553 switch (event.type()) {
1554 case CONFIG_ADDED:
1555 log.info("Interface Config added for {}", event.subject());
1556 configureNetwork();
1557 break;
1558 case CONFIG_UPDATED:
1559 log.info("Interface Config updated for {}", event.subject());
Charles Chan27fe1a52017-10-20 16:06:55 -07001560 createOrUpdateDeviceConfiguration();
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001561
1562 // Following code will be uncommented when [CORD-634] is fully implemented.
1563 // [CORD-634] Add dynamic config support for interfaces
1564 updateInterface((InterfaceConfig) event.config().get(),
1565 (InterfaceConfig) event.prevConfig().get());
Charles Chan5270ed02016-01-30 23:22:37 -08001566 // TODO support dynamic configuration
1567 break;
1568 default:
1569 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001570 }
Charles Chan5270ed02016-01-30 23:22:37 -08001571 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001572 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001573 switch (event.type()) {
1574 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001575 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001576 break;
1577 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001578 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001579 break;
1580 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001581 appCfgHandler.processAppConfigRemoved(event);
1582 break;
1583 default:
1584 break;
1585 }
Charles Chan03a73e02016-10-24 14:52:01 -07001586 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001587 } else if (event.configClass().equals(XConnectConfig.class)) {
1588 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1589 switch (event.type()) {
1590 case CONFIG_ADDED:
1591 xConnectHandler.processXConnectConfigAdded(event);
1592 break;
1593 case CONFIG_UPDATED:
1594 xConnectHandler.processXConnectConfigUpdated(event);
1595 break;
1596 case CONFIG_REMOVED:
1597 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001598 break;
1599 default:
1600 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001601 }
Pier Ventref34966c2016-11-07 16:21:04 -08001602 } else if (event.configClass().equals(PwaasConfig.class)) {
1603 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1604 switch (event.type()) {
1605 case CONFIG_ADDED:
1606 l2TunnelHandler.processPwaasConfigAdded(event);
1607 break;
1608 case CONFIG_UPDATED:
1609 l2TunnelHandler.processPwaasConfigUpdated(event);
1610 break;
1611 case CONFIG_REMOVED:
1612 l2TunnelHandler.processPwaasConfigRemoved(event);
1613 break;
1614 default:
1615 break;
1616 }
Charles Chand6832882015-10-05 17:50:33 -07001617 }
1618 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001619
1620 private final class ConfigChange implements Runnable {
1621 @Override
1622 public void run() {
1623 programmingScheduled.set(false);
1624 for (Device device : deviceService.getDevices()) {
1625 processDeviceAdded(device);
1626 }
1627 defaultRoutingHandler.startPopulationProcess();
1628 }
1629 }
Charles Chand6832882015-10-05 17:50:33 -07001630 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001631
1632 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001633 @Override
1634 public void event(HostEvent event) {
Charles Chan68aa62d2015-11-09 16:37:23 -08001635 switch (event.type()) {
1636 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001637 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001638 break;
1639 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001640 hostHandler.processHostMovedEvent(event);
Charles Chan2fde6d42017-08-23 14:46:43 -07001641 routeHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001642 break;
1643 case HOST_REMOVED:
Charles Chanf9a52702017-06-16 15:19:24 -07001644 hostHandler.processHostRemovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001645 break;
1646 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001647 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001648 break;
1649 default:
1650 log.warn("Unsupported host event type: {}", event.type());
1651 break;
1652 }
1653 }
1654 }
1655
Charles Chand55e84d2016-03-30 17:54:24 -07001656 private class InternalMcastListener implements McastListener {
1657 @Override
1658 public void event(McastEvent event) {
1659 switch (event.type()) {
1660 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001661 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001662 break;
1663 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001664 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001665 break;
1666 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001667 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001668 break;
1669 case ROUTE_ADDED:
1670 case ROUTE_REMOVED:
1671 default:
1672 break;
1673 }
1674 }
1675 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001676
Charles Chan03a73e02016-10-24 14:52:01 -07001677 private class InternalRouteEventListener implements RouteListener {
1678 @Override
1679 public void event(RouteEvent event) {
1680 switch (event.type()) {
1681 case ROUTE_ADDED:
1682 routeHandler.processRouteAdded(event);
1683 break;
1684 case ROUTE_UPDATED:
1685 routeHandler.processRouteUpdated(event);
1686 break;
1687 case ROUTE_REMOVED:
1688 routeHandler.processRouteRemoved(event);
1689 break;
Charles Chan2fde6d42017-08-23 14:46:43 -07001690 case ALTERNATIVE_ROUTES_CHANGED:
1691 routeHandler.processAlternativeRoutesChanged(event);
1692 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001693 default:
1694 break;
1695 }
1696 }
1697 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001698
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001699 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1700 try {
1701 Set<Interface> intfs = conf.getInterfaces();
1702 Set<Interface> prevIntfs = prevConf.getInterfaces();
1703
1704 // Now we only handle one interface config at each port.
1705 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1706 log.warn("Interface update aborted - one at a time is allowed, " +
1707 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1708 return;
1709 }
1710
1711 Interface intf = intfs.stream().findFirst().get();
1712 Interface prevIntf = prevIntfs.stream().findFirst().get();
1713
1714 DeviceId deviceId = intf.connectPoint().deviceId();
1715 PortNumber portNum = intf.connectPoint().port();
1716
1717 if (!mastershipService.isLocalMaster(deviceId)) {
1718 log.debug("CONFIG_UPDATED event for interfaces should be " +
1719 "handled by master node for device {}", deviceId);
1720 return;
1721 }
1722
1723 removeSubnetConfig(prevIntf.connectPoint(),
1724 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1725 new HashSet<>(intf.ipAddressesList())));
1726
1727 if (prevIntf.vlanNative() != VlanId.NONE && !intf.vlanNative().equals(prevIntf.vlanNative())) {
1728 // RemoveVlanNative
1729 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1730 }
1731
1732 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1733 // RemoveVlanTagged
1734 prevIntf.vlanTagged().stream().filter(i -> !intf.vlanTagged().contains(i)).forEach(
1735 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, false)
1736 );
1737 }
1738
1739 if (prevIntf.vlanUntagged() != VlanId.NONE && !intf.vlanUntagged().equals(prevIntf.vlanUntagged())) {
1740 // RemoveVlanUntagged
1741 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1742 }
1743
1744 if (intf.vlanNative() != VlanId.NONE && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1745 // AddVlanNative
1746 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1747 }
1748
1749 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1750 // AddVlanTagged
1751 intf.vlanTagged().stream().filter(i -> !prevIntf.vlanTagged().contains(i)).forEach(
1752 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, true)
1753 );
1754 }
1755
1756 if (intf.vlanUntagged() != VlanId.NONE && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())) {
1757 // AddVlanUntagged
1758 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1759 }
1760 addSubnetConfig(prevIntf.connectPoint(),
1761 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1762 new HashSet<>(prevIntf.ipAddressesList())));
1763 } catch (ConfigException e) {
1764 log.error("Error in configuration");
1765 }
1766 }
1767
1768 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1769 VlanId vlanId, boolean pushVlan, boolean install) {
1770 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1771 if (grpHandler == null) {
1772 log.warn("Failed to retrieve group handler for device {}", deviceId);
1773 return;
1774 }
1775
1776 // Update filtering objective for a single port
1777 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1778
1779 // Update filtering objective for multicast ingress port
1780 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1781
1782 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1783
1784 if (nextId != -1 && !install) {
1785 // Update next objective for a single port as an output port
1786 // Remove a single port from L2FG
1787 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1788 // Remove L2 Bridging rule and L3 Unicast rule to the host
1789 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1790 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1791 // only if there is no port configured on that VLAN ID
1792 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1793 // Remove broadcast forwarding rule for the VLAN
1794 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1795 // Remove L2FG for VLAN
1796 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1797 } else {
1798 // Remove L2IG of the port
1799 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1800 }
1801 } else if (install) {
1802 if (nextId != -1) {
1803 // Add a single port to L2FG
1804 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1805 } else {
1806 // Create L2FG for VLAN
1807 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1808 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1809 }
1810 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1811 } else {
1812 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1813 }
1814 }
1815
1816 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1817 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1818 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1819
1820 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1821 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1822 .filter(intf -> !intf.connectPoint().equals(cp))
1823 .flatMap(intf -> intf.ipAddressesList().stream())
1824 .collect(Collectors.toSet());
1825 // 1. Partial subnet population
1826 // Remove routing rules for removed subnet from previous configuration,
1827 // which does not also exist in other interfaces in the same device
1828 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1829 .map(InterfaceIpAddress::subnetAddress)
1830 .collect(Collectors.toSet());
1831
1832 defaultRoutingHandler.revokeSubnet(
1833 ipPrefixSet.stream()
1834 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1835 .collect(Collectors.toSet()));
1836
1837 // 2. Interface IP punts
1838 // Remove IP punts for old Intf address
1839 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1840 .map(InterfaceIpAddress::ipAddress)
1841 .collect(Collectors.toSet());
1842 ipAddressSet.stream()
1843 .map(InterfaceIpAddress::ipAddress)
1844 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1845 .forEach(interfaceIpAddress ->
1846 routingRulePopulator.revokeSingleIpPunts(
1847 cp.deviceId(), interfaceIpAddress));
1848
1849 // 3. Host unicast routing rule
1850 // Remove unicast routing rule
1851 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1852 }
1853
1854 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1855 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1856 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1857
1858 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1859 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1860 .filter(intf -> !intf.connectPoint().equals(cp))
1861 .flatMap(intf -> intf.ipAddressesList().stream())
1862 .collect(Collectors.toSet());
1863 // 1. Partial subnet population
1864 // Add routing rules for newly added subnet, which does not also exist in
1865 // other interfaces in the same device
1866 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1867 .map(InterfaceIpAddress::subnetAddress)
1868 .collect(Collectors.toSet());
1869
1870 defaultRoutingHandler.populateSubnet(
1871 Collections.singleton(cp),
1872 ipPrefixSet.stream()
1873 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1874 .collect(Collectors.toSet()));
1875
1876 // 2. Interface IP punts
1877 // Add IP punts for new Intf address
1878 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1879 .map(InterfaceIpAddress::ipAddress)
1880 .collect(Collectors.toSet());
1881 ipAddressSet.stream()
1882 .map(InterfaceIpAddress::ipAddress)
1883 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1884 .forEach(interfaceIpAddress ->
1885 routingRulePopulator.populateSingleIpPunts(
1886 cp.deviceId(), interfaceIpAddress));
1887
1888 // 3. Host unicast routing rule
1889 // Add unicast routing rule
1890 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1891 }
sanghob35a6192015-04-01 13:05:26 -07001892}