blob: 6f541c17ad808e711f965617656bedfdcadffdf0 [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.onlab.packet.Ethernet;
Pier Ventre735b8c82016-12-02 08:16:05 -080024import org.onlab.packet.ICMP6;
Charles Chanc42e84e2015-10-20 16:24:19 -070025import org.onlab.packet.IPv4;
Pier Ventre10bd8d12016-11-26 21:05:22 -080026import org.onlab.packet.IPv6;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070027import org.onlab.packet.IpAddress;
Charles Chanc42e84e2015-10-20 16:24:19 -070028import org.onlab.packet.IpPrefix;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070029import org.onlab.packet.VlanId;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070030import org.onlab.util.KryoNamespace;
Charles Chan47933752017-11-30 15:37:50 -080031import org.onlab.util.Tools;
Saurav Das80980c72016-03-23 11:22:49 -070032import org.onosproject.cfg.ComponentConfigService;
Saurav Das201762d2018-04-21 17:19:48 -070033import org.onosproject.cluster.ClusterEvent;
34import org.onosproject.cluster.ClusterEventListener;
Pier Luigieba73a02018-01-16 10:47:50 +010035import org.onosproject.cluster.ClusterService;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070036import org.onosproject.cluster.LeadershipService;
Pier96f63cb2018-04-17 16:29:56 +020037import org.onosproject.cluster.NodeId;
sanghob35a6192015-04-01 13:05:26 -070038import org.onosproject.core.ApplicationId;
39import org.onosproject.core.CoreService;
40import org.onosproject.event.Event;
Charles Chan50bb6ef2018-04-18 18:41:05 -070041import org.onosproject.mastership.MastershipEvent;
42import org.onosproject.mastership.MastershipListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070043import org.onosproject.mastership.MastershipService;
Pier3ee24552018-03-14 16:47:32 -070044import org.onosproject.mcast.api.McastEvent;
45import org.onosproject.mcast.api.McastListener;
46import org.onosproject.mcast.api.MulticastRouteService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080047import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070048import org.onosproject.net.Device;
49import org.onosproject.net.DeviceId;
Charles Chan9640c812017-08-23 13:55:39 -070050import org.onosproject.net.Host;
51import org.onosproject.net.HostId;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070052import org.onosproject.net.Link;
53import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080054import org.onosproject.net.PortNumber;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070055import org.onosproject.net.config.ConfigException;
Charles Chand6832882015-10-05 17:50:33 -070056import org.onosproject.net.config.ConfigFactory;
57import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070058import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070059import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070060import org.onosproject.net.config.basics.InterfaceConfig;
61import org.onosproject.net.config.basics.McastConfig;
Charles Chand6832882015-10-05 17:50:33 -070062import org.onosproject.net.config.basics.SubjectFactories;
Saurav Das45f48152018-01-18 12:07:33 -080063import org.onosproject.net.device.DeviceAdminService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070064import org.onosproject.net.device.DeviceEvent;
65import org.onosproject.net.device.DeviceListener;
66import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080067import org.onosproject.net.flow.TrafficSelector;
68import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070069import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chand5814aa2018-08-19 19:21:46 -070070import org.onosproject.net.flowobjective.NextObjective;
Charles Chan68aa62d2015-11-09 16:37:23 -080071import org.onosproject.net.host.HostEvent;
72import org.onosproject.net.host.HostListener;
Charles Chanff79dd92018-06-01 16:33:48 -070073import org.onosproject.net.host.HostProbingService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080074import org.onosproject.net.host.HostService;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070075import org.onosproject.net.host.InterfaceIpAddress;
Pier96f63cb2018-04-17 16:29:56 +020076import org.onosproject.net.intent.WorkPartitionService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070077import org.onosproject.net.intf.Interface;
78import org.onosproject.net.intf.InterfaceService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080079import org.onosproject.net.link.LinkEvent;
80import org.onosproject.net.link.LinkListener;
81import org.onosproject.net.link.LinkService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070082import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080083import org.onosproject.net.packet.InboundPacket;
84import org.onosproject.net.packet.PacketContext;
85import org.onosproject.net.packet.PacketProcessor;
86import org.onosproject.net.packet.PacketService;
Pier Luigi83f919b2018-02-15 16:33:08 +010087import org.onosproject.net.topology.TopologyEvent;
88import org.onosproject.net.topology.TopologyListener;
Charles Chand55e84d2016-03-30 17:54:24 -070089import org.onosproject.net.topology.TopologyService;
Charles Chan9640c812017-08-23 13:55:39 -070090import org.onosproject.routeservice.ResolvedRoute;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070091import org.onosproject.routeservice.RouteEvent;
92import org.onosproject.routeservice.RouteListener;
93import org.onosproject.routeservice.RouteService;
Charles Chand55e84d2016-03-30 17:54:24 -070094import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
95import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventref34966c2016-11-07 16:21:04 -080096import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070097import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -070098import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -070099import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700100import org.onosproject.segmentrouting.grouphandler.DestinationSet;
101import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Pier Luigi96fe0772018-02-28 12:10:50 +0100102import org.onosproject.segmentrouting.mcast.McastHandler;
103import org.onosproject.segmentrouting.mcast.McastRole;
Pier3e793752018-04-19 16:47:06 +0200104import org.onosproject.segmentrouting.mcast.McastRoleStoreKey;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700105import org.onosproject.segmentrouting.mcast.McastStoreKey;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700106import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700107import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800108import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700109import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800110import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700111import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700112import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800113import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Saurav Das7bcbe702017-06-13 15:35:54 -0700114import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700115import org.onosproject.segmentrouting.storekey.DummyVlanIdStoreKey;
Charles Chand2990362016-04-18 13:44:03 -0700116import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -0800117import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -0700118import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Charles Chanc7b3c452018-06-19 20:31:57 -0700119import org.onosproject.segmentrouting.xconnect.api.XconnectService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700120import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700121import org.onosproject.store.service.EventuallyConsistentMap;
122import org.onosproject.store.service.EventuallyConsistentMapBuilder;
123import org.onosproject.store.service.StorageService;
124import org.onosproject.store.service.WallClockTimestamp;
Charles Chan47933752017-11-30 15:37:50 -0800125import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700126import org.osgi.service.component.annotations.Activate;
127import org.osgi.service.component.annotations.Component;
128import org.osgi.service.component.annotations.Deactivate;
129import org.osgi.service.component.annotations.Modified;
130import org.osgi.service.component.annotations.Reference;
131import org.osgi.service.component.annotations.ReferenceCardinality;
Charles Chan8bdb2252019-02-27 11:46:32 -0800132import org.osgi.service.component.annotations.ReferencePolicy;
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;
Charles Chan07f15f22018-05-08 21:35:50 -0700148import java.util.concurrent.ExecutorService;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800149import java.util.concurrent.Executors;
150import java.util.concurrent.ScheduledExecutorService;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800151import java.util.concurrent.TimeUnit;
152import java.util.concurrent.atomic.AtomicBoolean;
153import java.util.stream.Collectors;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700154import java.util.stream.IntStream;
sanghob35a6192015-04-01 13:05:26 -0700155
Charles Chan3e783d02016-02-26 22:19:52 -0800156import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800157import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700158import static org.onlab.util.Tools.groupedThreads;
Saurav Dase7f51012018-02-09 17:26:45 -0800159import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
160import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Ray Milkey3101df62018-10-23 14:23:16 -0700161import static org.onosproject.segmentrouting.OsgiPropertyConstants.ACTIVE_PROBING_DEFAULT;
162import static org.onosproject.segmentrouting.OsgiPropertyConstants.DEFAULT_INTERNAL_VLAN_DEFAULT;
163import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ACTIVE_PROBING;
164import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_DEFAULT_INTERNAL_VLAN;
165import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_PW_TRANSPORT_VLAN;
166import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_RESPOND_TO_UNKNOWN_HOSTS;
167import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_DOUBLE_TAGGED_HOSTS;
168import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SINGLE_HOMED_DOWN;
Ray Milkeye52652c2018-11-02 17:12:21 -0700169import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SYMMETRIC_PROBING;
Ray Milkey3101df62018-10-23 14:23:16 -0700170import static org.onosproject.segmentrouting.OsgiPropertyConstants.PW_TRANSPORT_VLAN_DEFAULT;
171import static org.onosproject.segmentrouting.OsgiPropertyConstants.RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
172import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
173import static org.onosproject.segmentrouting.OsgiPropertyConstants.SINGLE_HOMED_DOWN_DEFAULT;
Ray Milkeye52652c2018-11-02 17:12:21 -0700174import static org.onosproject.segmentrouting.OsgiPropertyConstants.SYMMETRIC_PROBING_DEFAULT;
Charles Chan3e783d02016-02-26 22:19:52 -0800175
Charles Chane849c192016-01-11 18:28:54 -0800176/**
177 * Segment routing manager.
178 */
Ray Milkey3101df62018-10-23 14:23:16 -0700179@Component(
180 immediate = true,
181 service = SegmentRoutingService.class,
182 property = {
183 PROP_ACTIVE_PROBING + ":Boolean=" + ACTIVE_PROBING_DEFAULT,
184 PROP_SINGLE_HOMED_DOWN + ":Boolean=" + SINGLE_HOMED_DOWN_DEFAULT,
185 PROP_RESPOND_TO_UNKNOWN_HOSTS + ":Boolean=" + RESPOND_TO_UNKNOWN_HOSTS_DEFAULT,
186 PROP_ROUTE_DOUBLE_TAGGED_HOSTS + ":Boolean=" + ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT,
187 PROP_DEFAULT_INTERNAL_VLAN + ":Integer=" + DEFAULT_INTERNAL_VLAN_DEFAULT,
188 PROP_PW_TRANSPORT_VLAN + ":Integer=" + PW_TRANSPORT_VLAN_DEFAULT,
Ray Milkeye52652c2018-11-02 17:12:21 -0700189 PROP_SYMMETRIC_PROBING + ":Boolean=" + SYMMETRIC_PROBING_DEFAULT
Ray Milkey3101df62018-10-23 14:23:16 -0700190 }
191)
sangho1e575652015-05-14 00:39:53 -0700192public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700193
Charles Chan2c15aca2016-11-09 20:51:44 -0800194 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan2fde6d42017-08-23 14:46:43 -0700195 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sanghob35a6192015-04-01 13:05:26 -0700196
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700197 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan03a73e02016-10-24 14:52:01 -0700198 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700199
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700200 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800201 private NeighbourResolutionService neighbourResolutionService;
202
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700203 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100204 public CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700205
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700206 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan03a73e02016-10-24 14:52:01 -0700207 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700208
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700209 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan03a73e02016-10-24 14:52:01 -0700210 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700211
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700212 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chanff79dd92018-06-01 16:33:48 -0700213 HostProbingService probingService;
Charles Chan47933752017-11-30 15:37:50 -0800214
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700215 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100216 public DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700217
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700218 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Saurav Das45f48152018-01-18 12:07:33 -0800219 DeviceAdminService deviceAdminService;
220
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700221 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventre42287df2016-11-09 14:17:26 -0800222 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700223
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700224 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100225 public LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700226
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700227 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventre42287df2016-11-09 14:17:26 -0800228 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700229
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700230 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventre42287df2016-11-09 14:17:26 -0800231 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700232
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700233 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100234 public MulticastRouteService multicastRouteService;
Charles Chan03a73e02016-10-24 14:52:01 -0700235
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700236 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chanf237e1b2018-01-09 13:45:07 -0800237 public TopologyService topologyService;
Charles Chan03a73e02016-10-24 14:52:01 -0700238
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700239 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan03a73e02016-10-24 14:52:01 -0700240 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800241
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700242 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800243 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800244
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700245 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800246 public InterfaceService interfaceService;
247
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700248 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigieba73a02018-01-16 10:47:50 +0100249 public ClusterService clusterService;
250
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700251 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier96f63cb2018-04-17 16:29:56 +0200252 public WorkPartitionService workPartitionService;
Pier Luigieba73a02018-01-16 10:47:50 +0100253
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700254 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700255 public LeadershipService leadershipService;
256
Charles Chan8bdb2252019-02-27 11:46:32 -0800257 @Reference(cardinality = ReferenceCardinality.OPTIONAL,
258 bind = "bindXconnectService",
259 unbind = "unbindXconnectService",
260 policy = ReferencePolicy.DYNAMIC)
Charles Chanc7b3c452018-06-19 20:31:57 -0700261 public XconnectService xconnectService;
262
Ray Milkey3101df62018-10-23 14:23:16 -0700263 /** Enable active probing to discover dual-homed hosts. */
264 boolean activeProbing = ACTIVE_PROBING_DEFAULT;
Charles Chan47933752017-11-30 15:37:50 -0800265
Ray Milkeye52652c2018-11-02 17:12:21 -0700266 /** Enable only send probe on the same port number of the pair device. */
267 boolean symmetricProbing = SYMMETRIC_PROBING_DEFAULT;
Mayank Tiwaria61b0a82018-10-29 18:27:35 -0400268
Ray Milkey3101df62018-10-23 14:23:16 -0700269 /** Enable administratively taking down single-homed hosts. */
270 boolean singleHomedDown = SINGLE_HOMED_DOWN_DEFAULT;
Saurav Das9a554292018-04-27 18:42:30 -0700271
Ray Milkey3101df62018-10-23 14:23:16 -0700272 /** Enable this to respond to ARP/NDP requests from unknown hosts. */
273 boolean respondToUnknownHosts = RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
Andreas Pantelopoulosb2518012018-06-18 12:50:35 -0700274
Ray Milkey3101df62018-10-23 14:23:16 -0700275 /** Program flows and groups to pop and route double tagged hosts. */
276 boolean routeDoubleTaggedHosts = ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
Charles Chan57465d32018-07-19 14:55:31 -0700277
Ray Milkey3101df62018-10-23 14:23:16 -0700278 /** internal vlan assigned by default to unconfigured ports. */
279 private int defaultInternalVlan = DEFAULT_INTERNAL_VLAN_DEFAULT;
Saurav Das09c2c4d2018-08-13 15:34:26 -0700280
Ray Milkey3101df62018-10-23 14:23:16 -0700281 /** vlan used for transport of pseudowires between switches. */
282 private int pwTransportVlan = PW_TRANSPORT_VLAN_DEFAULT;
Saurav Das09c2c4d2018-08-13 15:34:26 -0700283
Charles Chan03a73e02016-10-24 14:52:01 -0700284 ArpHandler arpHandler = null;
285 IcmpHandler icmpHandler = null;
286 IpHandler ipHandler = null;
287 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700288 ApplicationId appId;
289 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700290
Charles Chan03a73e02016-10-24 14:52:01 -0700291 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700292 private TunnelHandler tunnelHandler = null;
293 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700294 private InternalPacketProcessor processor = null;
295 private InternalLinkListener linkListener = null;
296 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700297 private AppConfigHandler appCfgHandler = null;
Pier Luigi96fe0772018-02-28 12:10:50 +0100298 public XConnectHandler xConnectHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800299 McastHandler mcastHandler = null;
300 HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800301 private RouteHandler routeHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800302 LinkHandler linkHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800303 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800304 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigi83f919b2018-02-15 16:33:08 +0100305 private TopologyHandler topologyHandler = null;
Charles Chan5270ed02016-01-30 23:22:37 -0800306 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700307 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
308 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700309 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigi83f919b2018-02-15 16:33:08 +0100310 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chan50bb6ef2018-04-18 18:41:05 -0700311 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
Saurav Das201762d2018-04-21 17:19:48 -0700312 final InternalClusterListener clusterListener = new InternalClusterListener();
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200313 //Completable future for network configuration process to buffer config events handling during activation
314 private CompletableFuture<Boolean> networkConfigCompletion = null;
Ray Milkeyec9e4de2018-11-16 15:15:14 -0800315 private final Object networkConfigCompletionLock = new Object();
Charles Chan10b2fee2018-04-21 00:44:29 -0700316 private List<Event> queuedEvents = new CopyOnWriteArrayList<>();
sanghob35a6192015-04-01 13:05:26 -0700317
Charles Chan9b7217c2018-04-05 16:31:15 -0700318 // Handles device, link, topology and network config events
Charles Chan50bb6ef2018-04-18 18:41:05 -0700319 private ScheduledExecutorService mainEventExecutor;
sanghob35a6192015-04-01 13:05:26 -0700320
Charles Chan50bb6ef2018-04-18 18:41:05 -0700321 // Handles host, route and mcast events respectively
322 private ScheduledExecutorService hostEventExecutor;
323 private ScheduledExecutorService routeEventExecutor;
324 private ScheduledExecutorService mcastEventExecutor;
Charles Chan07f15f22018-05-08 21:35:50 -0700325 private ExecutorService packetExecutor;
Charles Chan9b7217c2018-04-05 16:31:15 -0700326
327 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chane849c192016-01-11 18:28:54 -0800328 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700329 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700330 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800331 */
Charles Chan47933752017-11-30 15:37:50 -0800332 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das7bcbe702017-06-13 15:35:54 -0700333 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800334 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700335 * Per device next objective ID store with (device id + vlanid) as key.
336 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800337 */
Charles Chan47933752017-11-30 15:37:50 -0800338 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800339 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800340 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700341 * Per device next objective ID store with (device id + port + treatment + meta) as key.
342 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chane849c192016-01-11 18:28:54 -0800343 */
Charles Chan47933752017-11-30 15:37:50 -0800344 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800345 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800346
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700347 /**
348 * Per port dummy VLAN ID store with (connect point + ip address) as key.
349 * Used to keep track on dummy VLAN ID allocation.
350 */
351 private EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId>
352 dummyVlanIdStore = null;
353
Saurav Das4ce45962015-11-24 23:21:05 -0800354 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
355 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700356
Saurav Das7bcbe702017-06-13 15:35:54 -0700357 private AtomicBoolean programmingScheduled = new AtomicBoolean();
358
Charles Chand55e84d2016-03-30 17:54:24 -0700359 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700360 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
361 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700362 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700363 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800364 public SegmentRoutingDeviceConfig createConfig() {
365 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700366 }
367 };
Pier Ventref34966c2016-11-07 16:21:04 -0800368
Charles Chand55e84d2016-03-30 17:54:24 -0700369 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700370 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
371 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700372 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800373 @Override
374 public SegmentRoutingAppConfig createConfig() {
375 return new SegmentRoutingAppConfig();
376 }
377 };
Pier Ventref34966c2016-11-07 16:21:04 -0800378
Charles Chanfc5c7802016-05-17 13:13:55 -0700379 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
380 new ConfigFactory<ApplicationId, XConnectConfig>(
381 SubjectFactories.APP_SUBJECT_FACTORY,
382 XConnectConfig.class, "xconnect") {
383 @Override
384 public XConnectConfig createConfig() {
385 return new XConnectConfig();
386 }
387 };
Pier Ventref34966c2016-11-07 16:21:04 -0800388
Charles Chand55e84d2016-03-30 17:54:24 -0700389 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700390 new ConfigFactory<ApplicationId, McastConfig>(
391 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700392 McastConfig.class, "multicast") {
393 @Override
394 public McastConfig createConfig() {
395 return new McastConfig();
396 }
397 };
398
Charles Chan116188d2016-02-18 14:22:42 -0800399 /**
400 * Segment Routing App ID.
401 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800402 public static final String APP_NAME = "org.onosproject.segmentrouting";
Saurav Das0e99e2b2015-10-28 12:39:42 -0700403
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700404 /**
405 * Minumum and maximum value of dummy VLAN ID to be allocated.
406 */
407 public static final int MIN_DUMMY_VLAN_ID = 2;
408 public static final int MAX_DUMMY_VLAN_ID = 4093;
409
Saurav Das9a554292018-04-27 18:42:30 -0700410 Instant lastEdgePortEvent = Instant.EPOCH;
411
Charles Chan8bdb2252019-02-27 11:46:32 -0800412 protected void bindXconnectService(XconnectService xconnectService) {
413 if (this.xconnectService == null) {
414 log.info("Binding XconnectService");
415 this.xconnectService = xconnectService;
416 } else {
417 log.warn("Trying to bind XconnectService but it is already bound");
418 }
419 }
420
421 protected void unbindXconnectService(XconnectService xconnectService) {
422 if (this.xconnectService == xconnectService) {
423 log.info("Unbinding XconnectService");
424 this.xconnectService = null;
425 } else {
426 log.warn("Trying to unbind XconnectService but it is already unbound");
427 }
428 }
429
sanghob35a6192015-04-01 13:05:26 -0700430 @Activate
Charles Chan47933752017-11-30 15:37:50 -0800431 protected void activate(ComponentContext context) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800432 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700433
Charles Chan50bb6ef2018-04-18 18:41:05 -0700434 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-main", "%d", log));
435 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-host", "%d", log));
436 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-route", "%d", log));
437 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-mcast", "%d", log));
Charles Chan07f15f22018-05-08 21:35:50 -0700438 packetExecutor = Executors.newSingleThreadExecutor(groupedThreads("sr-packet", "%d", log));
Charles Chan50bb6ef2018-04-18 18:41:05 -0700439
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700440 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700441 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700442 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700443 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700444 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700445 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700446 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700447 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700448 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700449
Charles Chan59cc16d2017-02-02 16:20:42 -0800450 log.debug("Creating EC map vlannextobjectivestore");
451 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
452 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
453 vlanNextObjStore = vlanNextObjMapBuilder
454 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700455 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700456 .withTimestampProvider((k, v) -> new WallClockTimestamp())
457 .build();
458
Saurav Das4ce45962015-11-24 23:21:05 -0800459 log.debug("Creating EC map subnetnextobjectivestore");
460 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
461 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
462 portNextObjStore = portNextObjMapBuilder
463 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700464 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800465 .withTimestampProvider((k, v) -> new WallClockTimestamp())
466 .build();
467
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700468 EventuallyConsistentMapBuilder<DummyVlanIdStoreKey, VlanId>
469 dummyVlanIdMapBuilder = storageService.eventuallyConsistentMapBuilder();
470 dummyVlanIdStore = dummyVlanIdMapBuilder
471 .withName("dummyvlanidstore")
472 .withSerializer(createSerializer())
473 .withTimestampProvider((k, v) -> new WallClockTimestamp())
474 .build();
475
sangho0b2b6d12015-05-20 22:16:38 -0700476 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
477 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700478 tunnelStore = tunnelMapBuilder
479 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700480 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700481 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700482 .build();
483
484 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
485 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700486 policyStore = policyMapBuilder
487 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700488 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700489 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700490 .build();
491
Saurav Das80980c72016-03-23 11:22:49 -0700492 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800493 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700494 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800495 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800496 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
497 "requestInterceptsEnabled", "false");
Charles Chand3baaba2017-08-08 15:13:37 -0700498 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800499 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700500 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800501 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800502 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
503 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800504 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
505 "forceUnprovision", "true");
Charles Chanef624ed2017-08-10 16:57:28 -0700506 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan73316522017-07-20 16:16:25 -0700507 "distributed", "true");
Charles Chan35a32322017-08-14 11:42:11 -0700508 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
509 "multihomingEnabled", "true");
Charles Chan807d87a2017-11-29 19:54:20 -0800510 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
511 "staleLinkAge", "15000");
512 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
513 "allowDuplicateIps", "false");
Yi Tsengfe13f3e2018-08-19 03:09:54 +0800514 // For P4 switches
515 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Charles Chanc76685f2018-08-30 17:30:45 -0700516 "fallbackFlowPollFrequency", "4");
Yi Tsengfe13f3e2018-08-19 03:09:54 +0800517 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Charles Chanc76685f2018-08-30 17:30:45 -0700518 "fallbackGroupPollFrequency", "3");
Charles Chan47933752017-11-30 15:37:50 -0800519 compCfgService.registerProperties(getClass());
520 modified(context);
Saurav Das80980c72016-03-23 11:22:49 -0700521
Charles Chanb8e10c82015-10-14 11:24:40 -0700522 processor = new InternalPacketProcessor();
523 linkListener = new InternalLinkListener();
524 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700525 appCfgHandler = new AppConfigHandler(this);
526 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700527 mcastHandler = new McastHandler(this);
528 hostHandler = new HostHandler(this);
Saurav Das45f48152018-01-18 12:07:33 -0800529 linkHandler = new LinkHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700530 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800531 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800532 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigi83f919b2018-02-15 16:33:08 +0100533 topologyHandler = new TopologyHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700534
Charles Chan3e783d02016-02-26 22:19:52 -0800535 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700536 cfgService.registerConfigFactory(deviceConfigFactory);
537 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700538 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700539 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase7f51012018-02-09 17:26:45 -0800540 log.info("Configuring network before adding listeners");
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200541
Charles Chan132393a2018-01-04 14:26:07 -0800542 cfgListener.configureNetwork();
543
Charles Chan5270ed02016-01-30 23:22:37 -0800544 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700545 packetService.addProcessor(processor, PacketProcessor.director(2));
546 linkService.addListener(linkListener);
547 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700548 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700549 routeService.addListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100550 topologyService.addListener(topologyListener);
Charles Chan50bb6ef2018-04-18 18:41:05 -0700551 mastershipService.addListener(mastershipListener);
Saurav Das201762d2018-04-21 17:19:48 -0700552 clusterService.addListener(clusterListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700553
Charles Chanb39777c2018-03-09 15:53:44 -0800554 linkHandler.init();
Andreas Pantelopoulos4a768c02018-01-11 07:53:48 -0800555 l2TunnelHandler.init();
556
Ray Milkeyec9e4de2018-11-16 15:15:14 -0800557 synchronized (networkConfigCompletionLock) {
558 networkConfigCompletion.whenComplete((value, ex) -> {
559 //setting to null for easier fall through
560 networkConfigCompletion = null;
561 //process all queued events
562 queuedEvents.forEach(event -> {
563 mainEventExecutor.execute(new InternalEventHandler(event));
564 });
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200565 });
Ray Milkeyec9e4de2018-11-16 15:15:14 -0800566 }
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200567
sanghob35a6192015-04-01 13:05:26 -0700568 log.info("Started");
569 }
570
Saurav Das45f48152018-01-18 12:07:33 -0800571 KryoNamespace.Builder createSerializer() {
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700572 return new KryoNamespace.Builder()
573 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700574 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800575 VlanNextObjectiveStoreKey.class,
576 DestinationSet.class,
Andreas Pantelopoulos67e09ae2018-05-29 13:11:14 -0700577 DestinationSet.DestinationSetType.class,
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800578 NextNeighbors.class,
579 Tunnel.class,
580 DefaultTunnel.class,
581 Policy.class,
582 TunnelPolicy.class,
583 Policy.Type.class,
584 PortNextObjectiveStoreKey.class,
585 XConnectStoreKey.class,
586 L2Tunnel.class,
587 L2TunnelPolicy.class,
588 DefaultL2Tunnel.class,
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700589 DefaultL2TunnelPolicy.class,
590 DummyVlanIdStoreKey.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700591 );
592 }
593
sanghob35a6192015-04-01 13:05:26 -0700594 @Deactivate
595 protected void deactivate() {
Charles Chan50bb6ef2018-04-18 18:41:05 -0700596 mainEventExecutor.shutdown();
597 hostEventExecutor.shutdown();
598 routeEventExecutor.shutdown();
599 mcastEventExecutor.shutdown();
Charles Chan07f15f22018-05-08 21:35:50 -0700600 packetExecutor.shutdown();
Charles Chan50bb6ef2018-04-18 18:41:05 -0700601
Ray Milkey749996b2018-05-11 09:59:19 -0700602 mainEventExecutor = null;
603 hostEventExecutor = null;
604 routeEventExecutor = null;
605 mcastEventExecutor = null;
606 packetExecutor = null;
607
Charles Chand6832882015-10-05 17:50:33 -0700608 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700609 cfgService.unregisterConfigFactory(deviceConfigFactory);
610 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700611 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700612 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan47933752017-11-30 15:37:50 -0800613 compCfgService.unregisterProperties(getClass(), false);
Charles Chand6832882015-10-05 17:50:33 -0700614
Charles Chanc7a8a682017-06-19 00:43:31 -0700615 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700616 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700617 linkService.removeListener(linkListener);
618 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700619 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700620 routeService.removeListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100621 topologyService.removeListener(topologyListener);
Charles Chan50bb6ef2018-04-18 18:41:05 -0700622 mastershipService.removeListener(mastershipListener);
Saurav Das201762d2018-04-21 17:19:48 -0700623 clusterService.removeListener(clusterListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700624
Charles Chan0aa674e2017-02-23 15:44:08 -0800625 neighbourResolutionService.unregisterNeighbourHandlers(appId);
626
sanghob35a6192015-04-01 13:05:26 -0700627 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700628 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700629 deviceListener = null;
Charles Chan6577f612018-04-19 13:10:01 -0700630 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chanb8e10c82015-10-14 11:24:40 -0700631 groupHandlerMap.clear();
Saurav Dasc6ff8f02018-04-23 18:42:12 -0700632 defaultRoutingHandler.shutdown();
Charles Chanb8e10c82015-10-14 11:24:40 -0700633
Saurav Das7bcbe702017-06-13 15:35:54 -0700634 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800635 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700636 portNextObjStore.destroy();
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700637 dummyVlanIdStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700638 tunnelStore.destroy();
639 policyStore.destroy();
Pier Luigib72201b2018-01-25 16:16:02 +0100640
641 mcastHandler.terminate();
sanghob35a6192015-04-01 13:05:26 -0700642 log.info("Stopped");
643 }
644
Charles Chan47933752017-11-30 15:37:50 -0800645 @Modified
646 private void modified(ComponentContext context) {
647 Dictionary<?, ?> properties = context.getProperties();
648 if (properties == null) {
649 return;
650 }
651
Ray Milkey88dd7e22018-10-24 10:04:03 -0700652 String strActiveProbing = Tools.get(properties, PROP_ACTIVE_PROBING);
Andreas Pantelopoulosb2518012018-06-18 12:50:35 -0700653 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProbing);
Charles Chan47933752017-11-30 15:37:50 -0800654 if (expectActiveProbing != activeProbing) {
655 activeProbing = expectActiveProbing;
656 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
657 }
Saurav Das9a554292018-04-27 18:42:30 -0700658
Ray Milkeye52652c2018-11-02 17:12:21 -0700659
660 String strSymmetricProbing = Tools.get(properties, PROP_SYMMETRIC_PROBING);
661 boolean expectSymmetricProbing = Boolean.parseBoolean(strSymmetricProbing);
Mayank Tiwaria61b0a82018-10-29 18:27:35 -0400662 if (expectSymmetricProbing != symmetricProbing) {
663 symmetricProbing = expectSymmetricProbing;
664 log.info("{} symmetric probing", symmetricProbing ? "Enabling" : "Disabling");
665 }
666
Ray Milkey88dd7e22018-10-24 10:04:03 -0700667 String strSingleHomedDown = Tools.get(properties, PROP_SINGLE_HOMED_DOWN);
Saurav Das9a554292018-04-27 18:42:30 -0700668 boolean expectSingleHomedDown = Boolean.parseBoolean(strSingleHomedDown);
669 if (expectSingleHomedDown != singleHomedDown) {
670 singleHomedDown = expectSingleHomedDown;
671 log.info("{} downing of single homed hosts for lost uplinks",
672 singleHomedDown ? "Enabling" : "Disabling");
673 if (singleHomedDown && linkHandler != null) {
674 hostService.getHosts().forEach(host -> host.locations()
675 .forEach(loc -> {
676 if (interfaceService.isConfigured(loc)) {
677 linkHandler.checkUplinksForHost(loc);
678 }
679 }));
680 } else {
681 log.warn("Disabling singleHomedDown does not re-enable already "
682 + "downed ports for single-homed hosts");
683 }
684 }
Andreas Pantelopoulosb2518012018-06-18 12:50:35 -0700685
Ray Milkey88dd7e22018-10-24 10:04:03 -0700686 String strRespondToUnknownHosts = Tools.get(properties, PROP_RESPOND_TO_UNKNOWN_HOSTS);
Andreas Pantelopoulosb2518012018-06-18 12:50:35 -0700687 boolean expectRespondToUnknownHosts = Boolean.parseBoolean(strRespondToUnknownHosts);
688 if (expectRespondToUnknownHosts != respondToUnknownHosts) {
689 respondToUnknownHosts = expectRespondToUnknownHosts;
690 log.info("{} responding to ARPs/NDPs from unknown hosts", respondToUnknownHosts ? "Enabling" : "Disabling");
691 }
Charles Chan57465d32018-07-19 14:55:31 -0700692
Ray Milkey88dd7e22018-10-24 10:04:03 -0700693 String strRouteDoubleTaggedHosts = Tools.get(properties, PROP_ROUTE_DOUBLE_TAGGED_HOSTS);
Charles Chanb67dfdd2018-07-24 16:40:35 -0700694 boolean expectRouteDoubleTaggedHosts = Boolean.parseBoolean(strRouteDoubleTaggedHosts);
695 if (expectRouteDoubleTaggedHosts != routeDoubleTaggedHosts) {
696 routeDoubleTaggedHosts = expectRouteDoubleTaggedHosts;
697 log.info("{} routing for double tagged hosts", routeDoubleTaggedHosts ? "Enabling" : "Disabling");
Charles Chan57465d32018-07-19 14:55:31 -0700698
Charles Chanb67dfdd2018-07-24 16:40:35 -0700699 if (routeDoubleTaggedHosts) {
Charles Chan57465d32018-07-19 14:55:31 -0700700 hostHandler.populateAllDoubleTaggedHost();
701 } else {
702 hostHandler.revokeAllDoubleTaggedHost();
703 }
704 }
Saurav Das09c2c4d2018-08-13 15:34:26 -0700705
Ray Milkey88dd7e22018-10-24 10:04:03 -0700706 String strDefaultInternalVlan = Tools.get(properties, PROP_DEFAULT_INTERNAL_VLAN);
Saurav Das09c2c4d2018-08-13 15:34:26 -0700707 int defIntVlan = Integer.parseInt(strDefaultInternalVlan);
708 if (defIntVlan != defaultInternalVlan) {
709 if (canUseVlanId(defIntVlan)) {
710 log.warn("Default internal vlan value changed from {} to {}.. "
711 + "re-programming filtering rules, but NOT any groups already "
712 + "created with the former value", defaultInternalVlan, defIntVlan);
713 VlanId oldDefIntVlan = VlanId.vlanId((short) defaultInternalVlan);
714 defaultInternalVlan = defIntVlan;
715 routingRulePopulator
716 .updateSpecialVlanFilteringRules(true, oldDefIntVlan,
717 VlanId.vlanId((short) defIntVlan));
718 } else {
719 log.warn("Cannot change default internal vlan to unusable "
720 + "value {}", defIntVlan);
721 }
722 }
723
Ray Milkey88dd7e22018-10-24 10:04:03 -0700724 String strPwTxpVlan = Tools.get(properties, PROP_PW_TRANSPORT_VLAN);
Saurav Das09c2c4d2018-08-13 15:34:26 -0700725 int pwTxpVlan = Integer.parseInt(strPwTxpVlan);
726 if (pwTxpVlan != pwTransportVlan) {
727 if (canUseVlanId(pwTxpVlan)) {
728 log.warn("Pseudowire transport vlan value changed from {} to {}.. "
729 + "re-programming filtering rules, but NOT any groups already "
730 + "created with the former value", pwTransportVlan,
731 pwTxpVlan);
732 VlanId oldPwTxpVlan = VlanId.vlanId((short) pwTransportVlan);
733 pwTransportVlan = pwTxpVlan;
734 routingRulePopulator
735 .updateSpecialVlanFilteringRules(false, oldPwTxpVlan,
736 VlanId.vlanId((short) pwTxpVlan));
737 } else {
738 log.warn("Cannot change pseudowire transport vlan to unusable "
739 + "value {}", pwTxpVlan);
740 }
741 }
742
743 }
744
745 /**
746 * Returns true if given vlan id is not being used in the system currently,
747 * either as one of the default system wide vlans or as one of the
748 * configured interface vlans.
749 *
750 * @param vlanId given vlan id
751 * @return true if vlan is not currently in use
752 */
753 public boolean canUseVlanId(int vlanId) {
754 if (vlanId >= 4095 || vlanId <= 1) {
755 log.error("Vlan id {} value is not in valid range 2 <--> 4094",
756 vlanId);
757 return false;
758 }
759
760 VlanId vid = VlanId.vlanId((short) vlanId);
761 if (getDefaultInternalVlan().equals(vid) || getPwTransportVlan().equals(vid)) {
762 log.warn("Vlan id {} value is already in use system-wide. "
763 + "DefaultInternalVlan:{} PwTransportVlan:{} ", vlanId,
764 getDefaultInternalVlan(), getPwTransportVlan());
765 return false;
766 }
767
768 if (interfaceService.inUse(vid)) {
769 log.warn("Vlan id {} value is already in use on a configured "
770 + "interface in the system", vlanId);
771 return false;
772 }
773 return true;
774 }
775
776 /**
777 * Returns the VlanId assigned internally by default to unconfigured ports.
778 *
779 * @return the default internal vlan id
780 */
781 public VlanId getDefaultInternalVlan() {
782 return VlanId.vlanId((short) defaultInternalVlan);
783 }
784
785 /**
786 * Returns the Vlan id used to transport pseudowire traffic across the
787 * network.
788 *
789 * @return the pseudowire transport vlan id
790 */
791 public VlanId getPwTransportVlan() {
792 return VlanId.vlanId((short) pwTransportVlan);
Charles Chan47933752017-11-30 15:37:50 -0800793 }
794
sangho1e575652015-05-14 00:39:53 -0700795 @Override
796 public List<Tunnel> getTunnels() {
797 return tunnelHandler.getTunnels();
798 }
799
800 @Override
sangho71abe1b2015-06-29 14:58:47 -0700801 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
802 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700803 }
804
805 @Override
sangho71abe1b2015-06-29 14:58:47 -0700806 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700807 for (Policy policy: policyHandler.getPolicies()) {
808 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
809 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
810 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
811 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700812 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700813 }
814 }
815 }
sangho71abe1b2015-06-29 14:58:47 -0700816 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700817 }
818
819 @Override
sangho71abe1b2015-06-29 14:58:47 -0700820 public PolicyHandler.Result removePolicy(Policy policy) {
821 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700822 }
823
824 @Override
sangho71abe1b2015-06-29 14:58:47 -0700825 public PolicyHandler.Result createPolicy(Policy policy) {
826 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700827 }
828
829 @Override
830 public List<Policy> getPolicies() {
831 return policyHandler.getPolicies();
832 }
833
Saurav Das59232cf2016-04-27 18:35:50 -0700834 @Override
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700835 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
836 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700837 }
838
839 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800840 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700841 return l2TunnelHandler.getL2Tunnels();
842 }
843
844 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800845 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700846 return l2TunnelHandler.getL2Policies();
847 }
848
Saurav Das5a356042018-04-06 20:16:01 -0700849 @Override
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700850 @Deprecated
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700851 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
852
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700853 // get both added and pending pseudowires
854 List<L2TunnelDescription> pseudowires = new ArrayList<>();
855 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
856 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700857 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700858
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700859 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
860
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700861 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
862 L2TunnelHandler.Result res;
863 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
864 res = addPseudowire(pw);
865 if (res != L2TunnelHandler.Result.SUCCESS) {
866 log.error("Pseudowire with id {} can not be instantiated !", res);
867 retRes = res;
868 }
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700869 }
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700870
871 return retRes;
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700872 }
873
874 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800875 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700876 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700877 }
878
879 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800880 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700881 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700882 }
883
884 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700885 public void rerouteNetwork() {
886 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700887 }
888
Charles Chanc81c45b2016-10-20 17:02:44 -0700889 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800890 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
891 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800892 deviceConfiguration.getRouters().forEach(device ->
893 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700894 return deviceSubnetMap;
895 }
896
Saurav Dasc88d4662017-05-15 15:34:25 -0700897
898 @Override
899 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
900 if (defaultRoutingHandler != null) {
901 return defaultRoutingHandler.getCurrentEmcpSpgMap();
902 } else {
903 return null;
904 }
905 }
906
907 @Override
Charles Chand5814aa2018-08-19 19:21:46 -0700908 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDstNextObjStore() {
Saurav Das7bcbe702017-06-13 15:35:54 -0700909 if (dsNextObjStore != null) {
910 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700911 } else {
912 return ImmutableMap.of();
913 }
914 }
915
Saurav Dasceccf242017-08-03 18:30:35 -0700916 @Override
Charles Chand5814aa2018-08-19 19:21:46 -0700917 public ImmutableMap<VlanNextObjectiveStoreKey, Integer> getVlanNextObjStore() {
918 if (vlanNextObjStore != null) {
919 return ImmutableMap.copyOf(vlanNextObjStore.entrySet());
920 } else {
921 return ImmutableMap.of();
922 }
923 }
924
925 @Override
926 public ImmutableMap<PortNextObjectiveStoreKey, Integer> getPortNextObjStore() {
927 if (portNextObjStore != null) {
928 return ImmutableMap.copyOf(portNextObjStore.entrySet());
929 } else {
930 return ImmutableMap.of();
931 }
932 }
933
934 @Override
935 public ImmutableMap<String, NextObjective> getPwInitNext() {
936 if (l2TunnelHandler != null) {
937 return l2TunnelHandler.getInitNext();
938 } else {
939 return ImmutableMap.of();
940 }
941 }
942
943 @Override
944 public ImmutableMap<String, NextObjective> getPwTermNext() {
945 if (l2TunnelHandler != null) {
946 return l2TunnelHandler.getTermNext();
947 } else {
948 return ImmutableMap.of();
949 }
950 }
951
952 @Override
953 public void invalidateNextObj(int nextId) {
954 if (dsNextObjStore != null) {
955 dsNextObjStore.entrySet().forEach(e -> {
956 if (e.getValue().nextId() == nextId) {
957 dsNextObjStore.remove(e.getKey());
958 }
959 });
960 }
961 if (vlanNextObjStore != null) {
962 vlanNextObjStore.entrySet().forEach(e -> {
963 if (e.getValue() == nextId) {
964 vlanNextObjStore.remove(e.getKey());
965 }
966 });
967 }
968 if (portNextObjStore != null) {
969 portNextObjStore.entrySet().forEach(e -> {
970 if (e.getValue() == nextId) {
971 portNextObjStore.remove(e.getKey());
972 }
973 });
974 }
975 if (mcastHandler != null) {
976 mcastHandler.removeNextId(nextId);
977 }
978 if (l2TunnelHandler != null) {
979 l2TunnelHandler.removeNextId(nextId);
980 }
981 if (xconnectService != null) {
982 xconnectService.removeNextId(nextId);
983 }
984 }
985
986 @Override
Saurav Dasceccf242017-08-03 18:30:35 -0700987 public void verifyGroups(DeviceId id) {
988 DefaultGroupHandler gh = groupHandlerMap.get(id);
989 if (gh != null) {
990 gh.triggerBucketCorrector();
991 }
992 }
993
Saurav Dasc568c342018-01-25 09:49:01 -0800994 @Override
995 public ImmutableMap<Link, Boolean> getSeenLinks() {
996 return linkHandler.getSeenLinks();
997 }
998
999 @Override
1000 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
1001 return linkHandler.getDownedPorts();
1002 }
1003
Pier Luigib29144d2018-01-15 18:06:43 +01001004 @Override
1005 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
Charles Chand5814aa2018-08-19 19:21:46 -07001006 return mcastHandler.getNextIds(mcastIp);
Pier Luigib29144d2018-01-15 18:06:43 +01001007 }
1008
1009 @Override
Pier Luigi96fe0772018-02-28 12:10:50 +01001010 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigib29144d2018-01-15 18:06:43 +01001011 return mcastHandler.getMcastRoles(mcastIp);
1012 }
1013
1014 @Override
Pier3e793752018-04-19 16:47:06 +02001015 public Map<McastRoleStoreKey, McastRole> getMcastRoles(IpAddress mcastIp, ConnectPoint sourcecp) {
1016 return mcastHandler.getMcastRoles(mcastIp, sourcecp);
1017 }
1018
1019 @Override
Pier Luigib29144d2018-01-15 18:06:43 +01001020 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
1021 return mcastHandler.getMcastPaths(mcastIp);
1022 }
1023
Pier96f63cb2018-04-17 16:29:56 +02001024 @Override
Pierb1fe7382018-04-17 17:25:22 +02001025 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
1026 ConnectPoint sourcecp) {
1027 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
1028 }
1029
1030 @Override
Pier96f63cb2018-04-17 16:29:56 +02001031 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
1032 return mcastHandler.getMcastLeaders(mcastIp);
1033 }
1034
Charles Chan8bc75ee2018-04-17 18:56:53 -07001035 @Override
1036 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
1037 return defaultRoutingHandler == null ? ImmutableMap.of() :
1038 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
1039 }
1040
1041 @Override
1042 public Map<DeviceId, Boolean> getShouldProgramCache() {
1043 return defaultRoutingHandler == null ? ImmutableMap.of() :
1044 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
1045 }
1046
Charles Chanc7b3c452018-06-19 20:31:57 -07001047 @Override
jayakumarthazhath655b9a82018-10-01 00:51:54 +05301048 public boolean shouldProgram(DeviceId deviceId) {
1049 return defaultRoutingHandler.shouldProgram(deviceId);
1050 }
1051
1052 @Override
Ray Milkeye4afdb52017-04-05 09:42:04 -07001053 public ApplicationId appId() {
1054 return appId;
1055 }
1056
1057 /**
1058 * Returns the device configuration.
1059 *
1060 * @return device configuration
1061 */
1062 public DeviceConfiguration deviceConfiguration() {
1063 return deviceConfiguration;
1064 }
1065
1066 /**
Saurav Das7bcbe702017-06-13 15:35:54 -07001067 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -07001068 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -07001069 *
1070 * @return next objective ID store
1071 */
Saurav Das7bcbe702017-06-13 15:35:54 -07001072 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
1073 dsNextObjStore() {
1074 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -07001075 }
1076
1077 /**
Charles Chan5bc32632017-08-22 15:07:34 -07001078 * Per device next objective ID store with (device id + vlanid) as key.
1079 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -07001080 *
1081 * @return vlan next object store
1082 */
1083 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
1084 return vlanNextObjStore;
1085 }
1086
1087 /**
Charles Chan5bc32632017-08-22 15:07:34 -07001088 * Per device next objective ID store with (device id + port + treatment + meta) as key.
1089 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -07001090 *
1091 * @return port next object store.
1092 */
1093 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
1094 return portNextObjStore;
1095 }
1096
1097 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001098 * Per port dummy VLAN ID store with (connect point + ip address) as key.
1099 * Used to keep track on dummy VLAN ID allocation.
1100 *
1101 * @return dummy vlan id store.
1102 */
1103 public EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId> dummyVlanIdStore() {
1104 return dummyVlanIdStore;
1105 }
1106
1107 /**
Saurav Das7bcbe702017-06-13 15:35:54 -07001108 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
1109 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -07001110 *
1111 * @return MPLS-ECMP value
1112 */
1113 public boolean getMplsEcmp() {
1114 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
1115 .getConfig(this.appId, SegmentRoutingAppConfig.class);
1116 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
1117 }
1118
1119 /**
sanghof9d0bf12015-05-19 11:57:42 -07001120 * Returns the tunnel object with the tunnel ID.
1121 *
1122 * @param tunnelId Tunnel ID
1123 * @return Tunnel reference
1124 */
sangho1e575652015-05-14 00:39:53 -07001125 public Tunnel getTunnel(String tunnelId) {
1126 return tunnelHandler.getTunnel(tunnelId);
1127 }
1128
Charles Chanc7b3c452018-06-19 20:31:57 -07001129 @Override
Pier Luigi96fe0772018-02-28 12:10:50 +01001130 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chan971d7ba2018-05-01 11:50:20 -07001131 VlanId untaggedVlanId = interfaceService.getUntaggedVlanId(connectPoint);
1132 VlanId nativeVlanId = interfaceService.getNativeVlanId(connectPoint);
Charles Chanf9a52702017-06-16 15:19:24 -07001133 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
1134 }
1135
Charles Chanc7b3c452018-06-19 20:31:57 -07001136 @Override
1137 public Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
Charles Chan65238242017-06-22 18:03:14 -07001138 SegmentRoutingDeviceConfig deviceConfig =
1139 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1140 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
1141 }
Charles Chanc7b3c452018-06-19 20:31:57 -07001142
1143 @Override
Saurav Das9a554292018-04-27 18:42:30 -07001144 public Optional<PortNumber> getPairLocalPort(DeviceId deviceId) {
Charles Chan65238242017-06-22 18:03:14 -07001145 SegmentRoutingDeviceConfig deviceConfig =
1146 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1147 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
1148 }
1149
1150 /**
Charles Chan9640c812017-08-23 13:55:39 -07001151 * Returns locations of given resolved route.
1152 *
1153 * @param resolvedRoute resolved route
1154 * @return locations of nexthop. Might be empty if next hop is not found
1155 */
1156 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
1157 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
1158 return Optional.ofNullable(hostService.getHost(hostId))
1159 .map(Host::locations).orElse(Sets.newHashSet())
1160 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
1161 }
1162
1163 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -08001164 * Returns vlan port map of given device.
1165 *
1166 * @param deviceId device id
1167 * @return vlan-port multimap
1168 */
1169 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
1170 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
1171
1172 interfaceService.getInterfaces().stream()
1173 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
1174 .forEach(intf -> {
1175 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -07001176 intf.vlanTagged().forEach(vlanTagged ->
1177 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
1178 );
Charles Chan7ffd81f2017-02-08 15:52:08 -08001179 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
1180 });
1181 vlanPortMap.removeAll(VlanId.NONE);
1182
1183 return vlanPortMap;
1184 }
1185
1186 /**
Charles Chan59cc16d2017-02-02 16:20:42 -08001187 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -08001188 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -07001189 *
1190 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -08001191 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -08001192 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -07001193 */
Charles Chan65238242017-06-22 18:03:14 -07001194 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -07001195 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -08001196 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
1197 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001198 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -08001199 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -08001200 + "device {} not found", deviceId);
1201 return -1;
1202 }
1203 }
1204
1205 /**
1206 * Returns the next objective ID for the given portNumber, given the treatment.
1207 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -07001208 * would result in different objectives. If the next object does not exist,
1209 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -08001210 *
1211 * @param deviceId Device ID
1212 * @param portNum port number on device for which NextObjective is queried
1213 * @param treatment the actions to apply on the packets (should include outport)
1214 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -07001215 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -07001216 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -08001217 */
1218 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
1219 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -07001220 TrafficSelector meta,
1221 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -08001222 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1223 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -07001224 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -08001225 } else {
Charles Chane849c192016-01-11 18:28:54 -08001226 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
1227 + " not found", deviceId);
1228 return -1;
1229 }
1230 }
1231
Saurav Dasc88d4662017-05-15 15:34:25 -07001232 /**
1233 * Returns the group handler object for the specified device id.
1234 *
1235 * @param devId the device identifier
1236 * @return the groupHandler object for the device id, or null if not found
1237 */
Charles Chan65238242017-06-22 18:03:14 -07001238 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001239 return groupHandlerMap.get(devId);
1240 }
1241
1242 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001243 * Returns the default routing handler object.
1244 *
1245 * @return the default routing handler object
1246 */
1247 public DefaultRoutingHandler getRoutingHandler() {
1248 return defaultRoutingHandler;
1249 }
1250
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001251 /**
1252 * Returns new dummy VLAN ID.
1253 * Dummy VLAN ID should be unique in each connect point.
1254 *
1255 * @param cp connect point
1256 * @param ipAddress IP address
1257 * @return new dummy VLAN ID. Returns VlanId.NONE if no VLAN ID is available.
1258 */
1259 public synchronized VlanId allocateDummyVlanId(ConnectPoint cp, IpAddress ipAddress) {
1260 Set<VlanId> usedVlanId = Sets.union(getVlanPortMap(cp.deviceId()).keySet(),
1261 dummyVlanIdStore.entrySet().stream().filter(entry ->
1262 (entry.getKey()).connectPoint().equals(cp))
1263 .map(Map.Entry::getValue)
1264 .collect(Collectors.toSet()));
1265
1266 VlanId dummyVlanId = IntStream.range(MIN_DUMMY_VLAN_ID, MAX_DUMMY_VLAN_ID).mapToObj(
1267 i -> VlanId.vlanId((short) (i & 0xFFFF))
1268 ).filter(vlanId -> !usedVlanId.contains(vlanId)).findFirst().orElse(VlanId.NONE);
1269
1270 if (!dummyVlanId.equals(VlanId.NONE)) {
1271 this.dummyVlanIdStore.put(new DummyVlanIdStoreKey(cp, ipAddress), dummyVlanId);
1272 log.debug("Dummy VLAN ID {} is allocated to {}, {}", dummyVlanId, cp, ipAddress);
1273 } else {
1274 log.error("Failed to allocate dummy VLAN ID for {}, {}", cp, ipAddress);
1275 }
1276 return dummyVlanId;
1277 }
1278
1279
sanghob35a6192015-04-01 13:05:26 -07001280 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -07001281 @Override
1282 public void process(PacketContext context) {
Charles Chan07f15f22018-05-08 21:35:50 -07001283 packetExecutor.execute(() -> processPacketInternal(context));
1284 }
sanghob35a6192015-04-01 13:05:26 -07001285
Charles Chan07f15f22018-05-08 21:35:50 -07001286 private void processPacketInternal(PacketContext context) {
sanghob35a6192015-04-01 13:05:26 -07001287 if (context.isHandled()) {
1288 return;
1289 }
1290
1291 InboundPacket pkt = context.inPacket();
1292 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -08001293
1294 if (ethernet == null) {
1295 return;
1296 }
1297
Saurav Das7bcbe702017-06-13 15:35:54 -07001298 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1299 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001300 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001301 log.warn("Received unexpected ARP packet on {}",
1302 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -07001303 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -08001304 return;
sanghob35a6192015-04-01 13:05:26 -07001305 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -08001306 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1307 //ipHandler.addToPacketBuffer(ipv4Packet);
1308 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1309 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001310 } else {
Charles Chan50035632017-01-13 17:20:44 -08001311 // NOTE: We don't support IP learning at this moment so this
1312 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -08001313 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001314 }
Pier Ventre10bd8d12016-11-26 21:05:22 -08001315 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1316 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -08001317 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -08001318 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -08001319 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1320 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1321 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1322 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1323 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1324 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -07001325 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -07001326 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -08001327 }
1328 } else {
1329 // NOTE: We don't support IP learning at this moment so this
1330 // is not necessary. Also it causes duplication of DHCPv6 packets.
1331 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1332 }
sanghob35a6192015-04-01 13:05:26 -07001333 }
1334 }
1335 }
1336
sanghob35a6192015-04-01 13:05:26 -07001337 private class InternalEventHandler implements Runnable {
Charles Chan9b7217c2018-04-05 16:31:15 -07001338 private Event event;
1339
1340 InternalEventHandler(Event event) {
1341 this.event = event;
1342 }
1343
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001344 @Override
sanghob35a6192015-04-01 13:05:26 -07001345 public void run() {
Charles Chan9b7217c2018-04-05 16:31:15 -07001346 try {
1347 // TODO We should also change SR routing and PW to listen to TopologyEvents
1348 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1349 event.type() == LinkEvent.Type.LINK_UPDATED) {
1350 linkHandler.processLinkAdded((Link) event.subject());
1351 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1352 linkHandler.processLinkRemoved((Link) event.subject());
1353 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1354 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1355 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1356 DeviceId deviceId = ((Device) event.subject()).id();
1357 if (deviceService.isAvailable(deviceId)) {
1358 log.info("** DEVICE UP Processing device event {} "
1359 + "for available device {}",
1360 event.type(), ((Device) event.subject()).id());
1361 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001362 } else {
Charles Chan7040b692018-08-29 21:21:51 -07001363 if (event.type() == DeviceEvent.Type.DEVICE_ADDED) {
1364 // Note: For p4 devices, the device will be added but unavailable at the beginning.
1365 // The device will later on being marked as available once the pipeline is pushed
1366 // to the device.
1367 log.info("** DEVICE ADDED but unavailable. Ignore");
1368 return;
1369 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001370 log.info(" ** DEVICE DOWN Processing device event {}"
1371 + " for unavailable device {}",
1372 event.type(), ((Device) event.subject()).id());
1373 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001374 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001375 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1376 // typically these calls come when device is added first time
1377 // so port filtering rules are handled at the device_added event.
1378 // port added calls represent all ports on the device,
1379 // enabled or not.
1380 log.trace("** PORT ADDED {}/{} -> {}",
1381 ((DeviceEvent) event).subject().id(),
1382 ((DeviceEvent) event).port().number(),
1383 event.type());
1384 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1385 // these calls happen for every subsequent event
1386 // ports enabled, disabled, switch goes away, comes back
1387 log.info("** PORT UPDATED {}/{} -> {}",
1388 event.subject(),
1389 ((DeviceEvent) event).port(),
1390 event.type());
Saurav Das9a554292018-04-27 18:42:30 -07001391 processPortUpdatedInternal(((Device) event.subject()),
Charles Chan9b7217c2018-04-05 16:31:15 -07001392 ((DeviceEvent) event).port());
1393 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1394 // Process topology event, needed for all modules relying on
1395 // topology service for path computation
1396 TopologyEvent topologyEvent = (TopologyEvent) event;
1397 log.info("Processing topology event {}, topology age {}, reasons {}",
1398 event.type(), topologyEvent.subject().time(),
1399 topologyEvent.reasons().size());
1400 topologyHandler.processTopologyChange(topologyEvent.reasons());
1401 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1402 hostHandler.processHostAddedEvent((HostEvent) event);
1403 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1404 hostHandler.processHostMovedEvent((HostEvent) event);
1405 routeHandler.processHostMovedEvent((HostEvent) event);
1406 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1407 hostHandler.processHostRemovedEvent((HostEvent) event);
1408 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1409 hostHandler.processHostUpdatedEvent((HostEvent) event);
1410 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1411 routeHandler.processRouteAdded((RouteEvent) event);
1412 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1413 routeHandler.processRouteUpdated((RouteEvent) event);
1414 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1415 routeHandler.processRouteRemoved((RouteEvent) event);
1416 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1417 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1418 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1419 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1420 event.type() == McastEvent.Type.SINKS_ADDED ||
1421 event.type() == McastEvent.Type.SINKS_REMOVED ||
1422 event.type() == McastEvent.Type.ROUTE_ADDED ||
1423 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1424 mcastHandler.processMcastEvent((McastEvent) event);
1425 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1426 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1427 Class configClass = netcfgEvent.configClass();
1428 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1429 appCfgHandler.processAppConfigAdded(netcfgEvent);
1430 log.info("App config event .. configuring network");
1431 cfgListener.configureNetwork();
1432 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1433 log.info("Segment Routing Device Config added for {}", event.subject());
1434 cfgListener.configureNetwork();
1435 } else if (configClass.equals(XConnectConfig.class)) {
1436 xConnectHandler.processXConnectConfigAdded(netcfgEvent);
1437 } else if (configClass.equals(InterfaceConfig.class)) {
1438 log.info("Interface Config added for {}", event.subject());
1439 cfgListener.configureNetwork();
1440 } else {
1441 log.error("Unhandled config class: {}", configClass);
1442 }
1443 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1444 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1445 Class configClass = netcfgEvent.configClass();
1446 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1447 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1448 log.info("App config event .. configuring network");
1449 cfgListener.configureNetwork();
1450 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1451 log.info("Segment Routing Device Config updated for {}", event.subject());
1452 createOrUpdateDeviceConfiguration();
1453 } else if (configClass.equals(XConnectConfig.class)) {
1454 xConnectHandler.processXConnectConfigUpdated(netcfgEvent);
1455 } else if (configClass.equals(InterfaceConfig.class)) {
1456 log.info("Interface Config updated for {}", event.subject());
1457 createOrUpdateDeviceConfiguration();
1458 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1459 (InterfaceConfig) netcfgEvent.prevConfig().get());
1460 } else {
1461 log.error("Unhandled config class: {}", configClass);
1462 }
1463 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1464 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1465 Class configClass = netcfgEvent.configClass();
1466 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1467 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1468 log.info("App config event .. configuring network");
1469 cfgListener.configureNetwork();
1470 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1471 // TODO Handle sr device config removal
1472 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
1473 } else if (configClass.equals(XConnectConfig.class)) {
1474 xConnectHandler.processXConnectConfigRemoved(netcfgEvent);
1475 } else if (configClass.equals(InterfaceConfig.class)) {
1476 // TODO Handle interface removal
1477 log.info("InterfaceConfig removal is not handled in current implementation");
1478 } else {
1479 log.error("Unhandled config class: {}", configClass);
1480 }
Saurav Das201762d2018-04-21 17:19:48 -07001481 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1482 MastershipEvent me = (MastershipEvent) event;
1483 DeviceId deviceId = me.subject();
1484 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1485 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1486 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1487 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1488 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1489 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan9b7217c2018-04-05 16:31:15 -07001490 } else {
1491 log.warn("Unhandled event type: {}", event.type());
sanghob35a6192015-04-01 13:05:26 -07001492 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001493 } catch (Exception e) {
1494 log.error("SegmentRouting event handler thread thrown an exception: {}",
1495 e.getMessage(), e);
sanghob35a6192015-04-01 13:05:26 -07001496 }
sanghob35a6192015-04-01 13:05:26 -07001497 }
1498 }
1499
Saurav Das45f48152018-01-18 12:07:33 -08001500 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001501 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001502
1503 // NOTE: Punt ARP/NDP even when the device is not configured.
1504 // Host learning without network config is required for CORD config generator.
1505 routingRulePopulator.populateIpPunts(device.id());
1506 routingRulePopulator.populateArpNdpPunts(device.id());
1507
Charles Chan0b4e6182015-11-03 10:42:14 -08001508 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001509 log.warn("Device configuration unavailable. Device {} will be "
1510 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001511 return;
1512 }
Charles Chan2199c302016-04-23 17:36:10 -07001513 processDeviceAddedInternal(device.id());
1514 }
1515
1516 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001517 // Irrespective of whether the local is a MASTER or not for this device,
1518 // we need to create a SR-group-handler instance. This is because in a
1519 // multi-instance setup, any instance can initiate forwarding/next-objectives
1520 // for any switch (even if this instance is a SLAVE or not even connected
1521 // to the switch). To handle this, a default-group-handler instance is necessary
1522 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001523 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1524 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001525 DefaultGroupHandler groupHandler;
1526 try {
1527 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001528 createGroupHandler(deviceId,
1529 appId,
1530 deviceConfiguration,
1531 linkService,
1532 flowObjectiveService,
1533 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001534 } catch (DeviceConfigNotFoundException e) {
1535 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1536 return;
1537 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001538 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001539 deviceId);
1540 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001541 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001542
Charles Chan2199c302016-04-23 17:36:10 -07001543 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001544 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001545 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001546 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001547 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001548 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001549 }
Charles Chan5270ed02016-01-30 23:22:37 -08001550
Charles Chan03a73e02016-10-24 14:52:01 -07001551 appCfgHandler.init(deviceId);
Charles Chan94549652018-04-29 18:11:37 -07001552 hostEventExecutor.execute(() -> hostHandler.init(deviceId));
1553 routeEventExecutor.execute(() -> routeHandler.init(deviceId));
sanghob35a6192015-04-01 13:05:26 -07001554 }
1555
Saurav Das80980c72016-03-23 11:22:49 -07001556 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001557 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001558 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan47933752017-11-30 15:37:50 -08001559 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001560 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001561 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001562 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001563 portNextObjStore.entrySet().stream()
1564 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001565 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Das45f48152018-01-18 12:07:33 -08001566 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001567
Saurav Dasceccf242017-08-03 18:30:35 -07001568 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1569 if (gh != null) {
1570 gh.shutdown();
1571 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001572 // Note that a switch going down is associated with all of its links
1573 // going down as well, but it is treated as a single switch down event
Saurav Das5a356042018-04-06 20:16:01 -07001574 // while the link-downs are ignored. We cannot rely on the ordering of
1575 // events - i.e we cannot expect all link-downs to come before the
1576 // switch down - so we purge all seen-links for the switch before
1577 // handling route-path changes for the switch-down
Saurav Dasc88d4662017-05-15 15:34:25 -07001578 defaultRoutingHandler
Saurav Das604ab3a2018-03-18 21:28:15 -07001579 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Dasc568c342018-01-25 09:49:01 -08001580 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001581 xConnectHandler.removeDevice(device.id());
Saurav Dasa4020382018-02-14 14:14:54 -08001582
1583 // Cleanup all internal groupHandler stores for this device. Should be
1584 // done after all rerouting or rehashing has been completed
1585 groupHandlerMap.entrySet()
1586 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Das80980c72016-03-23 11:22:49 -07001587 }
1588
Saurav Dasc6ff8f02018-04-23 18:42:12 -07001589 /**
1590 * Purge the destinationSet nextObjective store of entries with this device
1591 * as key. Erases app-level knowledge of hashed groups in this device.
1592 *
1593 * @param devId the device identifier
1594 */
Saurav Das201762d2018-04-21 17:19:48 -07001595 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Dasc6ff8f02018-04-23 18:42:12 -07001596 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Das201762d2018-04-21 17:19:48 -07001597 dsNextObjStore.entrySet().stream()
1598 .filter(entry -> entry.getKey().deviceId().equals(devId))
1599 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1600 }
1601
Saurav Das9a554292018-04-27 18:42:30 -07001602 private void processPortUpdatedInternal(Device device, Port port) {
Saurav Das1a129a02016-11-18 15:21:57 -08001603 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1604 log.warn("Device configuration uploading. Not handling port event for"
1605 + "dev: {} port: {}", device.id(), port.number());
1606 return;
1607 }
Saurav Das018605f2017-02-18 14:05:44 -08001608
Saurav Das9a554292018-04-27 18:42:30 -07001609 if (interfaceService.isConfigured(new ConnectPoint(device.id(), port.number()))) {
1610 lastEdgePortEvent = Instant.now();
1611 }
1612
Saurav Das018605f2017-02-18 14:05:44 -08001613 if (!mastershipService.isLocalMaster(device.id())) {
1614 log.debug("Not master for dev:{} .. not handling port updated event"
1615 + "for port {}", device.id(), port.number());
1616 return;
1617 }
Saurav Das9a554292018-04-27 18:42:30 -07001618 processPortUpdated(device.id(), port);
1619 }
Saurav Das018605f2017-02-18 14:05:44 -08001620
Saurav Das9a554292018-04-27 18:42:30 -07001621 /**
1622 * Adds or remove filtering rules for the given switchport. If switchport is
1623 * an edge facing port, additionally handles host probing and broadcast
1624 * rules. Must be called by local master of device.
1625 *
1626 * @param deviceId the device identifier
1627 * @param port the port to update
1628 */
1629 void processPortUpdated(DeviceId deviceId, Port port) {
Saurav Das018605f2017-02-18 14:05:44 -08001630 // first we handle filtering rules associated with the port
1631 if (port.isEnabled()) {
1632 log.info("Switchport {}/{} enabled..programming filters",
Saurav Das9a554292018-04-27 18:42:30 -07001633 deviceId, port.number());
1634 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001635 } else {
1636 log.info("Switchport {}/{} disabled..removing filters",
Saurav Das9a554292018-04-27 18:42:30 -07001637 deviceId, port.number());
1638 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001639 }
Saurav Das1a129a02016-11-18 15:21:57 -08001640
1641 // portUpdated calls are for ports that have gone down or up. For switch
1642 // to switch ports, link-events should take care of any re-routing or
1643 // group editing necessary for port up/down. Here we only process edge ports
1644 // that are already configured.
Saurav Das9a554292018-04-27 18:42:30 -07001645 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
Charles Chan971d7ba2018-05-01 11:50:20 -07001646 VlanId untaggedVlan = interfaceService.getUntaggedVlanId(cp);
1647 VlanId nativeVlan = interfaceService.getNativeVlanId(cp);
1648 Set<VlanId> taggedVlans = interfaceService.getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001649
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001650 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001651 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das9a554292018-04-27 18:42:30 -07001652 + "dev/port: {}/{}", deviceId, port.number());
Saurav Das1a129a02016-11-18 15:21:57 -08001653 return;
1654 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001655 if (untaggedVlan != null) {
Saurav Das9a554292018-04-27 18:42:30 -07001656 processEdgePort(deviceId, port, untaggedVlan, true);
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001657 }
1658 if (nativeVlan != null) {
Saurav Das9a554292018-04-27 18:42:30 -07001659 processEdgePort(deviceId, port, nativeVlan, true);
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001660 }
1661 if (!taggedVlans.isEmpty()) {
Saurav Das9a554292018-04-27 18:42:30 -07001662 taggedVlans.forEach(tag -> processEdgePort(deviceId, port, tag, false));
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001663 }
Saurav Das1a129a02016-11-18 15:21:57 -08001664 }
1665
Saurav Das9a554292018-04-27 18:42:30 -07001666 private void processEdgePort(DeviceId deviceId, Port port, VlanId vlanId,
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001667 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001668 boolean portUp = port.isEnabled();
1669 if (portUp) {
Saurav Das9a554292018-04-27 18:42:30 -07001670 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", deviceId,
Charles Chan59cc16d2017-02-02 16:20:42 -08001671 port.number(), vlanId);
Charles Chan94549652018-04-29 18:11:37 -07001672 hostEventExecutor.execute(() -> hostHandler.processPortUp(new ConnectPoint(deviceId, port.number())));
Saurav Das1a129a02016-11-18 15:21:57 -08001673 } else {
Saurav Das9a554292018-04-27 18:42:30 -07001674 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", deviceId,
Charles Chan59cc16d2017-02-02 16:20:42 -08001675 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001676 }
1677
Saurav Das9a554292018-04-27 18:42:30 -07001678 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001679 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001680 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001681 } else {
1682 log.warn("Group handler not found for dev:{}. Not handling edge port"
Saurav Das9a554292018-04-27 18:42:30 -07001683 + " {} event for port:{}", deviceId,
Saurav Das1a129a02016-11-18 15:21:57 -08001684 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001685 }
1686 }
sangho1e575652015-05-14 00:39:53 -07001687
Charles Chan27fe1a52017-10-20 16:06:55 -07001688 private void createOrUpdateDeviceConfiguration() {
1689 if (deviceConfiguration == null) {
Saurav Dase7f51012018-02-09 17:26:45 -08001690 log.info("Creating new DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001691 deviceConfiguration = new DeviceConfiguration(this);
1692 } else {
Saurav Dase7f51012018-02-09 17:26:45 -08001693 log.info("Updating DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001694 deviceConfiguration.updateConfig();
1695 }
1696 }
1697
Charles Chan50bb6ef2018-04-18 18:41:05 -07001698 private void createOrUpdateDefaultRoutingHandler() {
1699 if (defaultRoutingHandler == null) {
1700 log.info("Creating new DefaultRoutingHandler");
1701 defaultRoutingHandler = new DefaultRoutingHandler(this);
1702 } else {
1703 log.info("Updating DefaultRoutingHandler");
1704 defaultRoutingHandler.update(this);
1705 }
1706 }
1707
Pier Ventre10bd8d12016-11-26 21:05:22 -08001708 /**
1709 * Registers the given connect point with the NRS, this is necessary
1710 * to receive the NDP and ARP packets from the NRS.
1711 *
1712 * @param portToRegister connect point to register
1713 */
1714 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001715 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001716 portToRegister,
1717 neighbourHandler,
1718 appId
1719 );
1720 }
1721
Charles Chand6832882015-10-05 17:50:33 -07001722 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001723 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001724 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001725
Charles Chane849c192016-01-11 18:28:54 -08001726 /**
1727 * Constructs the internal network config listener.
1728 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001729 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001730 */
Charles Chan65238242017-06-22 18:03:14 -07001731 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001732 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001733 }
1734
Charles Chane849c192016-01-11 18:28:54 -08001735 /**
1736 * Reads network config and initializes related data structure accordingly.
1737 */
Charles Chan47933752017-11-30 15:37:50 -08001738 void configureNetwork() {
Saurav Dase7f51012018-02-09 17:26:45 -08001739 log.info("Configuring network ...");
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001740
1741 // Setting handling of network configuration events completable future
1742 // The completable future is needed because of the async behaviour of the configureNetwork,
1743 // listener registration and event arrival
1744 // Enables us to buffer the events and execute them when the configure network is done.
Ray Milkeyec9e4de2018-11-16 15:15:14 -08001745 synchronized (networkConfigCompletionLock) {
1746 networkConfigCompletion = new CompletableFuture<>();
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001747
Ray Milkeyec9e4de2018-11-16 15:15:14 -08001748 // add a small delay to absorb multiple network config added notifications
1749 if (!programmingScheduled.get()) {
1750 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1751 programmingScheduled.set(true);
1752 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1753 }
1754
1755 createOrUpdateDeviceConfiguration();
1756
1757 arpHandler = new ArpHandler(srManager);
1758 icmpHandler = new IcmpHandler(srManager);
1759 ipHandler = new IpHandler(srManager);
1760 routingRulePopulator = new RoutingRulePopulator(srManager);
1761 createOrUpdateDefaultRoutingHandler();
1762
1763 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1764 groupHandlerMap, tunnelStore);
1765 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1766 flowObjectiveService,
1767 tunnelHandler, policyStore);
1768 networkConfigCompletion.complete(true);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001769 }
1770
Charles Chan2199c302016-04-23 17:36:10 -07001771 mcastHandler.init();
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001772
Charles Chan4636be02015-10-07 14:21:45 -07001773 }
1774
Charles Chand6832882015-10-05 17:50:33 -07001775 @Override
1776 public void event(NetworkConfigEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001777 if (mainEventExecutor == null) {
1778 return;
1779 }
Charles Chan7a888e82018-03-21 16:57:47 -07001780 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
1781 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1782 switch (event.type()) {
1783 case CONFIG_ADDED:
1784 case CONFIG_UPDATED:
1785 case CONFIG_REMOVED:
1786 log.trace("Schedule Network Config event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001787 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1788 mainEventExecutor.execute(new InternalEventHandler(event));
1789 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001790 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001791 }
Charles Chan7a888e82018-03-21 16:57:47 -07001792 break;
1793 default:
1794 break;
Charles Chand6832882015-10-05 17:50:33 -07001795 }
1796 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001797
Charles Chan50443e82018-01-03 16:26:32 -08001798 @Override
1799 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase7f51012018-02-09 17:26:45 -08001800 if (event.type() == CONFIG_REGISTERED ||
1801 event.type() == CONFIG_UNREGISTERED) {
1802 log.debug("Ignore event {} due to type mismatch", event);
1803 return false;
Charles Chan50443e82018-01-03 16:26:32 -08001804 }
Saurav Dase7f51012018-02-09 17:26:45 -08001805
1806 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1807 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1808 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscd339592018-02-23 14:18:00 -08001809 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase7f51012018-02-09 17:26:45 -08001810 log.debug("Ignore event {} due to class mismatch", event);
1811 return false;
1812 }
1813
1814 return true;
Charles Chan50443e82018-01-03 16:26:32 -08001815 }
1816
Saurav Das7bcbe702017-06-13 15:35:54 -07001817 private final class ConfigChange implements Runnable {
1818 @Override
1819 public void run() {
1820 programmingScheduled.set(false);
Saurav Dase7f51012018-02-09 17:26:45 -08001821 log.info("Reacting to config changes after buffer delay");
Saurav Das7bcbe702017-06-13 15:35:54 -07001822 for (Device device : deviceService.getDevices()) {
1823 processDeviceAdded(device);
1824 }
1825 defaultRoutingHandler.startPopulationProcess();
1826 }
1827 }
Charles Chand6832882015-10-05 17:50:33 -07001828 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001829
Charles Chan7a888e82018-03-21 16:57:47 -07001830 private class InternalLinkListener implements LinkListener {
1831 @Override
1832 public void event(LinkEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001833 if (mainEventExecutor == null) {
1834 return;
1835 }
Charles Chan7a888e82018-03-21 16:57:47 -07001836 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1837 event.type() == LinkEvent.Type.LINK_UPDATED ||
1838 event.type() == LinkEvent.Type.LINK_REMOVED) {
1839 log.trace("Schedule Link event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001840 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1841 mainEventExecutor.execute(new InternalEventHandler(event));
1842 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001843 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001844 }
Charles Chan7a888e82018-03-21 16:57:47 -07001845 }
1846 }
1847 }
1848
1849 private class InternalDeviceListener implements DeviceListener {
1850 @Override
1851 public void event(DeviceEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001852 if (mainEventExecutor == null) {
1853 return;
1854 }
Charles Chan7a888e82018-03-21 16:57:47 -07001855 switch (event.type()) {
1856 case DEVICE_ADDED:
1857 case PORT_UPDATED:
1858 case PORT_ADDED:
1859 case DEVICE_UPDATED:
1860 case DEVICE_AVAILABILITY_CHANGED:
1861 log.trace("Schedule Device event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001862 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1863 mainEventExecutor.execute(new InternalEventHandler(event));
1864 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001865 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001866 }
Charles Chan7a888e82018-03-21 16:57:47 -07001867 break;
1868 default:
1869 }
1870 }
1871 }
1872
1873 private class InternalTopologyListener implements TopologyListener {
1874 @Override
1875 public void event(TopologyEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001876 if (mainEventExecutor == null) {
1877 return;
1878 }
Charles Chan7a888e82018-03-21 16:57:47 -07001879 switch (event.type()) {
1880 case TOPOLOGY_CHANGED:
1881 log.trace("Schedule Topology event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001882 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1883 mainEventExecutor.execute(new InternalEventHandler(event));
1884 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001885 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001886 }
Charles Chan7a888e82018-03-21 16:57:47 -07001887 break;
1888 default:
1889 }
1890 }
1891 }
1892
Charles Chan68aa62d2015-11-09 16:37:23 -08001893 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001894 @Override
1895 public void event(HostEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001896 if (hostEventExecutor == null) {
1897 return;
1898 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001899 switch (event.type()) {
1900 case HOST_ADDED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001901 case HOST_MOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001902 case HOST_REMOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001903 case HOST_UPDATED:
Charles Chan7a888e82018-03-21 16:57:47 -07001904 log.trace("Schedule Host event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001905 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chan68aa62d2015-11-09 16:37:23 -08001906 break;
1907 default:
1908 log.warn("Unsupported host event type: {}", event.type());
1909 break;
1910 }
1911 }
1912 }
1913
Charles Chand55e84d2016-03-30 17:54:24 -07001914 private class InternalMcastListener implements McastListener {
1915 @Override
1916 public void event(McastEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001917 if (mcastEventExecutor == null) {
1918 return;
1919 }
Charles Chand55e84d2016-03-30 17:54:24 -07001920 switch (event.type()) {
Pier3ee24552018-03-14 16:47:32 -07001921 case SOURCES_ADDED:
1922 case SOURCES_REMOVED:
1923 case SINKS_ADDED:
1924 case SINKS_REMOVED:
Charles Chand55e84d2016-03-30 17:54:24 -07001925 case ROUTE_REMOVED:
Pier96f63cb2018-04-17 16:29:56 +02001926 case ROUTE_ADDED:
Charles Chan7a888e82018-03-21 16:57:47 -07001927 log.trace("Schedule Mcast event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001928 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi9930da52018-02-02 16:19:11 +01001929 break;
Charles Chand55e84d2016-03-30 17:54:24 -07001930 default:
Charles Chan7a888e82018-03-21 16:57:47 -07001931 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chand55e84d2016-03-30 17:54:24 -07001932 break;
1933 }
1934 }
1935 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001936
Charles Chan03a73e02016-10-24 14:52:01 -07001937 private class InternalRouteEventListener implements RouteListener {
1938 @Override
1939 public void event(RouteEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001940 if (routeEventExecutor == null) {
1941 return;
1942 }
Charles Chan03a73e02016-10-24 14:52:01 -07001943 switch (event.type()) {
1944 case ROUTE_ADDED:
Charles Chan03a73e02016-10-24 14:52:01 -07001945 case ROUTE_UPDATED:
Charles Chan03a73e02016-10-24 14:52:01 -07001946 case ROUTE_REMOVED:
Charles Chan2fde6d42017-08-23 14:46:43 -07001947 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan7a888e82018-03-21 16:57:47 -07001948 log.trace("Schedule Route event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001949 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan2fde6d42017-08-23 14:46:43 -07001950 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001951 default:
Charles Chan7a888e82018-03-21 16:57:47 -07001952 log.warn("Unsupported route event type: {}", event.type());
Charles Chan03a73e02016-10-24 14:52:01 -07001953 break;
1954 }
1955 }
1956 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001957
Charles Chan50bb6ef2018-04-18 18:41:05 -07001958 private class InternalMastershipListener implements MastershipListener {
1959 @Override
1960 public void event(MastershipEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001961 if (mainEventExecutor == null) {
1962 return;
1963 }
Charles Chan50bb6ef2018-04-18 18:41:05 -07001964 switch (event.type()) {
Saurav Das201762d2018-04-21 17:19:48 -07001965 case MASTER_CHANGED:
1966 log.debug("Mastership event: {}/{}", event.subject(),
1967 event.roleInfo());
1968 mainEventExecutor.execute(new InternalEventHandler(event));
1969 break;
1970 case BACKUPS_CHANGED:
1971 case SUSPENDED:
1972 default:
1973 log.debug("Mastership event type {} not handled", event.type());
1974 break;
Charles Chan50bb6ef2018-04-18 18:41:05 -07001975 }
1976 }
1977 }
1978
Saurav Das201762d2018-04-21 17:19:48 -07001979 class InternalClusterListener implements ClusterEventListener {
1980 private Instant lastClusterEvent = Instant.EPOCH;
1981
1982 long timeSinceLastClusterEvent() {
1983 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
1984 }
1985
1986 @Override
1987 public void event(ClusterEvent event) {
1988 switch (event.type()) {
1989 case INSTANCE_ACTIVATED:
1990 case INSTANCE_ADDED:
1991 case INSTANCE_READY:
1992 log.debug("Cluster event {} ignored", event.type());
1993 break;
1994 case INSTANCE_DEACTIVATED:
1995 case INSTANCE_REMOVED:
1996 log.info("** Cluster event {}", event.type());
1997 lastClusterEvent = Instant.now();
1998 break;
1999 default:
2000 break;
2001 }
2002
2003 }
2004
2005 }
2006
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002007 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
2008 try {
2009 Set<Interface> intfs = conf.getInterfaces();
2010 Set<Interface> prevIntfs = prevConf.getInterfaces();
2011
2012 // Now we only handle one interface config at each port.
2013 if (intfs.size() != 1 || prevIntfs.size() != 1) {
2014 log.warn("Interface update aborted - one at a time is allowed, " +
2015 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
2016 return;
2017 }
2018
Andrea Campanella6468e1f2018-04-17 12:09:34 +02002019 //The system is in an incoherent state, abort
2020 if (defaultRoutingHandler == null) {
2021 log.warn("Interface update aborted, defaultRoutingHandler is null");
2022 return;
2023 }
2024
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002025 Interface intf = intfs.stream().findFirst().get();
2026 Interface prevIntf = prevIntfs.stream().findFirst().get();
2027
2028 DeviceId deviceId = intf.connectPoint().deviceId();
2029 PortNumber portNum = intf.connectPoint().port();
2030
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002031 removeSubnetConfig(prevIntf.connectPoint(),
2032 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
2033 new HashSet<>(intf.ipAddressesList())));
2034
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002035 if (!prevIntf.vlanNative().equals(VlanId.NONE)
2036 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
2037 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
2038 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
2039 // Update filtering objective and L2IG group bucket
2040 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
2041 } else {
2042 // RemoveVlanNative
2043 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
2044 }
2045 }
2046
2047 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
2048 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2049 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2050 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
2051 // Update filtering objective and L2IG group bucket
2052 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
2053 } else {
2054 // RemoveVlanUntagged
2055 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
2056 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002057 }
2058
2059 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2060 // RemoveVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002061 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
2062 .filter(i -> !intf.vlanUntagged().equals(i))
2063 .filter(i -> !intf.vlanNative().equals(i))
2064 .forEach(vlanId -> updateVlanConfigInternal(
2065 deviceId, portNum, vlanId, false, false));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002066 }
2067
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002068 if (!intf.vlanNative().equals(VlanId.NONE)
2069 && !prevIntf.vlanNative().equals(intf.vlanNative())
2070 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2071 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
2072 // Update filtering objective and L2IG group bucket
2073 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
2074 } else {
2075 // AddVlanNative
2076 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
2077 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002078 }
2079
2080 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2081 // AddVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002082 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
2083 .filter(i -> !prevIntf.vlanUntagged().equals(i))
2084 .filter(i -> !prevIntf.vlanNative().equals(i))
2085 .forEach(vlanId -> updateVlanConfigInternal(
2086 deviceId, portNum, vlanId, false, true)
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002087 );
2088 }
2089
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002090 if (!intf.vlanUntagged().equals(VlanId.NONE)
2091 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2092 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
2093 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
2094 // Update filtering objective and L2IG group bucket
2095 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
2096 } else {
2097 // AddVlanUntagged
2098 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
2099 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002100 }
2101 addSubnetConfig(prevIntf.connectPoint(),
2102 Sets.difference(new HashSet<>(intf.ipAddressesList()),
2103 new HashSet<>(prevIntf.ipAddressesList())));
2104 } catch (ConfigException e) {
2105 log.error("Error in configuration");
2106 }
2107 }
2108
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002109 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
2110 VlanId vlanId, boolean pushVlan) {
2111 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2112 if (grpHandler == null) {
2113 log.warn("Failed to retrieve group handler for device {}", deviceId);
2114 return;
2115 }
2116
2117 // Update filtering objective for a single port
2118 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
2119 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
2120
2121 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
2122 // Update L2IG bucket of the port
2123 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
2124 } else {
2125 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2126 }
2127 }
2128
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002129 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
2130 VlanId vlanId, boolean pushVlan, boolean install) {
2131 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2132 if (grpHandler == null) {
2133 log.warn("Failed to retrieve group handler for device {}", deviceId);
2134 return;
2135 }
2136
2137 // Update filtering objective for a single port
2138 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
2139
2140 // Update filtering objective for multicast ingress port
2141 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
2142
2143 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
2144
2145 if (nextId != -1 && !install) {
2146 // Update next objective for a single port as an output port
2147 // Remove a single port from L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002148 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002149 // Remove L2 Bridging rule and L3 Unicast rule to the host
Charles Chan94549652018-04-29 18:11:37 -07002150 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2151 vlanId, pushVlan, install));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002152 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
2153 // only if there is no port configured on that VLAN ID
2154 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
2155 // Remove broadcast forwarding rule for the VLAN
2156 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2157 // Remove L2FG for VLAN
2158 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
2159 } else {
2160 // Remove L2IG of the port
2161 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
2162 }
2163 } else if (install) {
2164 if (nextId != -1) {
2165 // Add a single port to L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002166 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002167 } else {
2168 // Create L2FG for VLAN
2169 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
2170 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2171 }
Charles Chan94549652018-04-29 18:11:37 -07002172 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2173 vlanId, pushVlan, install));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002174 } else {
2175 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2176 }
2177 }
2178
2179 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2180 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2181 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2182
2183 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2184 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2185 .filter(intf -> !intf.connectPoint().equals(cp))
2186 .flatMap(intf -> intf.ipAddressesList().stream())
2187 .collect(Collectors.toSet());
2188 // 1. Partial subnet population
2189 // Remove routing rules for removed subnet from previous configuration,
2190 // which does not also exist in other interfaces in the same device
2191 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2192 .map(InterfaceIpAddress::subnetAddress)
2193 .collect(Collectors.toSet());
2194
2195 defaultRoutingHandler.revokeSubnet(
2196 ipPrefixSet.stream()
2197 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2198 .collect(Collectors.toSet()));
2199
2200 // 2. Interface IP punts
2201 // Remove IP punts for old Intf address
2202 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2203 .map(InterfaceIpAddress::ipAddress)
2204 .collect(Collectors.toSet());
2205 ipAddressSet.stream()
2206 .map(InterfaceIpAddress::ipAddress)
2207 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2208 .forEach(interfaceIpAddress ->
2209 routingRulePopulator.revokeSingleIpPunts(
2210 cp.deviceId(), interfaceIpAddress));
2211
2212 // 3. Host unicast routing rule
2213 // Remove unicast routing rule
Charles Chan94549652018-04-29 18:11:37 -07002214 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002215 }
2216
2217 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2218 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2219 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2220
2221 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2222 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2223 .filter(intf -> !intf.connectPoint().equals(cp))
2224 .flatMap(intf -> intf.ipAddressesList().stream())
2225 .collect(Collectors.toSet());
2226 // 1. Partial subnet population
2227 // Add routing rules for newly added subnet, which does not also exist in
2228 // other interfaces in the same device
2229 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2230 .map(InterfaceIpAddress::subnetAddress)
2231 .collect(Collectors.toSet());
2232
2233 defaultRoutingHandler.populateSubnet(
2234 Collections.singleton(cp),
2235 ipPrefixSet.stream()
2236 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2237 .collect(Collectors.toSet()));
2238
2239 // 2. Interface IP punts
2240 // Add IP punts for new Intf address
2241 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2242 .map(InterfaceIpAddress::ipAddress)
2243 .collect(Collectors.toSet());
2244 ipAddressSet.stream()
2245 .map(InterfaceIpAddress::ipAddress)
2246 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2247 .forEach(interfaceIpAddress ->
2248 routingRulePopulator.populateSingleIpPunts(
2249 cp.deviceId(), interfaceIpAddress));
2250
2251 // 3. Host unicast routing rule
2252 // Add unicast routing rule
Charles Chan94549652018-04-29 18:11:37 -07002253 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002254 }
sanghob35a6192015-04-01 13:05:26 -07002255}