blob: b9912accad346187d155bc6b704fd4d529ebf95c [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
Jonathan Hart61e24e12017-11-30 18:23:42 -080018import com.google.common.collect.HashMultimap;
19import com.google.common.collect.ImmutableMap;
20import com.google.common.collect.Maps;
21import com.google.common.collect.Multimap;
Charles Chanf0ae41e2017-08-23 13:55:39 -070022import com.google.common.collect.Sets;
sangho80f11cb2015-04-01 13:05:26 -070023import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
Charles Chan873661e2017-11-30 15:37:50 -080026import org.apache.felix.scr.annotations.Modified;
27import org.apache.felix.scr.annotations.Property;
sangho80f11cb2015-04-01 13:05:26 -070028import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho27462c62015-05-14 00:39:53 -070030import org.apache.felix.scr.annotations.Service;
sangho80f11cb2015-04-01 13:05:26 -070031import org.onlab.packet.Ethernet;
Pier Ventreb6b81d52016-12-02 08:16:05 -080032import org.onlab.packet.ICMP6;
Charles Chan77277672015-10-20 16:24:19 -070033import org.onlab.packet.IPv4;
Pier Ventreb6a7f342016-11-26 21:05:22 -080034import org.onlab.packet.IPv6;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070035import org.onlab.packet.IpAddress;
Charles Chan77277672015-10-20 16:24:19 -070036import org.onlab.packet.IpPrefix;
Jonathan Hart54541d12016-04-12 15:39:44 -070037import org.onlab.packet.VlanId;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070038import org.onlab.util.KryoNamespace;
Charles Chan873661e2017-11-30 15:37:50 -080039import org.onlab.util.Tools;
Saurav Dasc3604f12016-03-23 11:22:49 -070040import org.onosproject.cfg.ComponentConfigService;
Pier Luigi580fd8a2018-01-16 10:47:50 +010041import org.onosproject.cluster.ClusterService;
42import org.onosproject.cluster.LeadershipService;
sangho80f11cb2015-04-01 13:05:26 -070043import org.onosproject.core.ApplicationId;
44import org.onosproject.core.CoreService;
45import org.onosproject.event.Event;
Jonathan Hart54541d12016-04-12 15:39:44 -070046import org.onosproject.mastership.MastershipService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080047import org.onosproject.net.ConnectPoint;
Jonathan Hart54541d12016-04-12 15:39:44 -070048import org.onosproject.net.Device;
49import org.onosproject.net.DeviceId;
Charles Chanf0ae41e2017-08-23 13:55:39 -070050import org.onosproject.net.Host;
51import org.onosproject.net.HostId;
Jonathan Hart54541d12016-04-12 15:39:44 -070052import org.onosproject.net.Link;
53import org.onosproject.net.Port;
Charles Chanf4586112015-11-09 16:37:23 -080054import org.onosproject.net.PortNumber;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070055import org.onosproject.net.config.ConfigException;
Charles Chan72f556a2015-10-05 17:50:33 -070056import org.onosproject.net.config.ConfigFactory;
57import org.onosproject.net.config.NetworkConfigEvent;
Charles Chan72f556a2015-10-05 17:50:33 -070058import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070059import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkeyae0068a2017-08-15 11:02:29 -070060import org.onosproject.net.config.basics.InterfaceConfig;
61import org.onosproject.net.config.basics.McastConfig;
Charles Chan72f556a2015-10-05 17:50:33 -070062import org.onosproject.net.config.basics.SubjectFactories;
Saurav Dase6c448a2018-01-18 12:07:33 -080063import org.onosproject.net.device.DeviceAdminService;
Jonathan Hart54541d12016-04-12 15:39:44 -070064import org.onosproject.net.device.DeviceEvent;
65import org.onosproject.net.device.DeviceListener;
66import org.onosproject.net.device.DeviceService;
Charles Chanf4586112015-11-09 16:37:23 -080067import org.onosproject.net.flow.TrafficSelector;
68import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart54541d12016-04-12 15:39:44 -070069import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chanf4586112015-11-09 16:37:23 -080070import org.onosproject.net.host.HostEvent;
71import org.onosproject.net.host.HostListener;
Charles Chan873661e2017-11-30 15:37:50 -080072import org.onosproject.net.host.HostLocationProbingService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080073import org.onosproject.net.host.HostService;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070074import org.onosproject.net.host.InterfaceIpAddress;
Ray Milkeyae0068a2017-08-15 11:02:29 -070075import org.onosproject.net.intf.Interface;
76import org.onosproject.net.intf.InterfaceService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080077import org.onosproject.net.link.LinkEvent;
78import org.onosproject.net.link.LinkListener;
79import org.onosproject.net.link.LinkService;
Charles Chanc91c8782016-03-30 17:54:24 -070080import org.onosproject.net.mcast.McastEvent;
81import org.onosproject.net.mcast.McastListener;
82import org.onosproject.net.mcast.MulticastRouteService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070083import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080084import org.onosproject.net.packet.InboundPacket;
85import org.onosproject.net.packet.PacketContext;
86import org.onosproject.net.packet.PacketProcessor;
87import org.onosproject.net.packet.PacketService;
Charles Chanc91c8782016-03-30 17:54:24 -070088import org.onosproject.net.topology.TopologyService;
Charles Chanf0ae41e2017-08-23 13:55:39 -070089import org.onosproject.routeservice.ResolvedRoute;
Ray Milkeyae0068a2017-08-15 11:02:29 -070090import org.onosproject.routeservice.RouteEvent;
91import org.onosproject.routeservice.RouteListener;
92import org.onosproject.routeservice.RouteService;
Charles Chanc91c8782016-03-30 17:54:24 -070093import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
94import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventre6b19e482016-11-07 16:21:04 -080095import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkeyae0068a2017-08-15 11:02:29 -070096import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chan82f19972016-05-17 13:13:55 -070097import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chanc91c8782016-03-30 17:54:24 -070098import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -070099import org.onosproject.segmentrouting.grouphandler.DestinationSet;
100import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700101import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800102import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700103import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800104import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
105
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800106import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkeyae0068a2017-08-15 11:02:29 -0700107import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800108import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800109import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
110
Saurav Das261c3002017-06-13 15:35:54 -0700111import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Pier Luigi0f9635b2018-01-15 18:06:43 +0100112import org.onosproject.segmentrouting.storekey.McastStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -0700113import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -0800114import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -0700115import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hart54541d12016-04-12 15:39:44 -0700116import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700117import org.onosproject.store.service.EventuallyConsistentMap;
118import org.onosproject.store.service.EventuallyConsistentMapBuilder;
119import org.onosproject.store.service.StorageService;
120import org.onosproject.store.service.WallClockTimestamp;
Charles Chan873661e2017-11-30 15:37:50 -0800121import org.osgi.service.component.ComponentContext;
sangho80f11cb2015-04-01 13:05:26 -0700122import org.slf4j.Logger;
123import org.slf4j.LoggerFactory;
124
Jonathan Hart61e24e12017-11-30 18:23:42 -0800125import java.util.Collections;
Charles Chan873661e2017-11-30 15:37:50 -0800126import java.util.Dictionary;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800127import java.util.HashSet;
128import java.util.List;
129import java.util.Map;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800130import java.util.Optional;
131import java.util.Set;
132import java.util.concurrent.ConcurrentHashMap;
133import java.util.concurrent.ConcurrentLinkedQueue;
134import java.util.concurrent.Executors;
135import java.util.concurrent.ScheduledExecutorService;
136import java.util.concurrent.ScheduledFuture;
137import java.util.concurrent.TimeUnit;
138import java.util.concurrent.atomic.AtomicBoolean;
139import java.util.stream.Collectors;
sangho80f11cb2015-04-01 13:05:26 -0700140
Charles Chand6d25332016-02-26 22:19:52 -0800141import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800142import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700143import static org.onlab.util.Tools.groupedThreads;
Saurav Dase321cff2018-02-09 17:26:45 -0800144import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
145import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800146import static org.onosproject.segmentrouting.pwaas.PwaasUtil.configurationValidity;
Charles Chand6d25332016-02-26 22:19:52 -0800147
Charles Chanb7f75ac2016-01-11 18:28:54 -0800148/**
149 * Segment routing manager.
150 */
Jonathan Hart54541d12016-04-12 15:39:44 -0700151@Service
152@Component(immediate = true)
sangho27462c62015-05-14 00:39:53 -0700153public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700154
Charles Chan46fdfaf2016-11-09 20:51:44 -0800155 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan910be6a2017-08-23 14:46:43 -0700156 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sangho80f11cb2015-04-01 13:05:26 -0700157
158 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700159 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700160
161 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventreb6a7f342016-11-26 21:05:22 -0800162 private NeighbourResolutionService neighbourResolutionService;
163
164 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700165 CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700166
167 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700168 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700169
170 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700171 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700172
173 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan873661e2017-11-30 15:37:50 -0800174 HostLocationProbingService probingService;
175
176 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700177 DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700178
179 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Saurav Dase6c448a2018-01-18 12:07:33 -0800180 DeviceAdminService deviceAdminService;
181
182 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800183 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700184
185 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700186 LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700187
Charles Chan82ab1932016-01-30 23:22:37 -0800188 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800189 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700190
191 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800192 public StorageService storageService;
Charles Chandebfea32016-10-24 14:52:01 -0700193
194 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
195 MulticastRouteService multicastRouteService;
196
197 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan83163812018-01-09 13:45:07 -0800198 public TopologyService topologyService;
Charles Chandebfea32016-10-24 14:52:01 -0700199
200 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700201 RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800202
203 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800204 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800205
Saurav Dasc3604f12016-03-23 11:22:49 -0700206 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800207 public InterfaceService interfaceService;
208
Pier Luigi580fd8a2018-01-16 10:47:50 +0100209 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
210 public ClusterService clusterService;
211
212 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
213 public LeadershipService leadershipService;
214
Charles Chan873661e2017-11-30 15:37:50 -0800215 @Property(name = "activeProbing", boolValue = true,
216 label = "Enable active probing to discover dual-homed hosts.")
217 boolean activeProbing = true;
218
Charles Chandebfea32016-10-24 14:52:01 -0700219 ArpHandler arpHandler = null;
220 IcmpHandler icmpHandler = null;
221 IpHandler ipHandler = null;
222 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700223 ApplicationId appId;
224 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700225
Charles Chandebfea32016-10-24 14:52:01 -0700226 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700227 private TunnelHandler tunnelHandler = null;
228 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700229 private InternalPacketProcessor processor = null;
230 private InternalLinkListener linkListener = null;
231 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700232 private AppConfigHandler appCfgHandler = null;
Charles Chandebfea32016-10-24 14:52:01 -0700233 XConnectHandler xConnectHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800234 McastHandler mcastHandler = null;
235 HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800236 private RouteHandler routeHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800237 LinkHandler linkHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800238 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800239 private DefaultL2TunnelHandler l2TunnelHandler = null;
sangho80f11cb2015-04-01 13:05:26 -0700240 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan82ab1932016-01-30 23:22:37 -0800241 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700242 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
243 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700244 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sangho80f11cb2015-04-01 13:05:26 -0700245
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700246 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700247 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sangho80f11cb2015-04-01 13:05:26 -0700248
Saurav Das2d94d312015-11-24 23:21:05 -0800249 @SuppressWarnings("unused")
sangho80f11cb2015-04-01 13:05:26 -0700250 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das2d94d312015-11-24 23:21:05 -0800251 @SuppressWarnings("rawtypes")
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700252 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Saurav Dase6c448a2018-01-18 12:07:33 -0800253 Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chanb7f75ac2016-01-11 18:28:54 -0800254 new ConcurrentHashMap<>();
255 /**
Saurav Das261c3002017-06-13 15:35:54 -0700256 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700257 * Used to keep track on MPLS group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800258 */
Charles Chan873661e2017-11-30 15:37:50 -0800259 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das261c3002017-06-13 15:35:54 -0700260 dsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800261 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700262 * Per device next objective ID store with (device id + vlanid) as key.
263 * Used to keep track on L2 flood group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800264 */
Charles Chan873661e2017-11-30 15:37:50 -0800265 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800266 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800267 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700268 * Per device next objective ID store with (device id + port + treatment + meta) as key.
269 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800270 */
Charles Chan873661e2017-11-30 15:37:50 -0800271 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800272 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800273
Saurav Das2d94d312015-11-24 23:21:05 -0800274 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
275 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700276
Saurav Das261c3002017-06-13 15:35:54 -0700277 private AtomicBoolean programmingScheduled = new AtomicBoolean();
278
Charles Chanc91c8782016-03-30 17:54:24 -0700279 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700280 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
281 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700282 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700283 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800284 public SegmentRoutingDeviceConfig createConfig() {
285 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700286 }
287 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800288
Charles Chanc91c8782016-03-30 17:54:24 -0700289 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700290 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
291 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700292 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800293 @Override
294 public SegmentRoutingAppConfig createConfig() {
295 return new SegmentRoutingAppConfig();
296 }
297 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800298
Charles Chan82f19972016-05-17 13:13:55 -0700299 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
300 new ConfigFactory<ApplicationId, XConnectConfig>(
301 SubjectFactories.APP_SUBJECT_FACTORY,
302 XConnectConfig.class, "xconnect") {
303 @Override
304 public XConnectConfig createConfig() {
305 return new XConnectConfig();
306 }
307 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800308
Charles Chanc91c8782016-03-30 17:54:24 -0700309 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700310 new ConfigFactory<ApplicationId, McastConfig>(
311 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700312 McastConfig.class, "multicast") {
313 @Override
314 public McastConfig createConfig() {
315 return new McastConfig();
316 }
317 };
318
Charles Chan3ed34d82017-06-22 18:03:14 -0700319 private static final Object THREAD_SCHED_LOCK = new Object();
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700320 private static int numOfEventsQueued = 0;
321 private static int numOfEventsExecuted = 0;
sangho80f11cb2015-04-01 13:05:26 -0700322 private static int numOfHandlerExecution = 0;
323 private static int numOfHandlerScheduled = 0;
324
Charles Chan1963f4f2016-02-18 14:22:42 -0800325 /**
326 * Segment Routing App ID.
327 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800328 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan10b0fb72017-02-02 16:20:42 -0800329
Charles Chanb7f75ac2016-01-11 18:28:54 -0800330 /**
331 * The default VLAN ID assigned to the interfaces without subnet config.
332 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800333 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das7c305372015-10-28 12:39:42 -0700334
sangho80f11cb2015-04-01 13:05:26 -0700335 @Activate
Charles Chan873661e2017-11-30 15:37:50 -0800336 protected void activate(ComponentContext context) {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800337 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700338
339 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700340 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700341 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700342 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700343 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700344 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700345 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700346 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700347 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700348
Charles Chan10b0fb72017-02-02 16:20:42 -0800349 log.debug("Creating EC map vlannextobjectivestore");
350 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
351 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
352 vlanNextObjStore = vlanNextObjMapBuilder
353 .withName("vlannextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700354 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700355 .withTimestampProvider((k, v) -> new WallClockTimestamp())
356 .build();
357
Saurav Das2d94d312015-11-24 23:21:05 -0800358 log.debug("Creating EC map subnetnextobjectivestore");
359 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
360 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
361 portNextObjStore = portNextObjMapBuilder
362 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700363 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800364 .withTimestampProvider((k, v) -> new WallClockTimestamp())
365 .build();
366
sangho4a5c42a2015-05-20 22:16:38 -0700367 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
368 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700369 tunnelStore = tunnelMapBuilder
370 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700371 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700372 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700373 .build();
374
375 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
376 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700377 policyStore = policyMapBuilder
378 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700379 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700380 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700381 .build();
382
Saurav Dasc3604f12016-03-23 11:22:49 -0700383 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800384 "purgeOnDisconnection", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700385 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800386 "purgeOnDisconnection", "true");
Pier Luigib9632ba2017-01-12 18:14:58 -0800387 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
388 "requestInterceptsEnabled", "false");
Charles Chanc1909e12017-08-08 15:13:37 -0700389 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800390 "requestInterceptsEnabled", "false");
Charles Chan9597f8d2017-07-24 15:56:10 -0700391 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800392 "arpEnabled", "false");
Pier Luigi0ebeb312017-02-02 22:31:34 -0800393 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
394 "greedyLearningIpv6", "true");
Charles Chancf8ea472017-02-28 15:15:17 -0800395 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
396 "forceUnprovision", "true");
Charles Chanc7b73c72017-08-10 16:57:28 -0700397 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan4c95c0d2017-07-20 16:16:25 -0700398 "distributed", "true");
Charles Chan804772f2017-08-14 11:42:11 -0700399 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
400 "multihomingEnabled", "true");
Charles Chan0c9c19f2017-11-29 19:54:20 -0800401 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
402 "staleLinkAge", "15000");
403 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
404 "allowDuplicateIps", "false");
Charles Chan873661e2017-11-30 15:37:50 -0800405 compCfgService.registerProperties(getClass());
406 modified(context);
Saurav Dasc3604f12016-03-23 11:22:49 -0700407
Charles Chan2b078ae2015-10-14 11:24:40 -0700408 processor = new InternalPacketProcessor();
409 linkListener = new InternalLinkListener();
410 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700411 appCfgHandler = new AppConfigHandler(this);
412 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700413 mcastHandler = new McastHandler(this);
414 hostHandler = new HostHandler(this);
Saurav Dase6c448a2018-01-18 12:07:33 -0800415 linkHandler = new LinkHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700416 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800417 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800418 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700419
Charles Chand6d25332016-02-26 22:19:52 -0800420 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700421 cfgService.registerConfigFactory(deviceConfigFactory);
422 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700423 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700424 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase321cff2018-02-09 17:26:45 -0800425 log.info("Configuring network before adding listeners");
Charles Chan6961f222018-01-04 14:26:07 -0800426 cfgListener.configureNetwork();
427
Charles Chan82ab1932016-01-30 23:22:37 -0800428 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700429 packetService.addProcessor(processor, PacketProcessor.director(2));
430 linkService.addListener(linkListener);
431 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700432 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700433 routeService.addListener(routeListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700434
Andreas Pantelopoulos75eef062018-01-11 07:53:48 -0800435 l2TunnelHandler.init();
436
sangho80f11cb2015-04-01 13:05:26 -0700437 log.info("Started");
438 }
439
Saurav Dase6c448a2018-01-18 12:07:33 -0800440 KryoNamespace.Builder createSerializer() {
Jonathan Hart54541d12016-04-12 15:39:44 -0700441 return new KryoNamespace.Builder()
442 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700443 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800444 VlanNextObjectiveStoreKey.class,
445 DestinationSet.class,
446 NextNeighbors.class,
447 Tunnel.class,
448 DefaultTunnel.class,
449 Policy.class,
450 TunnelPolicy.class,
451 Policy.Type.class,
452 PortNextObjectiveStoreKey.class,
453 XConnectStoreKey.class,
454 L2Tunnel.class,
455 L2TunnelPolicy.class,
456 DefaultL2Tunnel.class,
457 DefaultL2TunnelPolicy.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700458 );
459 }
460
sangho80f11cb2015-04-01 13:05:26 -0700461 @Deactivate
462 protected void deactivate() {
Charles Chan72f556a2015-10-05 17:50:33 -0700463 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700464 cfgService.unregisterConfigFactory(deviceConfigFactory);
465 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700466 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700467 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan873661e2017-11-30 15:37:50 -0800468 compCfgService.unregisterProperties(getClass(), false);
Charles Chan72f556a2015-10-05 17:50:33 -0700469
Charles Chanfd48c222017-06-19 00:43:31 -0700470 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700471 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700472 linkService.removeListener(linkListener);
473 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700474 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700475 routeService.removeListener(routeListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700476
Charles Chan2e71ef32017-02-23 15:44:08 -0800477 neighbourResolutionService.unregisterNeighbourHandlers(appId);
478
sangho80f11cb2015-04-01 13:05:26 -0700479 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700480 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700481 deviceListener = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700482 groupHandlerMap.clear();
483
Saurav Das261c3002017-06-13 15:35:54 -0700484 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800485 vlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700486 portNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700487 tunnelStore.destroy();
488 policyStore.destroy();
Pier Luigi35dab3f2018-01-25 16:16:02 +0100489
490 mcastHandler.terminate();
sangho80f11cb2015-04-01 13:05:26 -0700491 log.info("Stopped");
492 }
493
Charles Chan873661e2017-11-30 15:37:50 -0800494 @Modified
495 private void modified(ComponentContext context) {
496 Dictionary<?, ?> properties = context.getProperties();
497 if (properties == null) {
498 return;
499 }
500
501 String strActiveProving = Tools.get(properties, "activeProbing");
502 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
503
504 if (expectActiveProbing != activeProbing) {
505 activeProbing = expectActiveProbing;
506 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
507 }
508 }
509
sangho27462c62015-05-14 00:39:53 -0700510 @Override
511 public List<Tunnel> getTunnels() {
512 return tunnelHandler.getTunnels();
513 }
514
515 @Override
sanghobd812f82015-06-29 14:58:47 -0700516 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
517 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700518 }
519
520 @Override
sanghobd812f82015-06-29 14:58:47 -0700521 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700522 for (Policy policy: policyHandler.getPolicies()) {
523 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
524 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
525 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
526 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700527 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700528 }
529 }
530 }
sanghobd812f82015-06-29 14:58:47 -0700531 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700532 }
533
534 @Override
sanghobd812f82015-06-29 14:58:47 -0700535 public PolicyHandler.Result removePolicy(Policy policy) {
536 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700537 }
538
539 @Override
sanghobd812f82015-06-29 14:58:47 -0700540 public PolicyHandler.Result createPolicy(Policy policy) {
541 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700542 }
543
544 @Override
545 public List<Policy> getPolicies() {
546 return policyHandler.getPolicies();
547 }
548
Saurav Das07c74602016-04-27 18:35:50 -0700549 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800550 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700551 return l2TunnelHandler.getL2Tunnels();
552 }
553
554 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800555 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700556 return l2TunnelHandler.getL2Policies();
557 }
558
559 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800560 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
561
562 List<L2Tunnel> tunnels = getL2Tunnels();
563 List<L2TunnelPolicy> policies = getL2Policies();
564
565 // combine polices and tunnels to pseudowires
566 List<L2TunnelDescription> pseudowires = tunnels.stream()
567 .map(l2Tunnel -> {
568 L2TunnelPolicy policy = null;
569 for (L2TunnelPolicy l2Policy : policies) {
570 if (l2Policy.tunnelId() == l2Tunnel.tunnelId()) {
571 policy = l2Policy;
572 break;
573 }
574 }
575
576 return new DefaultL2TunnelDescription(l2Tunnel, policy);
577 })
578 .collect(Collectors.toList());
579
580
581 // creating a new list with the new pseudowire
582 Set<L2TunnelDescription> newPseudowires = new HashSet<>(pseudowires);
583
584 // corner case where we try to add the exact same pseudowire
585 if (newPseudowires.contains(l2TunnelDescription)) {
586 log.info("Pseudowire with {} already exists!", l2TunnelDescription);
587 return L2TunnelHandler.Result.SUCCESS;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700588 }
589
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800590 // add the new pseudowire to the Set
591 newPseudowires.add(l2TunnelDescription);
592
593 // validate the new set of pseudowires
594 boolean res = configurationValidity(newPseudowires);
595 if (res) {
596
597 // deploy a set with ONLY the new pseudowire
598 newPseudowires = new HashSet<>();
599 newPseudowires.add(l2TunnelDescription);
600 l2TunnelHandler.deploy(newPseudowires);
601
602 log.info("Pseudowire with {} deployment started, check log for any errors in this process!",
603 l2TunnelDescription.l2Tunnel().tunnelId());
604 return L2TunnelHandler.Result.SUCCESS;
605 } else {
606
607 log.error("Pseudowire with {} can not be added!", l2TunnelDescription.l2Tunnel().tunnelId());
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700608 return L2TunnelHandler.Result.ADDITION_ERROR;
609 }
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700610 }
611
612 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800613 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700614
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800615 List<L2Tunnel> tunnels = getL2Tunnels();
616 List<L2TunnelPolicy> policies = getL2Policies();
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700617
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800618 // get the pseudowire, if it exists
619 List<L2TunnelDescription> pseudowires = tunnels.stream().map(l2Tunnel -> {
620 L2TunnelPolicy policy = null;
621 for (L2TunnelPolicy l2Policy : policies) {
622 if (l2Policy.tunnelId() == l2Tunnel.tunnelId()) {
623 policy = l2Policy;
624 break;
625 }
626 }
627
628 return new DefaultL2TunnelDescription(l2Tunnel, policy);
629 }).filter(l2desc ->
630 l2desc.l2Tunnel().tunnelId() == pwId
631 ).collect(Collectors.toList());
632
633 if (pseudowires.size() == 0) {
634 log.error("Pseudowire with id {} does not exist", pwId);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700635 return L2TunnelHandler.Result.REMOVAL_ERROR;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800636 } else {
637
638 l2TunnelHandler.tearDown(new HashSet<>(pseudowires));
639
640 log.info("Removal of pseudowire with {} started, check log for any errors in this process!",
641 pwId);
642 return L2TunnelHandler.Result.SUCCESS;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700643 }
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700644 }
645
646 @Override
Saurav Das07c74602016-04-27 18:35:50 -0700647 public void rerouteNetwork() {
648 cfgListener.configureNetwork();
Saurav Das07c74602016-04-27 18:35:50 -0700649 }
650
Charles Chand7844e52016-10-20 17:02:44 -0700651 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800652 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
653 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart61e24e12017-11-30 18:23:42 -0800654 deviceConfiguration.getRouters().forEach(device ->
655 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chand7844e52016-10-20 17:02:44 -0700656 return deviceSubnetMap;
657 }
658
Saurav Das62ae6792017-05-15 15:34:25 -0700659
660 @Override
661 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
662 if (defaultRoutingHandler != null) {
663 return defaultRoutingHandler.getCurrentEmcpSpgMap();
664 } else {
665 return null;
666 }
667 }
668
669 @Override
Saurav Das261c3002017-06-13 15:35:54 -0700670 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
671 if (dsNextObjStore != null) {
672 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700673 } else {
674 return ImmutableMap.of();
675 }
676 }
677
Saurav Dasfbe74572017-08-03 18:30:35 -0700678 @Override
679 public void verifyGroups(DeviceId id) {
680 DefaultGroupHandler gh = groupHandlerMap.get(id);
681 if (gh != null) {
682 gh.triggerBucketCorrector();
683 }
684 }
685
Saurav Das6430f412018-01-25 09:49:01 -0800686 @Override
687 public ImmutableMap<Link, Boolean> getSeenLinks() {
688 return linkHandler.getSeenLinks();
689 }
690
691 @Override
692 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
693 return linkHandler.getDownedPorts();
694 }
695
Pier Luigi0f9635b2018-01-15 18:06:43 +0100696 @Override
697 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
698 return mcastHandler.getMcastNextIds(mcastIp);
699 }
700
701 @Override
702 public Map<McastStoreKey, McastHandler.McastRole> getMcastRoles(IpAddress mcastIp) {
703 return mcastHandler.getMcastRoles(mcastIp);
704 }
705
706 @Override
707 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
708 return mcastHandler.getMcastPaths(mcastIp);
709 }
710
sangho80f1f892015-05-19 11:57:42 -0700711 /**
Ray Milkeyb85de082017-04-05 09:42:04 -0700712 * Extracts the application ID from the manager.
713 *
714 * @return application ID
715 */
716 public ApplicationId appId() {
717 return appId;
718 }
719
720 /**
721 * Returns the device configuration.
722 *
723 * @return device configuration
724 */
725 public DeviceConfiguration deviceConfiguration() {
726 return deviceConfiguration;
727 }
728
729 /**
Saurav Das261c3002017-06-13 15:35:54 -0700730 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700731 * Used to keep track on MPLS group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700732 *
733 * @return next objective ID store
734 */
Saurav Das261c3002017-06-13 15:35:54 -0700735 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
736 dsNextObjStore() {
737 return dsNextObjStore;
Ray Milkeyb85de082017-04-05 09:42:04 -0700738 }
739
740 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700741 * Per device next objective ID store with (device id + vlanid) as key.
742 * Used to keep track on L2 flood group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700743 *
744 * @return vlan next object store
745 */
746 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
747 return vlanNextObjStore;
748 }
749
750 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700751 * Per device next objective ID store with (device id + port + treatment + meta) as key.
752 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700753 *
754 * @return port next object store.
755 */
756 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
757 return portNextObjStore;
758 }
759
760 /**
Saurav Das261c3002017-06-13 15:35:54 -0700761 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
762 * labeled packets should be programmed or not.
Pier Ventre7a78de22016-10-31 15:00:01 -0700763 *
764 * @return MPLS-ECMP value
765 */
766 public boolean getMplsEcmp() {
767 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
768 .getConfig(this.appId, SegmentRoutingAppConfig.class);
769 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
770 }
771
772 /**
sangho80f1f892015-05-19 11:57:42 -0700773 * Returns the tunnel object with the tunnel ID.
774 *
775 * @param tunnelId Tunnel ID
776 * @return Tunnel reference
777 */
sangho27462c62015-05-14 00:39:53 -0700778 public Tunnel getTunnel(String tunnelId) {
779 return tunnelHandler.getTunnel(tunnelId);
780 }
781
Charles Chan90772a72017-02-08 15:52:08 -0800782 // TODO Consider moving these to InterfaceService
sangho80f11cb2015-04-01 13:05:26 -0700783 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800784 * Returns untagged VLAN configured on given connect point.
Charles Chan90772a72017-02-08 15:52:08 -0800785 * <p>
786 * Only returns the first match if there are multiple untagged VLAN configured
787 * on the connect point.
sangho80f11cb2015-04-01 13:05:26 -0700788 *
Charles Chan10b0fb72017-02-02 16:20:42 -0800789 * @param connectPoint connect point
790 * @return untagged VLAN or null if not configured
sangho80f11cb2015-04-01 13:05:26 -0700791 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700792 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800793 return interfaceService.getInterfacesByPort(connectPoint).stream()
794 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
795 .map(Interface::vlanUntagged)
796 .findFirst().orElse(null);
sangho80f11cb2015-04-01 13:05:26 -0700797 }
798
sangho27462c62015-05-14 00:39:53 -0700799 /**
Charles Chan90772a72017-02-08 15:52:08 -0800800 * Returns tagged VLAN configured on given connect point.
801 * <p>
802 * Returns all matches if there are multiple tagged VLAN configured
803 * on the connect point.
804 *
805 * @param connectPoint connect point
806 * @return tagged VLAN or empty set if not configured
807 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700808 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan90772a72017-02-08 15:52:08 -0800809 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
810 return interfaces.stream()
811 .map(Interface::vlanTagged)
Charles Chan3ed34d82017-06-22 18:03:14 -0700812 .flatMap(Set::stream)
Charles Chan90772a72017-02-08 15:52:08 -0800813 .collect(Collectors.toSet());
814 }
815
816 /**
817 * Returns native VLAN configured on given connect point.
818 * <p>
819 * Only returns the first match if there are multiple native VLAN configured
820 * on the connect point.
821 *
822 * @param connectPoint connect point
823 * @return native VLAN or null if not configured
824 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700825 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan90772a72017-02-08 15:52:08 -0800826 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
827 return interfaces.stream()
828 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
829 .map(Interface::vlanNative)
830 .findFirst()
831 .orElse(null);
832 }
833
834 /**
Charles Chand9265a32017-06-16 15:19:24 -0700835 * Returns internal VLAN for untagged hosts on given connect point.
836 * <p>
837 * The internal VLAN is either vlan-untagged for an access port,
838 * or vlan-native for a trunk port.
839 *
840 * @param connectPoint connect point
841 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
842 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700843 VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chand9265a32017-06-16 15:19:24 -0700844 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
845 VlanId nativeVlanId = getNativeVlanId(connectPoint);
846 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
847 }
848
849 /**
Charles Chan3ed34d82017-06-22 18:03:14 -0700850 * Returns optional pair device ID of given device.
851 *
852 * @param deviceId device ID
853 * @return optional pair device ID. Might be empty if pair device is not configured
854 */
855 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
856 SegmentRoutingDeviceConfig deviceConfig =
857 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
858 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
859 }
860 /**
861 * Returns optional pair device local port of given device.
862 *
863 * @param deviceId device ID
864 * @return optional pair device ID. Might be empty if pair device is not configured
865 */
866 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
867 SegmentRoutingDeviceConfig deviceConfig =
868 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
869 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
870 }
871
872 /**
Charles Chan910be6a2017-08-23 14:46:43 -0700873 * Determine if current instance is the master of given connect point.
874 *
875 * @param cp connect point
876 * @return true if current instance is the master of given connect point
877 */
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700878 public boolean isMasterOf(ConnectPoint cp) {
Charles Chan910be6a2017-08-23 14:46:43 -0700879 boolean isMaster = mastershipService.isLocalMaster(cp.deviceId());
880 if (!isMaster) {
881 log.debug(NOT_MASTER, cp);
882 }
883 return isMaster;
884 }
885
886 /**
Charles Chanf0ae41e2017-08-23 13:55:39 -0700887 * Returns locations of given resolved route.
888 *
889 * @param resolvedRoute resolved route
890 * @return locations of nexthop. Might be empty if next hop is not found
891 */
892 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
893 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
894 return Optional.ofNullable(hostService.getHost(hostId))
895 .map(Host::locations).orElse(Sets.newHashSet())
896 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
897 }
898
899 /**
Charles Chan90772a72017-02-08 15:52:08 -0800900 * Returns vlan port map of given device.
901 *
902 * @param deviceId device id
903 * @return vlan-port multimap
904 */
905 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
906 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
907
908 interfaceService.getInterfaces().stream()
909 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
910 .forEach(intf -> {
911 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan3ed34d82017-06-22 18:03:14 -0700912 intf.vlanTagged().forEach(vlanTagged ->
913 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
914 );
Charles Chan90772a72017-02-08 15:52:08 -0800915 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
916 });
917 vlanPortMap.removeAll(VlanId.NONE);
918
919 return vlanPortMap;
920 }
921
922 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800923 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -0800924 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -0700925 *
926 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -0800927 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -0800928 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -0700929 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700930 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -0700931 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800932 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
933 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -0700934 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -0800935 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -0800936 + "device {} not found", deviceId);
937 return -1;
938 }
939 }
940
941 /**
942 * Returns the next objective ID for the given portNumber, given the treatment.
943 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -0700944 * would result in different objectives. If the next object does not exist,
945 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -0800946 *
947 * @param deviceId Device ID
948 * @param portNum port number on device for which NextObjective is queried
949 * @param treatment the actions to apply on the packets (should include outport)
950 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -0700951 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -0700952 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -0800953 */
954 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
955 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -0700956 TrafficSelector meta,
957 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -0800958 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
959 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -0700960 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -0800961 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800962 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
963 + " not found", deviceId);
964 return -1;
965 }
966 }
967
Saurav Das62ae6792017-05-15 15:34:25 -0700968 /**
969 * Returns the group handler object for the specified device id.
970 *
971 * @param devId the device identifier
972 * @return the groupHandler object for the device id, or null if not found
973 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700974 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Das62ae6792017-05-15 15:34:25 -0700975 return groupHandlerMap.get(devId);
976 }
977
978 /**
Saurav Dasfbe74572017-08-03 18:30:35 -0700979 * Returns the default routing handler object.
980 *
981 * @return the default routing handler object
982 */
983 public DefaultRoutingHandler getRoutingHandler() {
984 return defaultRoutingHandler;
985 }
986
sangho80f11cb2015-04-01 13:05:26 -0700987 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -0700988 @Override
989 public void process(PacketContext context) {
990
991 if (context.isHandled()) {
992 return;
993 }
994
995 InboundPacket pkt = context.inPacket();
996 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -0800997
998 if (ethernet == null) {
999 return;
1000 }
1001
Saurav Das261c3002017-06-13 15:35:54 -07001002 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1003 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001004 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -07001005 log.warn("Received unexpected ARP packet on {}",
1006 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -07001007 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001008 return;
sangho80f11cb2015-04-01 13:05:26 -07001009 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -08001010 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1011 //ipHandler.addToPacketBuffer(ipv4Packet);
1012 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1013 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001014 } else {
Charles Chand041ad82017-01-13 17:20:44 -08001015 // NOTE: We don't support IP learning at this moment so this
1016 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001017 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001018 }
Pier Ventreb6a7f342016-11-26 21:05:22 -08001019 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1020 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -08001021 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -08001022 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -08001023 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1024 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1025 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1026 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1027 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1028 } else {
Saurav Das62ae6792017-05-15 15:34:25 -07001029 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -07001030 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -08001031 }
1032 } else {
1033 // NOTE: We don't support IP learning at this moment so this
1034 // is not necessary. Also it causes duplication of DHCPv6 packets.
1035 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1036 }
sangho80f11cb2015-04-01 13:05:26 -07001037 }
1038 }
1039 }
1040
1041 private class InternalLinkListener implements LinkListener {
1042 @Override
1043 public void event(LinkEvent event) {
Charles Chan0cff8aa2017-03-29 16:39:05 -07001044 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1045 event.type() == LinkEvent.Type.LINK_UPDATED ||
1046 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001047 log.debug("Event {} received from Link Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001048 scheduleEventHandlerIfNotScheduled(event);
1049 }
1050 }
1051 }
1052
1053 private class InternalDeviceListener implements DeviceListener {
sangho80f11cb2015-04-01 13:05:26 -07001054 @Override
1055 public void event(DeviceEvent event) {
sangho80f11cb2015-04-01 13:05:26 -07001056 switch (event.type()) {
1057 case DEVICE_ADDED:
Saurav Dasf0f592d2016-11-18 15:21:57 -08001058 case PORT_UPDATED:
1059 case PORT_ADDED:
sanghofb7c7292015-04-13 15:15:58 -07001060 case DEVICE_UPDATED:
1061 case DEVICE_AVAILABILITY_CHANGED:
Saurav Das62ae6792017-05-15 15:34:25 -07001062 log.trace("Event {} received from Device Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001063 scheduleEventHandlerIfNotScheduled(event);
1064 break;
1065 default:
1066 }
1067 }
1068 }
1069
Saurav Das2d94d312015-11-24 23:21:05 -08001070 @SuppressWarnings("rawtypes")
sangho80f11cb2015-04-01 13:05:26 -07001071 private void scheduleEventHandlerIfNotScheduled(Event event) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001072 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001073 eventQueue.add(event);
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001074 numOfEventsQueued++;
1075
1076 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
1077 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001078 eventHandlerFuture = executorService
1079 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
1080 numOfHandlerScheduled++;
1081 }
Jonathan Hart54541d12016-04-12 15:39:44 -07001082 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001083 numOfEventsQueued,
1084 numOfHandlerScheduled);
sangho80f11cb2015-04-01 13:05:26 -07001085 }
sangho80f11cb2015-04-01 13:05:26 -07001086 }
1087
1088 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001089 @Override
sangho80f11cb2015-04-01 13:05:26 -07001090 public void run() {
Pier Luigi8f362362018-02-12 09:56:20 +01001091 while (true) {
1092 try {
Saurav Das2d94d312015-11-24 23:21:05 -08001093 @SuppressWarnings("rawtypes")
Charles Chan3ed34d82017-06-22 18:03:14 -07001094 Event event;
1095 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001096 if (!eventQueue.isEmpty()) {
1097 event = eventQueue.poll();
1098 numOfEventsExecuted++;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001099 } else {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001100 numOfHandlerExecution++;
1101 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
1102 numOfHandlerExecution, numOfEventsExecuted);
1103 break;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001104 }
sanghofb7c7292015-04-13 15:15:58 -07001105 }
Charles Chan0cff8aa2017-03-29 16:39:05 -07001106 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1107 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Dase6c448a2018-01-18 12:07:33 -08001108 linkHandler.processLinkAdded((Link) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001109 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Saurav Dase6c448a2018-01-18 12:07:33 -08001110 linkHandler.processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001111 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1112 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1113 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das62af8802015-12-04 10:52:59 -08001114 DeviceId deviceId = ((Device) event.subject()).id();
1115 if (deviceService.isAvailable(deviceId)) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001116 log.info("Processing device event {} for available device {}",
1117 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001118 processDeviceAdded((Device) event.subject());
Saurav Dasc3604f12016-03-23 11:22:49 -07001119 } else {
1120 log.info("Processing device event {} for unavailable device {}",
Pier Luigi8f362362018-02-12 09:56:20 +01001121 event.type(), ((Device) event.subject()).id());
Saurav Dasc3604f12016-03-23 11:22:49 -07001122 processDeviceRemoved((Device) event.subject());
1123 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001124 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd1872b02016-12-02 15:43:47 -08001125 // typically these calls come when device is added first time
1126 // so port filtering rules are handled at the device_added event.
1127 // port added calls represent all ports on the device,
1128 // enabled or not.
Saurav Das62ae6792017-05-15 15:34:25 -07001129 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd1872b02016-12-02 15:43:47 -08001130 ((DeviceEvent) event).subject().id(),
1131 ((DeviceEvent) event).port().number(),
1132 event.type());
Saurav Dasf0f592d2016-11-18 15:21:57 -08001133 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd1872b02016-12-02 15:43:47 -08001134 // these calls happen for every subsequent event
1135 // ports enabled, disabled, switch goes away, comes back
Saurav Dasf0f592d2016-11-18 15:21:57 -08001136 log.info("** PORT UPDATED {}/{} -> {}",
1137 event.subject(),
1138 ((DeviceEvent) event).port(),
1139 event.type());
1140 processPortUpdated(((Device) event.subject()),
1141 ((DeviceEvent) event).port());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001142 } else {
1143 log.warn("Unhandled event type: {}", event.type());
1144 }
Pier Luigi8f362362018-02-12 09:56:20 +01001145 } catch (Exception e) {
1146 log.error("SegmentRouting event handler thread thrown an exception: {}",
1147 e.getMessage(), e);
sangho80f11cb2015-04-01 13:05:26 -07001148 }
1149 }
sangho80f11cb2015-04-01 13:05:26 -07001150 }
1151 }
1152
Saurav Dase6c448a2018-01-18 12:07:33 -08001153 void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001154 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001155
1156 // NOTE: Punt ARP/NDP even when the device is not configured.
1157 // Host learning without network config is required for CORD config generator.
1158 routingRulePopulator.populateIpPunts(device.id());
1159 routingRulePopulator.populateArpNdpPunts(device.id());
1160
Charles Chan319d1a22015-11-03 10:42:14 -08001161 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001162 log.warn("Device configuration unavailable. Device {} will be "
1163 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001164 return;
1165 }
Charles Chan72779502016-04-23 17:36:10 -07001166 processDeviceAddedInternal(device.id());
1167 }
1168
1169 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001170 // Irrespective of whether the local is a MASTER or not for this device,
1171 // we need to create a SR-group-handler instance. This is because in a
1172 // multi-instance setup, any instance can initiate forwarding/next-objectives
1173 // for any switch (even if this instance is a SLAVE or not even connected
1174 // to the switch). To handle this, a default-group-handler instance is necessary
1175 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001176 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1177 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001178 DefaultGroupHandler groupHandler;
1179 try {
1180 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001181 createGroupHandler(deviceId,
1182 appId,
1183 deviceConfiguration,
1184 linkService,
1185 flowObjectiveService,
1186 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001187 } catch (DeviceConfigNotFoundException e) {
1188 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1189 return;
1190 }
Saurav Das2b6a00f2017-12-05 15:00:23 -08001191 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan72779502016-04-23 17:36:10 -07001192 deviceId);
1193 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001194 }
Saurav Dasb149be12016-06-07 10:08:06 -07001195
Charles Chan72779502016-04-23 17:36:10 -07001196 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001197 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chandebfea32016-10-24 14:52:01 -07001198 hostHandler.init(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -07001199 xConnectHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001200 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001201 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001202 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001203 }
Charles Chan82ab1932016-01-30 23:22:37 -08001204
Charles Chandebfea32016-10-24 14:52:01 -07001205 appCfgHandler.init(deviceId);
1206 routeHandler.init(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001207 }
1208
Saurav Dasc3604f12016-03-23 11:22:49 -07001209 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001210 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001211 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan873661e2017-11-30 15:37:50 -08001212 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan10b0fb72017-02-02 16:20:42 -08001213 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001214 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001215 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001216 portNextObjStore.entrySet().stream()
1217 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001218 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Dase6c448a2018-01-18 12:07:33 -08001219 linkHandler.processDeviceRemoved(device);
Charles Chan17d75d82017-06-15 00:44:51 -07001220
Saurav Dasfbe74572017-08-03 18:30:35 -07001221 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1222 if (gh != null) {
1223 gh.shutdown();
1224 }
Saurav Das62ae6792017-05-15 15:34:25 -07001225 // Note that a switch going down is associated with all of its links
1226 // going down as well, but it is treated as a single switch down event
1227 // while the link-downs are ignored.
1228 defaultRoutingHandler
1229 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Saurav Das6430f412018-01-25 09:49:01 -08001230 defaultRoutingHandler.purgeEcmpGraph(device.id());
Pier Luigi580fd8a2018-01-16 10:47:50 +01001231 mcastHandler.processDeviceDown(device.id());
Charles Chan82f19972016-05-17 13:13:55 -07001232 xConnectHandler.removeDevice(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -07001233 }
1234
Saurav Dasf0f592d2016-11-18 15:21:57 -08001235 private void processPortUpdated(Device device, Port port) {
1236 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1237 log.warn("Device configuration uploading. Not handling port event for"
1238 + "dev: {} port: {}", device.id(), port.number());
1239 return;
1240 }
Saurav Dasf9332192017-02-18 14:05:44 -08001241
1242 if (!mastershipService.isLocalMaster(device.id())) {
1243 log.debug("Not master for dev:{} .. not handling port updated event"
1244 + "for port {}", device.id(), port.number());
1245 return;
1246 }
1247
1248 // first we handle filtering rules associated with the port
1249 if (port.isEnabled()) {
1250 log.info("Switchport {}/{} enabled..programming filters",
1251 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001252 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001253 } else {
1254 log.info("Switchport {}/{} disabled..removing filters",
1255 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001256 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001257 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001258
1259 // portUpdated calls are for ports that have gone down or up. For switch
1260 // to switch ports, link-events should take care of any re-routing or
1261 // group editing necessary for port up/down. Here we only process edge ports
1262 // that are already configured.
Saurav Das3fb28272017-03-04 16:08:47 -08001263 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1264 VlanId untaggedVlan = getUntaggedVlanId(cp);
1265 VlanId nativeVlan = getNativeVlanId(cp);
1266 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001267
Saurav Das3fb28272017-03-04 16:08:47 -08001268 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001269 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasf0f592d2016-11-18 15:21:57 -08001270 + "dev/port: {}/{}", device.id(), port.number());
1271 return;
1272 }
Saurav Das3fb28272017-03-04 16:08:47 -08001273 if (untaggedVlan != null) {
1274 processEdgePort(device, port, untaggedVlan, true);
1275 }
1276 if (nativeVlan != null) {
1277 processEdgePort(device, port, nativeVlan, true);
1278 }
1279 if (!taggedVlans.isEmpty()) {
1280 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1281 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001282 }
1283
Saurav Das3fb28272017-03-04 16:08:47 -08001284 private void processEdgePort(Device device, Port port, VlanId vlanId,
1285 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001286 boolean portUp = port.isEnabled();
1287 if (portUp) {
Saurav Das3fb28272017-03-04 16:08:47 -08001288 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001289 port.number(), vlanId);
Charles Chan873661e2017-11-30 15:37:50 -08001290 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Dasf0f592d2016-11-18 15:21:57 -08001291 } else {
Saurav Das3fb28272017-03-04 16:08:47 -08001292 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001293 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001294 }
1295
Srikanth Vavilapalli64505482015-04-21 13:04:13 -07001296 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sangho80f11cb2015-04-01 13:05:26 -07001297 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001298 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001299 } else {
1300 log.warn("Group handler not found for dev:{}. Not handling edge port"
1301 + " {} event for port:{}", device.id(),
1302 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001303 }
1304 }
sangho27462c62015-05-14 00:39:53 -07001305
Charles Chan8ca5a122017-10-20 16:06:55 -07001306 private void createOrUpdateDeviceConfiguration() {
1307 if (deviceConfiguration == null) {
Saurav Dase321cff2018-02-09 17:26:45 -08001308 log.info("Creating new DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001309 deviceConfiguration = new DeviceConfiguration(this);
1310 } else {
Saurav Dase321cff2018-02-09 17:26:45 -08001311 log.info("Updating DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001312 deviceConfiguration.updateConfig();
1313 }
1314 }
1315
Pier Ventreb6a7f342016-11-26 21:05:22 -08001316 /**
1317 * Registers the given connect point with the NRS, this is necessary
1318 * to receive the NDP and ARP packets from the NRS.
1319 *
1320 * @param portToRegister connect point to register
1321 */
1322 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001323 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001324 portToRegister,
1325 neighbourHandler,
1326 appId
1327 );
1328 }
1329
Charles Chan72f556a2015-10-05 17:50:33 -07001330 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001331 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001332 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001333
Charles Chanb7f75ac2016-01-11 18:28:54 -08001334 /**
1335 * Constructs the internal network config listener.
1336 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001337 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001338 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001339 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001340 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001341 }
1342
Charles Chanb7f75ac2016-01-11 18:28:54 -08001343 /**
1344 * Reads network config and initializes related data structure accordingly.
1345 */
Charles Chan873661e2017-11-30 15:37:50 -08001346 void configureNetwork() {
Saurav Dase321cff2018-02-09 17:26:45 -08001347 log.info("Configuring network ...");
Charles Chan8ca5a122017-10-20 16:06:55 -07001348 createOrUpdateDeviceConfiguration();
Charles Chane7c61022015-10-07 14:21:45 -07001349
Charles Chan46fdfaf2016-11-09 20:51:44 -08001350 arpHandler = new ArpHandler(srManager);
1351 icmpHandler = new IcmpHandler(srManager);
1352 ipHandler = new IpHandler(srManager);
1353 routingRulePopulator = new RoutingRulePopulator(srManager);
1354 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chane7c61022015-10-07 14:21:45 -07001355
1356 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1357 groupHandlerMap, tunnelStore);
1358 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1359 flowObjectiveService,
1360 tunnelHandler, policyStore);
Saurav Das261c3002017-06-13 15:35:54 -07001361 // add a small delay to absorb multiple network config added notifications
1362 if (!programmingScheduled.get()) {
Saurav Dase321cff2018-02-09 17:26:45 -08001363 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
Saurav Das261c3002017-06-13 15:35:54 -07001364 programmingScheduled.set(true);
1365 executorService.schedule(new ConfigChange(), PROGRAM_DELAY,
1366 TimeUnit.SECONDS);
Charles Chane7c61022015-10-07 14:21:45 -07001367 }
Charles Chan72779502016-04-23 17:36:10 -07001368 mcastHandler.init();
Charles Chane7c61022015-10-07 14:21:45 -07001369 }
1370
Charles Chan72f556a2015-10-05 17:50:33 -07001371 @Override
1372 public void event(NetworkConfigEvent event) {
Charles Chan82ab1932016-01-30 23:22:37 -08001373 // TODO move this part to NetworkConfigEventHandler
1374 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1375 switch (event.type()) {
1376 case CONFIG_ADDED:
Charles Chan68363b12017-06-26 15:25:09 -07001377 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan82ab1932016-01-30 23:22:37 -08001378 configureNetwork();
1379 break;
1380 case CONFIG_UPDATED:
Charles Chan68363b12017-06-26 15:25:09 -07001381 log.info("Segment Routing Config updated for {}", event.subject());
Charles Chan8ca5a122017-10-20 16:06:55 -07001382 createOrUpdateDeviceConfiguration();
Charles Chan68363b12017-06-26 15:25:09 -07001383 // TODO support dynamic configuration
1384 break;
1385 default:
1386 break;
1387 }
1388 } else if (event.configClass().equals(InterfaceConfig.class)) {
1389 switch (event.type()) {
1390 case CONFIG_ADDED:
1391 log.info("Interface Config added for {}", event.subject());
1392 configureNetwork();
1393 break;
1394 case CONFIG_UPDATED:
1395 log.info("Interface Config updated for {}", event.subject());
Charles Chan8ca5a122017-10-20 16:06:55 -07001396 createOrUpdateDeviceConfiguration();
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001397
1398 // Following code will be uncommented when [CORD-634] is fully implemented.
1399 // [CORD-634] Add dynamic config support for interfaces
1400 updateInterface((InterfaceConfig) event.config().get(),
1401 (InterfaceConfig) event.prevConfig().get());
Charles Chan82ab1932016-01-30 23:22:37 -08001402 // TODO support dynamic configuration
1403 break;
1404 default:
1405 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001406 }
Charles Chan82ab1932016-01-30 23:22:37 -08001407 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chan82f19972016-05-17 13:13:55 -07001408 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan82ab1932016-01-30 23:22:37 -08001409 switch (event.type()) {
1410 case CONFIG_ADDED:
Charles Chan82f19972016-05-17 13:13:55 -07001411 appCfgHandler.processAppConfigAdded(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001412 break;
1413 case CONFIG_UPDATED:
Charles Chan82f19972016-05-17 13:13:55 -07001414 appCfgHandler.processAppConfigUpdated(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001415 break;
1416 case CONFIG_REMOVED:
Charles Chan82f19972016-05-17 13:13:55 -07001417 appCfgHandler.processAppConfigRemoved(event);
1418 break;
1419 default:
1420 break;
1421 }
Saurav Dase321cff2018-02-09 17:26:45 -08001422 log.info("App config event .. configuring network");
Charles Chandebfea32016-10-24 14:52:01 -07001423 configureNetwork();
Charles Chan82f19972016-05-17 13:13:55 -07001424 } else if (event.configClass().equals(XConnectConfig.class)) {
1425 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1426 switch (event.type()) {
1427 case CONFIG_ADDED:
1428 xConnectHandler.processXConnectConfigAdded(event);
1429 break;
1430 case CONFIG_UPDATED:
1431 xConnectHandler.processXConnectConfigUpdated(event);
1432 break;
1433 case CONFIG_REMOVED:
1434 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001435 break;
1436 default:
1437 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001438 }
Charles Chan72f556a2015-10-05 17:50:33 -07001439 }
1440 }
Saurav Das261c3002017-06-13 15:35:54 -07001441
Charles Chanc4a68c32018-01-03 16:26:32 -08001442 @Override
1443 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase321cff2018-02-09 17:26:45 -08001444 if (event.type() == CONFIG_REGISTERED ||
1445 event.type() == CONFIG_UNREGISTERED) {
1446 log.debug("Ignore event {} due to type mismatch", event);
1447 return false;
Charles Chanc4a68c32018-01-03 16:26:32 -08001448 }
Saurav Dase321cff2018-02-09 17:26:45 -08001449
1450 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1451 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1452 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -08001453 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase321cff2018-02-09 17:26:45 -08001454 log.debug("Ignore event {} due to class mismatch", event);
1455 return false;
1456 }
1457
1458 return true;
Charles Chanc4a68c32018-01-03 16:26:32 -08001459 }
1460
Saurav Das261c3002017-06-13 15:35:54 -07001461 private final class ConfigChange implements Runnable {
1462 @Override
1463 public void run() {
1464 programmingScheduled.set(false);
Saurav Dase321cff2018-02-09 17:26:45 -08001465 log.info("Reacting to config changes after buffer delay");
Saurav Das261c3002017-06-13 15:35:54 -07001466 for (Device device : deviceService.getDevices()) {
1467 processDeviceAdded(device);
1468 }
1469 defaultRoutingHandler.startPopulationProcess();
1470 }
1471 }
Charles Chan72f556a2015-10-05 17:50:33 -07001472 }
Charles Chanf4586112015-11-09 16:37:23 -08001473
1474 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001475 @Override
1476 public void event(HostEvent event) {
Charles Chanf4586112015-11-09 16:37:23 -08001477 switch (event.type()) {
1478 case HOST_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001479 hostHandler.processHostAddedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001480 break;
1481 case HOST_MOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001482 hostHandler.processHostMovedEvent(event);
Charles Chan910be6a2017-08-23 14:46:43 -07001483 routeHandler.processHostMovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001484 break;
1485 case HOST_REMOVED:
Charles Chand9265a32017-06-16 15:19:24 -07001486 hostHandler.processHostRemovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001487 break;
1488 case HOST_UPDATED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001489 hostHandler.processHostUpdatedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001490 break;
1491 default:
1492 log.warn("Unsupported host event type: {}", event.type());
1493 break;
1494 }
1495 }
1496 }
1497
Charles Chanc91c8782016-03-30 17:54:24 -07001498 private class InternalMcastListener implements McastListener {
1499 @Override
1500 public void event(McastEvent event) {
1501 switch (event.type()) {
1502 case SOURCE_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001503 mcastHandler.processSourceAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001504 break;
1505 case SINK_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001506 mcastHandler.processSinkAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001507 break;
1508 case SINK_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001509 mcastHandler.processSinkRemoved(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001510 break;
Charles Chanc91c8782016-03-30 17:54:24 -07001511 case ROUTE_REMOVED:
Pier Luigi6786b922018-02-02 16:19:11 +01001512 mcastHandler.processRouteRemoved(event);
1513 break;
1514 case ROUTE_ADDED:
Charles Chanc91c8782016-03-30 17:54:24 -07001515 default:
1516 break;
1517 }
1518 }
1519 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001520
Charles Chandebfea32016-10-24 14:52:01 -07001521 private class InternalRouteEventListener implements RouteListener {
1522 @Override
1523 public void event(RouteEvent event) {
1524 switch (event.type()) {
1525 case ROUTE_ADDED:
1526 routeHandler.processRouteAdded(event);
1527 break;
1528 case ROUTE_UPDATED:
1529 routeHandler.processRouteUpdated(event);
1530 break;
1531 case ROUTE_REMOVED:
1532 routeHandler.processRouteRemoved(event);
1533 break;
Charles Chan910be6a2017-08-23 14:46:43 -07001534 case ALTERNATIVE_ROUTES_CHANGED:
1535 routeHandler.processAlternativeRoutesChanged(event);
1536 break;
Charles Chandebfea32016-10-24 14:52:01 -07001537 default:
1538 break;
1539 }
1540 }
1541 }
Saurav Das62ae6792017-05-15 15:34:25 -07001542
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001543 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1544 try {
1545 Set<Interface> intfs = conf.getInterfaces();
1546 Set<Interface> prevIntfs = prevConf.getInterfaces();
1547
1548 // Now we only handle one interface config at each port.
1549 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1550 log.warn("Interface update aborted - one at a time is allowed, " +
1551 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1552 return;
1553 }
1554
1555 Interface intf = intfs.stream().findFirst().get();
1556 Interface prevIntf = prevIntfs.stream().findFirst().get();
1557
1558 DeviceId deviceId = intf.connectPoint().deviceId();
1559 PortNumber portNum = intf.connectPoint().port();
1560
1561 if (!mastershipService.isLocalMaster(deviceId)) {
1562 log.debug("CONFIG_UPDATED event for interfaces should be " +
1563 "handled by master node for device {}", deviceId);
1564 return;
1565 }
1566
1567 removeSubnetConfig(prevIntf.connectPoint(),
1568 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1569 new HashSet<>(intf.ipAddressesList())));
1570
1571 if (prevIntf.vlanNative() != VlanId.NONE && !intf.vlanNative().equals(prevIntf.vlanNative())) {
1572 // RemoveVlanNative
1573 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1574 }
1575
1576 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1577 // RemoveVlanTagged
1578 prevIntf.vlanTagged().stream().filter(i -> !intf.vlanTagged().contains(i)).forEach(
1579 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, false)
1580 );
1581 }
1582
1583 if (prevIntf.vlanUntagged() != VlanId.NONE && !intf.vlanUntagged().equals(prevIntf.vlanUntagged())) {
1584 // RemoveVlanUntagged
1585 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1586 }
1587
1588 if (intf.vlanNative() != VlanId.NONE && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1589 // AddVlanNative
1590 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1591 }
1592
1593 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1594 // AddVlanTagged
1595 intf.vlanTagged().stream().filter(i -> !prevIntf.vlanTagged().contains(i)).forEach(
1596 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, true)
1597 );
1598 }
1599
1600 if (intf.vlanUntagged() != VlanId.NONE && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())) {
1601 // AddVlanUntagged
1602 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1603 }
1604 addSubnetConfig(prevIntf.connectPoint(),
1605 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1606 new HashSet<>(prevIntf.ipAddressesList())));
1607 } catch (ConfigException e) {
1608 log.error("Error in configuration");
1609 }
1610 }
1611
1612 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1613 VlanId vlanId, boolean pushVlan, boolean install) {
1614 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1615 if (grpHandler == null) {
1616 log.warn("Failed to retrieve group handler for device {}", deviceId);
1617 return;
1618 }
1619
1620 // Update filtering objective for a single port
1621 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1622
1623 // Update filtering objective for multicast ingress port
1624 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1625
1626 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1627
1628 if (nextId != -1 && !install) {
1629 // Update next objective for a single port as an output port
1630 // Remove a single port from L2FG
1631 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1632 // Remove L2 Bridging rule and L3 Unicast rule to the host
1633 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1634 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1635 // only if there is no port configured on that VLAN ID
1636 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1637 // Remove broadcast forwarding rule for the VLAN
1638 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1639 // Remove L2FG for VLAN
1640 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1641 } else {
1642 // Remove L2IG of the port
1643 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1644 }
1645 } else if (install) {
1646 if (nextId != -1) {
1647 // Add a single port to L2FG
1648 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1649 } else {
1650 // Create L2FG for VLAN
1651 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1652 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1653 }
1654 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1655 } else {
1656 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1657 }
1658 }
1659
1660 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1661 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1662 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1663
1664 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1665 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1666 .filter(intf -> !intf.connectPoint().equals(cp))
1667 .flatMap(intf -> intf.ipAddressesList().stream())
1668 .collect(Collectors.toSet());
1669 // 1. Partial subnet population
1670 // Remove routing rules for removed subnet from previous configuration,
1671 // which does not also exist in other interfaces in the same device
1672 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1673 .map(InterfaceIpAddress::subnetAddress)
1674 .collect(Collectors.toSet());
1675
1676 defaultRoutingHandler.revokeSubnet(
1677 ipPrefixSet.stream()
1678 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1679 .collect(Collectors.toSet()));
1680
1681 // 2. Interface IP punts
1682 // Remove IP punts for old Intf address
1683 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1684 .map(InterfaceIpAddress::ipAddress)
1685 .collect(Collectors.toSet());
1686 ipAddressSet.stream()
1687 .map(InterfaceIpAddress::ipAddress)
1688 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1689 .forEach(interfaceIpAddress ->
1690 routingRulePopulator.revokeSingleIpPunts(
1691 cp.deviceId(), interfaceIpAddress));
1692
1693 // 3. Host unicast routing rule
1694 // Remove unicast routing rule
1695 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1696 }
1697
1698 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1699 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1700 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1701
1702 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1703 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1704 .filter(intf -> !intf.connectPoint().equals(cp))
1705 .flatMap(intf -> intf.ipAddressesList().stream())
1706 .collect(Collectors.toSet());
1707 // 1. Partial subnet population
1708 // Add routing rules for newly added subnet, which does not also exist in
1709 // other interfaces in the same device
1710 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1711 .map(InterfaceIpAddress::subnetAddress)
1712 .collect(Collectors.toSet());
1713
1714 defaultRoutingHandler.populateSubnet(
1715 Collections.singleton(cp),
1716 ipPrefixSet.stream()
1717 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1718 .collect(Collectors.toSet()));
1719
1720 // 2. Interface IP punts
1721 // Add IP punts for new Intf address
1722 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1723 .map(InterfaceIpAddress::ipAddress)
1724 .collect(Collectors.toSet());
1725 ipAddressSet.stream()
1726 .map(InterfaceIpAddress::ipAddress)
1727 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1728 .forEach(interfaceIpAddress ->
1729 routingRulePopulator.populateSingleIpPunts(
1730 cp.deviceId(), interfaceIpAddress));
1731
1732 // 3. Host unicast routing rule
1733 // Add unicast routing rule
1734 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1735 }
sangho80f11cb2015-04-01 13:05:26 -07001736}