blob: 9ac8d1ad4dacf718aa2713ffdb0f67c1928e987a [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sangho80f11cb2015-04-01 13:05:26 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.segmentrouting;
17
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;
Saurav Das261c3002017-06-13 15:35:54 -070087import org.onosproject.segmentrouting.grouphandler.DestinationSet;
88import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
89import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -070090import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -070091import org.onosproject.segmentrouting.storekey.SubnetAssignedVidStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -080092import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -070093import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Pier Ventre6b19e482016-11-07 16:21:04 -080094import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Jonathan Hart54541d12016-04-12 15:39:44 -070095import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-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;
sangho80f11cb2015-04-01 13:05:26 -0700100import org.slf4j.Logger;
101import org.slf4j.LoggerFactory;
102
sangho27462c62015-05-14 00:39:53 -0700103import java.util.List;
sangho80f11cb2015-04-01 13:05:26 -0700104import java.util.Map;
Saurav Das62ae6792017-05-15 15:34:25 -0700105import java.util.Map.Entry;
Saurav Das7c305372015-10-28 12:39:42 -0700106import java.util.Set;
sangho80f11cb2015-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 Das261c3002017-06-13 15:35:54 -0700113import java.util.concurrent.atomic.AtomicBoolean;
Charles Chan90772a72017-02-08 15:52:08 -0800114import java.util.stream.Collectors;
sangho80f11cb2015-04-01 13:05:26 -0700115
Charles Chand6d25332016-02-26 22:19:52 -0800116import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800117import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700118import static org.onlab.util.Tools.groupedThreads;
Charles Chand6d25332016-02-26 22:19:52 -0800119
Charles Chanb7f75ac2016-01-11 18:28:54 -0800120/**
121 * Segment routing manager.
122 */
Jonathan Hart54541d12016-04-12 15:39:44 -0700123@Service
124@Component(immediate = true)
sangho27462c62015-05-14 00:39:53 -0700125public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700126
Charles Chan46fdfaf2016-11-09 20:51:44 -0800127 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
sangho80f11cb2015-04-01 13:05:26 -0700128
129 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700130 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700131
132 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventreb6a7f342016-11-26 21:05:22 -0800133 private NeighbourResolutionService neighbourResolutionService;
134
135 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800136 public PathService pathService;
137
138 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700139 CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700140
141 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700142 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700143
144 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700145 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700146
147 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700148 DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700149
150 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800151 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700152
153 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700154 LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700155
Charles Chan82ab1932016-01-30 23:22:37 -0800156 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800157 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700158
159 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800160 public StorageService storageService;
Charles Chandebfea32016-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 Chandebfea32016-10-24 14:52:01 -0700169 RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800170
171 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800172 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800173
Saurav Dasc3604f12016-03-23 11:22:49 -0700174 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800175 public InterfaceService interfaceService;
176
Charles Chandebfea32016-10-24 14:52:01 -0700177 ArpHandler arpHandler = null;
178 IcmpHandler icmpHandler = null;
179 IpHandler ipHandler = null;
180 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700181 ApplicationId appId;
182 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700183
Charles Chandebfea32016-10-24 14:52:01 -0700184 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700185 private TunnelHandler tunnelHandler = null;
186 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700187 private InternalPacketProcessor processor = null;
188 private InternalLinkListener linkListener = null;
189 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700190 private AppConfigHandler appCfgHandler = null;
Charles Chandebfea32016-10-24 14:52:01 -0700191 XConnectHandler xConnectHandler = null;
Charles Chan1eaf4802016-04-18 13:44:03 -0700192 private McastHandler mcastHandler = null;
Charles Chandebfea32016-10-24 14:52:01 -0700193 HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800194 private RouteHandler routeHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800195 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventre6b19e482016-11-07 16:21:04 -0800196 private L2TunnelHandler l2TunnelHandler = null;
sangho80f11cb2015-04-01 13:05:26 -0700197 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan82ab1932016-01-30 23:22:37 -0800198 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700199 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
200 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700201 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sangho80f11cb2015-04-01 13:05:26 -0700202
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700203 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700204 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sangho80f11cb2015-04-01 13:05:26 -0700205
Saurav Das2d94d312015-11-24 23:21:05 -0800206 @SuppressWarnings("unused")
sangho80f11cb2015-04-01 13:05:26 -0700207 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das2d94d312015-11-24 23:21:05 -0800208 @SuppressWarnings("rawtypes")
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700209 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chanf4586112015-11-09 16:37:23 -0800210 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chanb7f75ac2016-01-11 18:28:54 -0800211 new ConcurrentHashMap<>();
212 /**
Saurav Das261c3002017-06-13 15:35:54 -0700213 * Per device next objective ID store with (device id + destination set) as key.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800214 */
Saurav Das261c3002017-06-13 15:35:54 -0700215 EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
216 dsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800217 /**
218 * Per device next objective ID store with (device id + subnet) as key.
219 */
Ray Milkeyb85de082017-04-05 09:42:04 -0700220 EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800221 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800222 /**
223 * Per device next objective ID store with (device id + port) as key.
224 */
Ray Milkeyb85de082017-04-05 09:42:04 -0700225 EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800226 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800227
Saurav Das62ae6792017-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 Das2d94d312015-11-24 23:21:05 -0800238 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
239 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700240
Saurav Das261c3002017-06-13 15:35:54 -0700241 private AtomicBoolean programmingScheduled = new AtomicBoolean();
242
Charles Chanc91c8782016-03-30 17:54:24 -0700243 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700244 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
245 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700246 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700247 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800248 public SegmentRoutingDeviceConfig createConfig() {
249 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700250 }
251 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800252
Charles Chanc91c8782016-03-30 17:54:24 -0700253 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700254 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
255 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700256 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800257 @Override
258 public SegmentRoutingAppConfig createConfig() {
259 return new SegmentRoutingAppConfig();
260 }
261 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800262
Charles Chan82f19972016-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 Ventre6b19e482016-11-07 16:21:04 -0800272
Charles Chanc91c8782016-03-30 17:54:24 -0700273 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700274 new ConfigFactory<ApplicationId, McastConfig>(
275 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700276 McastConfig.class, "multicast") {
277 @Override
278 public McastConfig createConfig() {
279 return new McastConfig();
280 }
281 };
282
Pier Ventre6b19e482016-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 Vavilapalli64d96c12015-05-14 20:22:47 -0700293 private Object threadSchedulerLock = new Object();
294 private static int numOfEventsQueued = 0;
295 private static int numOfEventsExecuted = 0;
sangho80f11cb2015-04-01 13:05:26 -0700296 private static int numOfHandlerExecution = 0;
297 private static int numOfHandlerScheduled = 0;
298
Charles Chan1963f4f2016-02-18 14:22:42 -0800299 /**
300 * Segment Routing App ID.
301 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800302 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan10b0fb72017-02-02 16:20:42 -0800303
Charles Chanb7f75ac2016-01-11 18:28:54 -0800304 /**
305 * The default VLAN ID assigned to the interfaces without subnet config.
306 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800307 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das7c305372015-10-28 12:39:42 -0700308
sangho80f11cb2015-04-01 13:05:26 -0700309 @Activate
310 protected void activate() {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800311 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700312
313 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700314 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700315 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700316 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700317 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700318 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700319 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700320 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700321 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700322
Charles Chan10b0fb72017-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 Hart54541d12016-04-12 15:39:44 -0700328 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700329 .withTimestampProvider((k, v) -> new WallClockTimestamp())
330 .build();
331
Saurav Das2d94d312015-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 Hart54541d12016-04-12 15:39:44 -0700337 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800338 .withTimestampProvider((k, v) -> new WallClockTimestamp())
339 .build();
340
sangho4a5c42a2015-05-20 22:16:38 -0700341 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
342 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700343 tunnelStore = tunnelMapBuilder
344 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700345 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700346 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700347 .build();
348
349 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
350 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700351 policyStore = policyMapBuilder
352 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700353 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700354 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700355 .build();
356
Saurav Dasc3604f12016-03-23 11:22:49 -0700357 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800358 "purgeOnDisconnection", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700359 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800360 "purgeOnDisconnection", "true");
Pier Luigib9632ba2017-01-12 18:14:58 -0800361 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
362 "requestInterceptsEnabled", "false");
Pier Luigibc976df2017-01-12 22:46:39 -0800363 compCfgService.preSetProperty("org.onosproject.incubator.net.neighbour.impl.NeighbourResolutionManager",
364 "requestInterceptsEnabled", "false");
Charles Chan9597f8d2017-07-24 15:56:10 -0700365 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800366 "arpEnabled", "false");
Pier Luigi0ebeb312017-02-02 22:31:34 -0800367 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
368 "greedyLearningIpv6", "true");
Charles Chancf8ea472017-02-28 15:15:17 -0800369 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
370 "forceUnprovision", "true");
Charles Chan4c95c0d2017-07-20 16:16:25 -0700371 compCfgService.preSetProperty("org.onosproject.incubator.store.routing.impl.RouteStoreImpl",
372 "distributed", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700373
Charles Chan2b078ae2015-10-14 11:24:40 -0700374 processor = new InternalPacketProcessor();
375 linkListener = new InternalLinkListener();
376 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700377 appCfgHandler = new AppConfigHandler(this);
378 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700379 mcastHandler = new McastHandler(this);
380 hostHandler = new HostHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700381 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800382 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventre6b19e482016-11-07 16:21:04 -0800383 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700384
Charles Chand6d25332016-02-26 22:19:52 -0800385 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700386 cfgService.registerConfigFactory(deviceConfigFactory);
387 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700388 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700389 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventre6b19e482016-11-07 16:21:04 -0800390 cfgService.registerConfigFactory(pwaasConfigFactory);
Charles Chan82ab1932016-01-30 23:22:37 -0800391 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700392 packetService.addProcessor(processor, PacketProcessor.director(2));
393 linkService.addListener(linkListener);
394 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700395 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700396 routeService.addListener(routeListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700397
398 cfgListener.configureNetwork();
399
sangho80f11cb2015-04-01 13:05:26 -0700400 log.info("Started");
401 }
402
Jonathan Hart54541d12016-04-12 15:39:44 -0700403 private KryoNamespace.Builder createSerializer() {
404 return new KryoNamespace.Builder()
405 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700406 .register(DestinationSetNextObjectiveStoreKey.class,
Charles Chan10b0fb72017-02-02 16:20:42 -0800407 VlanNextObjectiveStoreKey.class,
Jonathan Hart54541d12016-04-12 15:39:44 -0700408 SubnetAssignedVidStoreKey.class,
Saurav Das261c3002017-06-13 15:35:54 -0700409 DestinationSet.class,
410 NextNeighbors.class,
Jonathan Hart54541d12016-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 Chan82f19972016-05-17 13:13:55 -0700417 XConnectStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700418 );
419 }
420
sangho80f11cb2015-04-01 13:05:26 -0700421 @Deactivate
422 protected void deactivate() {
Charles Chan72f556a2015-10-05 17:50:33 -0700423 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700424 cfgService.unregisterConfigFactory(deviceConfigFactory);
425 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700426 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700427 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventre6b19e482016-11-07 16:21:04 -0800428 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chan72f556a2015-10-05 17:50:33 -0700429
Charles Chanfd48c222017-06-19 00:43:31 -0700430 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700431 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700432 linkService.removeListener(linkListener);
433 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700434 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700435 routeService.removeListener(routeListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700436
Charles Chan2e71ef32017-02-23 15:44:08 -0800437 neighbourResolutionService.unregisterNeighbourHandlers(appId);
438
sangho80f11cb2015-04-01 13:05:26 -0700439 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700440 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700441 deviceListener = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700442 groupHandlerMap.clear();
443
Saurav Das261c3002017-06-13 15:35:54 -0700444 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800445 vlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700446 portNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700447 tunnelStore.destroy();
448 policyStore.destroy();
sangho80f11cb2015-04-01 13:05:26 -0700449 log.info("Stopped");
450 }
451
sangho27462c62015-05-14 00:39:53 -0700452 @Override
453 public List<Tunnel> getTunnels() {
454 return tunnelHandler.getTunnels();
455 }
456
457 @Override
sanghobd812f82015-06-29 14:58:47 -0700458 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
459 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700460 }
461
462 @Override
sanghobd812f82015-06-29 14:58:47 -0700463 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-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");
sanghobd812f82015-06-29 14:58:47 -0700469 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700470 }
471 }
472 }
sanghobd812f82015-06-29 14:58:47 -0700473 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700474 }
475
476 @Override
sanghobd812f82015-06-29 14:58:47 -0700477 public PolicyHandler.Result removePolicy(Policy policy) {
478 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700479 }
480
481 @Override
sanghobd812f82015-06-29 14:58:47 -0700482 public PolicyHandler.Result createPolicy(Policy policy) {
483 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700484 }
485
486 @Override
487 public List<Policy> getPolicies() {
488 return policyHandler.getPolicies();
489 }
490
Saurav Das07c74602016-04-27 18:35:50 -0700491 @Override
492 public void rerouteNetwork() {
493 cfgListener.configureNetwork();
494 for (Device device : deviceService.getDevices()) {
Saurav Dasf9332192017-02-18 14:05:44 -0800495 if (mastershipService.isLocalMaster(device.id())) {
496 defaultRoutingHandler.populatePortAddressingRules(device.id());
497 }
Saurav Das07c74602016-04-27 18:35:50 -0700498 }
499 defaultRoutingHandler.startPopulationProcess();
500 }
501
Charles Chand7844e52016-10-20 17:02:44 -0700502 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800503 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
504 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Charles Chand7844e52016-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 Das62ae6792017-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 Das261c3002017-06-13 15:35:54 -0700522 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
523 if (dsNextObjStore != null) {
524 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700525 } else {
526 return ImmutableMap.of();
527 }
528 }
529
sangho80f1f892015-05-19 11:57:42 -0700530 /**
Ray Milkeyb85de082017-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 Das261c3002017-06-13 15:35:54 -0700549 * Per device next objective ID store with (device id + destination set) as key.
Ray Milkeyb85de082017-04-05 09:42:04 -0700550 *
551 * @return next objective ID store
552 */
Saurav Das261c3002017-06-13 15:35:54 -0700553 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
554 dsNextObjStore() {
555 return dsNextObjStore;
Ray Milkeyb85de082017-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 Das261c3002017-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 Ventre7a78de22016-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 /**
sangho80f1f892015-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 */
sangho27462c62015-05-14 00:39:53 -0700594 public Tunnel getTunnel(String tunnelId) {
595 return tunnelHandler.getTunnel(tunnelId);
596 }
597
Charles Chan90772a72017-02-08 15:52:08 -0800598 // TODO Consider moving these to InterfaceService
sangho80f11cb2015-04-01 13:05:26 -0700599 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800600 * Returns untagged VLAN configured on given connect point.
Charles Chan90772a72017-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.
sangho80f11cb2015-04-01 13:05:26 -0700604 *
Charles Chan10b0fb72017-02-02 16:20:42 -0800605 * @param connectPoint connect point
606 * @return untagged VLAN or null if not configured
sangho80f11cb2015-04-01 13:05:26 -0700607 */
Charles Chan10b0fb72017-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);
sangho80f11cb2015-04-01 13:05:26 -0700613 }
614
sangho27462c62015-05-14 00:39:53 -0700615 /**
Charles Chan90772a72017-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 /**
Charles Chand9265a32017-06-16 15:19:24 -0700651 * Returns internal VLAN for untagged hosts on given connect point.
652 * <p>
653 * The internal VLAN is either vlan-untagged for an access port,
654 * or vlan-native for a trunk port.
655 *
656 * @param connectPoint connect point
657 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
658 */
659 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
660 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
661 VlanId nativeVlanId = getNativeVlanId(connectPoint);
662 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
663 }
664
665 /**
Charles Chan90772a72017-02-08 15:52:08 -0800666 * Returns vlan port map of given device.
667 *
668 * @param deviceId device id
669 * @return vlan-port multimap
670 */
671 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
672 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
673
674 interfaceService.getInterfaces().stream()
675 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
676 .forEach(intf -> {
677 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
678 intf.vlanTagged().forEach(vlanTagged -> {
679 vlanPortMap.put(vlanTagged, intf.connectPoint().port());
680 });
681 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
682 });
683 vlanPortMap.removeAll(VlanId.NONE);
684
685 return vlanPortMap;
686 }
687
688 /**
Saurav Das2d94d312015-11-24 23:21:05 -0800689 * Returns the next objective ID for the given subnet prefix. It is expected
Charles Chan10b0fb72017-02-02 16:20:42 -0800690 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -0800691 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -0700692 *
693 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -0800694 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -0800695 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -0700696 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800697 public int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -0700698 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800699 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
700 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -0700701 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -0800702 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -0800703 + "device {} not found", deviceId);
704 return -1;
705 }
706 }
707
708 /**
709 * Returns the next objective ID for the given portNumber, given the treatment.
710 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -0700711 * would result in different objectives. If the next object does not exist,
712 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -0800713 *
714 * @param deviceId Device ID
715 * @param portNum port number on device for which NextObjective is queried
716 * @param treatment the actions to apply on the packets (should include outport)
717 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -0700718 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -0700719 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -0800720 */
721 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
722 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -0700723 TrafficSelector meta,
724 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -0800725 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
726 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -0700727 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -0800728 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800729 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
730 + " not found", deviceId);
731 return -1;
732 }
733 }
734
Saurav Das62ae6792017-05-15 15:34:25 -0700735 /**
736 * Returns the group handler object for the specified device id.
737 *
738 * @param devId the device identifier
739 * @return the groupHandler object for the device id, or null if not found
740 */
741 public DefaultGroupHandler getGroupHandler(DeviceId devId) {
742 return groupHandlerMap.get(devId);
743 }
744
745 /**
746 * Returns true if this controller instance has seen this link before. The
747 * link may not be currently up, but as long as the link had been seen before
748 * this method will return true. The one exception is when the link was
749 * indeed seen before, but this controller instance was forced to forget it
750 * by a call to purgeSeenLink method.
751 *
752 * @param link the infrastructure link being queried
753 * @return true if this controller instance has seen this link before
754 */
755 public boolean isSeenLink(Link link) {
756 return seenLinks.containsKey(link);
757 }
758
759 /**
760 * Updates the seen link store. Updates can be for links that are currently
761 * available or not.
762 *
763 * @param link the link to update in the seen-link local store
764 * @param up the status of the link, true if up, false if down
765 */
766 public void updateSeenLink(Link link, boolean up) {
767 seenLinks.put(link, up);
768 }
769
770 /**
771 * Returns the status of a seen-link (up or down). If the link has not
772 * been seen-before, a null object is returned.
773 *
774 * @param link the infrastructure link being queried
775 * @return null if the link was not seen-before;
776 * true if the seen-link is up;
777 * false if the seen-link is down
778 */
779 public Boolean isSeenLinkUp(Link link) {
780 return seenLinks.get(link);
781 }
782
783 /**
784 * Makes this controller instance forget a previously seen before link.
785 *
786 * @param link the infrastructure link to purge
787 */
788 public void purgeSeenLink(Link link) {
789 seenLinks.remove(link);
790 }
791
792 /**
793 * Returns the status of a link as parallel link. A parallel link
794 * is defined as a link which has common src and dst switches as another
795 * seen-link that is currently enabled. It is not necessary for the link being
796 * queried to be a seen-link.
797 *
798 * @param link the infrastructure link being queried
799 * @return true if a seen-link exists that is up, and shares the
800 * same src and dst switches as the link being queried
801 */
802 public boolean isParallelLink(Link link) {
803 for (Entry<Link, Boolean> seen : seenLinks.entrySet()) {
804 Link seenLink = seen.getKey();
805 if (seenLink.equals(link)) {
806 continue;
807 }
808 if (seenLink.src().deviceId().equals(link.src().deviceId()) &&
809 seenLink.dst().deviceId().equals(link.dst().deviceId()) &&
810 seen.getValue()) {
811 return true;
812 }
813 }
814 return false;
815 }
816
Saurav Das261c3002017-06-13 15:35:54 -0700817 /**
818 * Returns true if the link being queried is a bidirectional link. A bidi
819 * link is defined as a link, whose reverse link - ie. the link in the reverse
820 * direction - has been seen-before and is up. It is not necessary for the link
821 * being queried to be a seen-link.
822 *
823 * @param link the infrastructure link being queried
824 * @return true if another unidirectional link exists in the reverse direction,
825 * has been seen-before and is up
826 */
827 public boolean isBidirectional(Link link) {
828 Link reverseLink = linkService.getLink(link.dst(), link.src());
829 if (reverseLink == null) {
830 return false;
831 }
832 Boolean result = isSeenLinkUp(reverseLink);
833 if (result == null) {
834 return false;
835 }
836 return result.booleanValue();
837 }
838
839 /**
840 * Determines if the given link should be avoided in routing calculations
841 * by policy or design.
842 *
843 * @param link the infrastructure link being queried
844 * @return true if link should be avoided
845 */
846 public boolean avoidLink(Link link) {
847 // XXX currently only avoids all pair-links. In the future can be
848 // extended to avoid any generic link
849 DeviceId src = link.src().deviceId();
850 PortNumber srcPort = link.src().port();
851 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(src)) {
852 log.warn("Device {} not configured..cannot avoid link {}", src, link);
853 return false;
854 }
855 DeviceId pairDev;
856 PortNumber pairLocalPort, pairRemotePort = null;
857 try {
858 pairDev = deviceConfiguration.getPairDeviceId(src);
859 pairLocalPort = deviceConfiguration.getPairLocalPort(src);
860 if (pairDev != null) {
861 pairRemotePort = deviceConfiguration.getPairLocalPort(pairDev);
862 }
863 } catch (DeviceConfigNotFoundException e) {
864 log.warn("Pair dev for dev {} not configured..cannot avoid link {}",
865 src, link);
866 return false;
867 }
868
869 if (srcPort.equals(pairLocalPort) &&
870 link.dst().deviceId().equals(pairDev) &&
871 link.dst().port().equals(pairRemotePort)) {
872 return true;
873 }
874 return false;
875 }
876
sangho80f11cb2015-04-01 13:05:26 -0700877 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -0700878 @Override
879 public void process(PacketContext context) {
880
881 if (context.isHandled()) {
882 return;
883 }
884
885 InboundPacket pkt = context.inPacket();
886 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -0800887
888 if (ethernet == null) {
889 return;
890 }
891
Saurav Das261c3002017-06-13 15:35:54 -0700892 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
893 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800894 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -0700895 log.warn("Received unexpected ARP packet on {}",
896 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -0700897 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800898 return;
sangho80f11cb2015-04-01 13:05:26 -0700899 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -0800900 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
901 //ipHandler.addToPacketBuffer(ipv4Packet);
902 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
903 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700904 } else {
Charles Chand041ad82017-01-13 17:20:44 -0800905 // NOTE: We don't support IP learning at this moment so this
906 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800907 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700908 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800909 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
910 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -0800911 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -0800912 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -0800913 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
914 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
915 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
916 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
917 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
918 } else {
Saurav Das62ae6792017-05-15 15:34:25 -0700919 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -0700920 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -0800921 }
922 } else {
923 // NOTE: We don't support IP learning at this moment so this
924 // is not necessary. Also it causes duplication of DHCPv6 packets.
925 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
926 }
sangho80f11cb2015-04-01 13:05:26 -0700927 }
928 }
929 }
930
931 private class InternalLinkListener implements LinkListener {
932 @Override
933 public void event(LinkEvent event) {
Charles Chan0cff8aa2017-03-29 16:39:05 -0700934 if (event.type() == LinkEvent.Type.LINK_ADDED ||
935 event.type() == LinkEvent.Type.LINK_UPDATED ||
936 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700937 log.debug("Event {} received from Link Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -0700938 scheduleEventHandlerIfNotScheduled(event);
939 }
940 }
941 }
942
943 private class InternalDeviceListener implements DeviceListener {
sangho80f11cb2015-04-01 13:05:26 -0700944 @Override
945 public void event(DeviceEvent event) {
sangho80f11cb2015-04-01 13:05:26 -0700946 switch (event.type()) {
947 case DEVICE_ADDED:
Saurav Dasf0f592d2016-11-18 15:21:57 -0800948 case PORT_UPDATED:
949 case PORT_ADDED:
sanghofb7c7292015-04-13 15:15:58 -0700950 case DEVICE_UPDATED:
951 case DEVICE_AVAILABILITY_CHANGED:
Saurav Das62ae6792017-05-15 15:34:25 -0700952 log.trace("Event {} received from Device Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -0700953 scheduleEventHandlerIfNotScheduled(event);
954 break;
955 default:
956 }
957 }
958 }
959
Saurav Das2d94d312015-11-24 23:21:05 -0800960 @SuppressWarnings("rawtypes")
sangho80f11cb2015-04-01 13:05:26 -0700961 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700962 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700963 eventQueue.add(event);
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700964 numOfEventsQueued++;
965
966 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
967 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700968 eventHandlerFuture = executorService
969 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
970 numOfHandlerScheduled++;
971 }
Jonathan Hart54541d12016-04-12 15:39:44 -0700972 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700973 numOfEventsQueued,
974 numOfHandlerScheduled);
sangho80f11cb2015-04-01 13:05:26 -0700975 }
sangho80f11cb2015-04-01 13:05:26 -0700976 }
977
978 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700979 @Override
sangho80f11cb2015-04-01 13:05:26 -0700980 public void run() {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700981 try {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700982 while (true) {
Saurav Das2d94d312015-11-24 23:21:05 -0800983 @SuppressWarnings("rawtypes")
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700984 Event event = null;
985 synchronized (threadSchedulerLock) {
986 if (!eventQueue.isEmpty()) {
987 event = eventQueue.poll();
988 numOfEventsExecuted++;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700989 } else {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700990 numOfHandlerExecution++;
991 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
992 numOfHandlerExecution, numOfEventsExecuted);
993 break;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700994 }
sanghofb7c7292015-04-13 15:15:58 -0700995 }
Charles Chan0cff8aa2017-03-29 16:39:05 -0700996 if (event.type() == LinkEvent.Type.LINK_ADDED ||
997 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Das62ae6792017-05-15 15:34:25 -0700998 // Note: do not update seenLinks here, otherwise every
999 // link, even one seen for the first time, will be appear
1000 // to be a previously seen link
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001001 processLinkAdded((Link) event.subject());
1002 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre6d593892016-09-13 21:33:40 -07001003 Link linkRemoved = (Link) event.subject();
Saurav Das62ae6792017-05-15 15:34:25 -07001004 if (linkRemoved.type() == Link.Type.DIRECT) {
1005 updateSeenLink(linkRemoved, false);
1006 }
1007 // device availability check helps to ensure that
1008 // multiple link-removed events are actually treated as a
1009 // single switch removed event. purgeSeenLink is necessary
1010 // so we do rerouting (instead of rehashing) when switch
1011 // comes back.
Pier Ventre6d593892016-09-13 21:33:40 -07001012 if (linkRemoved.src().elementId() instanceof DeviceId &&
1013 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
Saurav Das62ae6792017-05-15 15:34:25 -07001014 purgeSeenLink(linkRemoved);
Pier Ventre6d593892016-09-13 21:33:40 -07001015 continue;
1016 }
1017 if (linkRemoved.dst().elementId() instanceof DeviceId &&
1018 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
Saurav Das62ae6792017-05-15 15:34:25 -07001019 purgeSeenLink(linkRemoved);
Pier Ventre6d593892016-09-13 21:33:40 -07001020 continue;
1021 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001022 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001023 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1024 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1025 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das62af8802015-12-04 10:52:59 -08001026 DeviceId deviceId = ((Device) event.subject()).id();
1027 if (deviceService.isAvailable(deviceId)) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001028 log.info("Processing device event {} for available device {}",
1029 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001030 processDeviceAdded((Device) event.subject());
Saurav Dasc3604f12016-03-23 11:22:49 -07001031 } else {
1032 log.info("Processing device event {} for unavailable device {}",
1033 event.type(), ((Device) event.subject()).id());
1034 processDeviceRemoved((Device) event.subject());
1035 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001036 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd1872b02016-12-02 15:43:47 -08001037 // typically these calls come when device is added first time
1038 // so port filtering rules are handled at the device_added event.
1039 // port added calls represent all ports on the device,
1040 // enabled or not.
Saurav Das62ae6792017-05-15 15:34:25 -07001041 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd1872b02016-12-02 15:43:47 -08001042 ((DeviceEvent) event).subject().id(),
1043 ((DeviceEvent) event).port().number(),
1044 event.type());
Saurav Dasf0f592d2016-11-18 15:21:57 -08001045 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd1872b02016-12-02 15:43:47 -08001046 // these calls happen for every subsequent event
1047 // ports enabled, disabled, switch goes away, comes back
Saurav Dasf0f592d2016-11-18 15:21:57 -08001048 log.info("** PORT UPDATED {}/{} -> {}",
1049 event.subject(),
1050 ((DeviceEvent) event).port(),
1051 event.type());
1052 processPortUpdated(((Device) event.subject()),
1053 ((DeviceEvent) event).port());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001054 } else {
1055 log.warn("Unhandled event type: {}", event.type());
1056 }
sangho80f11cb2015-04-01 13:05:26 -07001057 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001058 } catch (Exception e) {
1059 log.error("SegmentRouting event handler "
1060 + "thread thrown an exception: {}", e);
sangho80f11cb2015-04-01 13:05:26 -07001061 }
sangho80f11cb2015-04-01 13:05:26 -07001062 }
1063 }
1064
sangho80f11cb2015-04-01 13:05:26 -07001065 private void processLinkAdded(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -07001066 log.info("** LINK ADDED {}", link.toString());
Charles Chan319d1a22015-11-03 10:42:14 -08001067 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
Saurav Das261c3002017-06-13 15:35:54 -07001068 log.warn("Source device of this link is not configured..not processing");
Charles Chan319d1a22015-11-03 10:42:14 -08001069 return;
1070 }
Saurav Das62ae6792017-05-15 15:34:25 -07001071 if (link.type() != Link.Type.DIRECT) {
1072 // NOTE: A DIRECT link might be transiently marked as INDIRECT
1073 // if BDDP is received before LLDP. We can safely ignore that
1074 // until the LLDP is received and the link is marked as DIRECT.
1075 log.info("Ignore link {}->{}. Link type is {} instead of DIRECT.",
1076 link.src(), link.dst(), link.type());
1077 return;
1078 }
1079
1080 //Irrespective of whether the local is a MASTER or not for this device,
1081 //create group handler instance and push default TTP flow rules if needed,
1082 //as in a multi-instance setup, instances can initiate groups for any device.
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001083 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
1084 .deviceId());
1085 if (groupHandler != null) {
Saurav Das62ae6792017-05-15 15:34:25 -07001086 groupHandler.portUpForLink(link);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001087 } else {
1088 Device device = deviceService.getDevice(link.src().deviceId());
1089 if (device != null) {
1090 log.warn("processLinkAdded: Link Added "
1091 + "Notification without Device Added "
1092 + "event, still handling it");
1093 processDeviceAdded(device);
1094 groupHandler = groupHandlerMap.get(link.src()
1095 .deviceId());
Saurav Das62ae6792017-05-15 15:34:25 -07001096 groupHandler.portUpForLink(link);
sangho80f11cb2015-04-01 13:05:26 -07001097 }
1098 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001099
Saurav Das261c3002017-06-13 15:35:54 -07001100 /*// process link only if it is bidirectional
1101 if (!isBidirectional(link)) {
1102 log.debug("Link not bidirectional.. waiting for other direction "
1103 + "src {} --> dst {} ", link.dst(), link.src());
1104 // note that if we are not processing for routing, it should at least
1105 // be considered a seen-link
1106 updateSeenLink(link, true);
1107 return;
1108 }
1109 TO DO this ensure that rehash is still done correctly even if link is
1110 not processed for rerouting - perhaps rehash in both directions when
1111 it ultimately becomes bidi?
1112 */
1113
1114 log.debug("Starting optimized route population process for link "
1115 + "{} --> {}", link.src(), link.dst());
Saurav Das62ae6792017-05-15 15:34:25 -07001116 boolean seenBefore = isSeenLink(link);
1117 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null, link, null);
Saurav Das261c3002017-06-13 15:35:54 -07001118
1119 // It's possible that linkUp causes no route-path change as ECMP graph does
1120 // not change if the link is a parallel link (same src-dst as another link.
1121 // However we still need to update ECMP hash groups to include new buckets
1122 // for the link that has come up.
Saurav Das62ae6792017-05-15 15:34:25 -07001123 if (mastershipService.isLocalMaster(link.src().deviceId())) {
Saurav Das261c3002017-06-13 15:35:54 -07001124 if (!seenBefore && isParallelLink(link)) {
Saurav Das62ae6792017-05-15 15:34:25 -07001125 // if link seen first time, we need to ensure hash-groups have all ports
Saurav Das261c3002017-06-13 15:35:54 -07001126 log.debug("Attempting retryHash for paralled first-time link {}", link);
Saurav Das62ae6792017-05-15 15:34:25 -07001127 groupHandler.retryHash(link, false, true);
1128 } else {
1129 //seen before-link
1130 if (isParallelLink(link)) {
Saurav Das261c3002017-06-13 15:35:54 -07001131 log.debug("Attempting retryHash for paralled seen-before "
1132 + "link {}", link);
Saurav Das62ae6792017-05-15 15:34:25 -07001133 groupHandler.retryHash(link, false, false);
1134 }
1135 }
1136 }
Charles Chan72779502016-04-23 17:36:10 -07001137
1138 mcastHandler.init();
sangho80f11cb2015-04-01 13:05:26 -07001139 }
1140
1141 private void processLinkRemoved(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -07001142 log.info("** LINK REMOVED {}", link.toString());
Saurav Das62ae6792017-05-15 15:34:25 -07001143 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link, null, null);
1144
1145 // update local groupHandler stores
sangho2165d222015-05-01 09:38:25 -07001146 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
1147 if (groupHandler != null) {
Saurav Das62ae6792017-05-15 15:34:25 -07001148 if (mastershipService.isLocalMaster(link.src().deviceId()) &&
1149 isParallelLink(link)) {
1150 groupHandler.retryHash(link, true, false);
1151 }
1152 // ensure local stores are updated
1153 groupHandler.portDown(link.src().port());
1154 } else {
1155 log.warn("group handler not found for dev:{} when removing link: {}",
1156 link.src().deviceId(), link);
sangho2165d222015-05-01 09:38:25 -07001157 }
Charles Chan72779502016-04-23 17:36:10 -07001158
1159 mcastHandler.processLinkDown(link);
sangho80f11cb2015-04-01 13:05:26 -07001160 }
1161
1162 private void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001163 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001164
1165 // NOTE: Punt ARP/NDP even when the device is not configured.
1166 // Host learning without network config is required for CORD config generator.
1167 routingRulePopulator.populateIpPunts(device.id());
1168 routingRulePopulator.populateArpNdpPunts(device.id());
1169
Charles Chan319d1a22015-11-03 10:42:14 -08001170 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001171 log.warn("Device configuration unavailable. Device {} will be "
1172 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001173 return;
1174 }
Charles Chan72779502016-04-23 17:36:10 -07001175 processDeviceAddedInternal(device.id());
1176 }
1177
1178 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001179 // Irrespective of whether the local is a MASTER or not for this device,
1180 // we need to create a SR-group-handler instance. This is because in a
1181 // multi-instance setup, any instance can initiate forwarding/next-objectives
1182 // for any switch (even if this instance is a SLAVE or not even connected
1183 // to the switch). To handle this, a default-group-handler instance is necessary
1184 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001185 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1186 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001187 DefaultGroupHandler groupHandler;
1188 try {
1189 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001190 createGroupHandler(deviceId,
1191 appId,
1192 deviceConfiguration,
1193 linkService,
1194 flowObjectiveService,
1195 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001196 } catch (DeviceConfigNotFoundException e) {
1197 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1198 return;
1199 }
Charles Chan72779502016-04-23 17:36:10 -07001200 log.debug("updating groupHandlerMap with new config for device: {}",
1201 deviceId);
1202 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001203 }
Saurav Dasb149be12016-06-07 10:08:06 -07001204
Charles Chan72779502016-04-23 17:36:10 -07001205 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001206 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chandebfea32016-10-24 14:52:01 -07001207 hostHandler.init(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -07001208 xConnectHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001209 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001210 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001211 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001212 }
Charles Chan82ab1932016-01-30 23:22:37 -08001213
Charles Chandebfea32016-10-24 14:52:01 -07001214 appCfgHandler.init(deviceId);
1215 routeHandler.init(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001216 }
1217
Saurav Dasc3604f12016-03-23 11:22:49 -07001218 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001219 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001220 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1221 .forEach(entry -> {
Saurav Das261c3002017-06-13 15:35:54 -07001222 dsNextObjStore.remove(entry.getKey());
Saurav Dasc3604f12016-03-23 11:22:49 -07001223 });
Charles Chan10b0fb72017-02-02 16:20:42 -08001224 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001225 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1226 .forEach(entry -> {
Charles Chan10b0fb72017-02-02 16:20:42 -08001227 vlanNextObjStore.remove(entry.getKey());
Saurav Dasc3604f12016-03-23 11:22:49 -07001228 });
Saurav Dasc3604f12016-03-23 11:22:49 -07001229 portNextObjStore.entrySet().stream()
1230 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1231 .forEach(entry -> {
1232 portNextObjStore.remove(entry.getKey());
1233 });
Charles Chan17d75d82017-06-15 00:44:51 -07001234
1235 seenLinks.keySet().removeIf(key -> key.src().deviceId().equals(device.id()) ||
1236 key.dst().deviceId().equals(device.id()));
1237
Saurav Dasc3604f12016-03-23 11:22:49 -07001238 groupHandlerMap.remove(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -07001239 defaultRoutingHandler.purgeEcmpGraph(device.id());
Saurav Das62ae6792017-05-15 15:34:25 -07001240 // Note that a switch going down is associated with all of its links
1241 // going down as well, but it is treated as a single switch down event
1242 // while the link-downs are ignored.
1243 defaultRoutingHandler
1244 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Charles Chan72779502016-04-23 17:36:10 -07001245 mcastHandler.removeDevice(device.id());
Charles Chan82f19972016-05-17 13:13:55 -07001246 xConnectHandler.removeDevice(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -07001247 }
1248
Saurav Dasf0f592d2016-11-18 15:21:57 -08001249 private void processPortUpdated(Device device, Port port) {
1250 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1251 log.warn("Device configuration uploading. Not handling port event for"
1252 + "dev: {} port: {}", device.id(), port.number());
1253 return;
1254 }
Saurav Dasf9332192017-02-18 14:05:44 -08001255
1256 if (!mastershipService.isLocalMaster(device.id())) {
1257 log.debug("Not master for dev:{} .. not handling port updated event"
1258 + "for port {}", device.id(), port.number());
1259 return;
1260 }
1261
1262 // first we handle filtering rules associated with the port
1263 if (port.isEnabled()) {
1264 log.info("Switchport {}/{} enabled..programming filters",
1265 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001266 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001267 } else {
1268 log.info("Switchport {}/{} disabled..removing filters",
1269 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001270 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001271 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001272
1273 // portUpdated calls are for ports that have gone down or up. For switch
1274 // to switch ports, link-events should take care of any re-routing or
1275 // group editing necessary for port up/down. Here we only process edge ports
1276 // that are already configured.
Saurav Das3fb28272017-03-04 16:08:47 -08001277 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1278 VlanId untaggedVlan = getUntaggedVlanId(cp);
1279 VlanId nativeVlan = getNativeVlanId(cp);
1280 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001281
Saurav Das3fb28272017-03-04 16:08:47 -08001282 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001283 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasf0f592d2016-11-18 15:21:57 -08001284 + "dev/port: {}/{}", device.id(), port.number());
1285 return;
1286 }
Saurav Das3fb28272017-03-04 16:08:47 -08001287 if (untaggedVlan != null) {
1288 processEdgePort(device, port, untaggedVlan, true);
1289 }
1290 if (nativeVlan != null) {
1291 processEdgePort(device, port, nativeVlan, true);
1292 }
1293 if (!taggedVlans.isEmpty()) {
1294 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1295 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001296 }
1297
Saurav Das3fb28272017-03-04 16:08:47 -08001298 private void processEdgePort(Device device, Port port, VlanId vlanId,
1299 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001300 boolean portUp = port.isEnabled();
1301 if (portUp) {
Saurav Das3fb28272017-03-04 16:08:47 -08001302 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001303 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001304 } else {
Saurav Das3fb28272017-03-04 16:08:47 -08001305 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001306 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001307 }
1308
Srikanth Vavilapalli64505482015-04-21 13:04:13 -07001309 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sangho80f11cb2015-04-01 13:05:26 -07001310 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001311 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001312 } else {
1313 log.warn("Group handler not found for dev:{}. Not handling edge port"
1314 + " {} event for port:{}", device.id(),
1315 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001316 }
1317 }
sangho27462c62015-05-14 00:39:53 -07001318
Pier Ventreb6a7f342016-11-26 21:05:22 -08001319 /**
1320 * Registers the given connect point with the NRS, this is necessary
1321 * to receive the NDP and ARP packets from the NRS.
1322 *
1323 * @param portToRegister connect point to register
1324 */
1325 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001326 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001327 portToRegister,
1328 neighbourHandler,
1329 appId
1330 );
1331 }
1332
Charles Chan72f556a2015-10-05 17:50:33 -07001333 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001334 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001335 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001336
Charles Chanb7f75ac2016-01-11 18:28:54 -08001337 /**
1338 * Constructs the internal network config listener.
1339 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001340 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001341 */
Charles Chan46fdfaf2016-11-09 20:51:44 -08001342 public InternalConfigListener(SegmentRoutingManager srManager) {
1343 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001344 }
1345
Charles Chanb7f75ac2016-01-11 18:28:54 -08001346 /**
1347 * Reads network config and initializes related data structure accordingly.
1348 */
Charles Chane7c61022015-10-07 14:21:45 -07001349 public void configureNetwork() {
Saurav Das261c3002017-06-13 15:35:54 -07001350 if (deviceConfiguration == null) {
1351 deviceConfiguration = new DeviceConfiguration(srManager);
1352 } else {
1353 deviceConfiguration.updateConfig();
1354 }
Charles Chane7c61022015-10-07 14:21:45 -07001355
Charles Chan46fdfaf2016-11-09 20:51:44 -08001356 arpHandler = new ArpHandler(srManager);
1357 icmpHandler = new IcmpHandler(srManager);
1358 ipHandler = new IpHandler(srManager);
1359 routingRulePopulator = new RoutingRulePopulator(srManager);
1360 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chane7c61022015-10-07 14:21:45 -07001361
1362 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1363 groupHandlerMap, tunnelStore);
1364 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1365 flowObjectiveService,
1366 tunnelHandler, policyStore);
Saurav Das261c3002017-06-13 15:35:54 -07001367 // add a small delay to absorb multiple network config added notifications
1368 if (!programmingScheduled.get()) {
1369 programmingScheduled.set(true);
1370 executorService.schedule(new ConfigChange(), PROGRAM_DELAY,
1371 TimeUnit.SECONDS);
Charles Chane7c61022015-10-07 14:21:45 -07001372 }
Charles Chan72779502016-04-23 17:36:10 -07001373 mcastHandler.init();
Charles Chane7c61022015-10-07 14:21:45 -07001374 }
1375
Charles Chan72f556a2015-10-05 17:50:33 -07001376 @Override
1377 public void event(NetworkConfigEvent event) {
Charles Chan82ab1932016-01-30 23:22:37 -08001378 // TODO move this part to NetworkConfigEventHandler
1379 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1380 switch (event.type()) {
1381 case CONFIG_ADDED:
Charles Chan68363b12017-06-26 15:25:09 -07001382 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan82ab1932016-01-30 23:22:37 -08001383 configureNetwork();
1384 break;
1385 case CONFIG_UPDATED:
Charles Chan68363b12017-06-26 15:25:09 -07001386 log.info("Segment Routing Config updated for {}", event.subject());
1387 // TODO support dynamic configuration
1388 break;
1389 default:
1390 break;
1391 }
1392 } else if (event.configClass().equals(InterfaceConfig.class)) {
1393 switch (event.type()) {
1394 case CONFIG_ADDED:
1395 log.info("Interface Config added for {}", event.subject());
1396 configureNetwork();
1397 break;
1398 case CONFIG_UPDATED:
1399 log.info("Interface Config updated for {}", event.subject());
Charles Chan82ab1932016-01-30 23:22:37 -08001400 // TODO support dynamic configuration
1401 break;
1402 default:
1403 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001404 }
Charles Chan82ab1932016-01-30 23:22:37 -08001405 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chan82f19972016-05-17 13:13:55 -07001406 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan82ab1932016-01-30 23:22:37 -08001407 switch (event.type()) {
1408 case CONFIG_ADDED:
Charles Chan82f19972016-05-17 13:13:55 -07001409 appCfgHandler.processAppConfigAdded(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001410 break;
1411 case CONFIG_UPDATED:
Charles Chan82f19972016-05-17 13:13:55 -07001412 appCfgHandler.processAppConfigUpdated(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001413 break;
1414 case CONFIG_REMOVED:
Charles Chan82f19972016-05-17 13:13:55 -07001415 appCfgHandler.processAppConfigRemoved(event);
1416 break;
1417 default:
1418 break;
1419 }
Charles Chandebfea32016-10-24 14:52:01 -07001420 configureNetwork();
Charles Chan82f19972016-05-17 13:13:55 -07001421 } else if (event.configClass().equals(XConnectConfig.class)) {
1422 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1423 switch (event.type()) {
1424 case CONFIG_ADDED:
1425 xConnectHandler.processXConnectConfigAdded(event);
1426 break;
1427 case CONFIG_UPDATED:
1428 xConnectHandler.processXConnectConfigUpdated(event);
1429 break;
1430 case CONFIG_REMOVED:
1431 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001432 break;
1433 default:
1434 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001435 }
Pier Ventre6b19e482016-11-07 16:21:04 -08001436 } else if (event.configClass().equals(PwaasConfig.class)) {
1437 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1438 switch (event.type()) {
1439 case CONFIG_ADDED:
1440 l2TunnelHandler.processPwaasConfigAdded(event);
1441 break;
1442 case CONFIG_UPDATED:
1443 l2TunnelHandler.processPwaasConfigUpdated(event);
1444 break;
1445 case CONFIG_REMOVED:
1446 l2TunnelHandler.processPwaasConfigRemoved(event);
1447 break;
1448 default:
1449 break;
1450 }
Charles Chan72f556a2015-10-05 17:50:33 -07001451 }
1452 }
Saurav Das261c3002017-06-13 15:35:54 -07001453
1454 private final class ConfigChange implements Runnable {
1455 @Override
1456 public void run() {
1457 programmingScheduled.set(false);
1458 for (Device device : deviceService.getDevices()) {
1459 processDeviceAdded(device);
1460 }
1461 defaultRoutingHandler.startPopulationProcess();
1462 }
1463 }
Charles Chan72f556a2015-10-05 17:50:33 -07001464 }
Charles Chanf4586112015-11-09 16:37:23 -08001465
1466 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001467 @Override
1468 public void event(HostEvent event) {
1469 // Do not proceed without mastership
1470 DeviceId deviceId = event.subject().location().deviceId();
1471 if (!mastershipService.isLocalMaster(deviceId)) {
1472 return;
1473 }
1474
1475 switch (event.type()) {
1476 case HOST_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001477 hostHandler.processHostAddedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001478 break;
1479 case HOST_MOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001480 hostHandler.processHostMovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001481 break;
1482 case HOST_REMOVED:
Charles Chand9265a32017-06-16 15:19:24 -07001483 hostHandler.processHostRemovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001484 break;
1485 case HOST_UPDATED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001486 hostHandler.processHostUpdatedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001487 break;
1488 default:
1489 log.warn("Unsupported host event type: {}", event.type());
1490 break;
1491 }
1492 }
1493 }
1494
Charles Chanc91c8782016-03-30 17:54:24 -07001495 private class InternalMcastListener implements McastListener {
1496 @Override
1497 public void event(McastEvent event) {
1498 switch (event.type()) {
1499 case SOURCE_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001500 mcastHandler.processSourceAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001501 break;
1502 case SINK_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001503 mcastHandler.processSinkAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001504 break;
1505 case SINK_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001506 mcastHandler.processSinkRemoved(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001507 break;
1508 case ROUTE_ADDED:
1509 case ROUTE_REMOVED:
1510 default:
1511 break;
1512 }
1513 }
1514 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001515
Charles Chandebfea32016-10-24 14:52:01 -07001516 private class InternalRouteEventListener implements RouteListener {
1517 @Override
1518 public void event(RouteEvent event) {
1519 switch (event.type()) {
1520 case ROUTE_ADDED:
1521 routeHandler.processRouteAdded(event);
1522 break;
1523 case ROUTE_UPDATED:
1524 routeHandler.processRouteUpdated(event);
1525 break;
1526 case ROUTE_REMOVED:
1527 routeHandler.processRouteRemoved(event);
1528 break;
1529 default:
1530 break;
1531 }
1532 }
1533 }
Saurav Das62ae6792017-05-15 15:34:25 -07001534
sangho80f11cb2015-04-01 13:05:26 -07001535}