blob: 388d21333e5bb2b9828d46e095a7471c5bb30d45 [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
Charles Chan03a73e02016-10-24 14:52:01 -0700235 ArpHandler arpHandler = null;
236 IcmpHandler icmpHandler = null;
237 IpHandler ipHandler = null;
238 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700239 ApplicationId appId;
240 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700241
Charles Chan03a73e02016-10-24 14:52:01 -0700242 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700243 private TunnelHandler tunnelHandler = null;
244 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700245 private InternalPacketProcessor processor = null;
246 private InternalLinkListener linkListener = null;
247 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700248 private AppConfigHandler appCfgHandler = null;
Pier Luigi96fe0772018-02-28 12:10:50 +0100249 public XConnectHandler xConnectHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800250 McastHandler mcastHandler = null;
251 HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800252 private RouteHandler routeHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800253 LinkHandler linkHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800254 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800255 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigi83f919b2018-02-15 16:33:08 +0100256 private TopologyHandler topologyHandler = null;
Charles Chan5270ed02016-01-30 23:22:37 -0800257 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700258 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
259 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700260 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigi83f919b2018-02-15 16:33:08 +0100261 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chan50bb6ef2018-04-18 18:41:05 -0700262 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
Saurav Das201762d2018-04-21 17:19:48 -0700263 final InternalClusterListener clusterListener = new InternalClusterListener();
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200264 //Completable future for network configuration process to buffer config events handling during activation
265 private CompletableFuture<Boolean> networkConfigCompletion = null;
Charles Chan10b2fee2018-04-21 00:44:29 -0700266 private List<Event> queuedEvents = new CopyOnWriteArrayList<>();
sanghob35a6192015-04-01 13:05:26 -0700267
Charles Chan9b7217c2018-04-05 16:31:15 -0700268 // Handles device, link, topology and network config events
Charles Chan50bb6ef2018-04-18 18:41:05 -0700269 private ScheduledExecutorService mainEventExecutor;
sanghob35a6192015-04-01 13:05:26 -0700270
Charles Chan50bb6ef2018-04-18 18:41:05 -0700271 // Handles host, route and mcast events respectively
272 private ScheduledExecutorService hostEventExecutor;
273 private ScheduledExecutorService routeEventExecutor;
274 private ScheduledExecutorService mcastEventExecutor;
Charles Chan9b7217c2018-04-05 16:31:15 -0700275
276 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chane849c192016-01-11 18:28:54 -0800277 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700278 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700279 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800280 */
Charles Chan47933752017-11-30 15:37:50 -0800281 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das7bcbe702017-06-13 15:35:54 -0700282 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800283 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700284 * Per device next objective ID store with (device id + vlanid) as key.
285 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800286 */
Charles Chan47933752017-11-30 15:37:50 -0800287 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800288 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800289 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700290 * Per device next objective ID store with (device id + port + treatment + meta) as key.
291 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chane849c192016-01-11 18:28:54 -0800292 */
Charles Chan47933752017-11-30 15:37:50 -0800293 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800294 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800295
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700296 /**
297 * Per port dummy VLAN ID store with (connect point + ip address) as key.
298 * Used to keep track on dummy VLAN ID allocation.
299 */
300 private EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId>
301 dummyVlanIdStore = null;
302
Saurav Das4ce45962015-11-24 23:21:05 -0800303 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
304 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700305
Saurav Das7bcbe702017-06-13 15:35:54 -0700306 private AtomicBoolean programmingScheduled = new AtomicBoolean();
307
Charles Chand55e84d2016-03-30 17:54:24 -0700308 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700309 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
310 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700311 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700312 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800313 public SegmentRoutingDeviceConfig createConfig() {
314 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700315 }
316 };
Pier Ventref34966c2016-11-07 16:21:04 -0800317
Charles Chand55e84d2016-03-30 17:54:24 -0700318 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700319 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
320 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700321 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800322 @Override
323 public SegmentRoutingAppConfig createConfig() {
324 return new SegmentRoutingAppConfig();
325 }
326 };
Pier Ventref34966c2016-11-07 16:21:04 -0800327
Charles Chanfc5c7802016-05-17 13:13:55 -0700328 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
329 new ConfigFactory<ApplicationId, XConnectConfig>(
330 SubjectFactories.APP_SUBJECT_FACTORY,
331 XConnectConfig.class, "xconnect") {
332 @Override
333 public XConnectConfig createConfig() {
334 return new XConnectConfig();
335 }
336 };
Pier Ventref34966c2016-11-07 16:21:04 -0800337
Charles Chand55e84d2016-03-30 17:54:24 -0700338 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700339 new ConfigFactory<ApplicationId, McastConfig>(
340 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700341 McastConfig.class, "multicast") {
342 @Override
343 public McastConfig createConfig() {
344 return new McastConfig();
345 }
346 };
347
Charles Chan116188d2016-02-18 14:22:42 -0800348 /**
349 * Segment Routing App ID.
350 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800351 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan59cc16d2017-02-02 16:20:42 -0800352
Charles Chane849c192016-01-11 18:28:54 -0800353 /**
354 * The default VLAN ID assigned to the interfaces without subnet config.
355 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800356 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700357
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700358 /**
359 * Minumum and maximum value of dummy VLAN ID to be allocated.
360 */
361 public static final int MIN_DUMMY_VLAN_ID = 2;
362 public static final int MAX_DUMMY_VLAN_ID = 4093;
363
sanghob35a6192015-04-01 13:05:26 -0700364 @Activate
Charles Chan47933752017-11-30 15:37:50 -0800365 protected void activate(ComponentContext context) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800366 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700367
Charles Chan50bb6ef2018-04-18 18:41:05 -0700368 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-main", "%d", log));
369 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-host", "%d", log));
370 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-route", "%d", log));
371 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-mcast", "%d", log));
372
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700373 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700374 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700375 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700376 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700377 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700378 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700379 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700380 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700381 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700382
Charles Chan59cc16d2017-02-02 16:20:42 -0800383 log.debug("Creating EC map vlannextobjectivestore");
384 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
385 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
386 vlanNextObjStore = vlanNextObjMapBuilder
387 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700388 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700389 .withTimestampProvider((k, v) -> new WallClockTimestamp())
390 .build();
391
Saurav Das4ce45962015-11-24 23:21:05 -0800392 log.debug("Creating EC map subnetnextobjectivestore");
393 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
394 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
395 portNextObjStore = portNextObjMapBuilder
396 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700397 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800398 .withTimestampProvider((k, v) -> new WallClockTimestamp())
399 .build();
400
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700401 EventuallyConsistentMapBuilder<DummyVlanIdStoreKey, VlanId>
402 dummyVlanIdMapBuilder = storageService.eventuallyConsistentMapBuilder();
403 dummyVlanIdStore = dummyVlanIdMapBuilder
404 .withName("dummyvlanidstore")
405 .withSerializer(createSerializer())
406 .withTimestampProvider((k, v) -> new WallClockTimestamp())
407 .build();
408
sangho0b2b6d12015-05-20 22:16:38 -0700409 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
410 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700411 tunnelStore = tunnelMapBuilder
412 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700413 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700414 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700415 .build();
416
417 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
418 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700419 policyStore = policyMapBuilder
420 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700421 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700422 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700423 .build();
424
Saurav Das80980c72016-03-23 11:22:49 -0700425 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800426 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700427 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800428 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800429 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
430 "requestInterceptsEnabled", "false");
Charles Chand3baaba2017-08-08 15:13:37 -0700431 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800432 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700433 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800434 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800435 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
436 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800437 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
438 "forceUnprovision", "true");
Charles Chanef624ed2017-08-10 16:57:28 -0700439 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan73316522017-07-20 16:16:25 -0700440 "distributed", "true");
Charles Chan35a32322017-08-14 11:42:11 -0700441 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
442 "multihomingEnabled", "true");
Charles Chan807d87a2017-11-29 19:54:20 -0800443 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
444 "staleLinkAge", "15000");
445 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
446 "allowDuplicateIps", "false");
Charles Chan47933752017-11-30 15:37:50 -0800447 compCfgService.registerProperties(getClass());
448 modified(context);
Saurav Das80980c72016-03-23 11:22:49 -0700449
Charles Chanb8e10c82015-10-14 11:24:40 -0700450 processor = new InternalPacketProcessor();
451 linkListener = new InternalLinkListener();
452 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700453 appCfgHandler = new AppConfigHandler(this);
454 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700455 mcastHandler = new McastHandler(this);
456 hostHandler = new HostHandler(this);
Saurav Das45f48152018-01-18 12:07:33 -0800457 linkHandler = new LinkHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700458 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800459 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800460 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigi83f919b2018-02-15 16:33:08 +0100461 topologyHandler = new TopologyHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700462
Charles Chan3e783d02016-02-26 22:19:52 -0800463 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700464 cfgService.registerConfigFactory(deviceConfigFactory);
465 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700466 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700467 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase7f51012018-02-09 17:26:45 -0800468 log.info("Configuring network before adding listeners");
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200469
Charles Chan132393a2018-01-04 14:26:07 -0800470 cfgListener.configureNetwork();
471
Charles Chan5270ed02016-01-30 23:22:37 -0800472 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700473 packetService.addProcessor(processor, PacketProcessor.director(2));
474 linkService.addListener(linkListener);
475 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700476 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700477 routeService.addListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100478 topologyService.addListener(topologyListener);
Charles Chan50bb6ef2018-04-18 18:41:05 -0700479 mastershipService.addListener(mastershipListener);
Saurav Das201762d2018-04-21 17:19:48 -0700480 clusterService.addListener(clusterListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700481
Charles Chanb39777c2018-03-09 15:53:44 -0800482 linkHandler.init();
Andreas Pantelopoulos4a768c02018-01-11 07:53:48 -0800483 l2TunnelHandler.init();
484
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200485 networkConfigCompletion.whenComplete((value, ex) -> {
486 //setting to null for easier fall through
487 networkConfigCompletion = null;
488 //process all queued events
Charles Chan10b2fee2018-04-21 00:44:29 -0700489 queuedEvents.forEach(event -> {
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200490 mainEventExecutor.execute(new InternalEventHandler(event));
491 });
492 });
493
sanghob35a6192015-04-01 13:05:26 -0700494 log.info("Started");
495 }
496
Saurav Das45f48152018-01-18 12:07:33 -0800497 KryoNamespace.Builder createSerializer() {
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700498 return new KryoNamespace.Builder()
499 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700500 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800501 VlanNextObjectiveStoreKey.class,
502 DestinationSet.class,
503 NextNeighbors.class,
504 Tunnel.class,
505 DefaultTunnel.class,
506 Policy.class,
507 TunnelPolicy.class,
508 Policy.Type.class,
509 PortNextObjectiveStoreKey.class,
510 XConnectStoreKey.class,
511 L2Tunnel.class,
512 L2TunnelPolicy.class,
513 DefaultL2Tunnel.class,
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700514 DefaultL2TunnelPolicy.class,
515 DummyVlanIdStoreKey.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700516 );
517 }
518
sanghob35a6192015-04-01 13:05:26 -0700519 @Deactivate
520 protected void deactivate() {
Charles Chan50bb6ef2018-04-18 18:41:05 -0700521 mainEventExecutor.shutdown();
522 hostEventExecutor.shutdown();
523 routeEventExecutor.shutdown();
524 mcastEventExecutor.shutdown();
525
Charles Chand6832882015-10-05 17:50:33 -0700526 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700527 cfgService.unregisterConfigFactory(deviceConfigFactory);
528 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700529 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700530 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan47933752017-11-30 15:37:50 -0800531 compCfgService.unregisterProperties(getClass(), false);
Charles Chand6832882015-10-05 17:50:33 -0700532
Charles Chanc7a8a682017-06-19 00:43:31 -0700533 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700534 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700535 linkService.removeListener(linkListener);
536 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700537 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700538 routeService.removeListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100539 topologyService.removeListener(topologyListener);
Charles Chan50bb6ef2018-04-18 18:41:05 -0700540 mastershipService.removeListener(mastershipListener);
Saurav Das201762d2018-04-21 17:19:48 -0700541 clusterService.removeListener(clusterListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700542
Charles Chan0aa674e2017-02-23 15:44:08 -0800543 neighbourResolutionService.unregisterNeighbourHandlers(appId);
544
sanghob35a6192015-04-01 13:05:26 -0700545 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700546 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700547 deviceListener = null;
Charles Chan6577f612018-04-19 13:10:01 -0700548 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chanb8e10c82015-10-14 11:24:40 -0700549 groupHandlerMap.clear();
Saurav Dasc6ff8f02018-04-23 18:42:12 -0700550 defaultRoutingHandler.shutdown();
Charles Chanb8e10c82015-10-14 11:24:40 -0700551
Saurav Das7bcbe702017-06-13 15:35:54 -0700552 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800553 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700554 portNextObjStore.destroy();
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700555 dummyVlanIdStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700556 tunnelStore.destroy();
557 policyStore.destroy();
Pier Luigib72201b2018-01-25 16:16:02 +0100558
559 mcastHandler.terminate();
sanghob35a6192015-04-01 13:05:26 -0700560 log.info("Stopped");
561 }
562
Charles Chan47933752017-11-30 15:37:50 -0800563 @Modified
564 private void modified(ComponentContext context) {
565 Dictionary<?, ?> properties = context.getProperties();
566 if (properties == null) {
567 return;
568 }
569
570 String strActiveProving = Tools.get(properties, "activeProbing");
571 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
572
573 if (expectActiveProbing != activeProbing) {
574 activeProbing = expectActiveProbing;
575 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
576 }
577 }
578
sangho1e575652015-05-14 00:39:53 -0700579 @Override
580 public List<Tunnel> getTunnels() {
581 return tunnelHandler.getTunnels();
582 }
583
584 @Override
sangho71abe1b2015-06-29 14:58:47 -0700585 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
586 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700587 }
588
589 @Override
sangho71abe1b2015-06-29 14:58:47 -0700590 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700591 for (Policy policy: policyHandler.getPolicies()) {
592 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
593 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
594 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
595 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700596 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700597 }
598 }
599 }
sangho71abe1b2015-06-29 14:58:47 -0700600 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700601 }
602
603 @Override
sangho71abe1b2015-06-29 14:58:47 -0700604 public PolicyHandler.Result removePolicy(Policy policy) {
605 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700606 }
607
608 @Override
sangho71abe1b2015-06-29 14:58:47 -0700609 public PolicyHandler.Result createPolicy(Policy policy) {
610 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700611 }
612
613 @Override
614 public List<Policy> getPolicies() {
615 return policyHandler.getPolicies();
616 }
617
Saurav Das59232cf2016-04-27 18:35:50 -0700618 @Override
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700619 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
620 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700621 }
622
623 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800624 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700625 return l2TunnelHandler.getL2Tunnels();
626 }
627
628 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800629 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700630 return l2TunnelHandler.getL2Policies();
631 }
632
Saurav Das5a356042018-04-06 20:16:01 -0700633 @Override
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700634 @Deprecated
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700635 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
636
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700637 // get both added and pending pseudowires
638 List<L2TunnelDescription> pseudowires = new ArrayList<>();
639 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
640 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700641 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700642
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700643 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
644
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700645 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
646 L2TunnelHandler.Result res;
647 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
648 res = addPseudowire(pw);
649 if (res != L2TunnelHandler.Result.SUCCESS) {
650 log.error("Pseudowire with id {} can not be instantiated !", res);
651 retRes = res;
652 }
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700653 }
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700654
655 return retRes;
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700656 }
657
658 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800659 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700660 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700661 }
662
663 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800664 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700665 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700666 }
667
668 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700669 public void rerouteNetwork() {
670 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700671 }
672
Charles Chanc81c45b2016-10-20 17:02:44 -0700673 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800674 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
675 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800676 deviceConfiguration.getRouters().forEach(device ->
677 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700678 return deviceSubnetMap;
679 }
680
Saurav Dasc88d4662017-05-15 15:34:25 -0700681
682 @Override
683 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
684 if (defaultRoutingHandler != null) {
685 return defaultRoutingHandler.getCurrentEmcpSpgMap();
686 } else {
687 return null;
688 }
689 }
690
691 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700692 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
693 if (dsNextObjStore != null) {
694 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700695 } else {
696 return ImmutableMap.of();
697 }
698 }
699
Saurav Dasceccf242017-08-03 18:30:35 -0700700 @Override
701 public void verifyGroups(DeviceId id) {
702 DefaultGroupHandler gh = groupHandlerMap.get(id);
703 if (gh != null) {
704 gh.triggerBucketCorrector();
705 }
706 }
707
Saurav Dasc568c342018-01-25 09:49:01 -0800708 @Override
709 public ImmutableMap<Link, Boolean> getSeenLinks() {
710 return linkHandler.getSeenLinks();
711 }
712
713 @Override
714 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
715 return linkHandler.getDownedPorts();
716 }
717
Pier Luigib29144d2018-01-15 18:06:43 +0100718 @Override
719 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
720 return mcastHandler.getMcastNextIds(mcastIp);
721 }
722
723 @Override
Pier Luigi96fe0772018-02-28 12:10:50 +0100724 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigib29144d2018-01-15 18:06:43 +0100725 return mcastHandler.getMcastRoles(mcastIp);
726 }
727
728 @Override
729 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
730 return mcastHandler.getMcastPaths(mcastIp);
731 }
732
Pier96f63cb2018-04-17 16:29:56 +0200733 @Override
Pierb1fe7382018-04-17 17:25:22 +0200734 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
735 ConnectPoint sourcecp) {
736 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
737 }
738
739 @Override
Pier96f63cb2018-04-17 16:29:56 +0200740 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
741 return mcastHandler.getMcastLeaders(mcastIp);
742 }
743
Charles Chan8bc75ee2018-04-17 18:56:53 -0700744 @Override
745 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
746 return defaultRoutingHandler == null ? ImmutableMap.of() :
747 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
748 }
749
750 @Override
751 public Map<DeviceId, Boolean> getShouldProgramCache() {
752 return defaultRoutingHandler == null ? ImmutableMap.of() :
753 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
754 }
755
sanghof9d0bf12015-05-19 11:57:42 -0700756 /**
Ray Milkeye4afdb52017-04-05 09:42:04 -0700757 * Extracts the application ID from the manager.
758 *
759 * @return application ID
760 */
761 public ApplicationId appId() {
762 return appId;
763 }
764
765 /**
766 * Returns the device configuration.
767 *
768 * @return device configuration
769 */
770 public DeviceConfiguration deviceConfiguration() {
771 return deviceConfiguration;
772 }
773
774 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700775 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700776 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700777 *
778 * @return next objective ID store
779 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700780 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
781 dsNextObjStore() {
782 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700783 }
784
785 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700786 * Per device next objective ID store with (device id + vlanid) as key.
787 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700788 *
789 * @return vlan next object store
790 */
791 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
792 return vlanNextObjStore;
793 }
794
795 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700796 * Per device next objective ID store with (device id + port + treatment + meta) as key.
797 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700798 *
799 * @return port next object store.
800 */
801 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
802 return portNextObjStore;
803 }
804
805 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700806 * Per port dummy VLAN ID store with (connect point + ip address) as key.
807 * Used to keep track on dummy VLAN ID allocation.
808 *
809 * @return dummy vlan id store.
810 */
811 public EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId> dummyVlanIdStore() {
812 return dummyVlanIdStore;
813 }
814
815 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700816 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
817 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700818 *
819 * @return MPLS-ECMP value
820 */
821 public boolean getMplsEcmp() {
822 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
823 .getConfig(this.appId, SegmentRoutingAppConfig.class);
824 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
825 }
826
827 /**
sanghof9d0bf12015-05-19 11:57:42 -0700828 * Returns the tunnel object with the tunnel ID.
829 *
830 * @param tunnelId Tunnel ID
831 * @return Tunnel reference
832 */
sangho1e575652015-05-14 00:39:53 -0700833 public Tunnel getTunnel(String tunnelId) {
834 return tunnelHandler.getTunnel(tunnelId);
835 }
836
Charles Chan7ffd81f2017-02-08 15:52:08 -0800837 // TODO Consider moving these to InterfaceService
sanghob35a6192015-04-01 13:05:26 -0700838 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800839 * Returns untagged VLAN configured on given connect point.
Charles Chan7ffd81f2017-02-08 15:52:08 -0800840 * <p>
841 * Only returns the first match if there are multiple untagged VLAN configured
842 * on the connect point.
sanghob35a6192015-04-01 13:05:26 -0700843 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800844 * @param connectPoint connect point
845 * @return untagged VLAN or null if not configured
sanghob35a6192015-04-01 13:05:26 -0700846 */
Charles Chan65238242017-06-22 18:03:14 -0700847 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800848 return interfaceService.getInterfacesByPort(connectPoint).stream()
849 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
850 .map(Interface::vlanUntagged)
851 .findFirst().orElse(null);
sanghob35a6192015-04-01 13:05:26 -0700852 }
853
sangho1e575652015-05-14 00:39:53 -0700854 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800855 * Returns tagged VLAN configured on given connect point.
856 * <p>
857 * Returns all matches if there are multiple tagged VLAN configured
858 * on the connect point.
859 *
860 * @param connectPoint connect point
861 * @return tagged VLAN or empty set if not configured
862 */
Charles Chan65238242017-06-22 18:03:14 -0700863 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800864 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
865 return interfaces.stream()
866 .map(Interface::vlanTagged)
Charles Chan65238242017-06-22 18:03:14 -0700867 .flatMap(Set::stream)
Charles Chan7ffd81f2017-02-08 15:52:08 -0800868 .collect(Collectors.toSet());
869 }
870
871 /**
872 * Returns native VLAN configured on given connect point.
873 * <p>
874 * Only returns the first match if there are multiple native VLAN configured
875 * on the connect point.
876 *
877 * @param connectPoint connect point
878 * @return native VLAN or null if not configured
879 */
Charles Chan65238242017-06-22 18:03:14 -0700880 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800881 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
882 return interfaces.stream()
883 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
884 .map(Interface::vlanNative)
885 .findFirst()
886 .orElse(null);
887 }
888
889 /**
Charles Chanf9a52702017-06-16 15:19:24 -0700890 * Returns internal VLAN for untagged hosts on given connect point.
891 * <p>
892 * The internal VLAN is either vlan-untagged for an access port,
893 * or vlan-native for a trunk port.
894 *
895 * @param connectPoint connect point
896 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
897 */
Pier Luigi96fe0772018-02-28 12:10:50 +0100898 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chanf9a52702017-06-16 15:19:24 -0700899 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
900 VlanId nativeVlanId = getNativeVlanId(connectPoint);
901 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
902 }
903
904 /**
Charles Chan65238242017-06-22 18:03:14 -0700905 * Returns optional pair device ID of given device.
906 *
907 * @param deviceId device ID
908 * @return optional pair device ID. Might be empty if pair device is not configured
909 */
910 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
911 SegmentRoutingDeviceConfig deviceConfig =
912 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
913 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
914 }
915 /**
916 * Returns optional pair device local port of given device.
917 *
918 * @param deviceId device ID
919 * @return optional pair device ID. Might be empty if pair device is not configured
920 */
921 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
922 SegmentRoutingDeviceConfig deviceConfig =
923 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
924 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
925 }
926
927 /**
Charles Chan9640c812017-08-23 13:55:39 -0700928 * Returns locations of given resolved route.
929 *
930 * @param resolvedRoute resolved route
931 * @return locations of nexthop. Might be empty if next hop is not found
932 */
933 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
934 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
935 return Optional.ofNullable(hostService.getHost(hostId))
936 .map(Host::locations).orElse(Sets.newHashSet())
937 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
938 }
939
940 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800941 * Returns vlan port map of given device.
942 *
943 * @param deviceId device id
944 * @return vlan-port multimap
945 */
946 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
947 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
948
949 interfaceService.getInterfaces().stream()
950 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
951 .forEach(intf -> {
952 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -0700953 intf.vlanTagged().forEach(vlanTagged ->
954 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
955 );
Charles Chan7ffd81f2017-02-08 15:52:08 -0800956 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
957 });
958 vlanPortMap.removeAll(VlanId.NONE);
959
960 return vlanPortMap;
961 }
962
963 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800964 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800965 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700966 *
967 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800968 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800969 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700970 */
Charles Chan65238242017-06-22 18:03:14 -0700971 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700972 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800973 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
974 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700975 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800976 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800977 + "device {} not found", deviceId);
978 return -1;
979 }
980 }
981
982 /**
983 * Returns the next objective ID for the given portNumber, given the treatment.
984 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700985 * would result in different objectives. If the next object does not exist,
986 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800987 *
988 * @param deviceId Device ID
989 * @param portNum port number on device for which NextObjective is queried
990 * @param treatment the actions to apply on the packets (should include outport)
991 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700992 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700993 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800994 */
995 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
996 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700997 TrafficSelector meta,
998 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -0800999 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1000 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -07001001 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -08001002 } else {
Charles Chane849c192016-01-11 18:28:54 -08001003 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
1004 + " not found", deviceId);
1005 return -1;
1006 }
1007 }
1008
Saurav Dasc88d4662017-05-15 15:34:25 -07001009 /**
1010 * Returns the group handler object for the specified device id.
1011 *
1012 * @param devId the device identifier
1013 * @return the groupHandler object for the device id, or null if not found
1014 */
Charles Chan65238242017-06-22 18:03:14 -07001015 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001016 return groupHandlerMap.get(devId);
1017 }
1018
1019 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001020 * Returns the default routing handler object.
1021 *
1022 * @return the default routing handler object
1023 */
1024 public DefaultRoutingHandler getRoutingHandler() {
1025 return defaultRoutingHandler;
1026 }
1027
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001028 /**
1029 * Returns new dummy VLAN ID.
1030 * Dummy VLAN ID should be unique in each connect point.
1031 *
1032 * @param cp connect point
1033 * @param ipAddress IP address
1034 * @return new dummy VLAN ID. Returns VlanId.NONE if no VLAN ID is available.
1035 */
1036 public synchronized VlanId allocateDummyVlanId(ConnectPoint cp, IpAddress ipAddress) {
1037 Set<VlanId> usedVlanId = Sets.union(getVlanPortMap(cp.deviceId()).keySet(),
1038 dummyVlanIdStore.entrySet().stream().filter(entry ->
1039 (entry.getKey()).connectPoint().equals(cp))
1040 .map(Map.Entry::getValue)
1041 .collect(Collectors.toSet()));
1042
1043 VlanId dummyVlanId = IntStream.range(MIN_DUMMY_VLAN_ID, MAX_DUMMY_VLAN_ID).mapToObj(
1044 i -> VlanId.vlanId((short) (i & 0xFFFF))
1045 ).filter(vlanId -> !usedVlanId.contains(vlanId)).findFirst().orElse(VlanId.NONE);
1046
1047 if (!dummyVlanId.equals(VlanId.NONE)) {
1048 this.dummyVlanIdStore.put(new DummyVlanIdStoreKey(cp, ipAddress), dummyVlanId);
1049 log.debug("Dummy VLAN ID {} is allocated to {}, {}", dummyVlanId, cp, ipAddress);
1050 } else {
1051 log.error("Failed to allocate dummy VLAN ID for {}, {}", cp, ipAddress);
1052 }
1053 return dummyVlanId;
1054 }
1055
1056
sanghob35a6192015-04-01 13:05:26 -07001057 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -07001058 @Override
1059 public void process(PacketContext context) {
1060
1061 if (context.isHandled()) {
1062 return;
1063 }
1064
1065 InboundPacket pkt = context.inPacket();
1066 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -08001067
1068 if (ethernet == null) {
1069 return;
1070 }
1071
Saurav Das7bcbe702017-06-13 15:35:54 -07001072 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1073 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001074 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001075 log.warn("Received unexpected ARP packet on {}",
1076 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -07001077 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -08001078 return;
sanghob35a6192015-04-01 13:05:26 -07001079 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -08001080 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1081 //ipHandler.addToPacketBuffer(ipv4Packet);
1082 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1083 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001084 } else {
Charles Chan50035632017-01-13 17:20:44 -08001085 // NOTE: We don't support IP learning at this moment so this
1086 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -08001087 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001088 }
Pier Ventre10bd8d12016-11-26 21:05:22 -08001089 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1090 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -08001091 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -08001092 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -08001093 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1094 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1095 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1096 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1097 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1098 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -07001099 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -07001100 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -08001101 }
1102 } else {
1103 // NOTE: We don't support IP learning at this moment so this
1104 // is not necessary. Also it causes duplication of DHCPv6 packets.
1105 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1106 }
sanghob35a6192015-04-01 13:05:26 -07001107 }
1108 }
1109 }
1110
sanghob35a6192015-04-01 13:05:26 -07001111 private class InternalEventHandler implements Runnable {
Charles Chan9b7217c2018-04-05 16:31:15 -07001112 private Event event;
1113
1114 InternalEventHandler(Event event) {
1115 this.event = event;
1116 }
1117
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001118 @Override
sanghob35a6192015-04-01 13:05:26 -07001119 public void run() {
Charles Chan9b7217c2018-04-05 16:31:15 -07001120 try {
1121 // TODO We should also change SR routing and PW to listen to TopologyEvents
1122 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1123 event.type() == LinkEvent.Type.LINK_UPDATED) {
1124 linkHandler.processLinkAdded((Link) event.subject());
1125 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1126 linkHandler.processLinkRemoved((Link) event.subject());
1127 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1128 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1129 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1130 DeviceId deviceId = ((Device) event.subject()).id();
1131 if (deviceService.isAvailable(deviceId)) {
1132 log.info("** DEVICE UP Processing device event {} "
1133 + "for available device {}",
1134 event.type(), ((Device) event.subject()).id());
1135 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001136 } else {
Charles Chan9b7217c2018-04-05 16:31:15 -07001137 log.info(" ** DEVICE DOWN Processing device event {}"
1138 + " for unavailable device {}",
1139 event.type(), ((Device) event.subject()).id());
1140 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001141 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001142 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1143 // typically these calls come when device is added first time
1144 // so port filtering rules are handled at the device_added event.
1145 // port added calls represent all ports on the device,
1146 // enabled or not.
1147 log.trace("** PORT ADDED {}/{} -> {}",
1148 ((DeviceEvent) event).subject().id(),
1149 ((DeviceEvent) event).port().number(),
1150 event.type());
1151 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1152 // these calls happen for every subsequent event
1153 // ports enabled, disabled, switch goes away, comes back
1154 log.info("** PORT UPDATED {}/{} -> {}",
1155 event.subject(),
1156 ((DeviceEvent) event).port(),
1157 event.type());
1158 processPortUpdated(((Device) event.subject()),
1159 ((DeviceEvent) event).port());
1160 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1161 // Process topology event, needed for all modules relying on
1162 // topology service for path computation
1163 TopologyEvent topologyEvent = (TopologyEvent) event;
1164 log.info("Processing topology event {}, topology age {}, reasons {}",
1165 event.type(), topologyEvent.subject().time(),
1166 topologyEvent.reasons().size());
1167 topologyHandler.processTopologyChange(topologyEvent.reasons());
1168 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1169 hostHandler.processHostAddedEvent((HostEvent) event);
1170 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1171 hostHandler.processHostMovedEvent((HostEvent) event);
1172 routeHandler.processHostMovedEvent((HostEvent) event);
1173 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1174 hostHandler.processHostRemovedEvent((HostEvent) event);
1175 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1176 hostHandler.processHostUpdatedEvent((HostEvent) event);
1177 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1178 routeHandler.processRouteAdded((RouteEvent) event);
1179 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1180 routeHandler.processRouteUpdated((RouteEvent) event);
1181 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1182 routeHandler.processRouteRemoved((RouteEvent) event);
1183 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1184 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1185 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1186 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1187 event.type() == McastEvent.Type.SINKS_ADDED ||
1188 event.type() == McastEvent.Type.SINKS_REMOVED ||
1189 event.type() == McastEvent.Type.ROUTE_ADDED ||
1190 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1191 mcastHandler.processMcastEvent((McastEvent) event);
1192 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1193 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1194 Class configClass = netcfgEvent.configClass();
1195 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1196 appCfgHandler.processAppConfigAdded(netcfgEvent);
1197 log.info("App config event .. configuring network");
1198 cfgListener.configureNetwork();
1199 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1200 log.info("Segment Routing Device Config added for {}", event.subject());
1201 cfgListener.configureNetwork();
1202 } else if (configClass.equals(XConnectConfig.class)) {
1203 xConnectHandler.processXConnectConfigAdded(netcfgEvent);
1204 } else if (configClass.equals(InterfaceConfig.class)) {
1205 log.info("Interface Config added for {}", event.subject());
1206 cfgListener.configureNetwork();
1207 } else {
1208 log.error("Unhandled config class: {}", configClass);
1209 }
1210 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1211 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1212 Class configClass = netcfgEvent.configClass();
1213 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1214 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1215 log.info("App config event .. configuring network");
1216 cfgListener.configureNetwork();
1217 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1218 log.info("Segment Routing Device Config updated for {}", event.subject());
1219 createOrUpdateDeviceConfiguration();
1220 } else if (configClass.equals(XConnectConfig.class)) {
1221 xConnectHandler.processXConnectConfigUpdated(netcfgEvent);
1222 } else if (configClass.equals(InterfaceConfig.class)) {
1223 log.info("Interface Config updated for {}", event.subject());
1224 createOrUpdateDeviceConfiguration();
1225 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1226 (InterfaceConfig) netcfgEvent.prevConfig().get());
1227 } else {
1228 log.error("Unhandled config class: {}", configClass);
1229 }
1230 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1231 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1232 Class configClass = netcfgEvent.configClass();
1233 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1234 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1235 log.info("App config event .. configuring network");
1236 cfgListener.configureNetwork();
1237 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1238 // TODO Handle sr device config removal
1239 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
1240 } else if (configClass.equals(XConnectConfig.class)) {
1241 xConnectHandler.processXConnectConfigRemoved(netcfgEvent);
1242 } else if (configClass.equals(InterfaceConfig.class)) {
1243 // TODO Handle interface removal
1244 log.info("InterfaceConfig removal is not handled in current implementation");
1245 } else {
1246 log.error("Unhandled config class: {}", configClass);
1247 }
Saurav Das201762d2018-04-21 17:19:48 -07001248 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1249 MastershipEvent me = (MastershipEvent) event;
1250 DeviceId deviceId = me.subject();
1251 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1252 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1253 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1254 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1255 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1256 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan9b7217c2018-04-05 16:31:15 -07001257 } else {
1258 log.warn("Unhandled event type: {}", event.type());
sanghob35a6192015-04-01 13:05:26 -07001259 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001260 } catch (Exception e) {
1261 log.error("SegmentRouting event handler thread thrown an exception: {}",
1262 e.getMessage(), e);
sanghob35a6192015-04-01 13:05:26 -07001263 }
sanghob35a6192015-04-01 13:05:26 -07001264 }
1265 }
1266
Saurav Das45f48152018-01-18 12:07:33 -08001267 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001268 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001269
1270 // NOTE: Punt ARP/NDP even when the device is not configured.
1271 // Host learning without network config is required for CORD config generator.
1272 routingRulePopulator.populateIpPunts(device.id());
1273 routingRulePopulator.populateArpNdpPunts(device.id());
1274
Charles Chan0b4e6182015-11-03 10:42:14 -08001275 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001276 log.warn("Device configuration unavailable. Device {} will be "
1277 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001278 return;
1279 }
Charles Chan2199c302016-04-23 17:36:10 -07001280 processDeviceAddedInternal(device.id());
1281 }
1282
1283 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001284 // Irrespective of whether the local is a MASTER or not for this device,
1285 // we need to create a SR-group-handler instance. This is because in a
1286 // multi-instance setup, any instance can initiate forwarding/next-objectives
1287 // for any switch (even if this instance is a SLAVE or not even connected
1288 // to the switch). To handle this, a default-group-handler instance is necessary
1289 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001290 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1291 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001292 DefaultGroupHandler groupHandler;
1293 try {
1294 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001295 createGroupHandler(deviceId,
1296 appId,
1297 deviceConfiguration,
1298 linkService,
1299 flowObjectiveService,
1300 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001301 } catch (DeviceConfigNotFoundException e) {
1302 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1303 return;
1304 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001305 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001306 deviceId);
1307 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001308 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001309
Charles Chan2199c302016-04-23 17:36:10 -07001310 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001311 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001312 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001313 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001314 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001315 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001316 }
Charles Chan5270ed02016-01-30 23:22:37 -08001317
Charles Chan03a73e02016-10-24 14:52:01 -07001318 appCfgHandler.init(deviceId);
Charles Chan2ff1bac2018-03-29 16:03:41 -07001319 hostHandler.init(deviceId);
Charles Chan03a73e02016-10-24 14:52:01 -07001320 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001321 }
1322
Saurav Das80980c72016-03-23 11:22:49 -07001323 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001324 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001325 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan47933752017-11-30 15:37:50 -08001326 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001327 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001328 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001329 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001330 portNextObjStore.entrySet().stream()
1331 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001332 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Das45f48152018-01-18 12:07:33 -08001333 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001334
Saurav Dasceccf242017-08-03 18:30:35 -07001335 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1336 if (gh != null) {
1337 gh.shutdown();
1338 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001339 // Note that a switch going down is associated with all of its links
1340 // going down as well, but it is treated as a single switch down event
Saurav Das5a356042018-04-06 20:16:01 -07001341 // while the link-downs are ignored. We cannot rely on the ordering of
1342 // events - i.e we cannot expect all link-downs to come before the
1343 // switch down - so we purge all seen-links for the switch before
1344 // handling route-path changes for the switch-down
Saurav Dasc88d4662017-05-15 15:34:25 -07001345 defaultRoutingHandler
Saurav Das604ab3a2018-03-18 21:28:15 -07001346 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Dasc568c342018-01-25 09:49:01 -08001347 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001348 xConnectHandler.removeDevice(device.id());
Saurav Dasa4020382018-02-14 14:14:54 -08001349
1350 // Cleanup all internal groupHandler stores for this device. Should be
1351 // done after all rerouting or rehashing has been completed
1352 groupHandlerMap.entrySet()
1353 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Das80980c72016-03-23 11:22:49 -07001354 }
1355
Saurav Dasc6ff8f02018-04-23 18:42:12 -07001356 /**
1357 * Purge the destinationSet nextObjective store of entries with this device
1358 * as key. Erases app-level knowledge of hashed groups in this device.
1359 *
1360 * @param devId the device identifier
1361 */
Saurav Das201762d2018-04-21 17:19:48 -07001362 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Dasc6ff8f02018-04-23 18:42:12 -07001363 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Das201762d2018-04-21 17:19:48 -07001364 dsNextObjStore.entrySet().stream()
1365 .filter(entry -> entry.getKey().deviceId().equals(devId))
1366 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1367 }
1368
Saurav Das1a129a02016-11-18 15:21:57 -08001369 private void processPortUpdated(Device device, Port port) {
1370 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1371 log.warn("Device configuration uploading. Not handling port event for"
1372 + "dev: {} port: {}", device.id(), port.number());
1373 return;
1374 }
Saurav Das018605f2017-02-18 14:05:44 -08001375
1376 if (!mastershipService.isLocalMaster(device.id())) {
1377 log.debug("Not master for dev:{} .. not handling port updated event"
1378 + "for port {}", device.id(), port.number());
1379 return;
1380 }
1381
1382 // first we handle filtering rules associated with the port
1383 if (port.isEnabled()) {
1384 log.info("Switchport {}/{} enabled..programming filters",
1385 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001386 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001387 } else {
1388 log.info("Switchport {}/{} disabled..removing filters",
1389 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001390 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001391 }
Saurav Das1a129a02016-11-18 15:21:57 -08001392
1393 // portUpdated calls are for ports that have gone down or up. For switch
1394 // to switch ports, link-events should take care of any re-routing or
1395 // group editing necessary for port up/down. Here we only process edge ports
1396 // that are already configured.
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001397 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1398 VlanId untaggedVlan = getUntaggedVlanId(cp);
1399 VlanId nativeVlan = getNativeVlanId(cp);
1400 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001401
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001402 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001403 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das1a129a02016-11-18 15:21:57 -08001404 + "dev/port: {}/{}", device.id(), port.number());
1405 return;
1406 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001407 if (untaggedVlan != null) {
1408 processEdgePort(device, port, untaggedVlan, true);
1409 }
1410 if (nativeVlan != null) {
1411 processEdgePort(device, port, nativeVlan, true);
1412 }
1413 if (!taggedVlans.isEmpty()) {
1414 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1415 }
Saurav Das1a129a02016-11-18 15:21:57 -08001416 }
1417
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001418 private void processEdgePort(Device device, Port port, VlanId vlanId,
1419 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001420 boolean portUp = port.isEnabled();
1421 if (portUp) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001422 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001423 port.number(), vlanId);
Charles Chan47933752017-11-30 15:37:50 -08001424 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Das1a129a02016-11-18 15:21:57 -08001425 } else {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001426 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001427 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001428 }
1429
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001430 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001431 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001432 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001433 } else {
1434 log.warn("Group handler not found for dev:{}. Not handling edge port"
1435 + " {} event for port:{}", device.id(),
1436 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001437 }
1438 }
sangho1e575652015-05-14 00:39:53 -07001439
Charles Chan27fe1a52017-10-20 16:06:55 -07001440 private void createOrUpdateDeviceConfiguration() {
1441 if (deviceConfiguration == null) {
Saurav Dase7f51012018-02-09 17:26:45 -08001442 log.info("Creating new DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001443 deviceConfiguration = new DeviceConfiguration(this);
1444 } else {
Saurav Dase7f51012018-02-09 17:26:45 -08001445 log.info("Updating DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001446 deviceConfiguration.updateConfig();
1447 }
1448 }
1449
Charles Chan50bb6ef2018-04-18 18:41:05 -07001450 private void createOrUpdateDefaultRoutingHandler() {
1451 if (defaultRoutingHandler == null) {
1452 log.info("Creating new DefaultRoutingHandler");
1453 defaultRoutingHandler = new DefaultRoutingHandler(this);
1454 } else {
1455 log.info("Updating DefaultRoutingHandler");
1456 defaultRoutingHandler.update(this);
1457 }
1458 }
1459
Pier Ventre10bd8d12016-11-26 21:05:22 -08001460 /**
1461 * Registers the given connect point with the NRS, this is necessary
1462 * to receive the NDP and ARP packets from the NRS.
1463 *
1464 * @param portToRegister connect point to register
1465 */
1466 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001467 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001468 portToRegister,
1469 neighbourHandler,
1470 appId
1471 );
1472 }
1473
Charles Chand6832882015-10-05 17:50:33 -07001474 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001475 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001476 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001477
Charles Chane849c192016-01-11 18:28:54 -08001478 /**
1479 * Constructs the internal network config listener.
1480 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001481 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001482 */
Charles Chan65238242017-06-22 18:03:14 -07001483 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001484 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001485 }
1486
Charles Chane849c192016-01-11 18:28:54 -08001487 /**
1488 * Reads network config and initializes related data structure accordingly.
1489 */
Charles Chan47933752017-11-30 15:37:50 -08001490 void configureNetwork() {
Saurav Dase7f51012018-02-09 17:26:45 -08001491 log.info("Configuring network ...");
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001492
1493 // Setting handling of network configuration events completable future
1494 // The completable future is needed because of the async behaviour of the configureNetwork,
1495 // listener registration and event arrival
1496 // Enables us to buffer the events and execute them when the configure network is done.
1497 networkConfigCompletion = new CompletableFuture<>();
1498
1499 // add a small delay to absorb multiple network config added notifications
1500 if (!programmingScheduled.get()) {
1501 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1502 programmingScheduled.set(true);
1503 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1504 }
1505
Charles Chan27fe1a52017-10-20 16:06:55 -07001506 createOrUpdateDeviceConfiguration();
Charles Chan4636be02015-10-07 14:21:45 -07001507
Charles Chan2c15aca2016-11-09 20:51:44 -08001508 arpHandler = new ArpHandler(srManager);
1509 icmpHandler = new IcmpHandler(srManager);
1510 ipHandler = new IpHandler(srManager);
1511 routingRulePopulator = new RoutingRulePopulator(srManager);
Charles Chan50bb6ef2018-04-18 18:41:05 -07001512 createOrUpdateDefaultRoutingHandler();
Charles Chan4636be02015-10-07 14:21:45 -07001513
1514 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1515 groupHandlerMap, tunnelStore);
1516 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1517 flowObjectiveService,
1518 tunnelHandler, policyStore);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001519 networkConfigCompletion.complete(true);
1520
Charles Chan2199c302016-04-23 17:36:10 -07001521 mcastHandler.init();
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001522
Charles Chan4636be02015-10-07 14:21:45 -07001523 }
1524
Charles Chand6832882015-10-05 17:50:33 -07001525 @Override
1526 public void event(NetworkConfigEvent event) {
Charles Chan7a888e82018-03-21 16:57:47 -07001527 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
1528 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1529 switch (event.type()) {
1530 case CONFIG_ADDED:
1531 case CONFIG_UPDATED:
1532 case CONFIG_REMOVED:
1533 log.trace("Schedule Network Config event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001534 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1535 mainEventExecutor.execute(new InternalEventHandler(event));
1536 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001537 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001538 }
Charles Chan7a888e82018-03-21 16:57:47 -07001539 break;
1540 default:
1541 break;
Charles Chand6832882015-10-05 17:50:33 -07001542 }
1543 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001544
Charles Chan50443e82018-01-03 16:26:32 -08001545 @Override
1546 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase7f51012018-02-09 17:26:45 -08001547 if (event.type() == CONFIG_REGISTERED ||
1548 event.type() == CONFIG_UNREGISTERED) {
1549 log.debug("Ignore event {} due to type mismatch", event);
1550 return false;
Charles Chan50443e82018-01-03 16:26:32 -08001551 }
Saurav Dase7f51012018-02-09 17:26:45 -08001552
1553 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1554 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1555 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscd339592018-02-23 14:18:00 -08001556 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase7f51012018-02-09 17:26:45 -08001557 log.debug("Ignore event {} due to class mismatch", event);
1558 return false;
1559 }
1560
1561 return true;
Charles Chan50443e82018-01-03 16:26:32 -08001562 }
1563
Saurav Das7bcbe702017-06-13 15:35:54 -07001564 private final class ConfigChange implements Runnable {
1565 @Override
1566 public void run() {
1567 programmingScheduled.set(false);
Saurav Dase7f51012018-02-09 17:26:45 -08001568 log.info("Reacting to config changes after buffer delay");
Saurav Das7bcbe702017-06-13 15:35:54 -07001569 for (Device device : deviceService.getDevices()) {
1570 processDeviceAdded(device);
1571 }
1572 defaultRoutingHandler.startPopulationProcess();
1573 }
1574 }
Charles Chand6832882015-10-05 17:50:33 -07001575 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001576
Charles Chan7a888e82018-03-21 16:57:47 -07001577 private class InternalLinkListener implements LinkListener {
1578 @Override
1579 public void event(LinkEvent event) {
1580 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1581 event.type() == LinkEvent.Type.LINK_UPDATED ||
1582 event.type() == LinkEvent.Type.LINK_REMOVED) {
1583 log.trace("Schedule Link event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001584 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1585 mainEventExecutor.execute(new InternalEventHandler(event));
1586 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001587 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001588 }
Charles Chan7a888e82018-03-21 16:57:47 -07001589 }
1590 }
1591 }
1592
1593 private class InternalDeviceListener implements DeviceListener {
1594 @Override
1595 public void event(DeviceEvent event) {
1596 switch (event.type()) {
1597 case DEVICE_ADDED:
1598 case PORT_UPDATED:
1599 case PORT_ADDED:
1600 case DEVICE_UPDATED:
1601 case DEVICE_AVAILABILITY_CHANGED:
1602 log.trace("Schedule Device event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001603 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1604 mainEventExecutor.execute(new InternalEventHandler(event));
1605 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001606 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001607 }
Charles Chan7a888e82018-03-21 16:57:47 -07001608 break;
1609 default:
1610 }
1611 }
1612 }
1613
1614 private class InternalTopologyListener implements TopologyListener {
1615 @Override
1616 public void event(TopologyEvent event) {
1617 switch (event.type()) {
1618 case TOPOLOGY_CHANGED:
1619 log.trace("Schedule Topology event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001620 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1621 mainEventExecutor.execute(new InternalEventHandler(event));
1622 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001623 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001624 }
Charles Chan7a888e82018-03-21 16:57:47 -07001625 break;
1626 default:
1627 }
1628 }
1629 }
1630
Charles Chan68aa62d2015-11-09 16:37:23 -08001631 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001632 @Override
1633 public void event(HostEvent event) {
Charles Chan68aa62d2015-11-09 16:37:23 -08001634 switch (event.type()) {
1635 case HOST_ADDED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001636 case HOST_MOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001637 case HOST_REMOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001638 case HOST_UPDATED:
Charles Chan7a888e82018-03-21 16:57:47 -07001639 log.trace("Schedule Host event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001640 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chan68aa62d2015-11-09 16:37:23 -08001641 break;
1642 default:
1643 log.warn("Unsupported host event type: {}", event.type());
1644 break;
1645 }
1646 }
1647 }
1648
Charles Chand55e84d2016-03-30 17:54:24 -07001649 private class InternalMcastListener implements McastListener {
1650 @Override
1651 public void event(McastEvent event) {
1652 switch (event.type()) {
Pier3ee24552018-03-14 16:47:32 -07001653 case SOURCES_ADDED:
1654 case SOURCES_REMOVED:
1655 case SINKS_ADDED:
1656 case SINKS_REMOVED:
Charles Chand55e84d2016-03-30 17:54:24 -07001657 case ROUTE_REMOVED:
Pier96f63cb2018-04-17 16:29:56 +02001658 case ROUTE_ADDED:
Charles Chan7a888e82018-03-21 16:57:47 -07001659 log.trace("Schedule Mcast event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001660 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi9930da52018-02-02 16:19:11 +01001661 break;
Charles Chand55e84d2016-03-30 17:54:24 -07001662 default:
Charles Chan7a888e82018-03-21 16:57:47 -07001663 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chand55e84d2016-03-30 17:54:24 -07001664 break;
1665 }
1666 }
1667 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001668
Charles Chan03a73e02016-10-24 14:52:01 -07001669 private class InternalRouteEventListener implements RouteListener {
1670 @Override
1671 public void event(RouteEvent event) {
1672 switch (event.type()) {
1673 case ROUTE_ADDED:
Charles Chan03a73e02016-10-24 14:52:01 -07001674 case ROUTE_UPDATED:
Charles Chan03a73e02016-10-24 14:52:01 -07001675 case ROUTE_REMOVED:
Charles Chan2fde6d42017-08-23 14:46:43 -07001676 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan7a888e82018-03-21 16:57:47 -07001677 log.trace("Schedule Route event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001678 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan2fde6d42017-08-23 14:46:43 -07001679 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001680 default:
Charles Chan7a888e82018-03-21 16:57:47 -07001681 log.warn("Unsupported route event type: {}", event.type());
Charles Chan03a73e02016-10-24 14:52:01 -07001682 break;
1683 }
1684 }
1685 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001686
Charles Chan50bb6ef2018-04-18 18:41:05 -07001687 private class InternalMastershipListener implements MastershipListener {
1688 @Override
1689 public void event(MastershipEvent event) {
Charles Chan50bb6ef2018-04-18 18:41:05 -07001690 switch (event.type()) {
Saurav Das201762d2018-04-21 17:19:48 -07001691 case MASTER_CHANGED:
1692 log.debug("Mastership event: {}/{}", event.subject(),
1693 event.roleInfo());
1694 mainEventExecutor.execute(new InternalEventHandler(event));
1695 break;
1696 case BACKUPS_CHANGED:
1697 case SUSPENDED:
1698 default:
1699 log.debug("Mastership event type {} not handled", event.type());
1700 break;
Charles Chan50bb6ef2018-04-18 18:41:05 -07001701 }
1702 }
1703 }
1704
Saurav Das201762d2018-04-21 17:19:48 -07001705 class InternalClusterListener implements ClusterEventListener {
1706 private Instant lastClusterEvent = Instant.EPOCH;
1707
1708 long timeSinceLastClusterEvent() {
1709 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
1710 }
1711
1712 @Override
1713 public void event(ClusterEvent event) {
1714 switch (event.type()) {
1715 case INSTANCE_ACTIVATED:
1716 case INSTANCE_ADDED:
1717 case INSTANCE_READY:
1718 log.debug("Cluster event {} ignored", event.type());
1719 break;
1720 case INSTANCE_DEACTIVATED:
1721 case INSTANCE_REMOVED:
1722 log.info("** Cluster event {}", event.type());
1723 lastClusterEvent = Instant.now();
1724 break;
1725 default:
1726 break;
1727 }
1728
1729 }
1730
1731 }
1732
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001733 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1734 try {
1735 Set<Interface> intfs = conf.getInterfaces();
1736 Set<Interface> prevIntfs = prevConf.getInterfaces();
1737
1738 // Now we only handle one interface config at each port.
1739 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1740 log.warn("Interface update aborted - one at a time is allowed, " +
1741 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1742 return;
1743 }
1744
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001745 //The system is in an incoherent state, abort
1746 if (defaultRoutingHandler == null) {
1747 log.warn("Interface update aborted, defaultRoutingHandler is null");
1748 return;
1749 }
1750
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001751 Interface intf = intfs.stream().findFirst().get();
1752 Interface prevIntf = prevIntfs.stream().findFirst().get();
1753
1754 DeviceId deviceId = intf.connectPoint().deviceId();
1755 PortNumber portNum = intf.connectPoint().port();
1756
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001757 removeSubnetConfig(prevIntf.connectPoint(),
1758 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1759 new HashSet<>(intf.ipAddressesList())));
1760
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001761 if (!prevIntf.vlanNative().equals(VlanId.NONE)
1762 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
1763 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1764 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
1765 // Update filtering objective and L2IG group bucket
1766 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
1767 } else {
1768 // RemoveVlanNative
1769 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1770 }
1771 }
1772
1773 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
1774 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1775 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1776 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
1777 // Update filtering objective and L2IG group bucket
1778 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
1779 } else {
1780 // RemoveVlanUntagged
1781 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1782 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001783 }
1784
1785 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1786 // RemoveVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001787 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
1788 .filter(i -> !intf.vlanUntagged().equals(i))
1789 .filter(i -> !intf.vlanNative().equals(i))
1790 .forEach(vlanId -> updateVlanConfigInternal(
1791 deviceId, portNum, vlanId, false, false));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001792 }
1793
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001794 if (!intf.vlanNative().equals(VlanId.NONE)
1795 && !prevIntf.vlanNative().equals(intf.vlanNative())
1796 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1797 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
1798 // Update filtering objective and L2IG group bucket
1799 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
1800 } else {
1801 // AddVlanNative
1802 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1803 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001804 }
1805
1806 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1807 // AddVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001808 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
1809 .filter(i -> !prevIntf.vlanUntagged().equals(i))
1810 .filter(i -> !prevIntf.vlanNative().equals(i))
1811 .forEach(vlanId -> updateVlanConfigInternal(
1812 deviceId, portNum, vlanId, false, true)
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001813 );
1814 }
1815
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001816 if (!intf.vlanUntagged().equals(VlanId.NONE)
1817 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1818 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
1819 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
1820 // Update filtering objective and L2IG group bucket
1821 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
1822 } else {
1823 // AddVlanUntagged
1824 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1825 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001826 }
1827 addSubnetConfig(prevIntf.connectPoint(),
1828 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1829 new HashSet<>(prevIntf.ipAddressesList())));
1830 } catch (ConfigException e) {
1831 log.error("Error in configuration");
1832 }
1833 }
1834
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001835 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
1836 VlanId vlanId, boolean pushVlan) {
1837 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1838 if (grpHandler == null) {
1839 log.warn("Failed to retrieve group handler for device {}", deviceId);
1840 return;
1841 }
1842
1843 // Update filtering objective for a single port
1844 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
1845 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
1846
1847 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
1848 // Update L2IG bucket of the port
1849 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
1850 } else {
1851 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1852 }
1853 }
1854
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001855 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1856 VlanId vlanId, boolean pushVlan, boolean install) {
1857 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1858 if (grpHandler == null) {
1859 log.warn("Failed to retrieve group handler for device {}", deviceId);
1860 return;
1861 }
1862
1863 // Update filtering objective for a single port
1864 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1865
1866 // Update filtering objective for multicast ingress port
1867 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1868
1869 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1870
1871 if (nextId != -1 && !install) {
1872 // Update next objective for a single port as an output port
1873 // Remove a single port from L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001874 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001875 // Remove L2 Bridging rule and L3 Unicast rule to the host
1876 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1877 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1878 // only if there is no port configured on that VLAN ID
1879 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1880 // Remove broadcast forwarding rule for the VLAN
1881 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1882 // Remove L2FG for VLAN
1883 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1884 } else {
1885 // Remove L2IG of the port
1886 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1887 }
1888 } else if (install) {
1889 if (nextId != -1) {
1890 // Add a single port to L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001891 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001892 } else {
1893 // Create L2FG for VLAN
1894 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1895 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1896 }
1897 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1898 } else {
1899 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1900 }
1901 }
1902
1903 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1904 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1905 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1906
1907 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1908 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1909 .filter(intf -> !intf.connectPoint().equals(cp))
1910 .flatMap(intf -> intf.ipAddressesList().stream())
1911 .collect(Collectors.toSet());
1912 // 1. Partial subnet population
1913 // Remove routing rules for removed subnet from previous configuration,
1914 // which does not also exist in other interfaces in the same device
1915 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1916 .map(InterfaceIpAddress::subnetAddress)
1917 .collect(Collectors.toSet());
1918
1919 defaultRoutingHandler.revokeSubnet(
1920 ipPrefixSet.stream()
1921 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1922 .collect(Collectors.toSet()));
1923
1924 // 2. Interface IP punts
1925 // Remove IP punts for old Intf address
1926 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1927 .map(InterfaceIpAddress::ipAddress)
1928 .collect(Collectors.toSet());
1929 ipAddressSet.stream()
1930 .map(InterfaceIpAddress::ipAddress)
1931 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1932 .forEach(interfaceIpAddress ->
1933 routingRulePopulator.revokeSingleIpPunts(
1934 cp.deviceId(), interfaceIpAddress));
1935
1936 // 3. Host unicast routing rule
1937 // Remove unicast routing rule
1938 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1939 }
1940
1941 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1942 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1943 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1944
1945 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1946 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1947 .filter(intf -> !intf.connectPoint().equals(cp))
1948 .flatMap(intf -> intf.ipAddressesList().stream())
1949 .collect(Collectors.toSet());
1950 // 1. Partial subnet population
1951 // Add routing rules for newly added subnet, which does not also exist in
1952 // other interfaces in the same device
1953 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1954 .map(InterfaceIpAddress::subnetAddress)
1955 .collect(Collectors.toSet());
1956
1957 defaultRoutingHandler.populateSubnet(
1958 Collections.singleton(cp),
1959 ipPrefixSet.stream()
1960 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1961 .collect(Collectors.toSet()));
1962
1963 // 2. Interface IP punts
1964 // Add IP punts for new Intf address
1965 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1966 .map(InterfaceIpAddress::ipAddress)
1967 .collect(Collectors.toSet());
1968 ipAddressSet.stream()
1969 .map(InterfaceIpAddress::ipAddress)
1970 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1971 .forEach(interfaceIpAddress ->
1972 routingRulePopulator.populateSingleIpPunts(
1973 cp.deviceId(), interfaceIpAddress));
1974
1975 // 3. Host unicast routing rule
1976 // Add unicast routing rule
1977 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1978 }
sanghob35a6192015-04-01 13:05:26 -07001979}