blob: b11de607acfd5312dec33fd729ad9fbdd7c61acf [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sanghob35a6192015-04-01 13:05:26 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.segmentrouting;
17
Jonathan Hart8ca2bc02017-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 Chan9640c812017-08-23 13:55:39 -070022import com.google.common.collect.Sets;
sanghob35a6192015-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 Chan47933752017-11-30 15:37:50 -080026import org.apache.felix.scr.annotations.Modified;
27import org.apache.felix.scr.annotations.Property;
sanghob35a6192015-04-01 13:05:26 -070028import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho1e575652015-05-14 00:39:53 -070030import org.apache.felix.scr.annotations.Service;
sanghob35a6192015-04-01 13:05:26 -070031import org.onlab.packet.Ethernet;
Pier Ventre735b8c82016-12-02 08:16:05 -080032import org.onlab.packet.ICMP6;
Charles Chanc42e84e2015-10-20 16:24:19 -070033import org.onlab.packet.IPv4;
Pier Ventre10bd8d12016-11-26 21:05:22 -080034import org.onlab.packet.IPv6;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070035import org.onlab.packet.IpAddress;
Charles Chanc42e84e2015-10-20 16:24:19 -070036import org.onlab.packet.IpPrefix;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070037import org.onlab.packet.VlanId;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070038import org.onlab.util.KryoNamespace;
Charles Chan47933752017-11-30 15:37:50 -080039import org.onlab.util.Tools;
Saurav Das80980c72016-03-23 11:22:49 -070040import org.onosproject.cfg.ComponentConfigService;
Saurav Das201762d2018-04-21 17:19:48 -070041import org.onosproject.cluster.ClusterEvent;
42import org.onosproject.cluster.ClusterEventListener;
Pier Luigieba73a02018-01-16 10:47:50 +010043import org.onosproject.cluster.ClusterService;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070044import org.onosproject.cluster.LeadershipService;
Pier96f63cb2018-04-17 16:29:56 +020045import org.onosproject.cluster.NodeId;
sanghob35a6192015-04-01 13:05:26 -070046import org.onosproject.core.ApplicationId;
47import org.onosproject.core.CoreService;
48import org.onosproject.event.Event;
Charles Chan50bb6ef2018-04-18 18:41:05 -070049import org.onosproject.mastership.MastershipEvent;
50import org.onosproject.mastership.MastershipListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070051import org.onosproject.mastership.MastershipService;
Pier3ee24552018-03-14 16:47:32 -070052import org.onosproject.mcast.api.McastEvent;
53import org.onosproject.mcast.api.McastListener;
54import org.onosproject.mcast.api.MulticastRouteService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080055import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070056import org.onosproject.net.Device;
57import org.onosproject.net.DeviceId;
Charles Chan9640c812017-08-23 13:55:39 -070058import org.onosproject.net.Host;
59import org.onosproject.net.HostId;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070060import org.onosproject.net.Link;
61import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080062import org.onosproject.net.PortNumber;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070063import org.onosproject.net.config.ConfigException;
Charles Chand6832882015-10-05 17:50:33 -070064import org.onosproject.net.config.ConfigFactory;
65import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070066import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070067import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070068import org.onosproject.net.config.basics.InterfaceConfig;
69import org.onosproject.net.config.basics.McastConfig;
Charles Chand6832882015-10-05 17:50:33 -070070import org.onosproject.net.config.basics.SubjectFactories;
Saurav Das45f48152018-01-18 12:07:33 -080071import org.onosproject.net.device.DeviceAdminService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070072import org.onosproject.net.device.DeviceEvent;
73import org.onosproject.net.device.DeviceListener;
74import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080075import org.onosproject.net.flow.TrafficSelector;
76import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070077import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan68aa62d2015-11-09 16:37:23 -080078import org.onosproject.net.host.HostEvent;
79import org.onosproject.net.host.HostListener;
Charles Chan47933752017-11-30 15:37:50 -080080import org.onosproject.net.host.HostLocationProbingService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080081import org.onosproject.net.host.HostService;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070082import org.onosproject.net.host.InterfaceIpAddress;
Pier96f63cb2018-04-17 16:29:56 +020083import org.onosproject.net.intent.WorkPartitionService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070084import org.onosproject.net.intf.Interface;
85import org.onosproject.net.intf.InterfaceService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080086import org.onosproject.net.link.LinkEvent;
87import org.onosproject.net.link.LinkListener;
88import org.onosproject.net.link.LinkService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070089import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080090import org.onosproject.net.packet.InboundPacket;
91import org.onosproject.net.packet.PacketContext;
92import org.onosproject.net.packet.PacketProcessor;
93import org.onosproject.net.packet.PacketService;
Pier Luigi83f919b2018-02-15 16:33:08 +010094import org.onosproject.net.topology.TopologyEvent;
95import org.onosproject.net.topology.TopologyListener;
Charles Chand55e84d2016-03-30 17:54:24 -070096import org.onosproject.net.topology.TopologyService;
Charles Chan9640c812017-08-23 13:55:39 -070097import org.onosproject.routeservice.ResolvedRoute;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070098import org.onosproject.routeservice.RouteEvent;
99import org.onosproject.routeservice.RouteListener;
100import org.onosproject.routeservice.RouteService;
Charles Chand55e84d2016-03-30 17:54:24 -0700101import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
102import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventref34966c2016-11-07 16:21:04 -0800103import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700104import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -0700105import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -0700106import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700107import org.onosproject.segmentrouting.grouphandler.DestinationSet;
108import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Pier Luigi96fe0772018-02-28 12:10:50 +0100109import org.onosproject.segmentrouting.mcast.McastHandler;
110import org.onosproject.segmentrouting.mcast.McastRole;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700111import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700112import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800113import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700114import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800115
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800116import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700117import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800118import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800119import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
120
Saurav Das7bcbe702017-06-13 15:35:54 -0700121import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700122import org.onosproject.segmentrouting.storekey.DummyVlanIdStoreKey;
Pier Luigib29144d2018-01-15 18:06:43 +0100123import org.onosproject.segmentrouting.storekey.McastStoreKey;
Charles Chand2990362016-04-18 13:44:03 -0700124import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -0800125import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -0700126import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700127import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700128import org.onosproject.store.service.EventuallyConsistentMap;
129import org.onosproject.store.service.EventuallyConsistentMapBuilder;
130import org.onosproject.store.service.StorageService;
131import org.onosproject.store.service.WallClockTimestamp;
Charles Chan47933752017-11-30 15:37:50 -0800132import org.osgi.service.component.ComponentContext;
sanghob35a6192015-04-01 13:05:26 -0700133import org.slf4j.Logger;
134import org.slf4j.LoggerFactory;
135
Saurav Das201762d2018-04-21 17:19:48 -0700136import java.time.Instant;
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700137import java.util.ArrayList;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800138import java.util.Collections;
Charles Chan47933752017-11-30 15:37:50 -0800139import java.util.Dictionary;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800140import java.util.HashSet;
141import java.util.List;
142import java.util.Map;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800143import java.util.Optional;
144import java.util.Set;
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200145import java.util.concurrent.CompletableFuture;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800146import java.util.concurrent.ConcurrentHashMap;
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200147import java.util.concurrent.CopyOnWriteArrayList;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800148import java.util.concurrent.Executors;
149import java.util.concurrent.ScheduledExecutorService;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800150import java.util.concurrent.TimeUnit;
151import java.util.concurrent.atomic.AtomicBoolean;
152import java.util.stream.Collectors;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700153import java.util.stream.IntStream;
sanghob35a6192015-04-01 13:05:26 -0700154
Charles Chan3e783d02016-02-26 22:19:52 -0800155import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800156import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700157import static org.onlab.util.Tools.groupedThreads;
Saurav Dase7f51012018-02-09 17:26:45 -0800158import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
159import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Charles Chan3e783d02016-02-26 22:19:52 -0800160
Charles Chane849c192016-01-11 18:28:54 -0800161/**
162 * Segment routing manager.
163 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700164@Service
165@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700166public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700167
Charles Chan2c15aca2016-11-09 20:51:44 -0800168 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan2fde6d42017-08-23 14:46:43 -0700169 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sanghob35a6192015-04-01 13:05:26 -0700170
171 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700172 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700173
174 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800175 private NeighbourResolutionService neighbourResolutionService;
176
177 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100178 public CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700179
180 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700181 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700182
183 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700184 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700185
186 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan47933752017-11-30 15:37:50 -0800187 HostLocationProbingService probingService;
188
189 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100190 public DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700191
192 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Saurav Das45f48152018-01-18 12:07:33 -0800193 DeviceAdminService deviceAdminService;
194
195 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800196 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700197
198 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100199 public LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700200
Charles Chan5270ed02016-01-30 23:22:37 -0800201 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800202 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700203
204 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800205 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700206
207 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100208 public MulticastRouteService multicastRouteService;
Charles Chan03a73e02016-10-24 14:52:01 -0700209
210 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chanf237e1b2018-01-09 13:45:07 -0800211 public TopologyService topologyService;
Charles Chan03a73e02016-10-24 14:52:01 -0700212
213 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700214 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800215
216 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800217 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800218
Saurav Das80980c72016-03-23 11:22:49 -0700219 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800220 public InterfaceService interfaceService;
221
Pier Luigieba73a02018-01-16 10:47:50 +0100222 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
223 public ClusterService clusterService;
224
225 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier96f63cb2018-04-17 16:29:56 +0200226 public WorkPartitionService workPartitionService;
Pier Luigieba73a02018-01-16 10:47:50 +0100227
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700228 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
229 public LeadershipService leadershipService;
230
Charles Chan47933752017-11-30 15:37:50 -0800231 @Property(name = "activeProbing", boolValue = true,
232 label = "Enable active probing to discover dual-homed hosts.")
233 boolean activeProbing = true;
234
Saurav Das9a554292018-04-27 18:42:30 -0700235 @Property(name = "singleHomedDown", boolValue = false,
236 label = "Enable administratively taking down single-homed hosts "
237 + "when all uplinks are gone")
238 boolean singleHomedDown = false;
239
Charles Chan03a73e02016-10-24 14:52:01 -0700240 ArpHandler arpHandler = null;
241 IcmpHandler icmpHandler = null;
242 IpHandler ipHandler = null;
243 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700244 ApplicationId appId;
245 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700246
Charles Chan03a73e02016-10-24 14:52:01 -0700247 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700248 private TunnelHandler tunnelHandler = null;
249 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700250 private InternalPacketProcessor processor = null;
251 private InternalLinkListener linkListener = null;
252 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700253 private AppConfigHandler appCfgHandler = null;
Pier Luigi96fe0772018-02-28 12:10:50 +0100254 public XConnectHandler xConnectHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800255 McastHandler mcastHandler = null;
256 HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800257 private RouteHandler routeHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800258 LinkHandler linkHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800259 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800260 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigi83f919b2018-02-15 16:33:08 +0100261 private TopologyHandler topologyHandler = null;
Charles Chan5270ed02016-01-30 23:22:37 -0800262 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700263 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
264 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700265 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigi83f919b2018-02-15 16:33:08 +0100266 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chan50bb6ef2018-04-18 18:41:05 -0700267 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
Saurav Das201762d2018-04-21 17:19:48 -0700268 final InternalClusterListener clusterListener = new InternalClusterListener();
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200269 //Completable future for network configuration process to buffer config events handling during activation
270 private CompletableFuture<Boolean> networkConfigCompletion = null;
Charles Chan10b2fee2018-04-21 00:44:29 -0700271 private List<Event> queuedEvents = new CopyOnWriteArrayList<>();
sanghob35a6192015-04-01 13:05:26 -0700272
Charles Chan9b7217c2018-04-05 16:31:15 -0700273 // Handles device, link, topology and network config events
Charles Chan50bb6ef2018-04-18 18:41:05 -0700274 private ScheduledExecutorService mainEventExecutor;
sanghob35a6192015-04-01 13:05:26 -0700275
Charles Chan50bb6ef2018-04-18 18:41:05 -0700276 // Handles host, route and mcast events respectively
277 private ScheduledExecutorService hostEventExecutor;
278 private ScheduledExecutorService routeEventExecutor;
279 private ScheduledExecutorService mcastEventExecutor;
Charles Chan9b7217c2018-04-05 16:31:15 -0700280
281 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chane849c192016-01-11 18:28:54 -0800282 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700283 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700284 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800285 */
Charles Chan47933752017-11-30 15:37:50 -0800286 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das7bcbe702017-06-13 15:35:54 -0700287 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800288 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700289 * Per device next objective ID store with (device id + vlanid) as key.
290 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800291 */
Charles Chan47933752017-11-30 15:37:50 -0800292 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800293 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800294 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700295 * Per device next objective ID store with (device id + port + treatment + meta) as key.
296 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chane849c192016-01-11 18:28:54 -0800297 */
Charles Chan47933752017-11-30 15:37:50 -0800298 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800299 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800300
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700301 /**
302 * Per port dummy VLAN ID store with (connect point + ip address) as key.
303 * Used to keep track on dummy VLAN ID allocation.
304 */
305 private EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId>
306 dummyVlanIdStore = null;
307
Saurav Das4ce45962015-11-24 23:21:05 -0800308 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
309 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700310
Saurav Das7bcbe702017-06-13 15:35:54 -0700311 private AtomicBoolean programmingScheduled = new AtomicBoolean();
312
Charles Chand55e84d2016-03-30 17:54:24 -0700313 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700314 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
315 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700316 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700317 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800318 public SegmentRoutingDeviceConfig createConfig() {
319 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700320 }
321 };
Pier Ventref34966c2016-11-07 16:21:04 -0800322
Charles Chand55e84d2016-03-30 17:54:24 -0700323 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700324 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
325 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700326 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800327 @Override
328 public SegmentRoutingAppConfig createConfig() {
329 return new SegmentRoutingAppConfig();
330 }
331 };
Pier Ventref34966c2016-11-07 16:21:04 -0800332
Charles Chanfc5c7802016-05-17 13:13:55 -0700333 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
334 new ConfigFactory<ApplicationId, XConnectConfig>(
335 SubjectFactories.APP_SUBJECT_FACTORY,
336 XConnectConfig.class, "xconnect") {
337 @Override
338 public XConnectConfig createConfig() {
339 return new XConnectConfig();
340 }
341 };
Pier Ventref34966c2016-11-07 16:21:04 -0800342
Charles Chand55e84d2016-03-30 17:54:24 -0700343 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700344 new ConfigFactory<ApplicationId, McastConfig>(
345 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700346 McastConfig.class, "multicast") {
347 @Override
348 public McastConfig createConfig() {
349 return new McastConfig();
350 }
351 };
352
Charles Chan116188d2016-02-18 14:22:42 -0800353 /**
354 * Segment Routing App ID.
355 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800356 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan59cc16d2017-02-02 16:20:42 -0800357
Charles Chane849c192016-01-11 18:28:54 -0800358 /**
359 * The default VLAN ID assigned to the interfaces without subnet config.
360 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800361 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700362
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700363 /**
364 * Minumum and maximum value of dummy VLAN ID to be allocated.
365 */
366 public static final int MIN_DUMMY_VLAN_ID = 2;
367 public static final int MAX_DUMMY_VLAN_ID = 4093;
368
Saurav Das9a554292018-04-27 18:42:30 -0700369 Instant lastEdgePortEvent = Instant.EPOCH;
370
sanghob35a6192015-04-01 13:05:26 -0700371 @Activate
Charles Chan47933752017-11-30 15:37:50 -0800372 protected void activate(ComponentContext context) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800373 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700374
Charles Chan50bb6ef2018-04-18 18:41:05 -0700375 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-main", "%d", log));
376 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-host", "%d", log));
377 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-route", "%d", log));
378 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-mcast", "%d", log));
379
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700380 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700381 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700382 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700383 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700384 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700385 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700386 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700387 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700388 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700389
Charles Chan59cc16d2017-02-02 16:20:42 -0800390 log.debug("Creating EC map vlannextobjectivestore");
391 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
392 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
393 vlanNextObjStore = vlanNextObjMapBuilder
394 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700395 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700396 .withTimestampProvider((k, v) -> new WallClockTimestamp())
397 .build();
398
Saurav Das4ce45962015-11-24 23:21:05 -0800399 log.debug("Creating EC map subnetnextobjectivestore");
400 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
401 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
402 portNextObjStore = portNextObjMapBuilder
403 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700404 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800405 .withTimestampProvider((k, v) -> new WallClockTimestamp())
406 .build();
407
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700408 EventuallyConsistentMapBuilder<DummyVlanIdStoreKey, VlanId>
409 dummyVlanIdMapBuilder = storageService.eventuallyConsistentMapBuilder();
410 dummyVlanIdStore = dummyVlanIdMapBuilder
411 .withName("dummyvlanidstore")
412 .withSerializer(createSerializer())
413 .withTimestampProvider((k, v) -> new WallClockTimestamp())
414 .build();
415
sangho0b2b6d12015-05-20 22:16:38 -0700416 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
417 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700418 tunnelStore = tunnelMapBuilder
419 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700420 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700421 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700422 .build();
423
424 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
425 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700426 policyStore = policyMapBuilder
427 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700428 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700429 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700430 .build();
431
Saurav Das80980c72016-03-23 11:22:49 -0700432 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800433 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700434 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800435 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800436 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
437 "requestInterceptsEnabled", "false");
Charles Chand3baaba2017-08-08 15:13:37 -0700438 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800439 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700440 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800441 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800442 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
443 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800444 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
445 "forceUnprovision", "true");
Charles Chanef624ed2017-08-10 16:57:28 -0700446 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan73316522017-07-20 16:16:25 -0700447 "distributed", "true");
Charles Chan35a32322017-08-14 11:42:11 -0700448 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
449 "multihomingEnabled", "true");
Charles Chan807d87a2017-11-29 19:54:20 -0800450 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
451 "staleLinkAge", "15000");
452 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
453 "allowDuplicateIps", "false");
Charles Chan47933752017-11-30 15:37:50 -0800454 compCfgService.registerProperties(getClass());
455 modified(context);
Saurav Das80980c72016-03-23 11:22:49 -0700456
Charles Chanb8e10c82015-10-14 11:24:40 -0700457 processor = new InternalPacketProcessor();
458 linkListener = new InternalLinkListener();
459 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700460 appCfgHandler = new AppConfigHandler(this);
461 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700462 mcastHandler = new McastHandler(this);
463 hostHandler = new HostHandler(this);
Saurav Das45f48152018-01-18 12:07:33 -0800464 linkHandler = new LinkHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700465 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800466 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800467 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigi83f919b2018-02-15 16:33:08 +0100468 topologyHandler = new TopologyHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700469
Charles Chan3e783d02016-02-26 22:19:52 -0800470 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700471 cfgService.registerConfigFactory(deviceConfigFactory);
472 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700473 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700474 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase7f51012018-02-09 17:26:45 -0800475 log.info("Configuring network before adding listeners");
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200476
Charles Chan132393a2018-01-04 14:26:07 -0800477 cfgListener.configureNetwork();
478
Charles Chan5270ed02016-01-30 23:22:37 -0800479 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700480 packetService.addProcessor(processor, PacketProcessor.director(2));
481 linkService.addListener(linkListener);
482 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700483 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700484 routeService.addListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100485 topologyService.addListener(topologyListener);
Charles Chan50bb6ef2018-04-18 18:41:05 -0700486 mastershipService.addListener(mastershipListener);
Saurav Das201762d2018-04-21 17:19:48 -0700487 clusterService.addListener(clusterListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700488
Charles Chanb39777c2018-03-09 15:53:44 -0800489 linkHandler.init();
Andreas Pantelopoulos4a768c02018-01-11 07:53:48 -0800490 l2TunnelHandler.init();
491
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200492 networkConfigCompletion.whenComplete((value, ex) -> {
493 //setting to null for easier fall through
494 networkConfigCompletion = null;
495 //process all queued events
Charles Chan10b2fee2018-04-21 00:44:29 -0700496 queuedEvents.forEach(event -> {
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200497 mainEventExecutor.execute(new InternalEventHandler(event));
498 });
499 });
500
sanghob35a6192015-04-01 13:05:26 -0700501 log.info("Started");
502 }
503
Saurav Das45f48152018-01-18 12:07:33 -0800504 KryoNamespace.Builder createSerializer() {
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700505 return new KryoNamespace.Builder()
506 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700507 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800508 VlanNextObjectiveStoreKey.class,
509 DestinationSet.class,
510 NextNeighbors.class,
511 Tunnel.class,
512 DefaultTunnel.class,
513 Policy.class,
514 TunnelPolicy.class,
515 Policy.Type.class,
516 PortNextObjectiveStoreKey.class,
517 XConnectStoreKey.class,
518 L2Tunnel.class,
519 L2TunnelPolicy.class,
520 DefaultL2Tunnel.class,
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700521 DefaultL2TunnelPolicy.class,
522 DummyVlanIdStoreKey.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700523 );
524 }
525
sanghob35a6192015-04-01 13:05:26 -0700526 @Deactivate
527 protected void deactivate() {
Charles Chan50bb6ef2018-04-18 18:41:05 -0700528 mainEventExecutor.shutdown();
529 hostEventExecutor.shutdown();
530 routeEventExecutor.shutdown();
531 mcastEventExecutor.shutdown();
532
Charles Chand6832882015-10-05 17:50:33 -0700533 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700534 cfgService.unregisterConfigFactory(deviceConfigFactory);
535 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700536 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700537 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan47933752017-11-30 15:37:50 -0800538 compCfgService.unregisterProperties(getClass(), false);
Charles Chand6832882015-10-05 17:50:33 -0700539
Charles Chanc7a8a682017-06-19 00:43:31 -0700540 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700541 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700542 linkService.removeListener(linkListener);
543 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700544 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700545 routeService.removeListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100546 topologyService.removeListener(topologyListener);
Charles Chan50bb6ef2018-04-18 18:41:05 -0700547 mastershipService.removeListener(mastershipListener);
Saurav Das201762d2018-04-21 17:19:48 -0700548 clusterService.removeListener(clusterListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700549
Charles Chan0aa674e2017-02-23 15:44:08 -0800550 neighbourResolutionService.unregisterNeighbourHandlers(appId);
551
sanghob35a6192015-04-01 13:05:26 -0700552 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700553 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700554 deviceListener = null;
Charles Chan6577f612018-04-19 13:10:01 -0700555 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chanb8e10c82015-10-14 11:24:40 -0700556 groupHandlerMap.clear();
Saurav Dasc6ff8f02018-04-23 18:42:12 -0700557 defaultRoutingHandler.shutdown();
Charles Chanb8e10c82015-10-14 11:24:40 -0700558
Saurav Das7bcbe702017-06-13 15:35:54 -0700559 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800560 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700561 portNextObjStore.destroy();
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700562 dummyVlanIdStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700563 tunnelStore.destroy();
564 policyStore.destroy();
Pier Luigib72201b2018-01-25 16:16:02 +0100565
566 mcastHandler.terminate();
sanghob35a6192015-04-01 13:05:26 -0700567 log.info("Stopped");
568 }
569
Charles Chan47933752017-11-30 15:37:50 -0800570 @Modified
571 private void modified(ComponentContext context) {
572 Dictionary<?, ?> properties = context.getProperties();
573 if (properties == null) {
574 return;
575 }
576
577 String strActiveProving = Tools.get(properties, "activeProbing");
578 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
Charles Chan47933752017-11-30 15:37:50 -0800579 if (expectActiveProbing != activeProbing) {
580 activeProbing = expectActiveProbing;
581 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
582 }
Saurav Das9a554292018-04-27 18:42:30 -0700583
584 String strSingleHomedDown = Tools.get(properties, "singleHomedDown");
585 boolean expectSingleHomedDown = Boolean.parseBoolean(strSingleHomedDown);
586 if (expectSingleHomedDown != singleHomedDown) {
587 singleHomedDown = expectSingleHomedDown;
588 log.info("{} downing of single homed hosts for lost uplinks",
589 singleHomedDown ? "Enabling" : "Disabling");
590 if (singleHomedDown && linkHandler != null) {
591 hostService.getHosts().forEach(host -> host.locations()
592 .forEach(loc -> {
593 if (interfaceService.isConfigured(loc)) {
594 linkHandler.checkUplinksForHost(loc);
595 }
596 }));
597 } else {
598 log.warn("Disabling singleHomedDown does not re-enable already "
599 + "downed ports for single-homed hosts");
600 }
601 }
Charles Chan47933752017-11-30 15:37:50 -0800602 }
603
sangho1e575652015-05-14 00:39:53 -0700604 @Override
605 public List<Tunnel> getTunnels() {
606 return tunnelHandler.getTunnels();
607 }
608
609 @Override
sangho71abe1b2015-06-29 14:58:47 -0700610 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
611 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700612 }
613
614 @Override
sangho71abe1b2015-06-29 14:58:47 -0700615 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700616 for (Policy policy: policyHandler.getPolicies()) {
617 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
618 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
619 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
620 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700621 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700622 }
623 }
624 }
sangho71abe1b2015-06-29 14:58:47 -0700625 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700626 }
627
628 @Override
sangho71abe1b2015-06-29 14:58:47 -0700629 public PolicyHandler.Result removePolicy(Policy policy) {
630 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700631 }
632
633 @Override
sangho71abe1b2015-06-29 14:58:47 -0700634 public PolicyHandler.Result createPolicy(Policy policy) {
635 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700636 }
637
638 @Override
639 public List<Policy> getPolicies() {
640 return policyHandler.getPolicies();
641 }
642
Saurav Das59232cf2016-04-27 18:35:50 -0700643 @Override
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700644 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
645 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700646 }
647
648 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800649 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700650 return l2TunnelHandler.getL2Tunnels();
651 }
652
653 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800654 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700655 return l2TunnelHandler.getL2Policies();
656 }
657
Saurav Das5a356042018-04-06 20:16:01 -0700658 @Override
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700659 @Deprecated
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700660 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
661
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700662 // get both added and pending pseudowires
663 List<L2TunnelDescription> pseudowires = new ArrayList<>();
664 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
665 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700666 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700667
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700668 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
669
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700670 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
671 L2TunnelHandler.Result res;
672 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
673 res = addPseudowire(pw);
674 if (res != L2TunnelHandler.Result.SUCCESS) {
675 log.error("Pseudowire with id {} can not be instantiated !", res);
676 retRes = res;
677 }
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700678 }
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700679
680 return retRes;
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700681 }
682
683 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800684 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700685 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700686 }
687
688 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800689 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700690 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700691 }
692
693 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700694 public void rerouteNetwork() {
695 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700696 }
697
Charles Chanc81c45b2016-10-20 17:02:44 -0700698 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800699 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
700 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800701 deviceConfiguration.getRouters().forEach(device ->
702 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700703 return deviceSubnetMap;
704 }
705
Saurav Dasc88d4662017-05-15 15:34:25 -0700706
707 @Override
708 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
709 if (defaultRoutingHandler != null) {
710 return defaultRoutingHandler.getCurrentEmcpSpgMap();
711 } else {
712 return null;
713 }
714 }
715
716 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700717 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
718 if (dsNextObjStore != null) {
719 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700720 } else {
721 return ImmutableMap.of();
722 }
723 }
724
Saurav Dasceccf242017-08-03 18:30:35 -0700725 @Override
726 public void verifyGroups(DeviceId id) {
727 DefaultGroupHandler gh = groupHandlerMap.get(id);
728 if (gh != null) {
729 gh.triggerBucketCorrector();
730 }
731 }
732
Saurav Dasc568c342018-01-25 09:49:01 -0800733 @Override
734 public ImmutableMap<Link, Boolean> getSeenLinks() {
735 return linkHandler.getSeenLinks();
736 }
737
738 @Override
739 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
740 return linkHandler.getDownedPorts();
741 }
742
Pier Luigib29144d2018-01-15 18:06:43 +0100743 @Override
744 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
745 return mcastHandler.getMcastNextIds(mcastIp);
746 }
747
748 @Override
Pier Luigi96fe0772018-02-28 12:10:50 +0100749 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigib29144d2018-01-15 18:06:43 +0100750 return mcastHandler.getMcastRoles(mcastIp);
751 }
752
753 @Override
754 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
755 return mcastHandler.getMcastPaths(mcastIp);
756 }
757
Pier96f63cb2018-04-17 16:29:56 +0200758 @Override
Pierb1fe7382018-04-17 17:25:22 +0200759 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
760 ConnectPoint sourcecp) {
761 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
762 }
763
764 @Override
Pier96f63cb2018-04-17 16:29:56 +0200765 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
766 return mcastHandler.getMcastLeaders(mcastIp);
767 }
768
Charles Chan8bc75ee2018-04-17 18:56:53 -0700769 @Override
770 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
771 return defaultRoutingHandler == null ? ImmutableMap.of() :
772 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
773 }
774
775 @Override
776 public Map<DeviceId, Boolean> getShouldProgramCache() {
777 return defaultRoutingHandler == null ? ImmutableMap.of() :
778 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
779 }
780
sanghof9d0bf12015-05-19 11:57:42 -0700781 /**
Ray Milkeye4afdb52017-04-05 09:42:04 -0700782 * Extracts the application ID from the manager.
783 *
784 * @return application ID
785 */
786 public ApplicationId appId() {
787 return appId;
788 }
789
790 /**
791 * Returns the device configuration.
792 *
793 * @return device configuration
794 */
795 public DeviceConfiguration deviceConfiguration() {
796 return deviceConfiguration;
797 }
798
799 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700800 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700801 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700802 *
803 * @return next objective ID store
804 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700805 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
806 dsNextObjStore() {
807 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700808 }
809
810 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700811 * Per device next objective ID store with (device id + vlanid) as key.
812 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700813 *
814 * @return vlan next object store
815 */
816 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
817 return vlanNextObjStore;
818 }
819
820 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700821 * Per device next objective ID store with (device id + port + treatment + meta) as key.
822 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700823 *
824 * @return port next object store.
825 */
826 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
827 return portNextObjStore;
828 }
829
830 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700831 * Per port dummy VLAN ID store with (connect point + ip address) as key.
832 * Used to keep track on dummy VLAN ID allocation.
833 *
834 * @return dummy vlan id store.
835 */
836 public EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId> dummyVlanIdStore() {
837 return dummyVlanIdStore;
838 }
839
840 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700841 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
842 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700843 *
844 * @return MPLS-ECMP value
845 */
846 public boolean getMplsEcmp() {
847 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
848 .getConfig(this.appId, SegmentRoutingAppConfig.class);
849 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
850 }
851
852 /**
sanghof9d0bf12015-05-19 11:57:42 -0700853 * Returns the tunnel object with the tunnel ID.
854 *
855 * @param tunnelId Tunnel ID
856 * @return Tunnel reference
857 */
sangho1e575652015-05-14 00:39:53 -0700858 public Tunnel getTunnel(String tunnelId) {
859 return tunnelHandler.getTunnel(tunnelId);
860 }
861
Charles Chan7ffd81f2017-02-08 15:52:08 -0800862 /**
Charles Chanf9a52702017-06-16 15:19:24 -0700863 * Returns internal VLAN for untagged hosts on given connect point.
864 * <p>
865 * The internal VLAN is either vlan-untagged for an access port,
866 * or vlan-native for a trunk port.
867 *
868 * @param connectPoint connect point
869 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
870 */
Pier Luigi96fe0772018-02-28 12:10:50 +0100871 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chan971d7ba2018-05-01 11:50:20 -0700872 VlanId untaggedVlanId = interfaceService.getUntaggedVlanId(connectPoint);
873 VlanId nativeVlanId = interfaceService.getNativeVlanId(connectPoint);
Charles Chanf9a52702017-06-16 15:19:24 -0700874 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
875 }
876
877 /**
Charles Chan65238242017-06-22 18:03:14 -0700878 * Returns optional pair device ID of given device.
879 *
880 * @param deviceId device ID
881 * @return optional pair device ID. Might be empty if pair device is not configured
882 */
883 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
884 SegmentRoutingDeviceConfig deviceConfig =
885 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
886 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
887 }
888 /**
889 * Returns optional pair device local port of given device.
890 *
891 * @param deviceId device ID
892 * @return optional pair device ID. Might be empty if pair device is not configured
893 */
Saurav Das9a554292018-04-27 18:42:30 -0700894 public Optional<PortNumber> getPairLocalPort(DeviceId deviceId) {
Charles Chan65238242017-06-22 18:03:14 -0700895 SegmentRoutingDeviceConfig deviceConfig =
896 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
897 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
898 }
899
900 /**
Charles Chan9640c812017-08-23 13:55:39 -0700901 * Returns locations of given resolved route.
902 *
903 * @param resolvedRoute resolved route
904 * @return locations of nexthop. Might be empty if next hop is not found
905 */
906 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
907 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
908 return Optional.ofNullable(hostService.getHost(hostId))
909 .map(Host::locations).orElse(Sets.newHashSet())
910 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
911 }
912
913 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800914 * Returns vlan port map of given device.
915 *
916 * @param deviceId device id
917 * @return vlan-port multimap
918 */
919 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
920 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
921
922 interfaceService.getInterfaces().stream()
923 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
924 .forEach(intf -> {
925 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -0700926 intf.vlanTagged().forEach(vlanTagged ->
927 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
928 );
Charles Chan7ffd81f2017-02-08 15:52:08 -0800929 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
930 });
931 vlanPortMap.removeAll(VlanId.NONE);
932
933 return vlanPortMap;
934 }
935
936 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800937 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800938 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700939 *
940 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800941 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800942 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700943 */
Charles Chan65238242017-06-22 18:03:14 -0700944 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700945 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800946 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
947 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700948 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800949 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800950 + "device {} not found", deviceId);
951 return -1;
952 }
953 }
954
955 /**
956 * Returns the next objective ID for the given portNumber, given the treatment.
957 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700958 * would result in different objectives. If the next object does not exist,
959 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800960 *
961 * @param deviceId Device ID
962 * @param portNum port number on device for which NextObjective is queried
963 * @param treatment the actions to apply on the packets (should include outport)
964 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700965 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700966 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800967 */
968 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
969 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700970 TrafficSelector meta,
971 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -0800972 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
973 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -0700974 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -0800975 } else {
Charles Chane849c192016-01-11 18:28:54 -0800976 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
977 + " not found", deviceId);
978 return -1;
979 }
980 }
981
Saurav Dasc88d4662017-05-15 15:34:25 -0700982 /**
983 * Returns the group handler object for the specified device id.
984 *
985 * @param devId the device identifier
986 * @return the groupHandler object for the device id, or null if not found
987 */
Charles Chan65238242017-06-22 18:03:14 -0700988 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700989 return groupHandlerMap.get(devId);
990 }
991
992 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700993 * Returns the default routing handler object.
994 *
995 * @return the default routing handler object
996 */
997 public DefaultRoutingHandler getRoutingHandler() {
998 return defaultRoutingHandler;
999 }
1000
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001001 /**
1002 * Returns new dummy VLAN ID.
1003 * Dummy VLAN ID should be unique in each connect point.
1004 *
1005 * @param cp connect point
1006 * @param ipAddress IP address
1007 * @return new dummy VLAN ID. Returns VlanId.NONE if no VLAN ID is available.
1008 */
1009 public synchronized VlanId allocateDummyVlanId(ConnectPoint cp, IpAddress ipAddress) {
1010 Set<VlanId> usedVlanId = Sets.union(getVlanPortMap(cp.deviceId()).keySet(),
1011 dummyVlanIdStore.entrySet().stream().filter(entry ->
1012 (entry.getKey()).connectPoint().equals(cp))
1013 .map(Map.Entry::getValue)
1014 .collect(Collectors.toSet()));
1015
1016 VlanId dummyVlanId = IntStream.range(MIN_DUMMY_VLAN_ID, MAX_DUMMY_VLAN_ID).mapToObj(
1017 i -> VlanId.vlanId((short) (i & 0xFFFF))
1018 ).filter(vlanId -> !usedVlanId.contains(vlanId)).findFirst().orElse(VlanId.NONE);
1019
1020 if (!dummyVlanId.equals(VlanId.NONE)) {
1021 this.dummyVlanIdStore.put(new DummyVlanIdStoreKey(cp, ipAddress), dummyVlanId);
1022 log.debug("Dummy VLAN ID {} is allocated to {}, {}", dummyVlanId, cp, ipAddress);
1023 } else {
1024 log.error("Failed to allocate dummy VLAN ID for {}, {}", cp, ipAddress);
1025 }
1026 return dummyVlanId;
1027 }
1028
1029
sanghob35a6192015-04-01 13:05:26 -07001030 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -07001031 @Override
1032 public void process(PacketContext context) {
1033
1034 if (context.isHandled()) {
1035 return;
1036 }
1037
1038 InboundPacket pkt = context.inPacket();
1039 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -08001040
1041 if (ethernet == null) {
1042 return;
1043 }
1044
Saurav Das7bcbe702017-06-13 15:35:54 -07001045 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1046 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001047 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001048 log.warn("Received unexpected ARP packet on {}",
1049 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -07001050 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -08001051 return;
sanghob35a6192015-04-01 13:05:26 -07001052 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -08001053 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1054 //ipHandler.addToPacketBuffer(ipv4Packet);
1055 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1056 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001057 } else {
Charles Chan50035632017-01-13 17:20:44 -08001058 // NOTE: We don't support IP learning at this moment so this
1059 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -08001060 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001061 }
Pier Ventre10bd8d12016-11-26 21:05:22 -08001062 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1063 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -08001064 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -08001065 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -08001066 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1067 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1068 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1069 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1070 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1071 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -07001072 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -07001073 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -08001074 }
1075 } else {
1076 // NOTE: We don't support IP learning at this moment so this
1077 // is not necessary. Also it causes duplication of DHCPv6 packets.
1078 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1079 }
sanghob35a6192015-04-01 13:05:26 -07001080 }
1081 }
1082 }
1083
sanghob35a6192015-04-01 13:05:26 -07001084 private class InternalEventHandler implements Runnable {
Charles Chan9b7217c2018-04-05 16:31:15 -07001085 private Event event;
1086
1087 InternalEventHandler(Event event) {
1088 this.event = event;
1089 }
1090
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001091 @Override
sanghob35a6192015-04-01 13:05:26 -07001092 public void run() {
Charles Chan9b7217c2018-04-05 16:31:15 -07001093 try {
1094 // TODO We should also change SR routing and PW to listen to TopologyEvents
1095 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1096 event.type() == LinkEvent.Type.LINK_UPDATED) {
1097 linkHandler.processLinkAdded((Link) event.subject());
1098 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1099 linkHandler.processLinkRemoved((Link) event.subject());
1100 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1101 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1102 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1103 DeviceId deviceId = ((Device) event.subject()).id();
1104 if (deviceService.isAvailable(deviceId)) {
1105 log.info("** DEVICE UP Processing device event {} "
1106 + "for available device {}",
1107 event.type(), ((Device) event.subject()).id());
1108 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001109 } else {
Charles Chan9b7217c2018-04-05 16:31:15 -07001110 log.info(" ** DEVICE DOWN Processing device event {}"
1111 + " for unavailable device {}",
1112 event.type(), ((Device) event.subject()).id());
1113 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001114 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001115 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1116 // typically these calls come when device is added first time
1117 // so port filtering rules are handled at the device_added event.
1118 // port added calls represent all ports on the device,
1119 // enabled or not.
1120 log.trace("** PORT ADDED {}/{} -> {}",
1121 ((DeviceEvent) event).subject().id(),
1122 ((DeviceEvent) event).port().number(),
1123 event.type());
1124 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1125 // these calls happen for every subsequent event
1126 // ports enabled, disabled, switch goes away, comes back
1127 log.info("** PORT UPDATED {}/{} -> {}",
1128 event.subject(),
1129 ((DeviceEvent) event).port(),
1130 event.type());
Saurav Das9a554292018-04-27 18:42:30 -07001131 processPortUpdatedInternal(((Device) event.subject()),
Charles Chan9b7217c2018-04-05 16:31:15 -07001132 ((DeviceEvent) event).port());
1133 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1134 // Process topology event, needed for all modules relying on
1135 // topology service for path computation
1136 TopologyEvent topologyEvent = (TopologyEvent) event;
1137 log.info("Processing topology event {}, topology age {}, reasons {}",
1138 event.type(), topologyEvent.subject().time(),
1139 topologyEvent.reasons().size());
1140 topologyHandler.processTopologyChange(topologyEvent.reasons());
1141 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1142 hostHandler.processHostAddedEvent((HostEvent) event);
1143 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1144 hostHandler.processHostMovedEvent((HostEvent) event);
1145 routeHandler.processHostMovedEvent((HostEvent) event);
1146 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1147 hostHandler.processHostRemovedEvent((HostEvent) event);
1148 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1149 hostHandler.processHostUpdatedEvent((HostEvent) event);
1150 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1151 routeHandler.processRouteAdded((RouteEvent) event);
1152 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1153 routeHandler.processRouteUpdated((RouteEvent) event);
1154 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1155 routeHandler.processRouteRemoved((RouteEvent) event);
1156 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1157 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1158 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1159 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1160 event.type() == McastEvent.Type.SINKS_ADDED ||
1161 event.type() == McastEvent.Type.SINKS_REMOVED ||
1162 event.type() == McastEvent.Type.ROUTE_ADDED ||
1163 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1164 mcastHandler.processMcastEvent((McastEvent) event);
1165 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1166 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1167 Class configClass = netcfgEvent.configClass();
1168 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1169 appCfgHandler.processAppConfigAdded(netcfgEvent);
1170 log.info("App config event .. configuring network");
1171 cfgListener.configureNetwork();
1172 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1173 log.info("Segment Routing Device Config added for {}", event.subject());
1174 cfgListener.configureNetwork();
1175 } else if (configClass.equals(XConnectConfig.class)) {
1176 xConnectHandler.processXConnectConfigAdded(netcfgEvent);
1177 } else if (configClass.equals(InterfaceConfig.class)) {
1178 log.info("Interface Config added for {}", event.subject());
1179 cfgListener.configureNetwork();
1180 } else {
1181 log.error("Unhandled config class: {}", configClass);
1182 }
1183 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1184 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1185 Class configClass = netcfgEvent.configClass();
1186 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1187 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1188 log.info("App config event .. configuring network");
1189 cfgListener.configureNetwork();
1190 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1191 log.info("Segment Routing Device Config updated for {}", event.subject());
1192 createOrUpdateDeviceConfiguration();
1193 } else if (configClass.equals(XConnectConfig.class)) {
1194 xConnectHandler.processXConnectConfigUpdated(netcfgEvent);
1195 } else if (configClass.equals(InterfaceConfig.class)) {
1196 log.info("Interface Config updated for {}", event.subject());
1197 createOrUpdateDeviceConfiguration();
1198 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1199 (InterfaceConfig) netcfgEvent.prevConfig().get());
1200 } else {
1201 log.error("Unhandled config class: {}", configClass);
1202 }
1203 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1204 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1205 Class configClass = netcfgEvent.configClass();
1206 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1207 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1208 log.info("App config event .. configuring network");
1209 cfgListener.configureNetwork();
1210 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1211 // TODO Handle sr device config removal
1212 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
1213 } else if (configClass.equals(XConnectConfig.class)) {
1214 xConnectHandler.processXConnectConfigRemoved(netcfgEvent);
1215 } else if (configClass.equals(InterfaceConfig.class)) {
1216 // TODO Handle interface removal
1217 log.info("InterfaceConfig removal is not handled in current implementation");
1218 } else {
1219 log.error("Unhandled config class: {}", configClass);
1220 }
Saurav Das201762d2018-04-21 17:19:48 -07001221 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1222 MastershipEvent me = (MastershipEvent) event;
1223 DeviceId deviceId = me.subject();
1224 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1225 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1226 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1227 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1228 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1229 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan9b7217c2018-04-05 16:31:15 -07001230 } else {
1231 log.warn("Unhandled event type: {}", event.type());
sanghob35a6192015-04-01 13:05:26 -07001232 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001233 } catch (Exception e) {
1234 log.error("SegmentRouting event handler thread thrown an exception: {}",
1235 e.getMessage(), e);
sanghob35a6192015-04-01 13:05:26 -07001236 }
sanghob35a6192015-04-01 13:05:26 -07001237 }
1238 }
1239
Saurav Das45f48152018-01-18 12:07:33 -08001240 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001241 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001242
1243 // NOTE: Punt ARP/NDP even when the device is not configured.
1244 // Host learning without network config is required for CORD config generator.
1245 routingRulePopulator.populateIpPunts(device.id());
1246 routingRulePopulator.populateArpNdpPunts(device.id());
1247
Charles Chan0b4e6182015-11-03 10:42:14 -08001248 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001249 log.warn("Device configuration unavailable. Device {} will be "
1250 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001251 return;
1252 }
Charles Chan2199c302016-04-23 17:36:10 -07001253 processDeviceAddedInternal(device.id());
1254 }
1255
1256 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001257 // Irrespective of whether the local is a MASTER or not for this device,
1258 // we need to create a SR-group-handler instance. This is because in a
1259 // multi-instance setup, any instance can initiate forwarding/next-objectives
1260 // for any switch (even if this instance is a SLAVE or not even connected
1261 // to the switch). To handle this, a default-group-handler instance is necessary
1262 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001263 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1264 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001265 DefaultGroupHandler groupHandler;
1266 try {
1267 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001268 createGroupHandler(deviceId,
1269 appId,
1270 deviceConfiguration,
1271 linkService,
1272 flowObjectiveService,
1273 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001274 } catch (DeviceConfigNotFoundException e) {
1275 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1276 return;
1277 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001278 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001279 deviceId);
1280 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001281 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001282
Charles Chan2199c302016-04-23 17:36:10 -07001283 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001284 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001285 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001286 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001287 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001288 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001289 }
Charles Chan5270ed02016-01-30 23:22:37 -08001290
Charles Chan03a73e02016-10-24 14:52:01 -07001291 appCfgHandler.init(deviceId);
Charles Chan94549652018-04-29 18:11:37 -07001292 hostEventExecutor.execute(() -> hostHandler.init(deviceId));
1293 routeEventExecutor.execute(() -> routeHandler.init(deviceId));
sanghob35a6192015-04-01 13:05:26 -07001294 }
1295
Saurav Das80980c72016-03-23 11:22:49 -07001296 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001297 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001298 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan47933752017-11-30 15:37:50 -08001299 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001300 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001301 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001302 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001303 portNextObjStore.entrySet().stream()
1304 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001305 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Das45f48152018-01-18 12:07:33 -08001306 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001307
Saurav Dasceccf242017-08-03 18:30:35 -07001308 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1309 if (gh != null) {
1310 gh.shutdown();
1311 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001312 // Note that a switch going down is associated with all of its links
1313 // going down as well, but it is treated as a single switch down event
Saurav Das5a356042018-04-06 20:16:01 -07001314 // while the link-downs are ignored. We cannot rely on the ordering of
1315 // events - i.e we cannot expect all link-downs to come before the
1316 // switch down - so we purge all seen-links for the switch before
1317 // handling route-path changes for the switch-down
Saurav Dasc88d4662017-05-15 15:34:25 -07001318 defaultRoutingHandler
Saurav Das604ab3a2018-03-18 21:28:15 -07001319 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Dasc568c342018-01-25 09:49:01 -08001320 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001321 xConnectHandler.removeDevice(device.id());
Saurav Dasa4020382018-02-14 14:14:54 -08001322
1323 // Cleanup all internal groupHandler stores for this device. Should be
1324 // done after all rerouting or rehashing has been completed
1325 groupHandlerMap.entrySet()
1326 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Das80980c72016-03-23 11:22:49 -07001327 }
1328
Saurav Dasc6ff8f02018-04-23 18:42:12 -07001329 /**
1330 * Purge the destinationSet nextObjective store of entries with this device
1331 * as key. Erases app-level knowledge of hashed groups in this device.
1332 *
1333 * @param devId the device identifier
1334 */
Saurav Das201762d2018-04-21 17:19:48 -07001335 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Dasc6ff8f02018-04-23 18:42:12 -07001336 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Das201762d2018-04-21 17:19:48 -07001337 dsNextObjStore.entrySet().stream()
1338 .filter(entry -> entry.getKey().deviceId().equals(devId))
1339 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1340 }
1341
Saurav Das9a554292018-04-27 18:42:30 -07001342 private void processPortUpdatedInternal(Device device, Port port) {
Saurav Das1a129a02016-11-18 15:21:57 -08001343 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1344 log.warn("Device configuration uploading. Not handling port event for"
1345 + "dev: {} port: {}", device.id(), port.number());
1346 return;
1347 }
Saurav Das018605f2017-02-18 14:05:44 -08001348
Saurav Das9a554292018-04-27 18:42:30 -07001349 if (interfaceService.isConfigured(new ConnectPoint(device.id(), port.number()))) {
1350 lastEdgePortEvent = Instant.now();
1351 }
1352
Saurav Das018605f2017-02-18 14:05:44 -08001353 if (!mastershipService.isLocalMaster(device.id())) {
1354 log.debug("Not master for dev:{} .. not handling port updated event"
1355 + "for port {}", device.id(), port.number());
1356 return;
1357 }
Saurav Das9a554292018-04-27 18:42:30 -07001358 processPortUpdated(device.id(), port);
1359 }
Saurav Das018605f2017-02-18 14:05:44 -08001360
Saurav Das9a554292018-04-27 18:42:30 -07001361 /**
1362 * Adds or remove filtering rules for the given switchport. If switchport is
1363 * an edge facing port, additionally handles host probing and broadcast
1364 * rules. Must be called by local master of device.
1365 *
1366 * @param deviceId the device identifier
1367 * @param port the port to update
1368 */
1369 void processPortUpdated(DeviceId deviceId, Port port) {
Saurav Das018605f2017-02-18 14:05:44 -08001370 // first we handle filtering rules associated with the port
1371 if (port.isEnabled()) {
1372 log.info("Switchport {}/{} enabled..programming filters",
Saurav Das9a554292018-04-27 18:42:30 -07001373 deviceId, port.number());
1374 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001375 } else {
1376 log.info("Switchport {}/{} disabled..removing filters",
Saurav Das9a554292018-04-27 18:42:30 -07001377 deviceId, port.number());
1378 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001379 }
Saurav Das1a129a02016-11-18 15:21:57 -08001380
1381 // portUpdated calls are for ports that have gone down or up. For switch
1382 // to switch ports, link-events should take care of any re-routing or
1383 // group editing necessary for port up/down. Here we only process edge ports
1384 // that are already configured.
Saurav Das9a554292018-04-27 18:42:30 -07001385 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
Charles Chan971d7ba2018-05-01 11:50:20 -07001386 VlanId untaggedVlan = interfaceService.getUntaggedVlanId(cp);
1387 VlanId nativeVlan = interfaceService.getNativeVlanId(cp);
1388 Set<VlanId> taggedVlans = interfaceService.getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001389
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001390 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001391 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das9a554292018-04-27 18:42:30 -07001392 + "dev/port: {}/{}", deviceId, port.number());
Saurav Das1a129a02016-11-18 15:21:57 -08001393 return;
1394 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001395 if (untaggedVlan != null) {
Saurav Das9a554292018-04-27 18:42:30 -07001396 processEdgePort(deviceId, port, untaggedVlan, true);
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001397 }
1398 if (nativeVlan != null) {
Saurav Das9a554292018-04-27 18:42:30 -07001399 processEdgePort(deviceId, port, nativeVlan, true);
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001400 }
1401 if (!taggedVlans.isEmpty()) {
Saurav Das9a554292018-04-27 18:42:30 -07001402 taggedVlans.forEach(tag -> processEdgePort(deviceId, port, tag, false));
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001403 }
Saurav Das1a129a02016-11-18 15:21:57 -08001404 }
1405
Saurav Das9a554292018-04-27 18:42:30 -07001406 private void processEdgePort(DeviceId deviceId, Port port, VlanId vlanId,
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001407 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001408 boolean portUp = port.isEnabled();
1409 if (portUp) {
Saurav Das9a554292018-04-27 18:42:30 -07001410 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", deviceId,
Charles Chan59cc16d2017-02-02 16:20:42 -08001411 port.number(), vlanId);
Charles Chan94549652018-04-29 18:11:37 -07001412 hostEventExecutor.execute(() -> hostHandler.processPortUp(new ConnectPoint(deviceId, port.number())));
Saurav Das1a129a02016-11-18 15:21:57 -08001413 } else {
Saurav Das9a554292018-04-27 18:42:30 -07001414 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", deviceId,
Charles Chan59cc16d2017-02-02 16:20:42 -08001415 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001416 }
1417
Saurav Das9a554292018-04-27 18:42:30 -07001418 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001419 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001420 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001421 } else {
1422 log.warn("Group handler not found for dev:{}. Not handling edge port"
Saurav Das9a554292018-04-27 18:42:30 -07001423 + " {} event for port:{}", deviceId,
Saurav Das1a129a02016-11-18 15:21:57 -08001424 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001425 }
1426 }
sangho1e575652015-05-14 00:39:53 -07001427
Charles Chan27fe1a52017-10-20 16:06:55 -07001428 private void createOrUpdateDeviceConfiguration() {
1429 if (deviceConfiguration == null) {
Saurav Dase7f51012018-02-09 17:26:45 -08001430 log.info("Creating new DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001431 deviceConfiguration = new DeviceConfiguration(this);
1432 } else {
Saurav Dase7f51012018-02-09 17:26:45 -08001433 log.info("Updating DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001434 deviceConfiguration.updateConfig();
1435 }
1436 }
1437
Charles Chan50bb6ef2018-04-18 18:41:05 -07001438 private void createOrUpdateDefaultRoutingHandler() {
1439 if (defaultRoutingHandler == null) {
1440 log.info("Creating new DefaultRoutingHandler");
1441 defaultRoutingHandler = new DefaultRoutingHandler(this);
1442 } else {
1443 log.info("Updating DefaultRoutingHandler");
1444 defaultRoutingHandler.update(this);
1445 }
1446 }
1447
Pier Ventre10bd8d12016-11-26 21:05:22 -08001448 /**
1449 * Registers the given connect point with the NRS, this is necessary
1450 * to receive the NDP and ARP packets from the NRS.
1451 *
1452 * @param portToRegister connect point to register
1453 */
1454 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001455 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001456 portToRegister,
1457 neighbourHandler,
1458 appId
1459 );
1460 }
1461
Charles Chand6832882015-10-05 17:50:33 -07001462 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001463 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001464 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001465
Charles Chane849c192016-01-11 18:28:54 -08001466 /**
1467 * Constructs the internal network config listener.
1468 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001469 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001470 */
Charles Chan65238242017-06-22 18:03:14 -07001471 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001472 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001473 }
1474
Charles Chane849c192016-01-11 18:28:54 -08001475 /**
1476 * Reads network config and initializes related data structure accordingly.
1477 */
Charles Chan47933752017-11-30 15:37:50 -08001478 void configureNetwork() {
Saurav Dase7f51012018-02-09 17:26:45 -08001479 log.info("Configuring network ...");
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001480
1481 // Setting handling of network configuration events completable future
1482 // The completable future is needed because of the async behaviour of the configureNetwork,
1483 // listener registration and event arrival
1484 // Enables us to buffer the events and execute them when the configure network is done.
1485 networkConfigCompletion = new CompletableFuture<>();
1486
1487 // add a small delay to absorb multiple network config added notifications
1488 if (!programmingScheduled.get()) {
1489 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1490 programmingScheduled.set(true);
1491 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1492 }
1493
Charles Chan27fe1a52017-10-20 16:06:55 -07001494 createOrUpdateDeviceConfiguration();
Charles Chan4636be02015-10-07 14:21:45 -07001495
Charles Chan2c15aca2016-11-09 20:51:44 -08001496 arpHandler = new ArpHandler(srManager);
1497 icmpHandler = new IcmpHandler(srManager);
1498 ipHandler = new IpHandler(srManager);
1499 routingRulePopulator = new RoutingRulePopulator(srManager);
Charles Chan50bb6ef2018-04-18 18:41:05 -07001500 createOrUpdateDefaultRoutingHandler();
Charles Chan4636be02015-10-07 14:21:45 -07001501
1502 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1503 groupHandlerMap, tunnelStore);
1504 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1505 flowObjectiveService,
1506 tunnelHandler, policyStore);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001507 networkConfigCompletion.complete(true);
1508
Charles Chan2199c302016-04-23 17:36:10 -07001509 mcastHandler.init();
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001510
Charles Chan4636be02015-10-07 14:21:45 -07001511 }
1512
Charles Chand6832882015-10-05 17:50:33 -07001513 @Override
1514 public void event(NetworkConfigEvent event) {
Charles Chan7a888e82018-03-21 16:57:47 -07001515 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
1516 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1517 switch (event.type()) {
1518 case CONFIG_ADDED:
1519 case CONFIG_UPDATED:
1520 case CONFIG_REMOVED:
1521 log.trace("Schedule Network Config event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001522 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1523 mainEventExecutor.execute(new InternalEventHandler(event));
1524 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001525 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001526 }
Charles Chan7a888e82018-03-21 16:57:47 -07001527 break;
1528 default:
1529 break;
Charles Chand6832882015-10-05 17:50:33 -07001530 }
1531 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001532
Charles Chan50443e82018-01-03 16:26:32 -08001533 @Override
1534 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase7f51012018-02-09 17:26:45 -08001535 if (event.type() == CONFIG_REGISTERED ||
1536 event.type() == CONFIG_UNREGISTERED) {
1537 log.debug("Ignore event {} due to type mismatch", event);
1538 return false;
Charles Chan50443e82018-01-03 16:26:32 -08001539 }
Saurav Dase7f51012018-02-09 17:26:45 -08001540
1541 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1542 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1543 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscd339592018-02-23 14:18:00 -08001544 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase7f51012018-02-09 17:26:45 -08001545 log.debug("Ignore event {} due to class mismatch", event);
1546 return false;
1547 }
1548
1549 return true;
Charles Chan50443e82018-01-03 16:26:32 -08001550 }
1551
Saurav Das7bcbe702017-06-13 15:35:54 -07001552 private final class ConfigChange implements Runnable {
1553 @Override
1554 public void run() {
1555 programmingScheduled.set(false);
Saurav Dase7f51012018-02-09 17:26:45 -08001556 log.info("Reacting to config changes after buffer delay");
Saurav Das7bcbe702017-06-13 15:35:54 -07001557 for (Device device : deviceService.getDevices()) {
1558 processDeviceAdded(device);
1559 }
1560 defaultRoutingHandler.startPopulationProcess();
1561 }
1562 }
Charles Chand6832882015-10-05 17:50:33 -07001563 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001564
Charles Chan7a888e82018-03-21 16:57:47 -07001565 private class InternalLinkListener implements LinkListener {
1566 @Override
1567 public void event(LinkEvent event) {
1568 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1569 event.type() == LinkEvent.Type.LINK_UPDATED ||
1570 event.type() == LinkEvent.Type.LINK_REMOVED) {
1571 log.trace("Schedule Link event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001572 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1573 mainEventExecutor.execute(new InternalEventHandler(event));
1574 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001575 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001576 }
Charles Chan7a888e82018-03-21 16:57:47 -07001577 }
1578 }
1579 }
1580
1581 private class InternalDeviceListener implements DeviceListener {
1582 @Override
1583 public void event(DeviceEvent event) {
1584 switch (event.type()) {
1585 case DEVICE_ADDED:
1586 case PORT_UPDATED:
1587 case PORT_ADDED:
1588 case DEVICE_UPDATED:
1589 case DEVICE_AVAILABILITY_CHANGED:
1590 log.trace("Schedule Device event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001591 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1592 mainEventExecutor.execute(new InternalEventHandler(event));
1593 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001594 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001595 }
Charles Chan7a888e82018-03-21 16:57:47 -07001596 break;
1597 default:
1598 }
1599 }
1600 }
1601
1602 private class InternalTopologyListener implements TopologyListener {
1603 @Override
1604 public void event(TopologyEvent event) {
1605 switch (event.type()) {
1606 case TOPOLOGY_CHANGED:
1607 log.trace("Schedule Topology event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001608 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1609 mainEventExecutor.execute(new InternalEventHandler(event));
1610 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001611 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001612 }
Charles Chan7a888e82018-03-21 16:57:47 -07001613 break;
1614 default:
1615 }
1616 }
1617 }
1618
Charles Chan68aa62d2015-11-09 16:37:23 -08001619 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001620 @Override
1621 public void event(HostEvent event) {
Charles Chan68aa62d2015-11-09 16:37:23 -08001622 switch (event.type()) {
1623 case HOST_ADDED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001624 case HOST_MOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001625 case HOST_REMOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001626 case HOST_UPDATED:
Charles Chan7a888e82018-03-21 16:57:47 -07001627 log.trace("Schedule Host event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001628 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chan68aa62d2015-11-09 16:37:23 -08001629 break;
1630 default:
1631 log.warn("Unsupported host event type: {}", event.type());
1632 break;
1633 }
1634 }
1635 }
1636
Charles Chand55e84d2016-03-30 17:54:24 -07001637 private class InternalMcastListener implements McastListener {
1638 @Override
1639 public void event(McastEvent event) {
1640 switch (event.type()) {
Pier3ee24552018-03-14 16:47:32 -07001641 case SOURCES_ADDED:
1642 case SOURCES_REMOVED:
1643 case SINKS_ADDED:
1644 case SINKS_REMOVED:
Charles Chand55e84d2016-03-30 17:54:24 -07001645 case ROUTE_REMOVED:
Pier96f63cb2018-04-17 16:29:56 +02001646 case ROUTE_ADDED:
Charles Chan7a888e82018-03-21 16:57:47 -07001647 log.trace("Schedule Mcast event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001648 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi9930da52018-02-02 16:19:11 +01001649 break;
Charles Chand55e84d2016-03-30 17:54:24 -07001650 default:
Charles Chan7a888e82018-03-21 16:57:47 -07001651 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chand55e84d2016-03-30 17:54:24 -07001652 break;
1653 }
1654 }
1655 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001656
Charles Chan03a73e02016-10-24 14:52:01 -07001657 private class InternalRouteEventListener implements RouteListener {
1658 @Override
1659 public void event(RouteEvent event) {
1660 switch (event.type()) {
1661 case ROUTE_ADDED:
Charles Chan03a73e02016-10-24 14:52:01 -07001662 case ROUTE_UPDATED:
Charles Chan03a73e02016-10-24 14:52:01 -07001663 case ROUTE_REMOVED:
Charles Chan2fde6d42017-08-23 14:46:43 -07001664 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan7a888e82018-03-21 16:57:47 -07001665 log.trace("Schedule Route event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001666 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan2fde6d42017-08-23 14:46:43 -07001667 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001668 default:
Charles Chan7a888e82018-03-21 16:57:47 -07001669 log.warn("Unsupported route event type: {}", event.type());
Charles Chan03a73e02016-10-24 14:52:01 -07001670 break;
1671 }
1672 }
1673 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001674
Charles Chan50bb6ef2018-04-18 18:41:05 -07001675 private class InternalMastershipListener implements MastershipListener {
1676 @Override
1677 public void event(MastershipEvent event) {
Charles Chan50bb6ef2018-04-18 18:41:05 -07001678 switch (event.type()) {
Saurav Das201762d2018-04-21 17:19:48 -07001679 case MASTER_CHANGED:
1680 log.debug("Mastership event: {}/{}", event.subject(),
1681 event.roleInfo());
1682 mainEventExecutor.execute(new InternalEventHandler(event));
1683 break;
1684 case BACKUPS_CHANGED:
1685 case SUSPENDED:
1686 default:
1687 log.debug("Mastership event type {} not handled", event.type());
1688 break;
Charles Chan50bb6ef2018-04-18 18:41:05 -07001689 }
1690 }
1691 }
1692
Saurav Das201762d2018-04-21 17:19:48 -07001693 class InternalClusterListener implements ClusterEventListener {
1694 private Instant lastClusterEvent = Instant.EPOCH;
1695
1696 long timeSinceLastClusterEvent() {
1697 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
1698 }
1699
1700 @Override
1701 public void event(ClusterEvent event) {
1702 switch (event.type()) {
1703 case INSTANCE_ACTIVATED:
1704 case INSTANCE_ADDED:
1705 case INSTANCE_READY:
1706 log.debug("Cluster event {} ignored", event.type());
1707 break;
1708 case INSTANCE_DEACTIVATED:
1709 case INSTANCE_REMOVED:
1710 log.info("** Cluster event {}", event.type());
1711 lastClusterEvent = Instant.now();
1712 break;
1713 default:
1714 break;
1715 }
1716
1717 }
1718
1719 }
1720
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001721 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1722 try {
1723 Set<Interface> intfs = conf.getInterfaces();
1724 Set<Interface> prevIntfs = prevConf.getInterfaces();
1725
1726 // Now we only handle one interface config at each port.
1727 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1728 log.warn("Interface update aborted - one at a time is allowed, " +
1729 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1730 return;
1731 }
1732
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001733 //The system is in an incoherent state, abort
1734 if (defaultRoutingHandler == null) {
1735 log.warn("Interface update aborted, defaultRoutingHandler is null");
1736 return;
1737 }
1738
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001739 Interface intf = intfs.stream().findFirst().get();
1740 Interface prevIntf = prevIntfs.stream().findFirst().get();
1741
1742 DeviceId deviceId = intf.connectPoint().deviceId();
1743 PortNumber portNum = intf.connectPoint().port();
1744
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001745 removeSubnetConfig(prevIntf.connectPoint(),
1746 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1747 new HashSet<>(intf.ipAddressesList())));
1748
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001749 if (!prevIntf.vlanNative().equals(VlanId.NONE)
1750 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
1751 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1752 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
1753 // Update filtering objective and L2IG group bucket
1754 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
1755 } else {
1756 // RemoveVlanNative
1757 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1758 }
1759 }
1760
1761 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
1762 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1763 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1764 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
1765 // Update filtering objective and L2IG group bucket
1766 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
1767 } else {
1768 // RemoveVlanUntagged
1769 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1770 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001771 }
1772
1773 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1774 // RemoveVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001775 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
1776 .filter(i -> !intf.vlanUntagged().equals(i))
1777 .filter(i -> !intf.vlanNative().equals(i))
1778 .forEach(vlanId -> updateVlanConfigInternal(
1779 deviceId, portNum, vlanId, false, false));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001780 }
1781
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001782 if (!intf.vlanNative().equals(VlanId.NONE)
1783 && !prevIntf.vlanNative().equals(intf.vlanNative())
1784 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1785 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
1786 // Update filtering objective and L2IG group bucket
1787 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
1788 } else {
1789 // AddVlanNative
1790 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1791 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001792 }
1793
1794 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1795 // AddVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001796 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
1797 .filter(i -> !prevIntf.vlanUntagged().equals(i))
1798 .filter(i -> !prevIntf.vlanNative().equals(i))
1799 .forEach(vlanId -> updateVlanConfigInternal(
1800 deviceId, portNum, vlanId, false, true)
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001801 );
1802 }
1803
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001804 if (!intf.vlanUntagged().equals(VlanId.NONE)
1805 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1806 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
1807 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
1808 // Update filtering objective and L2IG group bucket
1809 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
1810 } else {
1811 // AddVlanUntagged
1812 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1813 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001814 }
1815 addSubnetConfig(prevIntf.connectPoint(),
1816 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1817 new HashSet<>(prevIntf.ipAddressesList())));
1818 } catch (ConfigException e) {
1819 log.error("Error in configuration");
1820 }
1821 }
1822
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001823 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
1824 VlanId vlanId, boolean pushVlan) {
1825 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1826 if (grpHandler == null) {
1827 log.warn("Failed to retrieve group handler for device {}", deviceId);
1828 return;
1829 }
1830
1831 // Update filtering objective for a single port
1832 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
1833 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
1834
1835 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
1836 // Update L2IG bucket of the port
1837 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
1838 } else {
1839 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1840 }
1841 }
1842
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001843 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1844 VlanId vlanId, boolean pushVlan, boolean install) {
1845 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1846 if (grpHandler == null) {
1847 log.warn("Failed to retrieve group handler for device {}", deviceId);
1848 return;
1849 }
1850
1851 // Update filtering objective for a single port
1852 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1853
1854 // Update filtering objective for multicast ingress port
1855 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1856
1857 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1858
1859 if (nextId != -1 && !install) {
1860 // Update next objective for a single port as an output port
1861 // Remove a single port from L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001862 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001863 // Remove L2 Bridging rule and L3 Unicast rule to the host
Charles Chan94549652018-04-29 18:11:37 -07001864 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
1865 vlanId, pushVlan, install));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001866 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1867 // only if there is no port configured on that VLAN ID
1868 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1869 // Remove broadcast forwarding rule for the VLAN
1870 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1871 // Remove L2FG for VLAN
1872 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1873 } else {
1874 // Remove L2IG of the port
1875 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1876 }
1877 } else if (install) {
1878 if (nextId != -1) {
1879 // Add a single port to L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001880 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001881 } else {
1882 // Create L2FG for VLAN
1883 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1884 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1885 }
Charles Chan94549652018-04-29 18:11:37 -07001886 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
1887 vlanId, pushVlan, install));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001888 } else {
1889 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1890 }
1891 }
1892
1893 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1894 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1895 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1896
1897 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1898 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1899 .filter(intf -> !intf.connectPoint().equals(cp))
1900 .flatMap(intf -> intf.ipAddressesList().stream())
1901 .collect(Collectors.toSet());
1902 // 1. Partial subnet population
1903 // Remove routing rules for removed subnet from previous configuration,
1904 // which does not also exist in other interfaces in the same device
1905 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1906 .map(InterfaceIpAddress::subnetAddress)
1907 .collect(Collectors.toSet());
1908
1909 defaultRoutingHandler.revokeSubnet(
1910 ipPrefixSet.stream()
1911 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1912 .collect(Collectors.toSet()));
1913
1914 // 2. Interface IP punts
1915 // Remove IP punts for old Intf address
1916 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1917 .map(InterfaceIpAddress::ipAddress)
1918 .collect(Collectors.toSet());
1919 ipAddressSet.stream()
1920 .map(InterfaceIpAddress::ipAddress)
1921 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1922 .forEach(interfaceIpAddress ->
1923 routingRulePopulator.revokeSingleIpPunts(
1924 cp.deviceId(), interfaceIpAddress));
1925
1926 // 3. Host unicast routing rule
1927 // Remove unicast routing rule
Charles Chan94549652018-04-29 18:11:37 -07001928 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001929 }
1930
1931 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1932 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1933 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1934
1935 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1936 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1937 .filter(intf -> !intf.connectPoint().equals(cp))
1938 .flatMap(intf -> intf.ipAddressesList().stream())
1939 .collect(Collectors.toSet());
1940 // 1. Partial subnet population
1941 // Add routing rules for newly added subnet, which does not also exist in
1942 // other interfaces in the same device
1943 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1944 .map(InterfaceIpAddress::subnetAddress)
1945 .collect(Collectors.toSet());
1946
1947 defaultRoutingHandler.populateSubnet(
1948 Collections.singleton(cp),
1949 ipPrefixSet.stream()
1950 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1951 .collect(Collectors.toSet()));
1952
1953 // 2. Interface IP punts
1954 // Add IP punts for new Intf address
1955 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1956 .map(InterfaceIpAddress::ipAddress)
1957 .collect(Collectors.toSet());
1958 ipAddressSet.stream()
1959 .map(InterfaceIpAddress::ipAddress)
1960 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1961 .forEach(interfaceIpAddress ->
1962 routingRulePopulator.populateSingleIpPunts(
1963 cp.deviceId(), interfaceIpAddress));
1964
1965 // 3. Host unicast routing rule
1966 // Add unicast routing rule
Charles Chan94549652018-04-29 18:11:37 -07001967 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001968 }
sanghob35a6192015-04-01 13:05:26 -07001969}