blob: 67a13f9c593ff73166b316d2908726786a0be144 [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor43b53542016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sangho80f11cb2015-04-01 13:05:26 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.segmentrouting;
17
Charles Chan90772a72017-02-08 15:52:08 -080018import com.google.common.collect.HashMultimap;
Saurav Das62ae6792017-05-15 15:34:25 -070019import com.google.common.collect.ImmutableMap;
Charles Chand7844e52016-10-20 17:02:44 -070020import com.google.common.collect.Maps;
Charles Chan90772a72017-02-08 15:52:08 -080021import com.google.common.collect.Multimap;
sangho80f11cb2015-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;
sangho27462c62015-05-14 00:39:53 -070027import org.apache.felix.scr.annotations.Service;
sangho80f11cb2015-04-01 13:05:26 -070028import org.onlab.packet.Ethernet;
Pier Ventreb6b81d52016-12-02 08:16:05 -080029import org.onlab.packet.ICMP6;
Charles Chan77277672015-10-20 16:24:19 -070030import org.onlab.packet.IPv4;
Pier Ventreb6a7f342016-11-26 21:05:22 -080031import org.onlab.packet.IPv6;
Charles Chan77277672015-10-20 16:24:19 -070032import org.onlab.packet.IpPrefix;
Jonathan Hart54541d12016-04-12 15:39:44 -070033import org.onlab.packet.VlanId;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070034import org.onlab.util.KryoNamespace;
Saurav Dasc3604f12016-03-23 11:22:49 -070035import org.onosproject.cfg.ComponentConfigService;
sangho80f11cb2015-04-01 13:05:26 -070036import org.onosproject.core.ApplicationId;
37import org.onosproject.core.CoreService;
38import org.onosproject.event.Event;
Charles Chan68363b12017-06-26 15:25:09 -070039import org.onosproject.incubator.net.config.basics.InterfaceConfig;
Charles Chanc91c8782016-03-30 17:54:24 -070040import org.onosproject.incubator.net.config.basics.McastConfig;
Pier Luigiad0a67f2017-02-03 13:34:21 -080041import org.onosproject.incubator.net.intf.Interface;
Charles Chan46fdfaf2016-11-09 20:51:44 -080042import org.onosproject.incubator.net.intf.InterfaceService;
Charles Chandebfea32016-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 Ventreb6b81d52016-12-02 08:16:05 -080046import org.onosproject.incubator.net.neighbour.NeighbourResolutionService;
Jonathan Hart54541d12016-04-12 15:39:44 -070047import org.onosproject.mastership.MastershipService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080048import org.onosproject.net.ConnectPoint;
Jonathan Hart54541d12016-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 Chanf4586112015-11-09 16:37:23 -080053import org.onosproject.net.PortNumber;
Charles Chan72f556a2015-10-05 17:50:33 -070054import org.onosproject.net.config.ConfigFactory;
55import org.onosproject.net.config.NetworkConfigEvent;
Charles Chan72f556a2015-10-05 17:50:33 -070056import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070057import org.onosproject.net.config.NetworkConfigRegistry;
Charles Chan72f556a2015-10-05 17:50:33 -070058import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hart54541d12016-04-12 15:39:44 -070059import org.onosproject.net.device.DeviceEvent;
60import org.onosproject.net.device.DeviceListener;
61import org.onosproject.net.device.DeviceService;
Charles Chanf4586112015-11-09 16:37:23 -080062import org.onosproject.net.flow.TrafficSelector;
63import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart54541d12016-04-12 15:39:44 -070064import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chanf4586112015-11-09 16:37:23 -080065import org.onosproject.net.host.HostEvent;
66import org.onosproject.net.host.HostListener;
Pier Ventreb6a7f342016-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 Chanc91c8782016-03-30 17:54:24 -070071import org.onosproject.net.mcast.McastEvent;
72import org.onosproject.net.mcast.McastListener;
73import org.onosproject.net.mcast.MulticastRouteService;
Pier Ventreb6a7f342016-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 Ventref3cf5b92016-11-09 14:17:26 -080078import org.onosproject.net.topology.PathService;
Charles Chanc91c8782016-03-30 17:54:24 -070079import org.onosproject.net.topology.TopologyService;
80import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
81import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventre6b19e482016-11-07 16:21:04 -080082import org.onosproject.segmentrouting.config.PwaasConfig;
Pier Ventreb6a7f342016-11-26 21:05:22 -080083import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Pier Ventre6b19e482016-11-07 16:21:04 -080084import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Charles Chan82f19972016-05-17 13:13:55 -070085import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chanc91c8782016-03-30 17:54:24 -070086import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
87import org.onosproject.segmentrouting.grouphandler.NeighborSet;
Charles Chan1eaf4802016-04-18 13:44:03 -070088import org.onosproject.segmentrouting.storekey.NeighborSetNextObjectiveStoreKey;
89import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -070090import org.onosproject.segmentrouting.storekey.SubnetAssignedVidStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -080091import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -070092import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Pier Ventre6b19e482016-11-07 16:21:04 -080093import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Jonathan Hart54541d12016-04-12 15:39:44 -070094import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070095import org.onosproject.store.service.EventuallyConsistentMap;
96import org.onosproject.store.service.EventuallyConsistentMapBuilder;
97import org.onosproject.store.service.StorageService;
98import org.onosproject.store.service.WallClockTimestamp;
sangho80f11cb2015-04-01 13:05:26 -070099import org.slf4j.Logger;
100import org.slf4j.LoggerFactory;
101
sangho27462c62015-05-14 00:39:53 -0700102import java.util.List;
sangho80f11cb2015-04-01 13:05:26 -0700103import java.util.Map;
Saurav Das62ae6792017-05-15 15:34:25 -0700104import java.util.Map.Entry;
Saurav Das7c305372015-10-28 12:39:42 -0700105import java.util.Set;
sangho80f11cb2015-04-01 13:05:26 -0700106import java.util.concurrent.ConcurrentHashMap;
107import java.util.concurrent.ConcurrentLinkedQueue;
108import java.util.concurrent.Executors;
109import java.util.concurrent.ScheduledExecutorService;
110import java.util.concurrent.ScheduledFuture;
111import java.util.concurrent.TimeUnit;
Charles Chan90772a72017-02-08 15:52:08 -0800112import java.util.stream.Collectors;
sangho80f11cb2015-04-01 13:05:26 -0700113
Charles Chand6d25332016-02-26 22:19:52 -0800114import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800115import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700116import static org.onlab.util.Tools.groupedThreads;
Charles Chand6d25332016-02-26 22:19:52 -0800117
Charles Chanb7f75ac2016-01-11 18:28:54 -0800118/**
119 * Segment routing manager.
120 */
Jonathan Hart54541d12016-04-12 15:39:44 -0700121@Service
122@Component(immediate = true)
sangho27462c62015-05-14 00:39:53 -0700123public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700124
Charles Chan46fdfaf2016-11-09 20:51:44 -0800125 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
sangho80f11cb2015-04-01 13:05:26 -0700126
127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700128 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700129
130 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventreb6a7f342016-11-26 21:05:22 -0800131 private NeighbourResolutionService neighbourResolutionService;
132
133 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800134 public PathService pathService;
135
136 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700137 CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700138
139 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700140 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700141
142 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700143 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700144
145 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700146 DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700147
148 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800149 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700150
151 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700152 LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700153
Charles Chan82ab1932016-01-30 23:22:37 -0800154 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800155 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700156
157 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800158 public StorageService storageService;
Charles Chandebfea32016-10-24 14:52:01 -0700159
160 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
161 MulticastRouteService multicastRouteService;
162
163 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
164 TopologyService topologyService;
165
166 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700167 RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800168
169 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800170 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800171
Saurav Dasc3604f12016-03-23 11:22:49 -0700172 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800173 public InterfaceService interfaceService;
174
Charles Chandebfea32016-10-24 14:52:01 -0700175 ArpHandler arpHandler = null;
176 IcmpHandler icmpHandler = null;
177 IpHandler ipHandler = null;
178 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700179 ApplicationId appId;
180 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700181
Charles Chandebfea32016-10-24 14:52:01 -0700182 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700183 private TunnelHandler tunnelHandler = null;
184 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700185 private InternalPacketProcessor processor = null;
186 private InternalLinkListener linkListener = null;
187 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700188 private AppConfigHandler appCfgHandler = null;
Charles Chandebfea32016-10-24 14:52:01 -0700189 XConnectHandler xConnectHandler = null;
Charles Chan1eaf4802016-04-18 13:44:03 -0700190 private McastHandler mcastHandler = null;
Charles Chandebfea32016-10-24 14:52:01 -0700191 HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800192 private RouteHandler routeHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800193 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventre6b19e482016-11-07 16:21:04 -0800194 private L2TunnelHandler l2TunnelHandler = null;
sangho80f11cb2015-04-01 13:05:26 -0700195 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan82ab1932016-01-30 23:22:37 -0800196 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700197 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
198 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700199 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sangho80f11cb2015-04-01 13:05:26 -0700200
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700201 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700202 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sangho80f11cb2015-04-01 13:05:26 -0700203
Saurav Das2d94d312015-11-24 23:21:05 -0800204 @SuppressWarnings("unused")
sangho80f11cb2015-04-01 13:05:26 -0700205 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das2d94d312015-11-24 23:21:05 -0800206 @SuppressWarnings("rawtypes")
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700207 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chanf4586112015-11-09 16:37:23 -0800208 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chanb7f75ac2016-01-11 18:28:54 -0800209 new ConcurrentHashMap<>();
210 /**
211 * Per device next objective ID store with (device id + neighbor set) as key.
212 */
Ray Milkeyb85de082017-04-05 09:42:04 -0700213 EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer>
Charles Chanf4586112015-11-09 16:37:23 -0800214 nsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800215 /**
216 * Per device next objective ID store with (device id + subnet) as key.
217 */
Ray Milkeyb85de082017-04-05 09:42:04 -0700218 EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800219 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800220 /**
221 * Per device next objective ID store with (device id + port) as key.
222 */
Ray Milkeyb85de082017-04-05 09:42:04 -0700223 EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800224 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800225
Saurav Das62ae6792017-05-15 15:34:25 -0700226 // Local store for all links seen and their present status, used for
227 // optimized routing. The existence of the link in the keys is enough to know
228 // if the link has been "seen-before" by this instance of the controller.
229 // The boolean value indicates if the link is currently up or not.
230 // XXX Currently the optimized routing logic depends on "forgetting" a link
231 // when a switch goes down, but "remembering" it when only the link goes down.
232 // Consider changing this logic so we can use the Link Service instead of
233 // a local cache.
234 private Map<Link, Boolean> seenLinks = new ConcurrentHashMap<>();
235
Saurav Das2d94d312015-11-24 23:21:05 -0800236 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
237 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700238
Charles Chanc91c8782016-03-30 17:54:24 -0700239 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700240 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
241 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700242 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700243 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800244 public SegmentRoutingDeviceConfig createConfig() {
245 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700246 }
247 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800248
Charles Chanc91c8782016-03-30 17:54:24 -0700249 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700250 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
251 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700252 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800253 @Override
254 public SegmentRoutingAppConfig createConfig() {
255 return new SegmentRoutingAppConfig();
256 }
257 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800258
Charles Chan82f19972016-05-17 13:13:55 -0700259 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
260 new ConfigFactory<ApplicationId, XConnectConfig>(
261 SubjectFactories.APP_SUBJECT_FACTORY,
262 XConnectConfig.class, "xconnect") {
263 @Override
264 public XConnectConfig createConfig() {
265 return new XConnectConfig();
266 }
267 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800268
Charles Chanc91c8782016-03-30 17:54:24 -0700269 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700270 new ConfigFactory<ApplicationId, McastConfig>(
271 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700272 McastConfig.class, "multicast") {
273 @Override
274 public McastConfig createConfig() {
275 return new McastConfig();
276 }
277 };
278
Pier Ventre6b19e482016-11-07 16:21:04 -0800279 private final ConfigFactory<ApplicationId, PwaasConfig> pwaasConfigFactory =
280 new ConfigFactory<ApplicationId, PwaasConfig>(
281 SubjectFactories.APP_SUBJECT_FACTORY,
282 PwaasConfig.class, "pwaas") {
283 @Override
284 public PwaasConfig createConfig() {
285 return new PwaasConfig();
286 }
287 };
288
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700289 private Object threadSchedulerLock = new Object();
290 private static int numOfEventsQueued = 0;
291 private static int numOfEventsExecuted = 0;
sangho80f11cb2015-04-01 13:05:26 -0700292 private static int numOfHandlerExecution = 0;
293 private static int numOfHandlerScheduled = 0;
294
Charles Chan1963f4f2016-02-18 14:22:42 -0800295 /**
296 * Segment Routing App ID.
297 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800298 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan10b0fb72017-02-02 16:20:42 -0800299
Charles Chanb7f75ac2016-01-11 18:28:54 -0800300 /**
301 * The default VLAN ID assigned to the interfaces without subnet config.
302 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800303 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das7c305372015-10-28 12:39:42 -0700304
sangho80f11cb2015-04-01 13:05:26 -0700305 @Activate
306 protected void activate() {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800307 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700308
309 log.debug("Creating EC map nsnextobjectivestore");
310 EventuallyConsistentMapBuilder<NeighborSetNextObjectiveStoreKey, Integer>
311 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700312 nsNextObjStore = nsNextObjMapBuilder
313 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700314 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700315 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700316 .build();
317 log.trace("Current size {}", nsNextObjStore.size());
318
Charles Chan10b0fb72017-02-02 16:20:42 -0800319 log.debug("Creating EC map vlannextobjectivestore");
320 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
321 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
322 vlanNextObjStore = vlanNextObjMapBuilder
323 .withName("vlannextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700324 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700325 .withTimestampProvider((k, v) -> new WallClockTimestamp())
326 .build();
327
Saurav Das2d94d312015-11-24 23:21:05 -0800328 log.debug("Creating EC map subnetnextobjectivestore");
329 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
330 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
331 portNextObjStore = portNextObjMapBuilder
332 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700333 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800334 .withTimestampProvider((k, v) -> new WallClockTimestamp())
335 .build();
336
sangho4a5c42a2015-05-20 22:16:38 -0700337 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
338 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700339 tunnelStore = tunnelMapBuilder
340 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700341 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700342 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700343 .build();
344
345 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
346 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700347 policyStore = policyMapBuilder
348 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700349 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700350 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700351 .build();
352
Saurav Dasc3604f12016-03-23 11:22:49 -0700353 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800354 "purgeOnDisconnection", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700355 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800356 "purgeOnDisconnection", "true");
Pier Luigib9632ba2017-01-12 18:14:58 -0800357 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
358 "requestInterceptsEnabled", "false");
Pier Luigibc976df2017-01-12 22:46:39 -0800359 compCfgService.preSetProperty("org.onosproject.incubator.net.neighbour.impl.NeighbourResolutionManager",
360 "requestInterceptsEnabled", "false");
361 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelay",
362 "arpEnabled", "false");
Pier Luigi0ebeb312017-02-02 22:31:34 -0800363 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
364 "greedyLearningIpv6", "true");
Charles Chancf8ea472017-02-28 15:15:17 -0800365 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
366 "forceUnprovision", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700367
Charles Chan2b078ae2015-10-14 11:24:40 -0700368 processor = new InternalPacketProcessor();
369 linkListener = new InternalLinkListener();
370 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700371 appCfgHandler = new AppConfigHandler(this);
372 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700373 mcastHandler = new McastHandler(this);
374 hostHandler = new HostHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700375 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800376 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventre6b19e482016-11-07 16:21:04 -0800377 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700378
Charles Chand6d25332016-02-26 22:19:52 -0800379 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700380 cfgService.registerConfigFactory(deviceConfigFactory);
381 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700382 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700383 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventre6b19e482016-11-07 16:21:04 -0800384 cfgService.registerConfigFactory(pwaasConfigFactory);
Charles Chan82ab1932016-01-30 23:22:37 -0800385 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700386 packetService.addProcessor(processor, PacketProcessor.director(2));
387 linkService.addListener(linkListener);
388 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700389 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700390 routeService.addListener(routeListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700391
392 cfgListener.configureNetwork();
393
sangho80f11cb2015-04-01 13:05:26 -0700394 log.info("Started");
395 }
396
Jonathan Hart54541d12016-04-12 15:39:44 -0700397 private KryoNamespace.Builder createSerializer() {
398 return new KryoNamespace.Builder()
399 .register(KryoNamespaces.API)
400 .register(NeighborSetNextObjectiveStoreKey.class,
Charles Chan10b0fb72017-02-02 16:20:42 -0800401 VlanNextObjectiveStoreKey.class,
Jonathan Hart54541d12016-04-12 15:39:44 -0700402 SubnetAssignedVidStoreKey.class,
403 NeighborSet.class,
404 Tunnel.class,
405 DefaultTunnel.class,
406 Policy.class,
407 TunnelPolicy.class,
408 Policy.Type.class,
409 PortNextObjectiveStoreKey.class,
Charles Chan82f19972016-05-17 13:13:55 -0700410 XConnectStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700411 );
412 }
413
sangho80f11cb2015-04-01 13:05:26 -0700414 @Deactivate
415 protected void deactivate() {
Charles Chan72f556a2015-10-05 17:50:33 -0700416 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700417 cfgService.unregisterConfigFactory(deviceConfigFactory);
418 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700419 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700420 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventre6b19e482016-11-07 16:21:04 -0800421 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chan72f556a2015-10-05 17:50:33 -0700422
Charles Chanfd48c222017-06-19 00:43:31 -0700423 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700424 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700425 linkService.removeListener(linkListener);
426 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700427 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700428 routeService.removeListener(routeListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700429
Charles Chan2e71ef32017-02-23 15:44:08 -0800430 neighbourResolutionService.unregisterNeighbourHandlers(appId);
431
sangho80f11cb2015-04-01 13:05:26 -0700432 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700433 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700434 deviceListener = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700435 groupHandlerMap.clear();
436
Charles Chanc91c8782016-03-30 17:54:24 -0700437 nsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800438 vlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700439 portNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700440 tunnelStore.destroy();
441 policyStore.destroy();
sangho80f11cb2015-04-01 13:05:26 -0700442 log.info("Stopped");
443 }
444
sangho27462c62015-05-14 00:39:53 -0700445 @Override
446 public List<Tunnel> getTunnels() {
447 return tunnelHandler.getTunnels();
448 }
449
450 @Override
sanghobd812f82015-06-29 14:58:47 -0700451 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
452 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700453 }
454
455 @Override
sanghobd812f82015-06-29 14:58:47 -0700456 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700457 for (Policy policy: policyHandler.getPolicies()) {
458 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
459 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
460 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
461 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700462 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700463 }
464 }
465 }
sanghobd812f82015-06-29 14:58:47 -0700466 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700467 }
468
469 @Override
sanghobd812f82015-06-29 14:58:47 -0700470 public PolicyHandler.Result removePolicy(Policy policy) {
471 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700472 }
473
474 @Override
sanghobd812f82015-06-29 14:58:47 -0700475 public PolicyHandler.Result createPolicy(Policy policy) {
476 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700477 }
478
479 @Override
480 public List<Policy> getPolicies() {
481 return policyHandler.getPolicies();
482 }
483
Saurav Das07c74602016-04-27 18:35:50 -0700484 @Override
485 public void rerouteNetwork() {
486 cfgListener.configureNetwork();
487 for (Device device : deviceService.getDevices()) {
Saurav Dasf9332192017-02-18 14:05:44 -0800488 if (mastershipService.isLocalMaster(device.id())) {
489 defaultRoutingHandler.populatePortAddressingRules(device.id());
490 }
Saurav Das07c74602016-04-27 18:35:50 -0700491 }
492 defaultRoutingHandler.startPopulationProcess();
493 }
494
Charles Chand7844e52016-10-20 17:02:44 -0700495 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800496 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
497 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Charles Chand7844e52016-10-20 17:02:44 -0700498 deviceService.getAvailableDevices().forEach(device -> {
499 deviceSubnetMap.put(device.id(), deviceConfiguration.getSubnets(device.id()));
500 });
501 return deviceSubnetMap;
502 }
503
Saurav Das62ae6792017-05-15 15:34:25 -0700504
505 @Override
506 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
507 if (defaultRoutingHandler != null) {
508 return defaultRoutingHandler.getCurrentEmcpSpgMap();
509 } else {
510 return null;
511 }
512 }
513
514 @Override
515 public ImmutableMap<NeighborSetNextObjectiveStoreKey, Integer> getNeighborSet() {
516 if (nsNextObjStore != null) {
517 return ImmutableMap.copyOf(nsNextObjStore.entrySet());
518 } else {
519 return ImmutableMap.of();
520 }
521 }
522
sangho80f1f892015-05-19 11:57:42 -0700523 /**
Ray Milkeyb85de082017-04-05 09:42:04 -0700524 * Extracts the application ID from the manager.
525 *
526 * @return application ID
527 */
528 public ApplicationId appId() {
529 return appId;
530 }
531
532 /**
533 * Returns the device configuration.
534 *
535 * @return device configuration
536 */
537 public DeviceConfiguration deviceConfiguration() {
538 return deviceConfiguration;
539 }
540
541 /**
542 * Per device next objective ID store with (device id + neighbor set) as key.
543 *
544 * @return next objective ID store
545 */
546 public EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer> nsNextObjStore() {
547 return nsNextObjStore;
548 }
549
550 /**
551 * Per device next objective ID store with (device id + subnet) as key.
552 *
553 * @return vlan next object store
554 */
555 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
556 return vlanNextObjStore;
557 }
558
559 /**
560 * Per device next objective ID store with (device id + port) as key.
561 *
562 * @return port next object store.
563 */
564 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
565 return portNextObjStore;
566 }
567
568 /**
Pier Ventre7a78de22016-10-31 15:00:01 -0700569 * Returns the MPLS-ECMP configuration.
570 *
571 * @return MPLS-ECMP value
572 */
573 public boolean getMplsEcmp() {
574 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
575 .getConfig(this.appId, SegmentRoutingAppConfig.class);
576 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
577 }
578
579 /**
sangho80f1f892015-05-19 11:57:42 -0700580 * Returns the tunnel object with the tunnel ID.
581 *
582 * @param tunnelId Tunnel ID
583 * @return Tunnel reference
584 */
sangho27462c62015-05-14 00:39:53 -0700585 public Tunnel getTunnel(String tunnelId) {
586 return tunnelHandler.getTunnel(tunnelId);
587 }
588
Charles Chan90772a72017-02-08 15:52:08 -0800589 // TODO Consider moving these to InterfaceService
sangho80f11cb2015-04-01 13:05:26 -0700590 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800591 * Returns untagged VLAN configured on given connect point.
Charles Chan90772a72017-02-08 15:52:08 -0800592 * <p>
593 * Only returns the first match if there are multiple untagged VLAN configured
594 * on the connect point.
sangho80f11cb2015-04-01 13:05:26 -0700595 *
Charles Chan10b0fb72017-02-02 16:20:42 -0800596 * @param connectPoint connect point
597 * @return untagged VLAN or null if not configured
sangho80f11cb2015-04-01 13:05:26 -0700598 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800599 public VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
600 return interfaceService.getInterfacesByPort(connectPoint).stream()
601 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
602 .map(Interface::vlanUntagged)
603 .findFirst().orElse(null);
sangho80f11cb2015-04-01 13:05:26 -0700604 }
605
sangho27462c62015-05-14 00:39:53 -0700606 /**
Charles Chan90772a72017-02-08 15:52:08 -0800607 * Returns tagged VLAN configured on given connect point.
608 * <p>
609 * Returns all matches if there are multiple tagged VLAN configured
610 * on the connect point.
611 *
612 * @param connectPoint connect point
613 * @return tagged VLAN or empty set if not configured
614 */
615 public Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
616 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
617 return interfaces.stream()
618 .map(Interface::vlanTagged)
619 .flatMap(vlanIds -> vlanIds.stream())
620 .collect(Collectors.toSet());
621 }
622
623 /**
624 * Returns native VLAN configured on given connect point.
625 * <p>
626 * Only returns the first match if there are multiple native VLAN configured
627 * on the connect point.
628 *
629 * @param connectPoint connect point
630 * @return native VLAN or null if not configured
631 */
632 public VlanId getNativeVlanId(ConnectPoint connectPoint) {
633 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
634 return interfaces.stream()
635 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
636 .map(Interface::vlanNative)
637 .findFirst()
638 .orElse(null);
639 }
640
641 /**
642 * Returns vlan port map of given device.
643 *
644 * @param deviceId device id
645 * @return vlan-port multimap
646 */
647 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
648 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
649
650 interfaceService.getInterfaces().stream()
651 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
652 .forEach(intf -> {
653 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
654 intf.vlanTagged().forEach(vlanTagged -> {
655 vlanPortMap.put(vlanTagged, intf.connectPoint().port());
656 });
657 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
658 });
659 vlanPortMap.removeAll(VlanId.NONE);
660
661 return vlanPortMap;
662 }
663
664 /**
Saurav Das2d94d312015-11-24 23:21:05 -0800665 * Returns the next objective ID for the given subnet prefix. It is expected
Charles Chan10b0fb72017-02-02 16:20:42 -0800666 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -0800667 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -0700668 *
669 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -0800670 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -0800671 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -0700672 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800673 public int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -0700674 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800675 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
676 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -0700677 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -0800678 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -0800679 + "device {} not found", deviceId);
680 return -1;
681 }
682 }
683
684 /**
685 * Returns the next objective ID for the given portNumber, given the treatment.
686 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -0700687 * would result in different objectives. If the next object does not exist,
688 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -0800689 *
690 * @param deviceId Device ID
691 * @param portNum port number on device for which NextObjective is queried
692 * @param treatment the actions to apply on the packets (should include outport)
693 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -0700694 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -0700695 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -0800696 */
697 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
698 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -0700699 TrafficSelector meta,
700 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -0800701 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
702 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -0700703 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -0800704 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800705 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
706 + " not found", deviceId);
707 return -1;
708 }
709 }
710
Saurav Das62ae6792017-05-15 15:34:25 -0700711 /**
712 * Returns the group handler object for the specified device id.
713 *
714 * @param devId the device identifier
715 * @return the groupHandler object for the device id, or null if not found
716 */
717 public DefaultGroupHandler getGroupHandler(DeviceId devId) {
718 return groupHandlerMap.get(devId);
719 }
720
721 /**
722 * Returns true if this controller instance has seen this link before. The
723 * link may not be currently up, but as long as the link had been seen before
724 * this method will return true. The one exception is when the link was
725 * indeed seen before, but this controller instance was forced to forget it
726 * by a call to purgeSeenLink method.
727 *
728 * @param link the infrastructure link being queried
729 * @return true if this controller instance has seen this link before
730 */
731 public boolean isSeenLink(Link link) {
732 return seenLinks.containsKey(link);
733 }
734
735 /**
736 * Updates the seen link store. Updates can be for links that are currently
737 * available or not.
738 *
739 * @param link the link to update in the seen-link local store
740 * @param up the status of the link, true if up, false if down
741 */
742 public void updateSeenLink(Link link, boolean up) {
743 seenLinks.put(link, up);
744 }
745
746 /**
747 * Returns the status of a seen-link (up or down). If the link has not
748 * been seen-before, a null object is returned.
749 *
750 * @param link the infrastructure link being queried
751 * @return null if the link was not seen-before;
752 * true if the seen-link is up;
753 * false if the seen-link is down
754 */
755 public Boolean isSeenLinkUp(Link link) {
756 return seenLinks.get(link);
757 }
758
759 /**
760 * Makes this controller instance forget a previously seen before link.
761 *
762 * @param link the infrastructure link to purge
763 */
764 public void purgeSeenLink(Link link) {
765 seenLinks.remove(link);
766 }
767
768 /**
769 * Returns the status of a link as parallel link. A parallel link
770 * is defined as a link which has common src and dst switches as another
771 * seen-link that is currently enabled. It is not necessary for the link being
772 * queried to be a seen-link.
773 *
774 * @param link the infrastructure link being queried
775 * @return true if a seen-link exists that is up, and shares the
776 * same src and dst switches as the link being queried
777 */
778 public boolean isParallelLink(Link link) {
779 for (Entry<Link, Boolean> seen : seenLinks.entrySet()) {
780 Link seenLink = seen.getKey();
781 if (seenLink.equals(link)) {
782 continue;
783 }
784 if (seenLink.src().deviceId().equals(link.src().deviceId()) &&
785 seenLink.dst().deviceId().equals(link.dst().deviceId()) &&
786 seen.getValue()) {
787 return true;
788 }
789 }
790 return false;
791 }
792
sangho80f11cb2015-04-01 13:05:26 -0700793 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -0700794 @Override
795 public void process(PacketContext context) {
796
797 if (context.isHandled()) {
798 return;
799 }
800
801 InboundPacket pkt = context.inPacket();
802 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -0800803
804 if (ethernet == null) {
805 return;
806 }
807
Saurav Das2d94d312015-11-24 23:21:05 -0800808 log.trace("Rcvd pktin: {}", ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800809 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -0700810 log.warn("Received unexpected ARP packet on {}",
811 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -0700812 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800813 return;
sangho80f11cb2015-04-01 13:05:26 -0700814 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -0800815 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
816 //ipHandler.addToPacketBuffer(ipv4Packet);
817 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
818 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700819 } else {
Charles Chand041ad82017-01-13 17:20:44 -0800820 // NOTE: We don't support IP learning at this moment so this
821 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800822 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700823 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800824 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
825 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -0800826 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -0800827 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -0800828 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
829 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
830 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
831 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
832 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
833 } else {
Saurav Das62ae6792017-05-15 15:34:25 -0700834 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -0700835 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -0800836 }
837 } else {
838 // NOTE: We don't support IP learning at this moment so this
839 // is not necessary. Also it causes duplication of DHCPv6 packets.
840 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
841 }
sangho80f11cb2015-04-01 13:05:26 -0700842 }
843 }
844 }
845
846 private class InternalLinkListener implements LinkListener {
847 @Override
848 public void event(LinkEvent event) {
Charles Chan0cff8aa2017-03-29 16:39:05 -0700849 if (event.type() == LinkEvent.Type.LINK_ADDED ||
850 event.type() == LinkEvent.Type.LINK_UPDATED ||
851 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700852 log.debug("Event {} received from Link Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -0700853 scheduleEventHandlerIfNotScheduled(event);
854 }
855 }
856 }
857
858 private class InternalDeviceListener implements DeviceListener {
sangho80f11cb2015-04-01 13:05:26 -0700859 @Override
860 public void event(DeviceEvent event) {
sangho80f11cb2015-04-01 13:05:26 -0700861 switch (event.type()) {
862 case DEVICE_ADDED:
Saurav Dasf0f592d2016-11-18 15:21:57 -0800863 case PORT_UPDATED:
864 case PORT_ADDED:
sanghofb7c7292015-04-13 15:15:58 -0700865 case DEVICE_UPDATED:
866 case DEVICE_AVAILABILITY_CHANGED:
Saurav Das62ae6792017-05-15 15:34:25 -0700867 log.trace("Event {} received from Device Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -0700868 scheduleEventHandlerIfNotScheduled(event);
869 break;
870 default:
871 }
872 }
873 }
874
Saurav Das2d94d312015-11-24 23:21:05 -0800875 @SuppressWarnings("rawtypes")
sangho80f11cb2015-04-01 13:05:26 -0700876 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700877 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700878 eventQueue.add(event);
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700879 numOfEventsQueued++;
880
881 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
882 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700883 eventHandlerFuture = executorService
884 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
885 numOfHandlerScheduled++;
886 }
Jonathan Hart54541d12016-04-12 15:39:44 -0700887 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700888 numOfEventsQueued,
889 numOfHandlerScheduled);
sangho80f11cb2015-04-01 13:05:26 -0700890 }
sangho80f11cb2015-04-01 13:05:26 -0700891 }
892
893 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700894 @Override
sangho80f11cb2015-04-01 13:05:26 -0700895 public void run() {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700896 try {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700897 while (true) {
Saurav Das2d94d312015-11-24 23:21:05 -0800898 @SuppressWarnings("rawtypes")
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700899 Event event = null;
900 synchronized (threadSchedulerLock) {
901 if (!eventQueue.isEmpty()) {
902 event = eventQueue.poll();
903 numOfEventsExecuted++;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700904 } else {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700905 numOfHandlerExecution++;
906 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
907 numOfHandlerExecution, numOfEventsExecuted);
908 break;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700909 }
sanghofb7c7292015-04-13 15:15:58 -0700910 }
Charles Chan0cff8aa2017-03-29 16:39:05 -0700911 if (event.type() == LinkEvent.Type.LINK_ADDED ||
912 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Das62ae6792017-05-15 15:34:25 -0700913 // Note: do not update seenLinks here, otherwise every
914 // link, even one seen for the first time, will be appear
915 // to be a previously seen link
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700916 processLinkAdded((Link) event.subject());
917 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre6d593892016-09-13 21:33:40 -0700918 Link linkRemoved = (Link) event.subject();
Saurav Das62ae6792017-05-15 15:34:25 -0700919 if (linkRemoved.type() == Link.Type.DIRECT) {
920 updateSeenLink(linkRemoved, false);
921 }
922 // device availability check helps to ensure that
923 // multiple link-removed events are actually treated as a
924 // single switch removed event. purgeSeenLink is necessary
925 // so we do rerouting (instead of rehashing) when switch
926 // comes back.
Pier Ventre6d593892016-09-13 21:33:40 -0700927 if (linkRemoved.src().elementId() instanceof DeviceId &&
928 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
Saurav Das62ae6792017-05-15 15:34:25 -0700929 purgeSeenLink(linkRemoved);
Pier Ventre6d593892016-09-13 21:33:40 -0700930 continue;
931 }
932 if (linkRemoved.dst().elementId() instanceof DeviceId &&
933 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
Saurav Das62ae6792017-05-15 15:34:25 -0700934 purgeSeenLink(linkRemoved);
Pier Ventre6d593892016-09-13 21:33:40 -0700935 continue;
936 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700937 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700938 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
939 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
940 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das62af8802015-12-04 10:52:59 -0800941 DeviceId deviceId = ((Device) event.subject()).id();
942 if (deviceService.isAvailable(deviceId)) {
Saurav Dasc28b3432015-10-30 17:45:38 -0700943 log.info("Processing device event {} for available device {}",
944 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700945 processDeviceAdded((Device) event.subject());
Saurav Dasc3604f12016-03-23 11:22:49 -0700946 } else {
947 log.info("Processing device event {} for unavailable device {}",
948 event.type(), ((Device) event.subject()).id());
949 processDeviceRemoved((Device) event.subject());
950 }
Saurav Dasf0f592d2016-11-18 15:21:57 -0800951 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd1872b02016-12-02 15:43:47 -0800952 // typically these calls come when device is added first time
953 // so port filtering rules are handled at the device_added event.
954 // port added calls represent all ports on the device,
955 // enabled or not.
Saurav Das62ae6792017-05-15 15:34:25 -0700956 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd1872b02016-12-02 15:43:47 -0800957 ((DeviceEvent) event).subject().id(),
958 ((DeviceEvent) event).port().number(),
959 event.type());
Saurav Dasf0f592d2016-11-18 15:21:57 -0800960 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd1872b02016-12-02 15:43:47 -0800961 // these calls happen for every subsequent event
962 // ports enabled, disabled, switch goes away, comes back
Saurav Dasf0f592d2016-11-18 15:21:57 -0800963 log.info("** PORT UPDATED {}/{} -> {}",
964 event.subject(),
965 ((DeviceEvent) event).port(),
966 event.type());
967 processPortUpdated(((Device) event.subject()),
968 ((DeviceEvent) event).port());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700969 } else {
970 log.warn("Unhandled event type: {}", event.type());
971 }
sangho80f11cb2015-04-01 13:05:26 -0700972 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700973 } catch (Exception e) {
974 log.error("SegmentRouting event handler "
975 + "thread thrown an exception: {}", e);
sangho80f11cb2015-04-01 13:05:26 -0700976 }
sangho80f11cb2015-04-01 13:05:26 -0700977 }
978 }
979
sangho80f11cb2015-04-01 13:05:26 -0700980 private void processLinkAdded(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -0700981 log.info("** LINK ADDED {}", link.toString());
Charles Chan319d1a22015-11-03 10:42:14 -0800982 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
983 log.warn("Source device of this link is not configured.");
984 return;
985 }
Saurav Das62ae6792017-05-15 15:34:25 -0700986 if (link.type() != Link.Type.DIRECT) {
987 // NOTE: A DIRECT link might be transiently marked as INDIRECT
988 // if BDDP is received before LLDP. We can safely ignore that
989 // until the LLDP is received and the link is marked as DIRECT.
990 log.info("Ignore link {}->{}. Link type is {} instead of DIRECT.",
991 link.src(), link.dst(), link.type());
992 return;
993 }
994
995 //Irrespective of whether the local is a MASTER or not for this device,
996 //create group handler instance and push default TTP flow rules if needed,
997 //as in a multi-instance setup, instances can initiate groups for any device.
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700998 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
999 .deviceId());
1000 if (groupHandler != null) {
Saurav Das62ae6792017-05-15 15:34:25 -07001001 groupHandler.portUpForLink(link);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001002 } else {
1003 Device device = deviceService.getDevice(link.src().deviceId());
1004 if (device != null) {
1005 log.warn("processLinkAdded: Link Added "
1006 + "Notification without Device Added "
1007 + "event, still handling it");
1008 processDeviceAdded(device);
1009 groupHandler = groupHandlerMap.get(link.src()
1010 .deviceId());
Saurav Das62ae6792017-05-15 15:34:25 -07001011 groupHandler.portUpForLink(link);
sangho80f11cb2015-04-01 13:05:26 -07001012 }
1013 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001014
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001015 log.trace("Starting optimized route population process");
Saurav Das62ae6792017-05-15 15:34:25 -07001016 boolean seenBefore = isSeenLink(link);
1017 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null, link, null);
1018 if (mastershipService.isLocalMaster(link.src().deviceId())) {
1019 if (!seenBefore) {
1020 // if link seen first time, we need to ensure hash-groups have all ports
1021 groupHandler.retryHash(link, false, true);
1022 } else {
1023 //seen before-link
1024 if (isParallelLink(link)) {
1025 groupHandler.retryHash(link, false, false);
1026 }
1027 }
1028 }
Charles Chan72779502016-04-23 17:36:10 -07001029
1030 mcastHandler.init();
sangho80f11cb2015-04-01 13:05:26 -07001031 }
1032
1033 private void processLinkRemoved(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -07001034 log.info("** LINK REMOVED {}", link.toString());
Saurav Das62ae6792017-05-15 15:34:25 -07001035 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link, null, null);
1036
1037 // update local groupHandler stores
sangho2165d222015-05-01 09:38:25 -07001038 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
1039 if (groupHandler != null) {
Saurav Das62ae6792017-05-15 15:34:25 -07001040 if (mastershipService.isLocalMaster(link.src().deviceId()) &&
1041 isParallelLink(link)) {
1042 groupHandler.retryHash(link, true, false);
1043 }
1044 // ensure local stores are updated
1045 groupHandler.portDown(link.src().port());
1046 } else {
1047 log.warn("group handler not found for dev:{} when removing link: {}",
1048 link.src().deviceId(), link);
sangho2165d222015-05-01 09:38:25 -07001049 }
Charles Chan72779502016-04-23 17:36:10 -07001050
1051 mcastHandler.processLinkDown(link);
sangho80f11cb2015-04-01 13:05:26 -07001052 }
1053
1054 private void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001055 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001056
1057 // NOTE: Punt ARP/NDP even when the device is not configured.
1058 // Host learning without network config is required for CORD config generator.
1059 routingRulePopulator.populateIpPunts(device.id());
1060 routingRulePopulator.populateArpNdpPunts(device.id());
1061
Charles Chan319d1a22015-11-03 10:42:14 -08001062 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das8ec0ec42015-11-03 14:39:27 -08001063 log.warn("Device configuration uploading. Device {} will be "
1064 + "processed after config completes.", device.id());
1065 return;
1066 }
Charles Chan72779502016-04-23 17:36:10 -07001067 processDeviceAddedInternal(device.id());
1068 }
1069
1070 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001071 // Irrespective of whether the local is a MASTER or not for this device,
1072 // we need to create a SR-group-handler instance. This is because in a
1073 // multi-instance setup, any instance can initiate forwarding/next-objectives
1074 // for any switch (even if this instance is a SLAVE or not even connected
1075 // to the switch). To handle this, a default-group-handler instance is necessary
1076 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001077 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1078 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001079 DefaultGroupHandler groupHandler;
1080 try {
1081 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001082 createGroupHandler(deviceId,
1083 appId,
1084 deviceConfiguration,
1085 linkService,
1086 flowObjectiveService,
1087 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001088 } catch (DeviceConfigNotFoundException e) {
1089 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1090 return;
1091 }
Charles Chan72779502016-04-23 17:36:10 -07001092 log.debug("updating groupHandlerMap with new config for device: {}",
1093 deviceId);
1094 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001095 }
Saurav Dasb149be12016-06-07 10:08:06 -07001096
Charles Chan72779502016-04-23 17:36:10 -07001097 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001098 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chandebfea32016-10-24 14:52:01 -07001099 hostHandler.init(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -07001100 xConnectHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001101 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001102 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001103 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001104 }
Charles Chan82ab1932016-01-30 23:22:37 -08001105
Charles Chandebfea32016-10-24 14:52:01 -07001106 appCfgHandler.init(deviceId);
1107 routeHandler.init(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001108 }
1109
Saurav Dasc3604f12016-03-23 11:22:49 -07001110 private void processDeviceRemoved(Device device) {
1111 nsNextObjStore.entrySet().stream()
1112 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1113 .forEach(entry -> {
1114 nsNextObjStore.remove(entry.getKey());
1115 });
Charles Chan10b0fb72017-02-02 16:20:42 -08001116 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001117 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1118 .forEach(entry -> {
Charles Chan10b0fb72017-02-02 16:20:42 -08001119 vlanNextObjStore.remove(entry.getKey());
Saurav Dasc3604f12016-03-23 11:22:49 -07001120 });
Saurav Dasc3604f12016-03-23 11:22:49 -07001121 portNextObjStore.entrySet().stream()
1122 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1123 .forEach(entry -> {
1124 portNextObjStore.remove(entry.getKey());
1125 });
Charles Chan17d75d82017-06-15 00:44:51 -07001126
1127 seenLinks.keySet().removeIf(key -> key.src().deviceId().equals(device.id()) ||
1128 key.dst().deviceId().equals(device.id()));
1129
Saurav Dasc3604f12016-03-23 11:22:49 -07001130 groupHandlerMap.remove(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -07001131 defaultRoutingHandler.purgeEcmpGraph(device.id());
Saurav Das62ae6792017-05-15 15:34:25 -07001132 // Note that a switch going down is associated with all of its links
1133 // going down as well, but it is treated as a single switch down event
1134 // while the link-downs are ignored.
1135 defaultRoutingHandler
1136 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Charles Chan72779502016-04-23 17:36:10 -07001137 mcastHandler.removeDevice(device.id());
Charles Chan82f19972016-05-17 13:13:55 -07001138 xConnectHandler.removeDevice(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -07001139 }
1140
Saurav Dasf0f592d2016-11-18 15:21:57 -08001141 private void processPortUpdated(Device device, Port port) {
1142 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1143 log.warn("Device configuration uploading. Not handling port event for"
1144 + "dev: {} port: {}", device.id(), port.number());
1145 return;
1146 }
Saurav Dasf9332192017-02-18 14:05:44 -08001147
1148 if (!mastershipService.isLocalMaster(device.id())) {
1149 log.debug("Not master for dev:{} .. not handling port updated event"
1150 + "for port {}", device.id(), port.number());
1151 return;
1152 }
1153
1154 // first we handle filtering rules associated with the port
1155 if (port.isEnabled()) {
1156 log.info("Switchport {}/{} enabled..programming filters",
1157 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001158 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001159 } else {
1160 log.info("Switchport {}/{} disabled..removing filters",
1161 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001162 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001163 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001164
1165 // portUpdated calls are for ports that have gone down or up. For switch
1166 // to switch ports, link-events should take care of any re-routing or
1167 // group editing necessary for port up/down. Here we only process edge ports
1168 // that are already configured.
Saurav Das3fb28272017-03-04 16:08:47 -08001169 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1170 VlanId untaggedVlan = getUntaggedVlanId(cp);
1171 VlanId nativeVlan = getNativeVlanId(cp);
1172 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001173
Saurav Das3fb28272017-03-04 16:08:47 -08001174 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001175 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasf0f592d2016-11-18 15:21:57 -08001176 + "dev/port: {}/{}", device.id(), port.number());
1177 return;
1178 }
Saurav Das3fb28272017-03-04 16:08:47 -08001179 if (untaggedVlan != null) {
1180 processEdgePort(device, port, untaggedVlan, true);
1181 }
1182 if (nativeVlan != null) {
1183 processEdgePort(device, port, nativeVlan, true);
1184 }
1185 if (!taggedVlans.isEmpty()) {
1186 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1187 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001188 }
1189
Saurav Das3fb28272017-03-04 16:08:47 -08001190 private void processEdgePort(Device device, Port port, VlanId vlanId,
1191 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001192 boolean portUp = port.isEnabled();
1193 if (portUp) {
Saurav Das3fb28272017-03-04 16:08:47 -08001194 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001195 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001196 } else {
Saurav Das3fb28272017-03-04 16:08:47 -08001197 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001198 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001199 }
1200
Srikanth Vavilapalli64505482015-04-21 13:04:13 -07001201 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sangho80f11cb2015-04-01 13:05:26 -07001202 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001203 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001204 } else {
1205 log.warn("Group handler not found for dev:{}. Not handling edge port"
1206 + " {} event for port:{}", device.id(),
1207 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001208 }
1209 }
sangho27462c62015-05-14 00:39:53 -07001210
Pier Ventreb6a7f342016-11-26 21:05:22 -08001211 /**
1212 * Registers the given connect point with the NRS, this is necessary
1213 * to receive the NDP and ARP packets from the NRS.
1214 *
1215 * @param portToRegister connect point to register
1216 */
1217 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001218 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001219 portToRegister,
1220 neighbourHandler,
1221 appId
1222 );
1223 }
1224
Charles Chan72f556a2015-10-05 17:50:33 -07001225 private class InternalConfigListener implements NetworkConfigListener {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001226 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001227
Charles Chanb7f75ac2016-01-11 18:28:54 -08001228 /**
1229 * Constructs the internal network config listener.
1230 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001231 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001232 */
Charles Chan46fdfaf2016-11-09 20:51:44 -08001233 public InternalConfigListener(SegmentRoutingManager srManager) {
1234 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001235 }
1236
Charles Chanb7f75ac2016-01-11 18:28:54 -08001237 /**
1238 * Reads network config and initializes related data structure accordingly.
1239 */
Charles Chane7c61022015-10-07 14:21:45 -07001240 public void configureNetwork() {
Pier Ventreb6a7f342016-11-26 21:05:22 -08001241
Charles Chan46fdfaf2016-11-09 20:51:44 -08001242 deviceConfiguration = new DeviceConfiguration(srManager);
Charles Chane7c61022015-10-07 14:21:45 -07001243
Charles Chan46fdfaf2016-11-09 20:51:44 -08001244 arpHandler = new ArpHandler(srManager);
1245 icmpHandler = new IcmpHandler(srManager);
1246 ipHandler = new IpHandler(srManager);
1247 routingRulePopulator = new RoutingRulePopulator(srManager);
1248 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chane7c61022015-10-07 14:21:45 -07001249
1250 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1251 groupHandlerMap, tunnelStore);
1252 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1253 flowObjectiveService,
1254 tunnelHandler, policyStore);
1255
Charles Chane7c61022015-10-07 14:21:45 -07001256 for (Device device : deviceService.getDevices()) {
Charles Chan72779502016-04-23 17:36:10 -07001257 processDeviceAddedInternal(device.id());
Charles Chane7c61022015-10-07 14:21:45 -07001258 }
1259
1260 defaultRoutingHandler.startPopulationProcess();
Charles Chan72779502016-04-23 17:36:10 -07001261 mcastHandler.init();
Charles Chane7c61022015-10-07 14:21:45 -07001262 }
1263
Charles Chan72f556a2015-10-05 17:50:33 -07001264 @Override
1265 public void event(NetworkConfigEvent event) {
Charles Chan82ab1932016-01-30 23:22:37 -08001266 // TODO move this part to NetworkConfigEventHandler
1267 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1268 switch (event.type()) {
1269 case CONFIG_ADDED:
Charles Chan68363b12017-06-26 15:25:09 -07001270 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan82ab1932016-01-30 23:22:37 -08001271 configureNetwork();
1272 break;
1273 case CONFIG_UPDATED:
Charles Chan68363b12017-06-26 15:25:09 -07001274 log.info("Segment Routing Config updated for {}", event.subject());
1275 // TODO support dynamic configuration
1276 break;
1277 default:
1278 break;
1279 }
1280 } else if (event.configClass().equals(InterfaceConfig.class)) {
1281 switch (event.type()) {
1282 case CONFIG_ADDED:
1283 log.info("Interface Config added for {}", event.subject());
1284 configureNetwork();
1285 break;
1286 case CONFIG_UPDATED:
1287 log.info("Interface Config updated for {}", event.subject());
Charles Chan82ab1932016-01-30 23:22:37 -08001288 // TODO support dynamic configuration
1289 break;
1290 default:
1291 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001292 }
Charles Chan82ab1932016-01-30 23:22:37 -08001293 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chan82f19972016-05-17 13:13:55 -07001294 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan82ab1932016-01-30 23:22:37 -08001295 switch (event.type()) {
1296 case CONFIG_ADDED:
Charles Chan82f19972016-05-17 13:13:55 -07001297 appCfgHandler.processAppConfigAdded(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001298 break;
1299 case CONFIG_UPDATED:
Charles Chan82f19972016-05-17 13:13:55 -07001300 appCfgHandler.processAppConfigUpdated(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001301 break;
1302 case CONFIG_REMOVED:
Charles Chan82f19972016-05-17 13:13:55 -07001303 appCfgHandler.processAppConfigRemoved(event);
1304 break;
1305 default:
1306 break;
1307 }
Charles Chandebfea32016-10-24 14:52:01 -07001308 configureNetwork();
Charles Chan82f19972016-05-17 13:13:55 -07001309 } else if (event.configClass().equals(XConnectConfig.class)) {
1310 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1311 switch (event.type()) {
1312 case CONFIG_ADDED:
1313 xConnectHandler.processXConnectConfigAdded(event);
1314 break;
1315 case CONFIG_UPDATED:
1316 xConnectHandler.processXConnectConfigUpdated(event);
1317 break;
1318 case CONFIG_REMOVED:
1319 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001320 break;
1321 default:
1322 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001323 }
Pier Ventre6b19e482016-11-07 16:21:04 -08001324 } else if (event.configClass().equals(PwaasConfig.class)) {
1325 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1326 switch (event.type()) {
1327 case CONFIG_ADDED:
1328 l2TunnelHandler.processPwaasConfigAdded(event);
1329 break;
1330 case CONFIG_UPDATED:
1331 l2TunnelHandler.processPwaasConfigUpdated(event);
1332 break;
1333 case CONFIG_REMOVED:
1334 l2TunnelHandler.processPwaasConfigRemoved(event);
1335 break;
1336 default:
1337 break;
1338 }
Charles Chan72f556a2015-10-05 17:50:33 -07001339 }
1340 }
1341 }
Charles Chanf4586112015-11-09 16:37:23 -08001342
1343 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001344 @Override
1345 public void event(HostEvent event) {
1346 // Do not proceed without mastership
1347 DeviceId deviceId = event.subject().location().deviceId();
1348 if (!mastershipService.isLocalMaster(deviceId)) {
1349 return;
1350 }
1351
1352 switch (event.type()) {
1353 case HOST_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001354 hostHandler.processHostAddedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001355 break;
1356 case HOST_MOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001357 hostHandler.processHostMovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001358 break;
1359 case HOST_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001360 hostHandler.processHostRemoveEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001361 break;
1362 case HOST_UPDATED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001363 hostHandler.processHostUpdatedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001364 break;
1365 default:
1366 log.warn("Unsupported host event type: {}", event.type());
1367 break;
1368 }
1369 }
1370 }
1371
Charles Chanc91c8782016-03-30 17:54:24 -07001372 private class InternalMcastListener implements McastListener {
1373 @Override
1374 public void event(McastEvent event) {
1375 switch (event.type()) {
1376 case SOURCE_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001377 mcastHandler.processSourceAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001378 break;
1379 case SINK_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001380 mcastHandler.processSinkAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001381 break;
1382 case SINK_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001383 mcastHandler.processSinkRemoved(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001384 break;
1385 case ROUTE_ADDED:
1386 case ROUTE_REMOVED:
1387 default:
1388 break;
1389 }
1390 }
1391 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001392
Charles Chandebfea32016-10-24 14:52:01 -07001393 private class InternalRouteEventListener implements RouteListener {
1394 @Override
1395 public void event(RouteEvent event) {
1396 switch (event.type()) {
1397 case ROUTE_ADDED:
1398 routeHandler.processRouteAdded(event);
1399 break;
1400 case ROUTE_UPDATED:
1401 routeHandler.processRouteUpdated(event);
1402 break;
1403 case ROUTE_REMOVED:
1404 routeHandler.processRouteRemoved(event);
1405 break;
1406 default:
1407 break;
1408 }
1409 }
1410 }
Saurav Das62ae6792017-05-15 15:34:25 -07001411
sangho80f11cb2015-04-01 13:05:26 -07001412}