blob: 1f126c6db80f50094ac504e8563fb564e0f81dfd [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
Charles Chan7ffd81f2017-02-08 15:52:08 -080018import com.google.common.collect.HashMultimap;
Saurav Dasc88d4662017-05-15 15:34:25 -070019import com.google.common.collect.ImmutableMap;
Charles Chanc81c45b2016-10-20 17:02:44 -070020import com.google.common.collect.Maps;
Charles Chan7ffd81f2017-02-08 15:52:08 -080021import com.google.common.collect.Multimap;
sanghob35a6192015-04-01 13:05:26 -070022import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho1e575652015-05-14 00:39:53 -070027import org.apache.felix.scr.annotations.Service;
sanghob35a6192015-04-01 13:05:26 -070028import org.onlab.packet.Ethernet;
Pier Ventre735b8c82016-12-02 08:16:05 -080029import org.onlab.packet.ICMP6;
Charles Chanc42e84e2015-10-20 16:24:19 -070030import org.onlab.packet.IPv4;
Pier Ventre10bd8d12016-11-26 21:05:22 -080031import org.onlab.packet.IPv6;
Charles Chanc42e84e2015-10-20 16:24:19 -070032import org.onlab.packet.IpPrefix;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070033import org.onlab.packet.VlanId;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070034import org.onlab.util.KryoNamespace;
Saurav Das80980c72016-03-23 11:22:49 -070035import org.onosproject.cfg.ComponentConfigService;
sanghob35a6192015-04-01 13:05:26 -070036import org.onosproject.core.ApplicationId;
37import org.onosproject.core.CoreService;
38import org.onosproject.event.Event;
Charles Chan278ce832017-06-26 15:25:09 -070039import org.onosproject.incubator.net.config.basics.InterfaceConfig;
Charles Chand55e84d2016-03-30 17:54:24 -070040import org.onosproject.incubator.net.config.basics.McastConfig;
Pier Luigi721b6622017-02-03 13:34:21 -080041import org.onosproject.incubator.net.intf.Interface;
Charles Chan2c15aca2016-11-09 20:51:44 -080042import org.onosproject.incubator.net.intf.InterfaceService;
Charles Chan03a73e02016-10-24 14:52:01 -070043import org.onosproject.incubator.net.routing.RouteEvent;
44import org.onosproject.incubator.net.routing.RouteListener;
45import org.onosproject.incubator.net.routing.RouteService;
Pier Ventre735b8c82016-12-02 08:16:05 -080046import org.onosproject.incubator.net.neighbour.NeighbourResolutionService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070047import org.onosproject.mastership.MastershipService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080048import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070049import org.onosproject.net.Device;
50import org.onosproject.net.DeviceId;
51import org.onosproject.net.Link;
52import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080053import org.onosproject.net.PortNumber;
Charles Chand6832882015-10-05 17:50:33 -070054import org.onosproject.net.config.ConfigFactory;
55import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070056import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070057import org.onosproject.net.config.NetworkConfigRegistry;
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;
68import org.onosproject.net.link.LinkEvent;
69import org.onosproject.net.link.LinkListener;
70import org.onosproject.net.link.LinkService;
Charles Chand55e84d2016-03-30 17:54:24 -070071import org.onosproject.net.mcast.McastEvent;
72import org.onosproject.net.mcast.McastListener;
73import org.onosproject.net.mcast.MulticastRouteService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080074import org.onosproject.net.packet.InboundPacket;
75import org.onosproject.net.packet.PacketContext;
76import org.onosproject.net.packet.PacketProcessor;
77import org.onosproject.net.packet.PacketService;
Pier Ventre42287df2016-11-09 14:17:26 -080078import org.onosproject.net.topology.PathService;
Charles Chand55e84d2016-03-30 17:54:24 -070079import org.onosproject.net.topology.TopologyService;
80import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
81import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventref34966c2016-11-07 16:21:04 -080082import org.onosproject.segmentrouting.config.PwaasConfig;
Pier Ventre10bd8d12016-11-26 21:05:22 -080083import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Pier Ventref34966c2016-11-07 16:21:04 -080084import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -070085import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -070086import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -070087import org.onosproject.segmentrouting.grouphandler.DestinationSet;
88import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
89import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Charles Chand2990362016-04-18 13:44:03 -070090import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chand2990362016-04-18 13:44:03 -070091import org.onosproject.segmentrouting.storekey.SubnetAssignedVidStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -080092import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -070093import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Pier Ventref34966c2016-11-07 16:21:04 -080094import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070095import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070096import org.onosproject.store.service.EventuallyConsistentMap;
97import org.onosproject.store.service.EventuallyConsistentMapBuilder;
98import org.onosproject.store.service.StorageService;
99import org.onosproject.store.service.WallClockTimestamp;
sanghob35a6192015-04-01 13:05:26 -0700100import org.slf4j.Logger;
101import org.slf4j.LoggerFactory;
102
sangho1e575652015-05-14 00:39:53 -0700103import java.util.List;
sanghob35a6192015-04-01 13:05:26 -0700104import java.util.Map;
Saurav Dasc88d4662017-05-15 15:34:25 -0700105import java.util.Map.Entry;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700106import java.util.Set;
sanghob35a6192015-04-01 13:05:26 -0700107import java.util.concurrent.ConcurrentHashMap;
108import java.util.concurrent.ConcurrentLinkedQueue;
109import java.util.concurrent.Executors;
110import java.util.concurrent.ScheduledExecutorService;
111import java.util.concurrent.ScheduledFuture;
112import java.util.concurrent.TimeUnit;
Saurav Das7bcbe702017-06-13 15:35:54 -0700113import java.util.concurrent.atomic.AtomicBoolean;
Charles Chan7ffd81f2017-02-08 15:52:08 -0800114import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -0700115
Charles Chan3e783d02016-02-26 22:19:52 -0800116import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800117import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700118import static org.onlab.util.Tools.groupedThreads;
Charles Chan3e783d02016-02-26 22:19:52 -0800119
Charles Chane849c192016-01-11 18:28:54 -0800120/**
121 * Segment routing manager.
122 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700123@Service
124@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700125public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700126
Charles Chan2c15aca2016-11-09 20:51:44 -0800127 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
sanghob35a6192015-04-01 13:05:26 -0700128
129 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700130 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700131
132 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800133 private NeighbourResolutionService neighbourResolutionService;
134
135 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800136 public PathService pathService;
137
138 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700139 CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700140
141 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700142 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700143
144 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700145 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700146
147 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700148 DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700149
150 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800151 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700152
153 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700154 LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700155
Charles Chan5270ed02016-01-30 23:22:37 -0800156 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800157 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700158
159 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800160 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700161
162 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
163 MulticastRouteService multicastRouteService;
164
165 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
166 TopologyService topologyService;
167
168 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700169 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800170
171 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800172 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800173
Saurav Das80980c72016-03-23 11:22:49 -0700174 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800175 public InterfaceService interfaceService;
176
Charles Chan03a73e02016-10-24 14:52:01 -0700177 ArpHandler arpHandler = null;
178 IcmpHandler icmpHandler = null;
179 IpHandler ipHandler = null;
180 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700181 ApplicationId appId;
182 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700183
Charles Chan03a73e02016-10-24 14:52:01 -0700184 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700185 private TunnelHandler tunnelHandler = null;
186 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700187 private InternalPacketProcessor processor = null;
188 private InternalLinkListener linkListener = null;
189 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700190 private AppConfigHandler appCfgHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700191 XConnectHandler xConnectHandler = null;
Charles Chand2990362016-04-18 13:44:03 -0700192 private McastHandler mcastHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700193 HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800194 private RouteHandler routeHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800195 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventref34966c2016-11-07 16:21:04 -0800196 private L2TunnelHandler l2TunnelHandler = null;
sanghob35a6192015-04-01 13:05:26 -0700197 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan5270ed02016-01-30 23:22:37 -0800198 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700199 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
200 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700201 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sanghob35a6192015-04-01 13:05:26 -0700202
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700203 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700204 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sanghob35a6192015-04-01 13:05:26 -0700205
Saurav Das4ce45962015-11-24 23:21:05 -0800206 @SuppressWarnings("unused")
sanghob35a6192015-04-01 13:05:26 -0700207 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800208 @SuppressWarnings("rawtypes")
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700209 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chan68aa62d2015-11-09 16:37:23 -0800210 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chane849c192016-01-11 18:28:54 -0800211 new ConcurrentHashMap<>();
212 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700213 * Per device next objective ID store with (device id + destination set) as key.
Charles Chane849c192016-01-11 18:28:54 -0800214 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700215 EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
216 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800217 /**
218 * Per device next objective ID store with (device id + subnet) as key.
219 */
Ray Milkeye4afdb52017-04-05 09:42:04 -0700220 EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800221 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800222 /**
223 * Per device next objective ID store with (device id + port) as key.
224 */
Ray Milkeye4afdb52017-04-05 09:42:04 -0700225 EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800226 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800227
Saurav Dasc88d4662017-05-15 15:34:25 -0700228 // Local store for all links seen and their present status, used for
229 // optimized routing. The existence of the link in the keys is enough to know
230 // if the link has been "seen-before" by this instance of the controller.
231 // The boolean value indicates if the link is currently up or not.
232 // XXX Currently the optimized routing logic depends on "forgetting" a link
233 // when a switch goes down, but "remembering" it when only the link goes down.
234 // Consider changing this logic so we can use the Link Service instead of
235 // a local cache.
236 private Map<Link, Boolean> seenLinks = new ConcurrentHashMap<>();
237
Saurav Das4ce45962015-11-24 23:21:05 -0800238 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
239 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700240
Saurav Das7bcbe702017-06-13 15:35:54 -0700241 private AtomicBoolean programmingScheduled = new AtomicBoolean();
242
Charles Chand55e84d2016-03-30 17:54:24 -0700243 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700244 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
245 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700246 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700247 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800248 public SegmentRoutingDeviceConfig createConfig() {
249 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700250 }
251 };
Pier Ventref34966c2016-11-07 16:21:04 -0800252
Charles Chand55e84d2016-03-30 17:54:24 -0700253 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700254 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
255 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700256 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800257 @Override
258 public SegmentRoutingAppConfig createConfig() {
259 return new SegmentRoutingAppConfig();
260 }
261 };
Pier Ventref34966c2016-11-07 16:21:04 -0800262
Charles Chanfc5c7802016-05-17 13:13:55 -0700263 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
264 new ConfigFactory<ApplicationId, XConnectConfig>(
265 SubjectFactories.APP_SUBJECT_FACTORY,
266 XConnectConfig.class, "xconnect") {
267 @Override
268 public XConnectConfig createConfig() {
269 return new XConnectConfig();
270 }
271 };
Pier Ventref34966c2016-11-07 16:21:04 -0800272
Charles Chand55e84d2016-03-30 17:54:24 -0700273 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700274 new ConfigFactory<ApplicationId, McastConfig>(
275 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700276 McastConfig.class, "multicast") {
277 @Override
278 public McastConfig createConfig() {
279 return new McastConfig();
280 }
281 };
282
Pier Ventref34966c2016-11-07 16:21:04 -0800283 private final ConfigFactory<ApplicationId, PwaasConfig> pwaasConfigFactory =
284 new ConfigFactory<ApplicationId, PwaasConfig>(
285 SubjectFactories.APP_SUBJECT_FACTORY,
286 PwaasConfig.class, "pwaas") {
287 @Override
288 public PwaasConfig createConfig() {
289 return new PwaasConfig();
290 }
291 };
292
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700293 private Object threadSchedulerLock = new Object();
294 private static int numOfEventsQueued = 0;
295 private static int numOfEventsExecuted = 0;
sanghob35a6192015-04-01 13:05:26 -0700296 private static int numOfHandlerExecution = 0;
297 private static int numOfHandlerScheduled = 0;
298
Charles Chan116188d2016-02-18 14:22:42 -0800299 /**
300 * Segment Routing App ID.
301 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800302 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan59cc16d2017-02-02 16:20:42 -0800303
Charles Chane849c192016-01-11 18:28:54 -0800304 /**
305 * The default VLAN ID assigned to the interfaces without subnet config.
306 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800307 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700308
sanghob35a6192015-04-01 13:05:26 -0700309 @Activate
310 protected void activate() {
Charles Chan2c15aca2016-11-09 20:51:44 -0800311 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700312
313 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700314 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700315 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700316 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700317 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700318 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700319 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700320 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700321 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700322
Charles Chan59cc16d2017-02-02 16:20:42 -0800323 log.debug("Creating EC map vlannextobjectivestore");
324 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
325 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
326 vlanNextObjStore = vlanNextObjMapBuilder
327 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700328 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700329 .withTimestampProvider((k, v) -> new WallClockTimestamp())
330 .build();
331
Saurav Das4ce45962015-11-24 23:21:05 -0800332 log.debug("Creating EC map subnetnextobjectivestore");
333 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
334 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
335 portNextObjStore = portNextObjMapBuilder
336 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700337 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800338 .withTimestampProvider((k, v) -> new WallClockTimestamp())
339 .build();
340
sangho0b2b6d12015-05-20 22:16:38 -0700341 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
342 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700343 tunnelStore = tunnelMapBuilder
344 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700345 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700346 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700347 .build();
348
349 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
350 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700351 policyStore = policyMapBuilder
352 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700353 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700354 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700355 .build();
356
Saurav Das80980c72016-03-23 11:22:49 -0700357 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800358 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700359 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800360 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800361 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
362 "requestInterceptsEnabled", "false");
Pier Luigi7e415132017-01-12 22:46:39 -0800363 compCfgService.preSetProperty("org.onosproject.incubator.net.neighbour.impl.NeighbourResolutionManager",
364 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700365 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800366 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800367 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
368 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800369 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
370 "forceUnprovision", "true");
Charles Chan73316522017-07-20 16:16:25 -0700371 compCfgService.preSetProperty("org.onosproject.incubator.store.routing.impl.RouteStoreImpl",
372 "distributed", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700373
Charles Chanb8e10c82015-10-14 11:24:40 -0700374 processor = new InternalPacketProcessor();
375 linkListener = new InternalLinkListener();
376 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700377 appCfgHandler = new AppConfigHandler(this);
378 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700379 mcastHandler = new McastHandler(this);
380 hostHandler = new HostHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700381 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800382 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventref34966c2016-11-07 16:21:04 -0800383 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700384
Charles Chan3e783d02016-02-26 22:19:52 -0800385 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700386 cfgService.registerConfigFactory(deviceConfigFactory);
387 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700388 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700389 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800390 cfgService.registerConfigFactory(pwaasConfigFactory);
Charles Chan5270ed02016-01-30 23:22:37 -0800391 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700392 packetService.addProcessor(processor, PacketProcessor.director(2));
393 linkService.addListener(linkListener);
394 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700395 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700396 routeService.addListener(routeListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700397
398 cfgListener.configureNetwork();
399
sanghob35a6192015-04-01 13:05:26 -0700400 log.info("Started");
401 }
402
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700403 private KryoNamespace.Builder createSerializer() {
404 return new KryoNamespace.Builder()
405 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700406 .register(DestinationSetNextObjectiveStoreKey.class,
Charles Chan59cc16d2017-02-02 16:20:42 -0800407 VlanNextObjectiveStoreKey.class,
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700408 SubnetAssignedVidStoreKey.class,
Saurav Das7bcbe702017-06-13 15:35:54 -0700409 DestinationSet.class,
410 NextNeighbors.class,
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700411 Tunnel.class,
412 DefaultTunnel.class,
413 Policy.class,
414 TunnelPolicy.class,
415 Policy.Type.class,
416 PortNextObjectiveStoreKey.class,
Charles Chanfc5c7802016-05-17 13:13:55 -0700417 XConnectStoreKey.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700418 );
419 }
420
sanghob35a6192015-04-01 13:05:26 -0700421 @Deactivate
422 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700423 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700424 cfgService.unregisterConfigFactory(deviceConfigFactory);
425 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700426 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700427 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800428 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chand6832882015-10-05 17:50:33 -0700429
Charles Chanc7a8a682017-06-19 00:43:31 -0700430 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700431 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700432 linkService.removeListener(linkListener);
433 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700434 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700435 routeService.removeListener(routeListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700436
Charles Chan0aa674e2017-02-23 15:44:08 -0800437 neighbourResolutionService.unregisterNeighbourHandlers(appId);
438
sanghob35a6192015-04-01 13:05:26 -0700439 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700440 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700441 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700442 groupHandlerMap.clear();
443
Saurav Das7bcbe702017-06-13 15:35:54 -0700444 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800445 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700446 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700447 tunnelStore.destroy();
448 policyStore.destroy();
sanghob35a6192015-04-01 13:05:26 -0700449 log.info("Stopped");
450 }
451
sangho1e575652015-05-14 00:39:53 -0700452 @Override
453 public List<Tunnel> getTunnels() {
454 return tunnelHandler.getTunnels();
455 }
456
457 @Override
sangho71abe1b2015-06-29 14:58:47 -0700458 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
459 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700460 }
461
462 @Override
sangho71abe1b2015-06-29 14:58:47 -0700463 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700464 for (Policy policy: policyHandler.getPolicies()) {
465 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
466 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
467 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
468 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700469 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700470 }
471 }
472 }
sangho71abe1b2015-06-29 14:58:47 -0700473 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700474 }
475
476 @Override
sangho71abe1b2015-06-29 14:58:47 -0700477 public PolicyHandler.Result removePolicy(Policy policy) {
478 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700479 }
480
481 @Override
sangho71abe1b2015-06-29 14:58:47 -0700482 public PolicyHandler.Result createPolicy(Policy policy) {
483 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700484 }
485
486 @Override
487 public List<Policy> getPolicies() {
488 return policyHandler.getPolicies();
489 }
490
Saurav Das59232cf2016-04-27 18:35:50 -0700491 @Override
492 public void rerouteNetwork() {
493 cfgListener.configureNetwork();
494 for (Device device : deviceService.getDevices()) {
Saurav Das018605f2017-02-18 14:05:44 -0800495 if (mastershipService.isLocalMaster(device.id())) {
496 defaultRoutingHandler.populatePortAddressingRules(device.id());
497 }
Saurav Das59232cf2016-04-27 18:35:50 -0700498 }
499 defaultRoutingHandler.startPopulationProcess();
500 }
501
Charles Chanc81c45b2016-10-20 17:02:44 -0700502 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800503 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
504 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Charles Chanc81c45b2016-10-20 17:02:44 -0700505 deviceService.getAvailableDevices().forEach(device -> {
506 deviceSubnetMap.put(device.id(), deviceConfiguration.getSubnets(device.id()));
507 });
508 return deviceSubnetMap;
509 }
510
Saurav Dasc88d4662017-05-15 15:34:25 -0700511
512 @Override
513 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
514 if (defaultRoutingHandler != null) {
515 return defaultRoutingHandler.getCurrentEmcpSpgMap();
516 } else {
517 return null;
518 }
519 }
520
521 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700522 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
523 if (dsNextObjStore != null) {
524 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700525 } else {
526 return ImmutableMap.of();
527 }
528 }
529
sanghof9d0bf12015-05-19 11:57:42 -0700530 /**
Ray Milkeye4afdb52017-04-05 09:42:04 -0700531 * Extracts the application ID from the manager.
532 *
533 * @return application ID
534 */
535 public ApplicationId appId() {
536 return appId;
537 }
538
539 /**
540 * Returns the device configuration.
541 *
542 * @return device configuration
543 */
544 public DeviceConfiguration deviceConfiguration() {
545 return deviceConfiguration;
546 }
547
548 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700549 * Per device next objective ID store with (device id + destination set) as key.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700550 *
551 * @return next objective ID store
552 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700553 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
554 dsNextObjStore() {
555 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700556 }
557
558 /**
559 * Per device next objective ID store with (device id + subnet) as key.
560 *
561 * @return vlan next object store
562 */
563 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
564 return vlanNextObjStore;
565 }
566
567 /**
568 * Per device next objective ID store with (device id + port) as key.
569 *
570 * @return port next object store.
571 */
572 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
573 return portNextObjStore;
574 }
575
576 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700577 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
578 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700579 *
580 * @return MPLS-ECMP value
581 */
582 public boolean getMplsEcmp() {
583 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
584 .getConfig(this.appId, SegmentRoutingAppConfig.class);
585 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
586 }
587
588 /**
sanghof9d0bf12015-05-19 11:57:42 -0700589 * Returns the tunnel object with the tunnel ID.
590 *
591 * @param tunnelId Tunnel ID
592 * @return Tunnel reference
593 */
sangho1e575652015-05-14 00:39:53 -0700594 public Tunnel getTunnel(String tunnelId) {
595 return tunnelHandler.getTunnel(tunnelId);
596 }
597
Charles Chan7ffd81f2017-02-08 15:52:08 -0800598 // TODO Consider moving these to InterfaceService
sanghob35a6192015-04-01 13:05:26 -0700599 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800600 * Returns untagged VLAN configured on given connect point.
Charles Chan7ffd81f2017-02-08 15:52:08 -0800601 * <p>
602 * Only returns the first match if there are multiple untagged VLAN configured
603 * on the connect point.
sanghob35a6192015-04-01 13:05:26 -0700604 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800605 * @param connectPoint connect point
606 * @return untagged VLAN or null if not configured
sanghob35a6192015-04-01 13:05:26 -0700607 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800608 public VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
609 return interfaceService.getInterfacesByPort(connectPoint).stream()
610 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
611 .map(Interface::vlanUntagged)
612 .findFirst().orElse(null);
sanghob35a6192015-04-01 13:05:26 -0700613 }
614
sangho1e575652015-05-14 00:39:53 -0700615 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800616 * Returns tagged VLAN configured on given connect point.
617 * <p>
618 * Returns all matches if there are multiple tagged VLAN configured
619 * on the connect point.
620 *
621 * @param connectPoint connect point
622 * @return tagged VLAN or empty set if not configured
623 */
624 public Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
625 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
626 return interfaces.stream()
627 .map(Interface::vlanTagged)
628 .flatMap(vlanIds -> vlanIds.stream())
629 .collect(Collectors.toSet());
630 }
631
632 /**
633 * Returns native VLAN configured on given connect point.
634 * <p>
635 * Only returns the first match if there are multiple native VLAN configured
636 * on the connect point.
637 *
638 * @param connectPoint connect point
639 * @return native VLAN or null if not configured
640 */
641 public VlanId getNativeVlanId(ConnectPoint connectPoint) {
642 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
643 return interfaces.stream()
644 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
645 .map(Interface::vlanNative)
646 .findFirst()
647 .orElse(null);
648 }
649
650 /**
651 * Returns vlan port map of given device.
652 *
653 * @param deviceId device id
654 * @return vlan-port multimap
655 */
656 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
657 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
658
659 interfaceService.getInterfaces().stream()
660 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
661 .forEach(intf -> {
662 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
663 intf.vlanTagged().forEach(vlanTagged -> {
664 vlanPortMap.put(vlanTagged, intf.connectPoint().port());
665 });
666 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
667 });
668 vlanPortMap.removeAll(VlanId.NONE);
669
670 return vlanPortMap;
671 }
672
673 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800674 * Returns the next objective ID for the given subnet prefix. It is expected
Charles Chan59cc16d2017-02-02 16:20:42 -0800675 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800676 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700677 *
678 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800679 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800680 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700681 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800682 public int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700683 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800684 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
685 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700686 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800687 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800688 + "device {} not found", deviceId);
689 return -1;
690 }
691 }
692
693 /**
694 * Returns the next objective ID for the given portNumber, given the treatment.
695 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700696 * would result in different objectives. If the next object does not exist,
697 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800698 *
699 * @param deviceId Device ID
700 * @param portNum port number on device for which NextObjective is queried
701 * @param treatment the actions to apply on the packets (should include outport)
702 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700703 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700704 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800705 */
706 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
707 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700708 TrafficSelector meta,
709 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -0800710 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
711 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -0700712 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -0800713 } else {
Charles Chane849c192016-01-11 18:28:54 -0800714 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
715 + " not found", deviceId);
716 return -1;
717 }
718 }
719
Saurav Dasc88d4662017-05-15 15:34:25 -0700720 /**
721 * Returns the group handler object for the specified device id.
722 *
723 * @param devId the device identifier
724 * @return the groupHandler object for the device id, or null if not found
725 */
726 public DefaultGroupHandler getGroupHandler(DeviceId devId) {
727 return groupHandlerMap.get(devId);
728 }
729
730 /**
731 * Returns true if this controller instance has seen this link before. The
732 * link may not be currently up, but as long as the link had been seen before
733 * this method will return true. The one exception is when the link was
734 * indeed seen before, but this controller instance was forced to forget it
735 * by a call to purgeSeenLink method.
736 *
737 * @param link the infrastructure link being queried
738 * @return true if this controller instance has seen this link before
739 */
740 public boolean isSeenLink(Link link) {
741 return seenLinks.containsKey(link);
742 }
743
744 /**
745 * Updates the seen link store. Updates can be for links that are currently
746 * available or not.
747 *
748 * @param link the link to update in the seen-link local store
749 * @param up the status of the link, true if up, false if down
750 */
751 public void updateSeenLink(Link link, boolean up) {
752 seenLinks.put(link, up);
753 }
754
755 /**
756 * Returns the status of a seen-link (up or down). If the link has not
757 * been seen-before, a null object is returned.
758 *
759 * @param link the infrastructure link being queried
760 * @return null if the link was not seen-before;
761 * true if the seen-link is up;
762 * false if the seen-link is down
763 */
764 public Boolean isSeenLinkUp(Link link) {
765 return seenLinks.get(link);
766 }
767
768 /**
769 * Makes this controller instance forget a previously seen before link.
770 *
771 * @param link the infrastructure link to purge
772 */
773 public void purgeSeenLink(Link link) {
774 seenLinks.remove(link);
775 }
776
777 /**
778 * Returns the status of a link as parallel link. A parallel link
779 * is defined as a link which has common src and dst switches as another
780 * seen-link that is currently enabled. It is not necessary for the link being
781 * queried to be a seen-link.
782 *
783 * @param link the infrastructure link being queried
784 * @return true if a seen-link exists that is up, and shares the
785 * same src and dst switches as the link being queried
786 */
787 public boolean isParallelLink(Link link) {
788 for (Entry<Link, Boolean> seen : seenLinks.entrySet()) {
789 Link seenLink = seen.getKey();
790 if (seenLink.equals(link)) {
791 continue;
792 }
793 if (seenLink.src().deviceId().equals(link.src().deviceId()) &&
794 seenLink.dst().deviceId().equals(link.dst().deviceId()) &&
795 seen.getValue()) {
796 return true;
797 }
798 }
799 return false;
800 }
801
Saurav Das7bcbe702017-06-13 15:35:54 -0700802 /**
803 * Returns true if the link being queried is a bidirectional link. A bidi
804 * link is defined as a link, whose reverse link - ie. the link in the reverse
805 * direction - has been seen-before and is up. It is not necessary for the link
806 * being queried to be a seen-link.
807 *
808 * @param link the infrastructure link being queried
809 * @return true if another unidirectional link exists in the reverse direction,
810 * has been seen-before and is up
811 */
812 public boolean isBidirectional(Link link) {
813 Link reverseLink = linkService.getLink(link.dst(), link.src());
814 if (reverseLink == null) {
815 return false;
816 }
817 Boolean result = isSeenLinkUp(reverseLink);
818 if (result == null) {
819 return false;
820 }
821 return result.booleanValue();
822 }
823
824 /**
825 * Determines if the given link should be avoided in routing calculations
826 * by policy or design.
827 *
828 * @param link the infrastructure link being queried
829 * @return true if link should be avoided
830 */
831 public boolean avoidLink(Link link) {
832 // XXX currently only avoids all pair-links. In the future can be
833 // extended to avoid any generic link
834 DeviceId src = link.src().deviceId();
835 PortNumber srcPort = link.src().port();
836 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(src)) {
837 log.warn("Device {} not configured..cannot avoid link {}", src, link);
838 return false;
839 }
840 DeviceId pairDev;
841 PortNumber pairLocalPort, pairRemotePort = null;
842 try {
843 pairDev = deviceConfiguration.getPairDeviceId(src);
844 pairLocalPort = deviceConfiguration.getPairLocalPort(src);
845 if (pairDev != null) {
846 pairRemotePort = deviceConfiguration.getPairLocalPort(pairDev);
847 }
848 } catch (DeviceConfigNotFoundException e) {
849 log.warn("Pair dev for dev {} not configured..cannot avoid link {}",
850 src, link);
851 return false;
852 }
853
854 if (srcPort.equals(pairLocalPort) &&
855 link.dst().deviceId().equals(pairDev) &&
856 link.dst().port().equals(pairRemotePort)) {
857 return true;
858 }
859 return false;
860 }
861
sanghob35a6192015-04-01 13:05:26 -0700862 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700863 @Override
864 public void process(PacketContext context) {
865
866 if (context.isHandled()) {
867 return;
868 }
869
870 InboundPacket pkt = context.inPacket();
871 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800872
873 if (ethernet == null) {
874 return;
875 }
876
Saurav Das7bcbe702017-06-13 15:35:54 -0700877 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
878 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800879 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700880 log.warn("Received unexpected ARP packet on {}",
881 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -0700882 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800883 return;
sanghob35a6192015-04-01 13:05:26 -0700884 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800885 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
886 //ipHandler.addToPacketBuffer(ipv4Packet);
887 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
888 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700889 } else {
Charles Chan50035632017-01-13 17:20:44 -0800890 // NOTE: We don't support IP learning at this moment so this
891 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800892 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700893 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800894 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
895 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -0800896 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -0800897 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -0800898 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
899 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
900 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
901 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
902 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
903 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -0700904 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -0700905 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -0800906 }
907 } else {
908 // NOTE: We don't support IP learning at this moment so this
909 // is not necessary. Also it causes duplication of DHCPv6 packets.
910 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
911 }
sanghob35a6192015-04-01 13:05:26 -0700912 }
913 }
914 }
915
916 private class InternalLinkListener implements LinkListener {
917 @Override
918 public void event(LinkEvent event) {
Charles Chanb1f8c762017-03-29 16:39:05 -0700919 if (event.type() == LinkEvent.Type.LINK_ADDED ||
920 event.type() == LinkEvent.Type.LINK_UPDATED ||
921 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700922 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700923 scheduleEventHandlerIfNotScheduled(event);
924 }
925 }
926 }
927
928 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -0700929 @Override
930 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -0700931 switch (event.type()) {
932 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -0800933 case PORT_UPDATED:
934 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -0700935 case DEVICE_UPDATED:
936 case DEVICE_AVAILABILITY_CHANGED:
Saurav Dasc88d4662017-05-15 15:34:25 -0700937 log.trace("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700938 scheduleEventHandlerIfNotScheduled(event);
939 break;
940 default:
941 }
942 }
943 }
944
Saurav Das4ce45962015-11-24 23:21:05 -0800945 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -0700946 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700947 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700948 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700949 numOfEventsQueued++;
950
951 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
952 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700953 eventHandlerFuture = executorService
954 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
955 numOfHandlerScheduled++;
956 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700957 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700958 numOfEventsQueued,
959 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -0700960 }
sanghob35a6192015-04-01 13:05:26 -0700961 }
962
963 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700964 @Override
sanghob35a6192015-04-01 13:05:26 -0700965 public void run() {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700966 try {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700967 while (true) {
Saurav Das4ce45962015-11-24 23:21:05 -0800968 @SuppressWarnings("rawtypes")
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700969 Event event = null;
970 synchronized (threadSchedulerLock) {
971 if (!eventQueue.isEmpty()) {
972 event = eventQueue.poll();
973 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700974 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700975 numOfHandlerExecution++;
976 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
977 numOfHandlerExecution, numOfEventsExecuted);
978 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700979 }
sangho20eff1d2015-04-13 15:15:58 -0700980 }
Charles Chanb1f8c762017-03-29 16:39:05 -0700981 if (event.type() == LinkEvent.Type.LINK_ADDED ||
982 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700983 // Note: do not update seenLinks here, otherwise every
984 // link, even one seen for the first time, will be appear
985 // to be a previously seen link
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700986 processLinkAdded((Link) event.subject());
987 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre2c515312016-09-13 21:33:40 -0700988 Link linkRemoved = (Link) event.subject();
Saurav Dasc88d4662017-05-15 15:34:25 -0700989 if (linkRemoved.type() == Link.Type.DIRECT) {
990 updateSeenLink(linkRemoved, false);
991 }
992 // device availability check helps to ensure that
993 // multiple link-removed events are actually treated as a
994 // single switch removed event. purgeSeenLink is necessary
995 // so we do rerouting (instead of rehashing) when switch
996 // comes back.
Pier Ventre2c515312016-09-13 21:33:40 -0700997 if (linkRemoved.src().elementId() instanceof DeviceId &&
998 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700999 purgeSeenLink(linkRemoved);
Pier Ventre2c515312016-09-13 21:33:40 -07001000 continue;
1001 }
1002 if (linkRemoved.dst().elementId() instanceof DeviceId &&
1003 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001004 purgeSeenLink(linkRemoved);
Pier Ventre2c515312016-09-13 21:33:40 -07001005 continue;
1006 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001007 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001008 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1009 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1010 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001011 DeviceId deviceId = ((Device) event.subject()).id();
1012 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001013 log.info("Processing device event {} for available device {}",
1014 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001015 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -07001016 } else {
1017 log.info("Processing device event {} for unavailable device {}",
1018 event.type(), ((Device) event.subject()).id());
1019 processDeviceRemoved((Device) event.subject());
1020 }
Saurav Das1a129a02016-11-18 15:21:57 -08001021 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001022 // typically these calls come when device is added first time
1023 // so port filtering rules are handled at the device_added event.
1024 // port added calls represent all ports on the device,
1025 // enabled or not.
Saurav Dasc88d4662017-05-15 15:34:25 -07001026 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001027 ((DeviceEvent) event).subject().id(),
1028 ((DeviceEvent) event).port().number(),
1029 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -08001030 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001031 // these calls happen for every subsequent event
1032 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -08001033 log.info("** PORT UPDATED {}/{} -> {}",
1034 event.subject(),
1035 ((DeviceEvent) event).port(),
1036 event.type());
1037 processPortUpdated(((Device) event.subject()),
1038 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001039 } else {
1040 log.warn("Unhandled event type: {}", event.type());
1041 }
sanghob35a6192015-04-01 13:05:26 -07001042 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001043 } catch (Exception e) {
1044 log.error("SegmentRouting event handler "
1045 + "thread thrown an exception: {}", e);
sanghob35a6192015-04-01 13:05:26 -07001046 }
sanghob35a6192015-04-01 13:05:26 -07001047 }
1048 }
1049
sanghob35a6192015-04-01 13:05:26 -07001050 private void processLinkAdded(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001051 log.info("** LINK ADDED {}", link.toString());
Charles Chan0b4e6182015-11-03 10:42:14 -08001052 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001053 log.warn("Source device of this link is not configured..not processing");
Charles Chan0b4e6182015-11-03 10:42:14 -08001054 return;
1055 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001056 if (link.type() != Link.Type.DIRECT) {
1057 // NOTE: A DIRECT link might be transiently marked as INDIRECT
1058 // if BDDP is received before LLDP. We can safely ignore that
1059 // until the LLDP is received and the link is marked as DIRECT.
1060 log.info("Ignore link {}->{}. Link type is {} instead of DIRECT.",
1061 link.src(), link.dst(), link.type());
1062 return;
1063 }
1064
1065 //Irrespective of whether the local is a MASTER or not for this device,
1066 //create group handler instance and push default TTP flow rules if needed,
1067 //as in a multi-instance setup, instances can initiate groups for any device.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001068 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
1069 .deviceId());
1070 if (groupHandler != null) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001071 groupHandler.portUpForLink(link);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001072 } else {
1073 Device device = deviceService.getDevice(link.src().deviceId());
1074 if (device != null) {
1075 log.warn("processLinkAdded: Link Added "
1076 + "Notification without Device Added "
1077 + "event, still handling it");
1078 processDeviceAdded(device);
1079 groupHandler = groupHandlerMap.get(link.src()
1080 .deviceId());
Saurav Dasc88d4662017-05-15 15:34:25 -07001081 groupHandler.portUpForLink(link);
sanghob35a6192015-04-01 13:05:26 -07001082 }
1083 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001084
Saurav Das7bcbe702017-06-13 15:35:54 -07001085 /*// process link only if it is bidirectional
1086 if (!isBidirectional(link)) {
1087 log.debug("Link not bidirectional.. waiting for other direction "
1088 + "src {} --> dst {} ", link.dst(), link.src());
1089 // note that if we are not processing for routing, it should at least
1090 // be considered a seen-link
1091 updateSeenLink(link, true);
1092 return;
1093 }
1094 TO DO this ensure that rehash is still done correctly even if link is
1095 not processed for rerouting - perhaps rehash in both directions when
1096 it ultimately becomes bidi?
1097 */
1098
1099 log.debug("Starting optimized route population process for link "
1100 + "{} --> {}", link.src(), link.dst());
Saurav Dasc88d4662017-05-15 15:34:25 -07001101 boolean seenBefore = isSeenLink(link);
1102 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null, link, null);
Saurav Das7bcbe702017-06-13 15:35:54 -07001103
1104 // It's possible that linkUp causes no route-path change as ECMP graph does
1105 // not change if the link is a parallel link (same src-dst as another link.
1106 // However we still need to update ECMP hash groups to include new buckets
1107 // for the link that has come up.
Saurav Dasc88d4662017-05-15 15:34:25 -07001108 if (mastershipService.isLocalMaster(link.src().deviceId())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001109 if (!seenBefore && isParallelLink(link)) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001110 // if link seen first time, we need to ensure hash-groups have all ports
Saurav Das7bcbe702017-06-13 15:35:54 -07001111 log.debug("Attempting retryHash for paralled first-time link {}", link);
Saurav Dasc88d4662017-05-15 15:34:25 -07001112 groupHandler.retryHash(link, false, true);
1113 } else {
1114 //seen before-link
1115 if (isParallelLink(link)) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001116 log.debug("Attempting retryHash for paralled seen-before "
1117 + "link {}", link);
Saurav Dasc88d4662017-05-15 15:34:25 -07001118 groupHandler.retryHash(link, false, false);
1119 }
1120 }
1121 }
Charles Chan2199c302016-04-23 17:36:10 -07001122
1123 mcastHandler.init();
sanghob35a6192015-04-01 13:05:26 -07001124 }
1125
1126 private void processLinkRemoved(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001127 log.info("** LINK REMOVED {}", link.toString());
Saurav Dasc88d4662017-05-15 15:34:25 -07001128 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link, null, null);
1129
1130 // update local groupHandler stores
sangho834e4b02015-05-01 09:38:25 -07001131 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
1132 if (groupHandler != null) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001133 if (mastershipService.isLocalMaster(link.src().deviceId()) &&
1134 isParallelLink(link)) {
1135 groupHandler.retryHash(link, true, false);
1136 }
1137 // ensure local stores are updated
1138 groupHandler.portDown(link.src().port());
1139 } else {
1140 log.warn("group handler not found for dev:{} when removing link: {}",
1141 link.src().deviceId(), link);
sangho834e4b02015-05-01 09:38:25 -07001142 }
Charles Chan2199c302016-04-23 17:36:10 -07001143
1144 mcastHandler.processLinkDown(link);
sanghob35a6192015-04-01 13:05:26 -07001145 }
1146
1147 private void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001148 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001149
1150 // NOTE: Punt ARP/NDP even when the device is not configured.
1151 // Host learning without network config is required for CORD config generator.
1152 routingRulePopulator.populateIpPunts(device.id());
1153 routingRulePopulator.populateArpNdpPunts(device.id());
1154
Charles Chan0b4e6182015-11-03 10:42:14 -08001155 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001156 log.warn("Device configuration unavailable. Device {} will be "
1157 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001158 return;
1159 }
Charles Chan2199c302016-04-23 17:36:10 -07001160 processDeviceAddedInternal(device.id());
1161 }
1162
1163 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001164 // Irrespective of whether the local is a MASTER or not for this device,
1165 // we need to create a SR-group-handler instance. This is because in a
1166 // multi-instance setup, any instance can initiate forwarding/next-objectives
1167 // for any switch (even if this instance is a SLAVE or not even connected
1168 // to the switch). To handle this, a default-group-handler instance is necessary
1169 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001170 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1171 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001172 DefaultGroupHandler groupHandler;
1173 try {
1174 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001175 createGroupHandler(deviceId,
1176 appId,
1177 deviceConfiguration,
1178 linkService,
1179 flowObjectiveService,
1180 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001181 } catch (DeviceConfigNotFoundException e) {
1182 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1183 return;
1184 }
Charles Chan2199c302016-04-23 17:36:10 -07001185 log.debug("updating groupHandlerMap with new config for device: {}",
1186 deviceId);
1187 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001188 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001189
Charles Chan2199c302016-04-23 17:36:10 -07001190 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001191 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan03a73e02016-10-24 14:52:01 -07001192 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001193 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001194 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001195 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001196 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001197 }
Charles Chan5270ed02016-01-30 23:22:37 -08001198
Charles Chan03a73e02016-10-24 14:52:01 -07001199 appCfgHandler.init(deviceId);
1200 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001201 }
1202
Saurav Das80980c72016-03-23 11:22:49 -07001203 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001204 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001205 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1206 .forEach(entry -> {
Saurav Das7bcbe702017-06-13 15:35:54 -07001207 dsNextObjStore.remove(entry.getKey());
Saurav Das80980c72016-03-23 11:22:49 -07001208 });
Charles Chan59cc16d2017-02-02 16:20:42 -08001209 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001210 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1211 .forEach(entry -> {
Charles Chan59cc16d2017-02-02 16:20:42 -08001212 vlanNextObjStore.remove(entry.getKey());
Saurav Das80980c72016-03-23 11:22:49 -07001213 });
Saurav Das80980c72016-03-23 11:22:49 -07001214 portNextObjStore.entrySet().stream()
1215 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1216 .forEach(entry -> {
1217 portNextObjStore.remove(entry.getKey());
1218 });
Charles Chaned3742352017-06-15 00:44:51 -07001219
1220 seenLinks.keySet().removeIf(key -> key.src().deviceId().equals(device.id()) ||
1221 key.dst().deviceId().equals(device.id()));
1222
Saurav Das80980c72016-03-23 11:22:49 -07001223 groupHandlerMap.remove(device.id());
Saurav Das80980c72016-03-23 11:22:49 -07001224 defaultRoutingHandler.purgeEcmpGraph(device.id());
Saurav Dasc88d4662017-05-15 15:34:25 -07001225 // Note that a switch going down is associated with all of its links
1226 // going down as well, but it is treated as a single switch down event
1227 // while the link-downs are ignored.
1228 defaultRoutingHandler
1229 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Charles Chan2199c302016-04-23 17:36:10 -07001230 mcastHandler.removeDevice(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001231 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -07001232 }
1233
Saurav Das1a129a02016-11-18 15:21:57 -08001234 private void processPortUpdated(Device device, Port port) {
1235 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1236 log.warn("Device configuration uploading. Not handling port event for"
1237 + "dev: {} port: {}", device.id(), port.number());
1238 return;
1239 }
Saurav Das018605f2017-02-18 14:05:44 -08001240
1241 if (!mastershipService.isLocalMaster(device.id())) {
1242 log.debug("Not master for dev:{} .. not handling port updated event"
1243 + "for port {}", device.id(), port.number());
1244 return;
1245 }
1246
1247 // first we handle filtering rules associated with the port
1248 if (port.isEnabled()) {
1249 log.info("Switchport {}/{} enabled..programming filters",
1250 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001251 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001252 } else {
1253 log.info("Switchport {}/{} disabled..removing filters",
1254 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001255 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001256 }
Saurav Das1a129a02016-11-18 15:21:57 -08001257
1258 // portUpdated calls are for ports that have gone down or up. For switch
1259 // to switch ports, link-events should take care of any re-routing or
1260 // group editing necessary for port up/down. Here we only process edge ports
1261 // that are already configured.
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001262 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1263 VlanId untaggedVlan = getUntaggedVlanId(cp);
1264 VlanId nativeVlan = getNativeVlanId(cp);
1265 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001266
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001267 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001268 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das1a129a02016-11-18 15:21:57 -08001269 + "dev/port: {}/{}", device.id(), port.number());
1270 return;
1271 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001272 if (untaggedVlan != null) {
1273 processEdgePort(device, port, untaggedVlan, true);
1274 }
1275 if (nativeVlan != null) {
1276 processEdgePort(device, port, nativeVlan, true);
1277 }
1278 if (!taggedVlans.isEmpty()) {
1279 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1280 }
Saurav Das1a129a02016-11-18 15:21:57 -08001281 }
1282
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001283 private void processEdgePort(Device device, Port port, VlanId vlanId,
1284 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001285 boolean portUp = port.isEnabled();
1286 if (portUp) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001287 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001288 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001289 } else {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001290 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001291 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001292 }
1293
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001294 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001295 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001296 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001297 } else {
1298 log.warn("Group handler not found for dev:{}. Not handling edge port"
1299 + " {} event for port:{}", device.id(),
1300 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001301 }
1302 }
sangho1e575652015-05-14 00:39:53 -07001303
Pier Ventre10bd8d12016-11-26 21:05:22 -08001304 /**
1305 * Registers the given connect point with the NRS, this is necessary
1306 * to receive the NDP and ARP packets from the NRS.
1307 *
1308 * @param portToRegister connect point to register
1309 */
1310 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001311 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001312 portToRegister,
1313 neighbourHandler,
1314 appId
1315 );
1316 }
1317
Charles Chand6832882015-10-05 17:50:33 -07001318 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001319 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001320 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001321
Charles Chane849c192016-01-11 18:28:54 -08001322 /**
1323 * Constructs the internal network config listener.
1324 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001325 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001326 */
Charles Chan2c15aca2016-11-09 20:51:44 -08001327 public InternalConfigListener(SegmentRoutingManager srManager) {
1328 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001329 }
1330
Charles Chane849c192016-01-11 18:28:54 -08001331 /**
1332 * Reads network config and initializes related data structure accordingly.
1333 */
Charles Chan4636be02015-10-07 14:21:45 -07001334 public void configureNetwork() {
Saurav Das7bcbe702017-06-13 15:35:54 -07001335 if (deviceConfiguration == null) {
1336 deviceConfiguration = new DeviceConfiguration(srManager);
1337 } else {
1338 deviceConfiguration.updateConfig();
1339 }
Charles Chan4636be02015-10-07 14:21:45 -07001340
Charles Chan2c15aca2016-11-09 20:51:44 -08001341 arpHandler = new ArpHandler(srManager);
1342 icmpHandler = new IcmpHandler(srManager);
1343 ipHandler = new IpHandler(srManager);
1344 routingRulePopulator = new RoutingRulePopulator(srManager);
1345 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001346
1347 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1348 groupHandlerMap, tunnelStore);
1349 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1350 flowObjectiveService,
1351 tunnelHandler, policyStore);
Saurav Das7bcbe702017-06-13 15:35:54 -07001352 // add a small delay to absorb multiple network config added notifications
1353 if (!programmingScheduled.get()) {
1354 programmingScheduled.set(true);
1355 executorService.schedule(new ConfigChange(), PROGRAM_DELAY,
1356 TimeUnit.SECONDS);
Charles Chan4636be02015-10-07 14:21:45 -07001357 }
Charles Chan2199c302016-04-23 17:36:10 -07001358 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001359 }
1360
Charles Chand6832882015-10-05 17:50:33 -07001361 @Override
1362 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001363 // TODO move this part to NetworkConfigEventHandler
1364 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1365 switch (event.type()) {
1366 case CONFIG_ADDED:
Charles Chan278ce832017-06-26 15:25:09 -07001367 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan5270ed02016-01-30 23:22:37 -08001368 configureNetwork();
1369 break;
1370 case CONFIG_UPDATED:
Charles Chan278ce832017-06-26 15:25:09 -07001371 log.info("Segment Routing Config updated for {}", event.subject());
1372 // TODO support dynamic configuration
1373 break;
1374 default:
1375 break;
1376 }
1377 } else if (event.configClass().equals(InterfaceConfig.class)) {
1378 switch (event.type()) {
1379 case CONFIG_ADDED:
1380 log.info("Interface Config added for {}", event.subject());
1381 configureNetwork();
1382 break;
1383 case CONFIG_UPDATED:
1384 log.info("Interface Config updated for {}", event.subject());
Charles Chan5270ed02016-01-30 23:22:37 -08001385 // TODO support dynamic configuration
1386 break;
1387 default:
1388 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001389 }
Charles Chan5270ed02016-01-30 23:22:37 -08001390 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001391 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001392 switch (event.type()) {
1393 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001394 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001395 break;
1396 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001397 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001398 break;
1399 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001400 appCfgHandler.processAppConfigRemoved(event);
1401 break;
1402 default:
1403 break;
1404 }
Charles Chan03a73e02016-10-24 14:52:01 -07001405 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001406 } else if (event.configClass().equals(XConnectConfig.class)) {
1407 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1408 switch (event.type()) {
1409 case CONFIG_ADDED:
1410 xConnectHandler.processXConnectConfigAdded(event);
1411 break;
1412 case CONFIG_UPDATED:
1413 xConnectHandler.processXConnectConfigUpdated(event);
1414 break;
1415 case CONFIG_REMOVED:
1416 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001417 break;
1418 default:
1419 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001420 }
Pier Ventref34966c2016-11-07 16:21:04 -08001421 } else if (event.configClass().equals(PwaasConfig.class)) {
1422 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1423 switch (event.type()) {
1424 case CONFIG_ADDED:
1425 l2TunnelHandler.processPwaasConfigAdded(event);
1426 break;
1427 case CONFIG_UPDATED:
1428 l2TunnelHandler.processPwaasConfigUpdated(event);
1429 break;
1430 case CONFIG_REMOVED:
1431 l2TunnelHandler.processPwaasConfigRemoved(event);
1432 break;
1433 default:
1434 break;
1435 }
Charles Chand6832882015-10-05 17:50:33 -07001436 }
1437 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001438
1439 private final class ConfigChange implements Runnable {
1440 @Override
1441 public void run() {
1442 programmingScheduled.set(false);
1443 for (Device device : deviceService.getDevices()) {
1444 processDeviceAdded(device);
1445 }
1446 defaultRoutingHandler.startPopulationProcess();
1447 }
1448 }
Charles Chand6832882015-10-05 17:50:33 -07001449 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001450
1451 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001452 @Override
1453 public void event(HostEvent event) {
1454 // Do not proceed without mastership
1455 DeviceId deviceId = event.subject().location().deviceId();
1456 if (!mastershipService.isLocalMaster(deviceId)) {
1457 return;
1458 }
1459
1460 switch (event.type()) {
1461 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001462 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001463 break;
1464 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001465 hostHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001466 break;
1467 case HOST_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001468 hostHandler.processHostRemoveEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001469 break;
1470 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001471 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001472 break;
1473 default:
1474 log.warn("Unsupported host event type: {}", event.type());
1475 break;
1476 }
1477 }
1478 }
1479
Charles Chand55e84d2016-03-30 17:54:24 -07001480 private class InternalMcastListener implements McastListener {
1481 @Override
1482 public void event(McastEvent event) {
1483 switch (event.type()) {
1484 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001485 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001486 break;
1487 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001488 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001489 break;
1490 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001491 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001492 break;
1493 case ROUTE_ADDED:
1494 case ROUTE_REMOVED:
1495 default:
1496 break;
1497 }
1498 }
1499 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001500
Charles Chan03a73e02016-10-24 14:52:01 -07001501 private class InternalRouteEventListener implements RouteListener {
1502 @Override
1503 public void event(RouteEvent event) {
1504 switch (event.type()) {
1505 case ROUTE_ADDED:
1506 routeHandler.processRouteAdded(event);
1507 break;
1508 case ROUTE_UPDATED:
1509 routeHandler.processRouteUpdated(event);
1510 break;
1511 case ROUTE_REMOVED:
1512 routeHandler.processRouteRemoved(event);
1513 break;
1514 default:
1515 break;
1516 }
1517 }
1518 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001519
sanghob35a6192015-04-01 13:05:26 -07001520}