blob: 0e4373b377520c990f0e7d72fd33bf295163c94b [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;
Pier Luigid8a15162018-02-15 16:33:08 +010088import org.onosproject.net.topology.TopologyEvent;
89import org.onosproject.net.topology.TopologyListener;
Charles Chanc91c8782016-03-30 17:54:24 -070090import org.onosproject.net.topology.TopologyService;
Charles Chanf0ae41e2017-08-23 13:55:39 -070091import org.onosproject.routeservice.ResolvedRoute;
Ray Milkeyae0068a2017-08-15 11:02:29 -070092import org.onosproject.routeservice.RouteEvent;
93import org.onosproject.routeservice.RouteListener;
94import org.onosproject.routeservice.RouteService;
Charles Chanc91c8782016-03-30 17:54:24 -070095import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
96import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventre6b19e482016-11-07 16:21:04 -080097import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkeyae0068a2017-08-15 11:02:29 -070098import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chan82f19972016-05-17 13:13:55 -070099import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chanc91c8782016-03-30 17:54:24 -0700100import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -0700101import org.onosproject.segmentrouting.grouphandler.DestinationSet;
102import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Pier Luigi69f774d2018-02-28 12:10:50 +0100103import org.onosproject.segmentrouting.mcast.McastHandler;
104import org.onosproject.segmentrouting.mcast.McastRole;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700105import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800106import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700107import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800108import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
109
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800110import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkeyae0068a2017-08-15 11:02:29 -0700111import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800112import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800113import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
114
Saurav Das261c3002017-06-13 15:35:54 -0700115import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Pier Luigi0f9635b2018-01-15 18:06:43 +0100116import org.onosproject.segmentrouting.storekey.McastStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -0700117import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -0800118import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -0700119import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hart54541d12016-04-12 15:39:44 -0700120import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700121import org.onosproject.store.service.EventuallyConsistentMap;
122import org.onosproject.store.service.EventuallyConsistentMapBuilder;
123import org.onosproject.store.service.StorageService;
124import org.onosproject.store.service.WallClockTimestamp;
Charles Chan873661e2017-11-30 15:37:50 -0800125import org.osgi.service.component.ComponentContext;
sangho80f11cb2015-04-01 13:05:26 -0700126import org.slf4j.Logger;
127import org.slf4j.LoggerFactory;
128
Jonathan Hart61e24e12017-11-30 18:23:42 -0800129import java.util.Collections;
Charles Chan873661e2017-11-30 15:37:50 -0800130import java.util.Dictionary;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800131import java.util.HashSet;
132import java.util.List;
133import java.util.Map;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800134import java.util.Optional;
135import java.util.Set;
136import java.util.concurrent.ConcurrentHashMap;
137import java.util.concurrent.ConcurrentLinkedQueue;
138import java.util.concurrent.Executors;
139import java.util.concurrent.ScheduledExecutorService;
140import java.util.concurrent.ScheduledFuture;
141import java.util.concurrent.TimeUnit;
142import java.util.concurrent.atomic.AtomicBoolean;
143import java.util.stream.Collectors;
sangho80f11cb2015-04-01 13:05:26 -0700144
Charles Chand6d25332016-02-26 22:19:52 -0800145import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800146import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700147import static org.onlab.util.Tools.groupedThreads;
Saurav Dase321cff2018-02-09 17:26:45 -0800148import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
149import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800150import static org.onosproject.segmentrouting.pwaas.PwaasUtil.configurationValidity;
Charles Chand6d25332016-02-26 22:19:52 -0800151
Charles Chanb7f75ac2016-01-11 18:28:54 -0800152/**
153 * Segment routing manager.
154 */
Jonathan Hart54541d12016-04-12 15:39:44 -0700155@Service
156@Component(immediate = true)
sangho27462c62015-05-14 00:39:53 -0700157public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700158
Charles Chan46fdfaf2016-11-09 20:51:44 -0800159 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan910be6a2017-08-23 14:46:43 -0700160 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sangho80f11cb2015-04-01 13:05:26 -0700161
162 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700163 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700164
165 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventreb6a7f342016-11-26 21:05:22 -0800166 private NeighbourResolutionService neighbourResolutionService;
167
168 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100169 public CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700170
171 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700172 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700173
174 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700175 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700176
177 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan873661e2017-11-30 15:37:50 -0800178 HostLocationProbingService probingService;
179
180 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100181 public DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700182
183 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Saurav Dase6c448a2018-01-18 12:07:33 -0800184 DeviceAdminService deviceAdminService;
185
186 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800187 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700188
189 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100190 public LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700191
Charles Chan82ab1932016-01-30 23:22:37 -0800192 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800193 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700194
195 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800196 public StorageService storageService;
Charles Chandebfea32016-10-24 14:52:01 -0700197
198 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100199 public MulticastRouteService multicastRouteService;
Charles Chandebfea32016-10-24 14:52:01 -0700200
201 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan83163812018-01-09 13:45:07 -0800202 public TopologyService topologyService;
Charles Chandebfea32016-10-24 14:52:01 -0700203
204 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700205 RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800206
207 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800208 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800209
Saurav Dasc3604f12016-03-23 11:22:49 -0700210 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800211 public InterfaceService interfaceService;
212
Pier Luigi580fd8a2018-01-16 10:47:50 +0100213 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
214 public ClusterService clusterService;
215
216 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
217 public LeadershipService leadershipService;
218
Charles Chan873661e2017-11-30 15:37:50 -0800219 @Property(name = "activeProbing", boolValue = true,
220 label = "Enable active probing to discover dual-homed hosts.")
221 boolean activeProbing = true;
222
Charles Chandebfea32016-10-24 14:52:01 -0700223 ArpHandler arpHandler = null;
224 IcmpHandler icmpHandler = null;
225 IpHandler ipHandler = null;
226 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700227 ApplicationId appId;
228 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700229
Charles Chandebfea32016-10-24 14:52:01 -0700230 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700231 private TunnelHandler tunnelHandler = null;
232 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700233 private InternalPacketProcessor processor = null;
234 private InternalLinkListener linkListener = null;
235 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700236 private AppConfigHandler appCfgHandler = null;
Pier Luigi69f774d2018-02-28 12:10:50 +0100237 public XConnectHandler xConnectHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800238 McastHandler mcastHandler = null;
239 HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800240 private RouteHandler routeHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800241 LinkHandler linkHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800242 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800243 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigid8a15162018-02-15 16:33:08 +0100244 private TopologyHandler topologyHandler = null;
sangho80f11cb2015-04-01 13:05:26 -0700245 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan82ab1932016-01-30 23:22:37 -0800246 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700247 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
248 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700249 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigid8a15162018-02-15 16:33:08 +0100250 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
sangho80f11cb2015-04-01 13:05:26 -0700251
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700252 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700253 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sangho80f11cb2015-04-01 13:05:26 -0700254
Saurav Das2d94d312015-11-24 23:21:05 -0800255 @SuppressWarnings("unused")
sangho80f11cb2015-04-01 13:05:26 -0700256 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das2d94d312015-11-24 23:21:05 -0800257 @SuppressWarnings("rawtypes")
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700258 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Saurav Dase6c448a2018-01-18 12:07:33 -0800259 Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chanb7f75ac2016-01-11 18:28:54 -0800260 new ConcurrentHashMap<>();
261 /**
Saurav Das261c3002017-06-13 15:35:54 -0700262 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700263 * Used to keep track on MPLS group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800264 */
Charles Chan873661e2017-11-30 15:37:50 -0800265 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das261c3002017-06-13 15:35:54 -0700266 dsNextObjStore = 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 + vlanid) as key.
269 * Used to keep track on L2 flood group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800270 */
Charles Chan873661e2017-11-30 15:37:50 -0800271 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800272 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800273 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700274 * Per device next objective ID store with (device id + port + treatment + meta) as key.
275 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800276 */
Charles Chan873661e2017-11-30 15:37:50 -0800277 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800278 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800279
Saurav Das2d94d312015-11-24 23:21:05 -0800280 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
281 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700282
Saurav Das261c3002017-06-13 15:35:54 -0700283 private AtomicBoolean programmingScheduled = new AtomicBoolean();
284
Charles Chanc91c8782016-03-30 17:54:24 -0700285 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700286 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
287 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700288 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700289 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800290 public SegmentRoutingDeviceConfig createConfig() {
291 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700292 }
293 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800294
Charles Chanc91c8782016-03-30 17:54:24 -0700295 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700296 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
297 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700298 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800299 @Override
300 public SegmentRoutingAppConfig createConfig() {
301 return new SegmentRoutingAppConfig();
302 }
303 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800304
Charles Chan82f19972016-05-17 13:13:55 -0700305 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
306 new ConfigFactory<ApplicationId, XConnectConfig>(
307 SubjectFactories.APP_SUBJECT_FACTORY,
308 XConnectConfig.class, "xconnect") {
309 @Override
310 public XConnectConfig createConfig() {
311 return new XConnectConfig();
312 }
313 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800314
Charles Chanc91c8782016-03-30 17:54:24 -0700315 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700316 new ConfigFactory<ApplicationId, McastConfig>(
317 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700318 McastConfig.class, "multicast") {
319 @Override
320 public McastConfig createConfig() {
321 return new McastConfig();
322 }
323 };
324
Charles Chan3ed34d82017-06-22 18:03:14 -0700325 private static final Object THREAD_SCHED_LOCK = new Object();
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700326 private static int numOfEventsQueued = 0;
327 private static int numOfEventsExecuted = 0;
sangho80f11cb2015-04-01 13:05:26 -0700328 private static int numOfHandlerExecution = 0;
329 private static int numOfHandlerScheduled = 0;
330
Charles Chan1963f4f2016-02-18 14:22:42 -0800331 /**
332 * Segment Routing App ID.
333 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800334 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan10b0fb72017-02-02 16:20:42 -0800335
Charles Chanb7f75ac2016-01-11 18:28:54 -0800336 /**
337 * The default VLAN ID assigned to the interfaces without subnet config.
338 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800339 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das7c305372015-10-28 12:39:42 -0700340
sangho80f11cb2015-04-01 13:05:26 -0700341 @Activate
Charles Chan873661e2017-11-30 15:37:50 -0800342 protected void activate(ComponentContext context) {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800343 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700344
345 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700346 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700347 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700348 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700349 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700350 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700351 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700352 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700353 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700354
Charles Chan10b0fb72017-02-02 16:20:42 -0800355 log.debug("Creating EC map vlannextobjectivestore");
356 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
357 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
358 vlanNextObjStore = vlanNextObjMapBuilder
359 .withName("vlannextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700360 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700361 .withTimestampProvider((k, v) -> new WallClockTimestamp())
362 .build();
363
Saurav Das2d94d312015-11-24 23:21:05 -0800364 log.debug("Creating EC map subnetnextobjectivestore");
365 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
366 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
367 portNextObjStore = portNextObjMapBuilder
368 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700369 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800370 .withTimestampProvider((k, v) -> new WallClockTimestamp())
371 .build();
372
sangho4a5c42a2015-05-20 22:16:38 -0700373 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
374 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700375 tunnelStore = tunnelMapBuilder
376 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700377 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700378 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700379 .build();
380
381 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
382 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700383 policyStore = policyMapBuilder
384 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700385 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700386 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700387 .build();
388
Saurav Dasc3604f12016-03-23 11:22:49 -0700389 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800390 "purgeOnDisconnection", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700391 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800392 "purgeOnDisconnection", "true");
Pier Luigib9632ba2017-01-12 18:14:58 -0800393 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
394 "requestInterceptsEnabled", "false");
Charles Chanc1909e12017-08-08 15:13:37 -0700395 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800396 "requestInterceptsEnabled", "false");
Charles Chan9597f8d2017-07-24 15:56:10 -0700397 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800398 "arpEnabled", "false");
Pier Luigi0ebeb312017-02-02 22:31:34 -0800399 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
400 "greedyLearningIpv6", "true");
Charles Chancf8ea472017-02-28 15:15:17 -0800401 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
402 "forceUnprovision", "true");
Charles Chanc7b73c72017-08-10 16:57:28 -0700403 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan4c95c0d2017-07-20 16:16:25 -0700404 "distributed", "true");
Charles Chan804772f2017-08-14 11:42:11 -0700405 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
406 "multihomingEnabled", "true");
Charles Chan0c9c19f2017-11-29 19:54:20 -0800407 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
408 "staleLinkAge", "15000");
409 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
410 "allowDuplicateIps", "false");
Charles Chan873661e2017-11-30 15:37:50 -0800411 compCfgService.registerProperties(getClass());
412 modified(context);
Saurav Dasc3604f12016-03-23 11:22:49 -0700413
Charles Chan2b078ae2015-10-14 11:24:40 -0700414 processor = new InternalPacketProcessor();
415 linkListener = new InternalLinkListener();
416 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700417 appCfgHandler = new AppConfigHandler(this);
418 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700419 mcastHandler = new McastHandler(this);
420 hostHandler = new HostHandler(this);
Saurav Dase6c448a2018-01-18 12:07:33 -0800421 linkHandler = new LinkHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700422 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800423 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800424 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigid8a15162018-02-15 16:33:08 +0100425 topologyHandler = new TopologyHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700426
Charles Chand6d25332016-02-26 22:19:52 -0800427 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700428 cfgService.registerConfigFactory(deviceConfigFactory);
429 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700430 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700431 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase321cff2018-02-09 17:26:45 -0800432 log.info("Configuring network before adding listeners");
Charles Chan6961f222018-01-04 14:26:07 -0800433 cfgListener.configureNetwork();
434
Charles Chan82ab1932016-01-30 23:22:37 -0800435 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700436 packetService.addProcessor(processor, PacketProcessor.director(2));
437 linkService.addListener(linkListener);
438 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700439 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700440 routeService.addListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100441 topologyService.addListener(topologyListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700442
Charles Chanc12c3d02018-03-09 15:53:44 -0800443 linkHandler.init();
Andreas Pantelopoulos75eef062018-01-11 07:53:48 -0800444 l2TunnelHandler.init();
445
sangho80f11cb2015-04-01 13:05:26 -0700446 log.info("Started");
447 }
448
Saurav Dase6c448a2018-01-18 12:07:33 -0800449 KryoNamespace.Builder createSerializer() {
Jonathan Hart54541d12016-04-12 15:39:44 -0700450 return new KryoNamespace.Builder()
451 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700452 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800453 VlanNextObjectiveStoreKey.class,
454 DestinationSet.class,
455 NextNeighbors.class,
456 Tunnel.class,
457 DefaultTunnel.class,
458 Policy.class,
459 TunnelPolicy.class,
460 Policy.Type.class,
461 PortNextObjectiveStoreKey.class,
462 XConnectStoreKey.class,
463 L2Tunnel.class,
464 L2TunnelPolicy.class,
465 DefaultL2Tunnel.class,
466 DefaultL2TunnelPolicy.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700467 );
468 }
469
sangho80f11cb2015-04-01 13:05:26 -0700470 @Deactivate
471 protected void deactivate() {
Charles Chan72f556a2015-10-05 17:50:33 -0700472 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700473 cfgService.unregisterConfigFactory(deviceConfigFactory);
474 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700475 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700476 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan873661e2017-11-30 15:37:50 -0800477 compCfgService.unregisterProperties(getClass(), false);
Charles Chan72f556a2015-10-05 17:50:33 -0700478
Charles Chanfd48c222017-06-19 00:43:31 -0700479 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700480 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700481 linkService.removeListener(linkListener);
482 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700483 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700484 routeService.removeListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100485 topologyService.removeListener(topologyListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700486
Charles Chan2e71ef32017-02-23 15:44:08 -0800487 neighbourResolutionService.unregisterNeighbourHandlers(appId);
488
sangho80f11cb2015-04-01 13:05:26 -0700489 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700490 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700491 deviceListener = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700492 groupHandlerMap.clear();
493
Saurav Das261c3002017-06-13 15:35:54 -0700494 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800495 vlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700496 portNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700497 tunnelStore.destroy();
498 policyStore.destroy();
Pier Luigi35dab3f2018-01-25 16:16:02 +0100499
500 mcastHandler.terminate();
sangho80f11cb2015-04-01 13:05:26 -0700501 log.info("Stopped");
502 }
503
Charles Chan873661e2017-11-30 15:37:50 -0800504 @Modified
505 private void modified(ComponentContext context) {
506 Dictionary<?, ?> properties = context.getProperties();
507 if (properties == null) {
508 return;
509 }
510
511 String strActiveProving = Tools.get(properties, "activeProbing");
512 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
513
514 if (expectActiveProbing != activeProbing) {
515 activeProbing = expectActiveProbing;
516 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
517 }
518 }
519
sangho27462c62015-05-14 00:39:53 -0700520 @Override
521 public List<Tunnel> getTunnels() {
522 return tunnelHandler.getTunnels();
523 }
524
525 @Override
sanghobd812f82015-06-29 14:58:47 -0700526 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
527 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700528 }
529
530 @Override
sanghobd812f82015-06-29 14:58:47 -0700531 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700532 for (Policy policy: policyHandler.getPolicies()) {
533 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
534 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
535 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
536 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700537 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700538 }
539 }
540 }
sanghobd812f82015-06-29 14:58:47 -0700541 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700542 }
543
544 @Override
sanghobd812f82015-06-29 14:58:47 -0700545 public PolicyHandler.Result removePolicy(Policy policy) {
546 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700547 }
548
549 @Override
sanghobd812f82015-06-29 14:58:47 -0700550 public PolicyHandler.Result createPolicy(Policy policy) {
551 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700552 }
553
554 @Override
555 public List<Policy> getPolicies() {
556 return policyHandler.getPolicies();
557 }
558
Saurav Das07c74602016-04-27 18:35:50 -0700559 @Override
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700560 public Set<L2TunnelDescription> getL2TunnelDescriptions() {
561 return l2TunnelHandler.getL2Descriptions();
562 }
563
564 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800565 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700566 return l2TunnelHandler.getL2Tunnels();
567 }
568
569 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800570 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700571 return l2TunnelHandler.getL2Policies();
572 }
573
574 @Override
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700575 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
576
577 Set<L2TunnelDescription> pseudowires = l2TunnelHandler.getL2Descriptions();
578
579 pseudowires.addAll(bulkPseudowires);
580 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
581
582 // check global validity for all the new pseudowires, if it fails
583 // do not add any of them
584 boolean res = configurationValidity(pseudowires);
585 if (res) {
586 l2TunnelHandler.deploy(newPseudowires);
587 return L2TunnelHandler.Result.SUCCESS;
588 } else {
589 log.error("Bulk pseudowires {} can not be added, error in global configuration!",
590 newPseudowires);
591 return L2TunnelHandler.Result.ADDITION_ERROR;
592 }
593 }
594
595 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800596 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
597
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700598 Set<L2TunnelDescription> newPseudowires = l2TunnelHandler.getL2Descriptions();
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800599
600 // corner case where we try to add the exact same pseudowire
601 if (newPseudowires.contains(l2TunnelDescription)) {
602 log.info("Pseudowire with {} already exists!", l2TunnelDescription);
603 return L2TunnelHandler.Result.SUCCESS;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700604 }
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800605 // add the new pseudowire to the Set
606 newPseudowires.add(l2TunnelDescription);
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800607 // validate the new set of pseudowires
608 boolean res = configurationValidity(newPseudowires);
609 if (res) {
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800610 // deploy a set with ONLY the new pseudowire
611 newPseudowires = new HashSet<>();
612 newPseudowires.add(l2TunnelDescription);
613 l2TunnelHandler.deploy(newPseudowires);
614
615 log.info("Pseudowire with {} deployment started, check log for any errors in this process!",
616 l2TunnelDescription.l2Tunnel().tunnelId());
617 return L2TunnelHandler.Result.SUCCESS;
618 } else {
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800619 log.error("Pseudowire with {} can not be added!", l2TunnelDescription.l2Tunnel().tunnelId());
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700620 return L2TunnelHandler.Result.ADDITION_ERROR;
621 }
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700622 }
623
624 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800625 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700626
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800627 List<L2Tunnel> tunnels = getL2Tunnels();
628 List<L2TunnelPolicy> policies = getL2Policies();
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700629
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800630 // get the pseudowire, if it exists
631 List<L2TunnelDescription> pseudowires = tunnels.stream().map(l2Tunnel -> {
632 L2TunnelPolicy policy = null;
633 for (L2TunnelPolicy l2Policy : policies) {
634 if (l2Policy.tunnelId() == l2Tunnel.tunnelId()) {
635 policy = l2Policy;
636 break;
637 }
638 }
639
640 return new DefaultL2TunnelDescription(l2Tunnel, policy);
641 }).filter(l2desc ->
642 l2desc.l2Tunnel().tunnelId() == pwId
643 ).collect(Collectors.toList());
644
645 if (pseudowires.size() == 0) {
646 log.error("Pseudowire with id {} does not exist", pwId);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700647 return L2TunnelHandler.Result.REMOVAL_ERROR;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800648 } else {
649
650 l2TunnelHandler.tearDown(new HashSet<>(pseudowires));
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800651 log.info("Removal of pseudowire with {} started, check log for any errors in this process!",
652 pwId);
653 return L2TunnelHandler.Result.SUCCESS;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700654 }
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700655 }
656
657 @Override
Saurav Das07c74602016-04-27 18:35:50 -0700658 public void rerouteNetwork() {
659 cfgListener.configureNetwork();
Saurav Das07c74602016-04-27 18:35:50 -0700660 }
661
Charles Chand7844e52016-10-20 17:02:44 -0700662 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800663 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
664 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart61e24e12017-11-30 18:23:42 -0800665 deviceConfiguration.getRouters().forEach(device ->
666 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chand7844e52016-10-20 17:02:44 -0700667 return deviceSubnetMap;
668 }
669
Saurav Das62ae6792017-05-15 15:34:25 -0700670
671 @Override
672 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
673 if (defaultRoutingHandler != null) {
674 return defaultRoutingHandler.getCurrentEmcpSpgMap();
675 } else {
676 return null;
677 }
678 }
679
680 @Override
Saurav Das261c3002017-06-13 15:35:54 -0700681 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
682 if (dsNextObjStore != null) {
683 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700684 } else {
685 return ImmutableMap.of();
686 }
687 }
688
Saurav Dasfbe74572017-08-03 18:30:35 -0700689 @Override
690 public void verifyGroups(DeviceId id) {
691 DefaultGroupHandler gh = groupHandlerMap.get(id);
692 if (gh != null) {
693 gh.triggerBucketCorrector();
694 }
695 }
696
Saurav Das6430f412018-01-25 09:49:01 -0800697 @Override
698 public ImmutableMap<Link, Boolean> getSeenLinks() {
699 return linkHandler.getSeenLinks();
700 }
701
702 @Override
703 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
704 return linkHandler.getDownedPorts();
705 }
706
Pier Luigi0f9635b2018-01-15 18:06:43 +0100707 @Override
708 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
709 return mcastHandler.getMcastNextIds(mcastIp);
710 }
711
712 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +0100713 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigi0f9635b2018-01-15 18:06:43 +0100714 return mcastHandler.getMcastRoles(mcastIp);
715 }
716
717 @Override
718 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
719 return mcastHandler.getMcastPaths(mcastIp);
720 }
721
sangho80f1f892015-05-19 11:57:42 -0700722 /**
Ray Milkeyb85de082017-04-05 09:42:04 -0700723 * Extracts the application ID from the manager.
724 *
725 * @return application ID
726 */
727 public ApplicationId appId() {
728 return appId;
729 }
730
731 /**
732 * Returns the device configuration.
733 *
734 * @return device configuration
735 */
736 public DeviceConfiguration deviceConfiguration() {
737 return deviceConfiguration;
738 }
739
740 /**
Saurav Das261c3002017-06-13 15:35:54 -0700741 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700742 * Used to keep track on MPLS group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700743 *
744 * @return next objective ID store
745 */
Saurav Das261c3002017-06-13 15:35:54 -0700746 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
747 dsNextObjStore() {
748 return dsNextObjStore;
Ray Milkeyb85de082017-04-05 09:42:04 -0700749 }
750
751 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700752 * Per device next objective ID store with (device id + vlanid) as key.
753 * Used to keep track on L2 flood group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700754 *
755 * @return vlan next object store
756 */
757 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
758 return vlanNextObjStore;
759 }
760
761 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700762 * Per device next objective ID store with (device id + port + treatment + meta) as key.
763 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700764 *
765 * @return port next object store.
766 */
767 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
768 return portNextObjStore;
769 }
770
771 /**
Saurav Das261c3002017-06-13 15:35:54 -0700772 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
773 * labeled packets should be programmed or not.
Pier Ventre7a78de22016-10-31 15:00:01 -0700774 *
775 * @return MPLS-ECMP value
776 */
777 public boolean getMplsEcmp() {
778 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
779 .getConfig(this.appId, SegmentRoutingAppConfig.class);
780 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
781 }
782
783 /**
sangho80f1f892015-05-19 11:57:42 -0700784 * Returns the tunnel object with the tunnel ID.
785 *
786 * @param tunnelId Tunnel ID
787 * @return Tunnel reference
788 */
sangho27462c62015-05-14 00:39:53 -0700789 public Tunnel getTunnel(String tunnelId) {
790 return tunnelHandler.getTunnel(tunnelId);
791 }
792
Charles Chan90772a72017-02-08 15:52:08 -0800793 // TODO Consider moving these to InterfaceService
sangho80f11cb2015-04-01 13:05:26 -0700794 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800795 * Returns untagged VLAN configured on given connect point.
Charles Chan90772a72017-02-08 15:52:08 -0800796 * <p>
797 * Only returns the first match if there are multiple untagged VLAN configured
798 * on the connect point.
sangho80f11cb2015-04-01 13:05:26 -0700799 *
Charles Chan10b0fb72017-02-02 16:20:42 -0800800 * @param connectPoint connect point
801 * @return untagged VLAN or null if not configured
sangho80f11cb2015-04-01 13:05:26 -0700802 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700803 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800804 return interfaceService.getInterfacesByPort(connectPoint).stream()
805 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
806 .map(Interface::vlanUntagged)
807 .findFirst().orElse(null);
sangho80f11cb2015-04-01 13:05:26 -0700808 }
809
sangho27462c62015-05-14 00:39:53 -0700810 /**
Charles Chan90772a72017-02-08 15:52:08 -0800811 * Returns tagged VLAN configured on given connect point.
812 * <p>
813 * Returns all matches if there are multiple tagged VLAN configured
814 * on the connect point.
815 *
816 * @param connectPoint connect point
817 * @return tagged VLAN or empty set if not configured
818 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700819 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan90772a72017-02-08 15:52:08 -0800820 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
821 return interfaces.stream()
822 .map(Interface::vlanTagged)
Charles Chan3ed34d82017-06-22 18:03:14 -0700823 .flatMap(Set::stream)
Charles Chan90772a72017-02-08 15:52:08 -0800824 .collect(Collectors.toSet());
825 }
826
827 /**
828 * Returns native VLAN configured on given connect point.
829 * <p>
830 * Only returns the first match if there are multiple native VLAN configured
831 * on the connect point.
832 *
833 * @param connectPoint connect point
834 * @return native VLAN or null if not configured
835 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700836 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan90772a72017-02-08 15:52:08 -0800837 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
838 return interfaces.stream()
839 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
840 .map(Interface::vlanNative)
841 .findFirst()
842 .orElse(null);
843 }
844
845 /**
Charles Chand9265a32017-06-16 15:19:24 -0700846 * Returns internal VLAN for untagged hosts on given connect point.
847 * <p>
848 * The internal VLAN is either vlan-untagged for an access port,
849 * or vlan-native for a trunk port.
850 *
851 * @param connectPoint connect point
852 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
853 */
Pier Luigi69f774d2018-02-28 12:10:50 +0100854 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chand9265a32017-06-16 15:19:24 -0700855 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
856 VlanId nativeVlanId = getNativeVlanId(connectPoint);
857 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
858 }
859
860 /**
Charles Chan3ed34d82017-06-22 18:03:14 -0700861 * Returns optional pair device ID 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<DeviceId> getPairDeviceId(DeviceId deviceId) {
867 SegmentRoutingDeviceConfig deviceConfig =
868 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
869 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
870 }
871 /**
872 * Returns optional pair device local port of given device.
873 *
874 * @param deviceId device ID
875 * @return optional pair device ID. Might be empty if pair device is not configured
876 */
877 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
878 SegmentRoutingDeviceConfig deviceConfig =
879 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
880 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
881 }
882
883 /**
Charles Chan910be6a2017-08-23 14:46:43 -0700884 * Determine if current instance is the master of given connect point.
885 *
886 * @param cp connect point
887 * @return true if current instance is the master of given connect point
888 */
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700889 public boolean isMasterOf(ConnectPoint cp) {
Charles Chan910be6a2017-08-23 14:46:43 -0700890 boolean isMaster = mastershipService.isLocalMaster(cp.deviceId());
891 if (!isMaster) {
892 log.debug(NOT_MASTER, cp);
893 }
894 return isMaster;
895 }
896
897 /**
Charles Chanf0ae41e2017-08-23 13:55:39 -0700898 * Returns locations of given resolved route.
899 *
900 * @param resolvedRoute resolved route
901 * @return locations of nexthop. Might be empty if next hop is not found
902 */
903 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
904 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
905 return Optional.ofNullable(hostService.getHost(hostId))
906 .map(Host::locations).orElse(Sets.newHashSet())
907 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
908 }
909
910 /**
Charles Chan90772a72017-02-08 15:52:08 -0800911 * Returns vlan port map of given device.
912 *
913 * @param deviceId device id
914 * @return vlan-port multimap
915 */
916 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
917 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
918
919 interfaceService.getInterfaces().stream()
920 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
921 .forEach(intf -> {
922 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan3ed34d82017-06-22 18:03:14 -0700923 intf.vlanTagged().forEach(vlanTagged ->
924 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
925 );
Charles Chan90772a72017-02-08 15:52:08 -0800926 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
927 });
928 vlanPortMap.removeAll(VlanId.NONE);
929
930 return vlanPortMap;
931 }
932
933 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800934 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -0800935 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -0700936 *
937 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -0800938 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -0800939 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -0700940 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700941 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -0700942 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800943 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
944 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -0700945 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -0800946 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -0800947 + "device {} not found", deviceId);
948 return -1;
949 }
950 }
951
952 /**
953 * Returns the next objective ID for the given portNumber, given the treatment.
954 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -0700955 * would result in different objectives. If the next object does not exist,
956 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -0800957 *
958 * @param deviceId Device ID
959 * @param portNum port number on device for which NextObjective is queried
960 * @param treatment the actions to apply on the packets (should include outport)
961 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -0700962 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -0700963 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -0800964 */
965 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
966 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -0700967 TrafficSelector meta,
968 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -0800969 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
970 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -0700971 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -0800972 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800973 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
974 + " not found", deviceId);
975 return -1;
976 }
977 }
978
Saurav Das62ae6792017-05-15 15:34:25 -0700979 /**
980 * Returns the group handler object for the specified device id.
981 *
982 * @param devId the device identifier
983 * @return the groupHandler object for the device id, or null if not found
984 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700985 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Das62ae6792017-05-15 15:34:25 -0700986 return groupHandlerMap.get(devId);
987 }
988
989 /**
Saurav Dasfbe74572017-08-03 18:30:35 -0700990 * Returns the default routing handler object.
991 *
992 * @return the default routing handler object
993 */
994 public DefaultRoutingHandler getRoutingHandler() {
995 return defaultRoutingHandler;
996 }
997
sangho80f11cb2015-04-01 13:05:26 -0700998 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -0700999 @Override
1000 public void process(PacketContext context) {
1001
1002 if (context.isHandled()) {
1003 return;
1004 }
1005
1006 InboundPacket pkt = context.inPacket();
1007 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -08001008
1009 if (ethernet == null) {
1010 return;
1011 }
1012
Saurav Das261c3002017-06-13 15:35:54 -07001013 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1014 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001015 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -07001016 log.warn("Received unexpected ARP packet on {}",
1017 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -07001018 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001019 return;
sangho80f11cb2015-04-01 13:05:26 -07001020 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -08001021 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1022 //ipHandler.addToPacketBuffer(ipv4Packet);
1023 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1024 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001025 } else {
Charles Chand041ad82017-01-13 17:20:44 -08001026 // NOTE: We don't support IP learning at this moment so this
1027 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001028 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001029 }
Pier Ventreb6a7f342016-11-26 21:05:22 -08001030 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1031 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -08001032 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -08001033 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -08001034 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1035 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1036 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1037 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1038 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1039 } else {
Saurav Das62ae6792017-05-15 15:34:25 -07001040 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -07001041 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -08001042 }
1043 } else {
1044 // NOTE: We don't support IP learning at this moment so this
1045 // is not necessary. Also it causes duplication of DHCPv6 packets.
1046 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1047 }
sangho80f11cb2015-04-01 13:05:26 -07001048 }
1049 }
1050 }
1051
1052 private class InternalLinkListener implements LinkListener {
1053 @Override
1054 public void event(LinkEvent event) {
Charles Chan0cff8aa2017-03-29 16:39:05 -07001055 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1056 event.type() == LinkEvent.Type.LINK_UPDATED ||
1057 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001058 log.debug("Event {} received from Link Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001059 scheduleEventHandlerIfNotScheduled(event);
1060 }
1061 }
1062 }
1063
1064 private class InternalDeviceListener implements DeviceListener {
sangho80f11cb2015-04-01 13:05:26 -07001065 @Override
1066 public void event(DeviceEvent event) {
sangho80f11cb2015-04-01 13:05:26 -07001067 switch (event.type()) {
1068 case DEVICE_ADDED:
Saurav Dasf0f592d2016-11-18 15:21:57 -08001069 case PORT_UPDATED:
1070 case PORT_ADDED:
sanghofb7c7292015-04-13 15:15:58 -07001071 case DEVICE_UPDATED:
1072 case DEVICE_AVAILABILITY_CHANGED:
Saurav Das62ae6792017-05-15 15:34:25 -07001073 log.trace("Event {} received from Device Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001074 scheduleEventHandlerIfNotScheduled(event);
1075 break;
1076 default:
1077 }
1078 }
1079 }
1080
Pier Luigid8a15162018-02-15 16:33:08 +01001081 /**
1082 * Internal listener for topology events.
1083 */
1084 private class InternalTopologyListener implements TopologyListener {
1085 @Override
1086 public void event(TopologyEvent event) {
1087 switch (event.type()) {
1088 case TOPOLOGY_CHANGED:
1089 log.debug("Event {} received from TopologyService", event.type());
1090 scheduleEventHandlerIfNotScheduled(event);
1091 break;
1092 default:
1093 }
1094 }
1095 }
1096
Saurav Das2d94d312015-11-24 23:21:05 -08001097 @SuppressWarnings("rawtypes")
sangho80f11cb2015-04-01 13:05:26 -07001098 private void scheduleEventHandlerIfNotScheduled(Event event) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001099 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001100 eventQueue.add(event);
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001101 numOfEventsQueued++;
1102
1103 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
1104 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001105 eventHandlerFuture = executorService
1106 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
1107 numOfHandlerScheduled++;
1108 }
Jonathan Hart54541d12016-04-12 15:39:44 -07001109 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001110 numOfEventsQueued,
1111 numOfHandlerScheduled);
sangho80f11cb2015-04-01 13:05:26 -07001112 }
sangho80f11cb2015-04-01 13:05:26 -07001113 }
1114
1115 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001116 @Override
sangho80f11cb2015-04-01 13:05:26 -07001117 public void run() {
Pier Luigi8f362362018-02-12 09:56:20 +01001118 while (true) {
1119 try {
Saurav Das2d94d312015-11-24 23:21:05 -08001120 @SuppressWarnings("rawtypes")
Charles Chan3ed34d82017-06-22 18:03:14 -07001121 Event event;
1122 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001123 if (!eventQueue.isEmpty()) {
1124 event = eventQueue.poll();
1125 numOfEventsExecuted++;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001126 } else {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001127 numOfHandlerExecution++;
1128 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
1129 numOfHandlerExecution, numOfEventsExecuted);
1130 break;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001131 }
sanghofb7c7292015-04-13 15:15:58 -07001132 }
Pier Luigid8a15162018-02-15 16:33:08 +01001133 // TODO We should also change SR routing and PW to listen to TopologyEvents
Charles Chan0cff8aa2017-03-29 16:39:05 -07001134 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1135 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Dase6c448a2018-01-18 12:07:33 -08001136 linkHandler.processLinkAdded((Link) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001137 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Saurav Dase6c448a2018-01-18 12:07:33 -08001138 linkHandler.processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001139 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1140 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1141 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das62af8802015-12-04 10:52:59 -08001142 DeviceId deviceId = ((Device) event.subject()).id();
1143 if (deviceService.isAvailable(deviceId)) {
Saurav Dascea556f2018-03-05 14:37:16 -08001144 log.info("** DEVICE UP Processing device event {} "
1145 + "for available device {}",
Saurav Dasc28b3432015-10-30 17:45:38 -07001146 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001147 processDeviceAdded((Device) event.subject());
Saurav Dasc3604f12016-03-23 11:22:49 -07001148 } else {
Saurav Dascea556f2018-03-05 14:37:16 -08001149 log.info(" ** DEVICE DOWN Processing device event {}"
1150 + " for unavailable device {}",
Pier Luigi8f362362018-02-12 09:56:20 +01001151 event.type(), ((Device) event.subject()).id());
Saurav Dasc3604f12016-03-23 11:22:49 -07001152 processDeviceRemoved((Device) event.subject());
1153 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001154 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd1872b02016-12-02 15:43:47 -08001155 // typically these calls come when device is added first time
1156 // so port filtering rules are handled at the device_added event.
1157 // port added calls represent all ports on the device,
1158 // enabled or not.
Saurav Das62ae6792017-05-15 15:34:25 -07001159 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd1872b02016-12-02 15:43:47 -08001160 ((DeviceEvent) event).subject().id(),
1161 ((DeviceEvent) event).port().number(),
1162 event.type());
Saurav Dasf0f592d2016-11-18 15:21:57 -08001163 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd1872b02016-12-02 15:43:47 -08001164 // these calls happen for every subsequent event
1165 // ports enabled, disabled, switch goes away, comes back
Saurav Dasf0f592d2016-11-18 15:21:57 -08001166 log.info("** PORT UPDATED {}/{} -> {}",
1167 event.subject(),
1168 ((DeviceEvent) event).port(),
1169 event.type());
1170 processPortUpdated(((Device) event.subject()),
1171 ((DeviceEvent) event).port());
Pier Luigid8a15162018-02-15 16:33:08 +01001172 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1173 // Process topology event, needed for all modules relying on
1174 // topology service for path computation
1175 TopologyEvent topologyEvent = (TopologyEvent) event;
1176 log.info("Processing topology event {}, topology age {}, reasons {}",
1177 event.type(), topologyEvent.subject().time(),
1178 topologyEvent.reasons().size());
1179 topologyHandler.processTopologyChange(topologyEvent.reasons());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001180 } else {
1181 log.warn("Unhandled event type: {}", event.type());
1182 }
Pier Luigi8f362362018-02-12 09:56:20 +01001183 } catch (Exception e) {
1184 log.error("SegmentRouting event handler thread thrown an exception: {}",
1185 e.getMessage(), e);
sangho80f11cb2015-04-01 13:05:26 -07001186 }
1187 }
sangho80f11cb2015-04-01 13:05:26 -07001188 }
1189 }
1190
Saurav Dase6c448a2018-01-18 12:07:33 -08001191 void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001192 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001193
1194 // NOTE: Punt ARP/NDP even when the device is not configured.
1195 // Host learning without network config is required for CORD config generator.
1196 routingRulePopulator.populateIpPunts(device.id());
1197 routingRulePopulator.populateArpNdpPunts(device.id());
1198
Charles Chan319d1a22015-11-03 10:42:14 -08001199 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001200 log.warn("Device configuration unavailable. Device {} will be "
1201 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001202 return;
1203 }
Charles Chan72779502016-04-23 17:36:10 -07001204 processDeviceAddedInternal(device.id());
1205 }
1206
1207 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001208 // Irrespective of whether the local is a MASTER or not for this device,
1209 // we need to create a SR-group-handler instance. This is because in a
1210 // multi-instance setup, any instance can initiate forwarding/next-objectives
1211 // for any switch (even if this instance is a SLAVE or not even connected
1212 // to the switch). To handle this, a default-group-handler instance is necessary
1213 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001214 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1215 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001216 DefaultGroupHandler groupHandler;
1217 try {
1218 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001219 createGroupHandler(deviceId,
1220 appId,
1221 deviceConfiguration,
1222 linkService,
1223 flowObjectiveService,
1224 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001225 } catch (DeviceConfigNotFoundException e) {
1226 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1227 return;
1228 }
Saurav Das2b6a00f2017-12-05 15:00:23 -08001229 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan72779502016-04-23 17:36:10 -07001230 deviceId);
1231 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001232 }
Saurav Dasb149be12016-06-07 10:08:06 -07001233
Charles Chan72779502016-04-23 17:36:10 -07001234 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001235 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chandebfea32016-10-24 14:52:01 -07001236 hostHandler.init(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -07001237 xConnectHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001238 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001239 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001240 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001241 }
Charles Chan82ab1932016-01-30 23:22:37 -08001242
Charles Chandebfea32016-10-24 14:52:01 -07001243 appCfgHandler.init(deviceId);
1244 routeHandler.init(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001245 }
1246
Saurav Dasc3604f12016-03-23 11:22:49 -07001247 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001248 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001249 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan873661e2017-11-30 15:37:50 -08001250 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan10b0fb72017-02-02 16:20:42 -08001251 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001252 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001253 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001254 portNextObjStore.entrySet().stream()
1255 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001256 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Dase6c448a2018-01-18 12:07:33 -08001257 linkHandler.processDeviceRemoved(device);
Charles Chan17d75d82017-06-15 00:44:51 -07001258
Saurav Dasfbe74572017-08-03 18:30:35 -07001259 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1260 if (gh != null) {
1261 gh.shutdown();
1262 }
Saurav Das62ae6792017-05-15 15:34:25 -07001263 // Note that a switch going down is associated with all of its links
1264 // going down as well, but it is treated as a single switch down event
1265 // while the link-downs are ignored.
1266 defaultRoutingHandler
1267 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Saurav Das6430f412018-01-25 09:49:01 -08001268 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan82f19972016-05-17 13:13:55 -07001269 xConnectHandler.removeDevice(device.id());
Saurav Das97241862018-02-14 14:14:54 -08001270
1271 // Cleanup all internal groupHandler stores for this device. Should be
1272 // done after all rerouting or rehashing has been completed
1273 groupHandlerMap.entrySet()
1274 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001275 }
1276
Saurav Dasf0f592d2016-11-18 15:21:57 -08001277 private void processPortUpdated(Device device, Port port) {
1278 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1279 log.warn("Device configuration uploading. Not handling port event for"
1280 + "dev: {} port: {}", device.id(), port.number());
1281 return;
1282 }
Saurav Dasf9332192017-02-18 14:05:44 -08001283
1284 if (!mastershipService.isLocalMaster(device.id())) {
1285 log.debug("Not master for dev:{} .. not handling port updated event"
1286 + "for port {}", device.id(), port.number());
1287 return;
1288 }
1289
1290 // first we handle filtering rules associated with the port
1291 if (port.isEnabled()) {
1292 log.info("Switchport {}/{} enabled..programming filters",
1293 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001294 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001295 } else {
1296 log.info("Switchport {}/{} disabled..removing filters",
1297 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001298 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001299 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001300
1301 // portUpdated calls are for ports that have gone down or up. For switch
1302 // to switch ports, link-events should take care of any re-routing or
1303 // group editing necessary for port up/down. Here we only process edge ports
1304 // that are already configured.
Saurav Das3fb28272017-03-04 16:08:47 -08001305 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1306 VlanId untaggedVlan = getUntaggedVlanId(cp);
1307 VlanId nativeVlan = getNativeVlanId(cp);
1308 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001309
Saurav Das3fb28272017-03-04 16:08:47 -08001310 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001311 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasf0f592d2016-11-18 15:21:57 -08001312 + "dev/port: {}/{}", device.id(), port.number());
1313 return;
1314 }
Saurav Das3fb28272017-03-04 16:08:47 -08001315 if (untaggedVlan != null) {
1316 processEdgePort(device, port, untaggedVlan, true);
1317 }
1318 if (nativeVlan != null) {
1319 processEdgePort(device, port, nativeVlan, true);
1320 }
1321 if (!taggedVlans.isEmpty()) {
1322 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1323 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001324 }
1325
Saurav Das3fb28272017-03-04 16:08:47 -08001326 private void processEdgePort(Device device, Port port, VlanId vlanId,
1327 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001328 boolean portUp = port.isEnabled();
1329 if (portUp) {
Saurav Das3fb28272017-03-04 16:08:47 -08001330 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001331 port.number(), vlanId);
Charles Chan873661e2017-11-30 15:37:50 -08001332 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Dasf0f592d2016-11-18 15:21:57 -08001333 } else {
Saurav Das3fb28272017-03-04 16:08:47 -08001334 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001335 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001336 }
1337
Srikanth Vavilapalli64505482015-04-21 13:04:13 -07001338 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sangho80f11cb2015-04-01 13:05:26 -07001339 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001340 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001341 } else {
1342 log.warn("Group handler not found for dev:{}. Not handling edge port"
1343 + " {} event for port:{}", device.id(),
1344 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001345 }
1346 }
sangho27462c62015-05-14 00:39:53 -07001347
Charles Chan8ca5a122017-10-20 16:06:55 -07001348 private void createOrUpdateDeviceConfiguration() {
1349 if (deviceConfiguration == null) {
Saurav Dase321cff2018-02-09 17:26:45 -08001350 log.info("Creating new DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001351 deviceConfiguration = new DeviceConfiguration(this);
1352 } else {
Saurav Dase321cff2018-02-09 17:26:45 -08001353 log.info("Updating DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001354 deviceConfiguration.updateConfig();
1355 }
1356 }
1357
Pier Ventreb6a7f342016-11-26 21:05:22 -08001358 /**
1359 * Registers the given connect point with the NRS, this is necessary
1360 * to receive the NDP and ARP packets from the NRS.
1361 *
1362 * @param portToRegister connect point to register
1363 */
1364 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001365 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001366 portToRegister,
1367 neighbourHandler,
1368 appId
1369 );
1370 }
1371
Charles Chan72f556a2015-10-05 17:50:33 -07001372 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001373 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001374 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001375
Charles Chanb7f75ac2016-01-11 18:28:54 -08001376 /**
1377 * Constructs the internal network config listener.
1378 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001379 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001380 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001381 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001382 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001383 }
1384
Charles Chanb7f75ac2016-01-11 18:28:54 -08001385 /**
1386 * Reads network config and initializes related data structure accordingly.
1387 */
Charles Chan873661e2017-11-30 15:37:50 -08001388 void configureNetwork() {
Saurav Dase321cff2018-02-09 17:26:45 -08001389 log.info("Configuring network ...");
Charles Chan8ca5a122017-10-20 16:06:55 -07001390 createOrUpdateDeviceConfiguration();
Charles Chane7c61022015-10-07 14:21:45 -07001391
Charles Chan46fdfaf2016-11-09 20:51:44 -08001392 arpHandler = new ArpHandler(srManager);
1393 icmpHandler = new IcmpHandler(srManager);
1394 ipHandler = new IpHandler(srManager);
1395 routingRulePopulator = new RoutingRulePopulator(srManager);
1396 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chane7c61022015-10-07 14:21:45 -07001397
1398 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1399 groupHandlerMap, tunnelStore);
1400 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1401 flowObjectiveService,
1402 tunnelHandler, policyStore);
Saurav Das261c3002017-06-13 15:35:54 -07001403 // add a small delay to absorb multiple network config added notifications
1404 if (!programmingScheduled.get()) {
Saurav Dase321cff2018-02-09 17:26:45 -08001405 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
Saurav Das261c3002017-06-13 15:35:54 -07001406 programmingScheduled.set(true);
1407 executorService.schedule(new ConfigChange(), PROGRAM_DELAY,
1408 TimeUnit.SECONDS);
Charles Chane7c61022015-10-07 14:21:45 -07001409 }
Charles Chan72779502016-04-23 17:36:10 -07001410 mcastHandler.init();
Charles Chane7c61022015-10-07 14:21:45 -07001411 }
1412
Charles Chan72f556a2015-10-05 17:50:33 -07001413 @Override
1414 public void event(NetworkConfigEvent event) {
Charles Chan82ab1932016-01-30 23:22:37 -08001415 // TODO move this part to NetworkConfigEventHandler
1416 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1417 switch (event.type()) {
1418 case CONFIG_ADDED:
Charles Chan68363b12017-06-26 15:25:09 -07001419 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan82ab1932016-01-30 23:22:37 -08001420 configureNetwork();
1421 break;
1422 case CONFIG_UPDATED:
Charles Chan68363b12017-06-26 15:25:09 -07001423 log.info("Segment Routing Config updated for {}", event.subject());
Charles Chan8ca5a122017-10-20 16:06:55 -07001424 createOrUpdateDeviceConfiguration();
Charles Chan68363b12017-06-26 15:25:09 -07001425 // TODO support dynamic configuration
1426 break;
1427 default:
1428 break;
1429 }
1430 } else if (event.configClass().equals(InterfaceConfig.class)) {
1431 switch (event.type()) {
1432 case CONFIG_ADDED:
1433 log.info("Interface Config added for {}", event.subject());
1434 configureNetwork();
1435 break;
1436 case CONFIG_UPDATED:
1437 log.info("Interface Config updated for {}", event.subject());
Charles Chan8ca5a122017-10-20 16:06:55 -07001438 createOrUpdateDeviceConfiguration();
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001439
1440 // Following code will be uncommented when [CORD-634] is fully implemented.
1441 // [CORD-634] Add dynamic config support for interfaces
1442 updateInterface((InterfaceConfig) event.config().get(),
1443 (InterfaceConfig) event.prevConfig().get());
Charles Chan82ab1932016-01-30 23:22:37 -08001444 // TODO support dynamic configuration
1445 break;
1446 default:
1447 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001448 }
Charles Chan82ab1932016-01-30 23:22:37 -08001449 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chan82f19972016-05-17 13:13:55 -07001450 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan82ab1932016-01-30 23:22:37 -08001451 switch (event.type()) {
1452 case CONFIG_ADDED:
Charles Chan82f19972016-05-17 13:13:55 -07001453 appCfgHandler.processAppConfigAdded(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001454 break;
1455 case CONFIG_UPDATED:
Charles Chan82f19972016-05-17 13:13:55 -07001456 appCfgHandler.processAppConfigUpdated(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001457 break;
1458 case CONFIG_REMOVED:
Charles Chan82f19972016-05-17 13:13:55 -07001459 appCfgHandler.processAppConfigRemoved(event);
1460 break;
1461 default:
1462 break;
1463 }
Saurav Dase321cff2018-02-09 17:26:45 -08001464 log.info("App config event .. configuring network");
Charles Chandebfea32016-10-24 14:52:01 -07001465 configureNetwork();
Charles Chan82f19972016-05-17 13:13:55 -07001466 } else if (event.configClass().equals(XConnectConfig.class)) {
1467 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1468 switch (event.type()) {
1469 case CONFIG_ADDED:
1470 xConnectHandler.processXConnectConfigAdded(event);
1471 break;
1472 case CONFIG_UPDATED:
1473 xConnectHandler.processXConnectConfigUpdated(event);
1474 break;
1475 case CONFIG_REMOVED:
1476 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001477 break;
1478 default:
1479 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001480 }
Charles Chan72f556a2015-10-05 17:50:33 -07001481 }
1482 }
Saurav Das261c3002017-06-13 15:35:54 -07001483
Charles Chanc4a68c32018-01-03 16:26:32 -08001484 @Override
1485 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase321cff2018-02-09 17:26:45 -08001486 if (event.type() == CONFIG_REGISTERED ||
1487 event.type() == CONFIG_UNREGISTERED) {
1488 log.debug("Ignore event {} due to type mismatch", event);
1489 return false;
Charles Chanc4a68c32018-01-03 16:26:32 -08001490 }
Saurav Dase321cff2018-02-09 17:26:45 -08001491
1492 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1493 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1494 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -08001495 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase321cff2018-02-09 17:26:45 -08001496 log.debug("Ignore event {} due to class mismatch", event);
1497 return false;
1498 }
1499
1500 return true;
Charles Chanc4a68c32018-01-03 16:26:32 -08001501 }
1502
Saurav Das261c3002017-06-13 15:35:54 -07001503 private final class ConfigChange implements Runnable {
1504 @Override
1505 public void run() {
1506 programmingScheduled.set(false);
Saurav Dase321cff2018-02-09 17:26:45 -08001507 log.info("Reacting to config changes after buffer delay");
Saurav Das261c3002017-06-13 15:35:54 -07001508 for (Device device : deviceService.getDevices()) {
1509 processDeviceAdded(device);
1510 }
1511 defaultRoutingHandler.startPopulationProcess();
1512 }
1513 }
Charles Chan72f556a2015-10-05 17:50:33 -07001514 }
Charles Chanf4586112015-11-09 16:37:23 -08001515
1516 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001517 @Override
1518 public void event(HostEvent event) {
Charles Chanf4586112015-11-09 16:37:23 -08001519 switch (event.type()) {
1520 case HOST_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001521 hostHandler.processHostAddedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001522 break;
1523 case HOST_MOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001524 hostHandler.processHostMovedEvent(event);
Charles Chan910be6a2017-08-23 14:46:43 -07001525 routeHandler.processHostMovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001526 break;
1527 case HOST_REMOVED:
Charles Chand9265a32017-06-16 15:19:24 -07001528 hostHandler.processHostRemovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001529 break;
1530 case HOST_UPDATED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001531 hostHandler.processHostUpdatedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001532 break;
1533 default:
1534 log.warn("Unsupported host event type: {}", event.type());
1535 break;
1536 }
1537 }
1538 }
1539
Charles Chanc91c8782016-03-30 17:54:24 -07001540 private class InternalMcastListener implements McastListener {
1541 @Override
1542 public void event(McastEvent event) {
1543 switch (event.type()) {
1544 case SOURCE_ADDED:
Pier Luigie80d6b42018-02-26 12:31:38 +01001545 case SOURCE_UPDATED:
Charles Chanc91c8782016-03-30 17:54:24 -07001546 case SINK_ADDED:
Charles Chanc91c8782016-03-30 17:54:24 -07001547 case SINK_REMOVED:
Charles Chanc91c8782016-03-30 17:54:24 -07001548 case ROUTE_REMOVED:
Pier Luigid29ca7c2018-02-28 17:24:03 +01001549 mcastHandler.processMcastEvent(event);
Pier Luigi6786b922018-02-02 16:19:11 +01001550 break;
1551 case ROUTE_ADDED:
Charles Chanc91c8782016-03-30 17:54:24 -07001552 default:
1553 break;
1554 }
1555 }
1556 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001557
Charles Chandebfea32016-10-24 14:52:01 -07001558 private class InternalRouteEventListener implements RouteListener {
1559 @Override
1560 public void event(RouteEvent event) {
1561 switch (event.type()) {
1562 case ROUTE_ADDED:
1563 routeHandler.processRouteAdded(event);
1564 break;
1565 case ROUTE_UPDATED:
1566 routeHandler.processRouteUpdated(event);
1567 break;
1568 case ROUTE_REMOVED:
1569 routeHandler.processRouteRemoved(event);
1570 break;
Charles Chan910be6a2017-08-23 14:46:43 -07001571 case ALTERNATIVE_ROUTES_CHANGED:
1572 routeHandler.processAlternativeRoutesChanged(event);
1573 break;
Charles Chandebfea32016-10-24 14:52:01 -07001574 default:
1575 break;
1576 }
1577 }
1578 }
Saurav Das62ae6792017-05-15 15:34:25 -07001579
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001580 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1581 try {
1582 Set<Interface> intfs = conf.getInterfaces();
1583 Set<Interface> prevIntfs = prevConf.getInterfaces();
1584
1585 // Now we only handle one interface config at each port.
1586 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1587 log.warn("Interface update aborted - one at a time is allowed, " +
1588 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1589 return;
1590 }
1591
1592 Interface intf = intfs.stream().findFirst().get();
1593 Interface prevIntf = prevIntfs.stream().findFirst().get();
1594
1595 DeviceId deviceId = intf.connectPoint().deviceId();
1596 PortNumber portNum = intf.connectPoint().port();
1597
1598 if (!mastershipService.isLocalMaster(deviceId)) {
1599 log.debug("CONFIG_UPDATED event for interfaces should be " +
1600 "handled by master node for device {}", deviceId);
1601 return;
1602 }
1603
1604 removeSubnetConfig(prevIntf.connectPoint(),
1605 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1606 new HashSet<>(intf.ipAddressesList())));
1607
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001608 if (!prevIntf.vlanNative().equals(VlanId.NONE)
1609 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
1610 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1611 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
1612 // Update filtering objective and L2IG group bucket
1613 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
1614 } else {
1615 // RemoveVlanNative
1616 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1617 }
1618 }
1619
1620 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
1621 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1622 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1623 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
1624 // Update filtering objective and L2IG group bucket
1625 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
1626 } else {
1627 // RemoveVlanUntagged
1628 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1629 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001630 }
1631
1632 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1633 // RemoveVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001634 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
1635 .filter(i -> !intf.vlanUntagged().equals(i))
1636 .filter(i -> !intf.vlanNative().equals(i))
1637 .forEach(vlanId -> updateVlanConfigInternal(
1638 deviceId, portNum, vlanId, false, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001639 }
1640
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001641 if (!intf.vlanNative().equals(VlanId.NONE)
1642 && !prevIntf.vlanNative().equals(intf.vlanNative())
1643 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1644 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
1645 // Update filtering objective and L2IG group bucket
1646 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
1647 } else {
1648 // AddVlanNative
1649 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1650 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001651 }
1652
1653 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1654 // AddVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001655 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
1656 .filter(i -> !prevIntf.vlanUntagged().equals(i))
1657 .filter(i -> !prevIntf.vlanNative().equals(i))
1658 .forEach(vlanId -> updateVlanConfigInternal(
1659 deviceId, portNum, vlanId, false, true)
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001660 );
1661 }
1662
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001663 if (!intf.vlanUntagged().equals(VlanId.NONE)
1664 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1665 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
1666 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
1667 // Update filtering objective and L2IG group bucket
1668 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
1669 } else {
1670 // AddVlanUntagged
1671 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1672 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001673 }
1674 addSubnetConfig(prevIntf.connectPoint(),
1675 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1676 new HashSet<>(prevIntf.ipAddressesList())));
1677 } catch (ConfigException e) {
1678 log.error("Error in configuration");
1679 }
1680 }
1681
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001682 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
1683 VlanId vlanId, boolean pushVlan) {
1684 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1685 if (grpHandler == null) {
1686 log.warn("Failed to retrieve group handler for device {}", deviceId);
1687 return;
1688 }
1689
1690 // Update filtering objective for a single port
1691 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
1692 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
1693
1694 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
1695 // Update L2IG bucket of the port
1696 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
1697 } else {
1698 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1699 }
1700 }
1701
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001702 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1703 VlanId vlanId, boolean pushVlan, boolean install) {
1704 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1705 if (grpHandler == null) {
1706 log.warn("Failed to retrieve group handler for device {}", deviceId);
1707 return;
1708 }
1709
1710 // Update filtering objective for a single port
1711 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1712
1713 // Update filtering objective for multicast ingress port
1714 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1715
1716 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1717
1718 if (nextId != -1 && !install) {
1719 // Update next objective for a single port as an output port
1720 // Remove a single port from L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001721 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001722 // Remove L2 Bridging rule and L3 Unicast rule to the host
1723 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1724 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1725 // only if there is no port configured on that VLAN ID
1726 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1727 // Remove broadcast forwarding rule for the VLAN
1728 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1729 // Remove L2FG for VLAN
1730 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1731 } else {
1732 // Remove L2IG of the port
1733 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1734 }
1735 } else if (install) {
1736 if (nextId != -1) {
1737 // Add a single port to L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001738 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001739 } else {
1740 // Create L2FG for VLAN
1741 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1742 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1743 }
1744 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1745 } else {
1746 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1747 }
1748 }
1749
1750 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1751 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1752 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1753
1754 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1755 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1756 .filter(intf -> !intf.connectPoint().equals(cp))
1757 .flatMap(intf -> intf.ipAddressesList().stream())
1758 .collect(Collectors.toSet());
1759 // 1. Partial subnet population
1760 // Remove routing rules for removed subnet from previous configuration,
1761 // which does not also exist in other interfaces in the same device
1762 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1763 .map(InterfaceIpAddress::subnetAddress)
1764 .collect(Collectors.toSet());
1765
1766 defaultRoutingHandler.revokeSubnet(
1767 ipPrefixSet.stream()
1768 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1769 .collect(Collectors.toSet()));
1770
1771 // 2. Interface IP punts
1772 // Remove IP punts for old Intf address
1773 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1774 .map(InterfaceIpAddress::ipAddress)
1775 .collect(Collectors.toSet());
1776 ipAddressSet.stream()
1777 .map(InterfaceIpAddress::ipAddress)
1778 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1779 .forEach(interfaceIpAddress ->
1780 routingRulePopulator.revokeSingleIpPunts(
1781 cp.deviceId(), interfaceIpAddress));
1782
1783 // 3. Host unicast routing rule
1784 // Remove unicast routing rule
1785 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1786 }
1787
1788 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1789 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1790 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1791
1792 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1793 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1794 .filter(intf -> !intf.connectPoint().equals(cp))
1795 .flatMap(intf -> intf.ipAddressesList().stream())
1796 .collect(Collectors.toSet());
1797 // 1. Partial subnet population
1798 // Add routing rules for newly added subnet, which does not also exist in
1799 // other interfaces in the same device
1800 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1801 .map(InterfaceIpAddress::subnetAddress)
1802 .collect(Collectors.toSet());
1803
1804 defaultRoutingHandler.populateSubnet(
1805 Collections.singleton(cp),
1806 ipPrefixSet.stream()
1807 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1808 .collect(Collectors.toSet()));
1809
1810 // 2. Interface IP punts
1811 // Add IP punts for new Intf address
1812 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1813 .map(InterfaceIpAddress::ipAddress)
1814 .collect(Collectors.toSet());
1815 ipAddressSet.stream()
1816 .map(InterfaceIpAddress::ipAddress)
1817 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1818 .forEach(interfaceIpAddress ->
1819 routingRulePopulator.populateSingleIpPunts(
1820 cp.deviceId(), interfaceIpAddress));
1821
1822 // 3. Host unicast routing rule
1823 // Add unicast routing rule
1824 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1825 }
sangho80f11cb2015-04-01 13:05:26 -07001826}